Qt switch button

1.

 

 

 

#ifndef SWITCHBUTTON_H
#define SWITCHBUTTON_H

#include <QObject>
#include <QLabel>
class SwitchButton : public QLabel
{
    typedef enum _buttonStyle{
      Rectage,     // rectangular pattern 
      Ellipse,     // elliptical pattern 
    } ButtonStyle;

    Q_OBJECT
public:
    explicit SwitchButton(QWidget  *parent = nullptr);
    ~SwitchButton();
signals:
    void sigClicked();
    void clicked(bool is_selected);
public slots:
public:
    void SetButtonStyle(ButtonStyle button_style);
    void SetSize(int width, int height);
    void SetSize(const QSize& size);
    void SetBackgroundColor(const QColor& selected_color, const QColor& not_selected_color);
    void SetSliderColor(const QColor& selected_color, const QColor& not_selected_color);
    void SetRound(int round);

    // if the anti-aliasing 
    void EnableAntiAliasing ( BOOL enable);
     BOOL IsAntiAliasing ();

    // currently selected is 
    void setSelected ( BOOL is_selected);
     BOOL IsSelected ();

    //是否启用
    void SetEnabel(bool is_enable);
    bool IsEnable();
protected:
    virtual void paintEvent(QPaintEvent* event);
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);
private:
    void DrawBackRect(QPainter* painter, const QRectF& rect);

    void DrawSliderRect(QPainter* painter, const QRectF& rect);

Private :
     BOOL isEnable;   // whether to enable

    BOOL isSelected;     // currently selected status 
    BOOL isAntiAliasing;     // if the anti-aliasing

    int buttonWidth;
    int buttonHeight;

    QColor backgroundColorSelected;
    QColor backgroundColorNotSelected;

    QColor sliderColorSelected;
    QColor sliderColorNotSelected;

    int rectRound;
    ButtonStyle buttonStyle;

};

#endif // SWITCHBUTTON_H

 

#include "switchbutton.h"

#include <QPainter>
 // adjust the position 
const  int X_MARGIN = . 5 ;
 const  int Y_MARGIN = . 5 ;
SwitchButton::SwitchButton(QWidget  *parent) : QLabel(parent),
    isSelected(true), buttonWidth(100), buttonHeight(40),
    backgroundColorSelected("#52C300"), backgroundColorNotSelected("#6B797B"),
    sliderColorSelected("#FEFBEF"), sliderColorNotSelected("#F7F5F7"),
   rectRound(10), isAntiAliasing(true), buttonStyle(Rectage), isEnable(true)

{
    resize(buttonWidth, buttonHeight);
    setMouseTracking(true);
}

SwitchButton::~SwitchButton()
{

}
void SwitchButton::SetButtonStyle(ButtonStyle button_style)
{
    buttonStyle = button_style;
    repaint();
}

void SwitchButton::SetSize(int width, int height)
{
    buttonWidth = width;
    buttonHeight = height;

    resize(buttonWidth, buttonHeight);

    repaint();
}

void SwitchButton::SetSize(const QSize& size)
{
    buttonWidth = size.width()+X_MARGIN;
    buttonHeight = size.height()+Y_MARGIN;

    resize(buttonWidth, buttonHeight);

    repaint();
}

void SwitchButton::SetBackgroundColor(const QColor& selected_color, const QColor& not_selected_color)
{
    backgroundColorSelected = selected_color;
    backgroundColorNotSelected = not_selected_color;

    repaint();
}

void SwitchButton::SetSliderColor(const QColor& selected_color, const QColor& not_selected_color)
{
    sliderColorSelected = selected_color;
    sliderColorNotSelected = not_selected_color;

    repaint();
}

void SwitchButton::SetRound(int round)
{
    rectRound = round;
    repaint();
}

void SwitchButton::EnableAntiAliasing(bool enable)
{
    isAntiAliasing = enable;
    repaint();
}

bool SwitchButton::IsAntiAliasing()
{
    return isAntiAliasing;
}

void SwitchButton::SetSelected(bool is_selected)
{
    isSelected = is_selected;

    repaint();
}

bool SwitchButton::IsSelected()
{
    return isSelected;
}

void SwitchButton::SetEnabel(bool is_enable)
{
    isEnable = is_enable;

    repaint();
}

bool SwitchButton::IsEnable()
{
    return isEnable;
}

void SwitchButton::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);

    if (isAntiAliasing)
    {
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setRenderHint(QPainter::SmoothPixmapTransform);
    }

    IF (! isEnable)   // is not enabled 
    {
        painter.setPen(QPen(QColor("#F4F4F4")));
        painter.setBrush(QBrush(QColor("#F4F4F4")));
        DrawBackRect(&painter, QRectF(0, 0, buttonWidth, buttonHeight));

        painter.setPen(QPen(QColor("#ADB2B5")));
        painter.setBrush(QBrush(QColor("#ADB2B5")));
        DrawSliderRect(&painter, QRectF(buttonWidth*2/3-2+X_MARGIN, 2.5+Y_MARGIN, buttonWidth/3, buttonHeight-5));
    }
    else if (isSelected)
    {
        painter.setPen(QPen(backgroundColorSelected));
        painter.setBrush(QBrush(backgroundColorSelected));
        DrawBackRect(&painter, QRectF(0+X_MARGIN, 0+Y_MARGIN, buttonWidth, buttonHeight));

        painter.setPen(QPen(sliderColorSelected));
        painter.setBrush(QBrush(sliderColorSelected));
        DrawSliderRect(&painter, QRectF(buttonWidth*2/3-2+X_MARGIN, 2.5+Y_MARGIN, buttonWidth/3, buttonHeight-5));

        QFont do;
        //font.setBold(true);
        font.setPointSize(10);
        painter.setFont(font);
        painter.setPen(Qt::white);
        painter.drawText(QRectF(X_MARGIN+10, Y_MARGIN+5, buttonWidth, buttonHeight),QStringLiteral("CW"));

    }
    else
    {
        painter.setPen(QPen(backgroundColorNotSelected));
        painter.setBrush(QBrush(backgroundColorNotSelected));
        DrawBackRect(&painter,QRectF(X_MARGIN, Y_MARGIN, buttonWidth, buttonHeight));

        painter.setPen(QPen(sliderColorNotSelected));
        painter.setBrush(QBrush(sliderColorNotSelected));
        DrawSliderRect(&painter,QRectF(2+X_MARGIN, 2.5+Y_MARGIN, buttonWidth/3, buttonHeight-5));

        QFont do;
        //font.setBold(true);
        font.setPointSize(10);
        painter.setFont(font);
        painter.setPen(Qt::white);
        painter.drawText(QRectF(buttonWidth/2+5,Y_MARGIN+5, buttonWidth, buttonHeight),QStringLiteral("CCW"));
    }

    return QLabel::paintEvent(event);
}

void SwitchButton::mousePressEvent(QMouseEvent *event)
{
    if (isEnable)
    {
        isSelected = !isSelected;
        repaint();

        emit sigClicked();
        emit clicked(isSelected);
    }

    return QLabel::mousePressEvent(event);
}

void SwitchButton::mouseMoveEvent(QMouseEvent *event)
{
    setCursor(Qt::PointingHandCursor);

    return QLabel::mouseMoveEvent(event);
}

void SwitchButton::DrawBackRect(QPainter* painter, const QRectF& rect)
{
    switch (buttonStyle)
    {
    case Rectage:
        painter->drawRoundRect(rect, rectRound, rectRound);
        break;
    case Ellipse:
        painter->drawEllipse(0, 0, buttonHeight, buttonHeight);
        painter->drawEllipse(buttonWidth - buttonHeight, 0, buttonHeight, buttonHeight);
        painter->drawRect(buttonHeight/2, 0, buttonWidth-buttonHeight, buttonHeight);
        break;
    default:
        break;
    }
}

void SwitchButton::DrawSliderRect(QPainter* painter, const QRectF& rect)
{
    switch (buttonStyle)
    {
    case Rectage:
        painter->drawRoundRect(rect, rectRound, rectRound);
        break;
    case Ellipse:
        painter->drawEllipse(rect);
        break;
    default:
        break;
    }
}

 

Guess you like

Origin www.cnblogs.com/ike_li/p/12215000.html