commit 8c3899ac317b85a487f1794949c690aa05413739 Author: tianzhendong <1203886034@qq.com> Date: Mon May 15 17:46:45 2023 +0800 finished diff --git a/SwitchButton.cpp b/SwitchButton.cpp new file mode 100644 index 0000000..76db8ce --- /dev/null +++ b/SwitchButton.cpp @@ -0,0 +1,387 @@ +#include "SwitchButton.h" +#include +#include + +SwitchButton::SwitchButton(QWidget *parent) : QWidget(parent) +{ + m_space = 2; + m_radius = 5; + m_checked = false; + m_showText = true; + m_showText = false; + m_animation = true; +// m_bgColorOn = QColor(21, 156, 119); + m_bgColorOn = QColor(0x4e96fb); + m_bgColorOff = QColor(111, 122, 126); + m_sliderColorOn = QColor(255, 255, 255); + m_sliderColorOff = QColor(255, 255, 255); + m_textColor = QColor(255, 255, 255); + m_textOn = tr("Yes"); + m_textOff = tr("No"); + m_step = 0; + m_startX = 0; + m_endX = 0; +// m_timer = new QTimer(this); + m_timer = std::make_shared(this); + m_timer.get()->setInterval(30); + connect(m_timer.get(), SIGNAL(timeout()), this, SLOT(updateValue())); +} + +void SwitchButton::drawBackGround(QPainter *painter) +{ + painter->save(); + painter->setPen(Qt::NoPen); + QColor bgColor = m_checked ? m_bgColorOn : m_bgColorOff; + if (isEnabled()) + { + bgColor.setAlpha(255); + } + painter->setBrush(bgColor); + QRect rect(0, 0, width(), height()); + int side = qMin(width(), height()); + //左侧半圆 + QPainterPath path1; + path1.addEllipse(rect.x(), rect.y(), side, side); + //右侧半圆 + QPainterPath path2; + path2.addEllipse(rect.width() - side, rect.y(), side, side); + //中间的矩形 + QPainterPath path3; + path3.addRect(rect.x() + side / 2, rect.y(), rect.width() - side, height()); + QPainterPath path = path1 + path2 + path3; + painter->drawPath(path); + //绘制文本 + //滑块半径 + int sliderWidth = qMin(height(), width()) - m_space * 2 - 5; + if (m_checked) + { + QRect textRect(0, 0, width() - sliderWidth, height()); + painter->setPen(QPen(m_textColor)); + painter->drawText(textRect, Qt::AlignCenter, m_textOn); + } + else + { + QRect textRect(sliderWidth, 0, width() - sliderWidth, height()); + painter->setPen(QPen(m_textColor)); + painter->drawText(textRect, Qt::AlignCenter, m_textOff); + } + painter->restore(); +} + +void SwitchButton::drawSlider(QPainter *painter) +{ + painter->save(); + painter->setPen(Qt::NoPen); + QColor color = m_checked ? m_sliderColorOn : m_sliderColorOff; + painter->setBrush(QBrush(color)); + int sliderWidth = qMin(width(), height()) - m_space * 2; + QRect rect(m_space + m_startX, m_space, sliderWidth, sliderWidth); + painter->drawEllipse(rect); + painter->restore(); +} + +void SwitchButton::paintEvent(QPaintEvent *) +{ + //启用反锯齿 + QPainter painter(this); + painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); + //绘制背景 + drawBackGround(&painter); + //绘制滑块 + drawSlider(&painter); +} + +void SwitchButton::mousePressEvent(QMouseEvent *ev) +{ + Q_UNUSED(ev) + m_checked = !m_checked; + emit checkedChanged(m_checked); + //计算步长 + m_step = width() / 10; + //计算滑块X轴终点坐标 + if (m_checked) + { + m_endX = width() - height(); + } + else + { + m_endX = 0; + } + //判断是否使用动画 + if (m_animation) + { + m_timer.get()->start(); + } + else + { + m_startX = m_endX; + update(); + } +} + +void SwitchButton::updateValue() +{ + if (m_checked) + { + if (m_startX < m_endX) + { + m_startX += m_step; + } + else + { + m_startX = m_endX; + m_timer.get()->stop(); + } + } + else + { + if (m_startX > m_endX) + { + m_startX -= m_step; + } + else + { + m_startX = m_endX; + m_timer.get()->stop(); + } + } + update(); +} + +int SwitchButton::space() const +{ + return m_space; +} + +int SwitchButton::radius() const +{ + return m_radius; +} + +bool SwitchButton::checked() const +{ + return m_checked; +} + +bool SwitchButton::showText() const +{ + return m_showText; +} + +bool SwitchButton::showCircel() const +{ + return m_showCircle; +} + +bool SwitchButton::animation() const +{ + return m_animation; +} + +QColor SwitchButton::bgColorOn() const +{ + return m_bgColorOn; +} + +QColor SwitchButton::bgColorOff() const +{ + return m_bgColorOff; +} + +QColor SwitchButton::sliderColorOn() const +{ + return m_sliderColorOn; +} + +QColor SwitchButton::sliderColorOff() const +{ + return m_sliderColorOff; +} + +QColor SwitchButton::textColor() const +{ + return m_textColor; +} + +QString SwitchButton::textOn() const +{ + return m_textOn; +} + +QString SwitchButton::textOff() const +{ + return m_textOff; +} + +int SwitchButton::step() const +{ + return m_step; +} + +int SwitchButton::startX() const +{ + return m_startX; +} + +int SwitchButton::endX() const +{ + return m_endX; +} + +void SwitchButton::setSpace(int space) +{ + if (m_space != space) + { + m_space = space; + update(); + } +} + +void SwitchButton::setRadius(int radius) +{ + if (m_radius != radius) + { + m_radius = radius; + update(); + } +} + +void SwitchButton::setChecked(bool checked) +{ + if (m_checked != checked) + { + m_checked = checked; + update(); + } + if (m_checked) + { + //计算步长 + m_step = width() / 10; + m_endX = width() - height(); + m_startX = m_endX; + update(); + } + else + { + m_endX = 0; + } +// //判断是否使用动画 +// if (m_animation) +// { +// m_timer.get()->start(); +// } +// else +// { +// m_startX = m_endX; +// update(); +// } +} + +void SwitchButton::setShowText(bool show) +{ + if (m_showText != show) + { + m_showText = show; + update(); + } +} + +void SwitchButton::setShowCircle(bool show) +{ + if (m_showCircle != show) + { + m_showCircle = show; + update(); + } +} + +void SwitchButton::setAnimation(bool ok) +{ + if (m_animation != ok) + { + m_animation = ok; + update(); + } +} + +void SwitchButton::setBgColorOn(const QColor &color) +{ + if (m_bgColorOn != color) + { + m_bgColorOn = color; + update(); + } +} + +void SwitchButton::setBgColorOff(const QColor &color) +{ + if (m_bgColorOff != color) + { + m_bgColorOff = color; + update(); + } +} + +void SwitchButton::setSliderColorOn(const QColor &color) +{ + if (m_sliderColorOn != color) + { + m_sliderColorOn = color; + update(); + } +} + +void SwitchButton::setSliderColorOff(const QColor &color) +{ + if (m_sliderColorOff != color) + { + m_sliderColorOff = color; + update(); + } +} + +void SwitchButton::setTextColor(const QColor &color) +{ + if (m_textColor != color) + { + m_textColor = color; + update(); + } +} + +void SwitchButton::setTextOn(const QString &text) +{ + if (m_textOn != text) + { + m_textOn = text; + update(); + } +} + +void SwitchButton::setTextOff(const QString &text) +{ + if (m_textOff != text) + { + m_textOff = text; + update(); + } +} + +#if 0 +void SwitchButton::setStep(int step) +{ + if (m_step != step) + { + m_step = step; + update(); + } +} + +void SwitchButton::setStartX(int startX) +{ +} + +void SwitchButton::setEndX(int endX) +{ +} +#endif diff --git a/SwitchButton.dll b/SwitchButton.dll new file mode 100644 index 0000000..37a9da3 Binary files /dev/null and b/SwitchButton.dll differ diff --git a/SwitchButton.h b/SwitchButton.h new file mode 100644 index 0000000..d388a0a --- /dev/null +++ b/SwitchButton.h @@ -0,0 +1,126 @@ +#ifndef SWITCHBUTTON_H +#define SWITCHBUTTON_H + +#include "SwitchButton_global.h" + +#include +#include +#include +#include + +class SWITCHBUTTON_EXPORT SwitchButton : public QWidget +{ + Q_OBJECT +// Q_PROPERTY(int m_space READ space WRITE setSpace) +// Q_PROPERTY(int m_radius READ radius WRITE setRadius) +// Q_PROPERTY(bool m_checked READ checked WRITE setChecked) +// Q_PROPERTY(bool m_showText READ showText WRITE setShowText) +// Q_PROPERTY(bool m_showCircle READ showCircel WRITE setShowCircle) +// Q_PROPERTY(bool m_animation READ animation WRITE setAnimation) + +// Q_PROPERTY(QColor m_bgColorOn READ bgColorOn WRITE setBgColorOn) +// Q_PROPERTY(QColor m_bgColorOff READ bgColorOff WRITE setBgColorOff) +// Q_PROPERTY(QColor m_sliderColorOn READ sliderColorOn WRITE setSliderColorOn) +// Q_PROPERTY(QColor m_sliderColorOff READ sliderColorOff WRITE setSliderColorOff) +// Q_PROPERTY(QColor m_textColor READ textColor WRITE setTextColor) + +// Q_PROPERTY(QString m_textOn READ textOn WRITE setTextOn) +// Q_PROPERTY(QString m_textOff READ textOff WRITE setTextOff) + +// Q_PROPERTY(int m_step READ step WRITE setStep) +// Q_PROPERTY(int m_startX READ startX WRITE setStartX) +// Q_PROPERTY(int m_endX READ endX WRITE setEndX) + +public: + explicit SwitchButton(QWidget *parent = 0); + +signals: +// void statusChanged(bool checked); + void checkedChanged(bool checked); + +public slots: + +private slots: + void updateValue(); + +private: + void drawBackGround(QPainter *painter); + void drawSlider(QPainter *painter); + +protected: + void paintEvent(QPaintEvent *ev); + void mousePressEvent(QMouseEvent *ev); + +private: + int m_space; //滑块距离边界距离 + int m_radius; //圆角角度 + + bool m_checked; //是否选中 + bool m_showText; //是否显示文字 + bool m_showCircle; //是否显示圆圈 + bool m_animation; //是否使用动画 + + QColor m_bgColorOn; //打开时候的背景色 + QColor m_bgColorOff; //关闭时候的背景色 + QColor m_sliderColorOn; //打开时候滑块颜色 + QColor m_sliderColorOff; //关闭时候滑块颜色 + QColor m_textColor; //文字颜色 + + QString m_textOn; //打开时候的文字 + QString m_textOff; //关闭时候的文字 + +// QTimer *m_timer; //动画定时器 + std::shared_ptr m_timer; + int m_step; //动画步长 + int m_startX; //滑块开始X轴坐标 + int m_endX; //滑块结束X轴坐标 + +public: + int space() const; + int radius() const; + bool checked() const; + bool showText() const; + bool showCircel() const; + bool animation() const; + + QColor bgColorOn() const; + QColor bgColorOff() const; + QColor sliderColorOn() const; + QColor sliderColorOff() const; + QColor textColor() const; + + QString textOn() const; + QString textOff() const; + + int step() const; + int startX() const; + int endX() const; + + +public Q_SLOTS: + void setSpace(int space); + void setRadius(int radius); + void setChecked(bool checked); + void setShowText(bool show); + void setShowCircle(bool show); + void setAnimation(bool ok); + + void setBgColorOn(const QColor &color); + void setBgColorOff(const QColor &color); + void setSliderColorOn(const QColor &color); + void setSliderColorOff(const QColor &color); + void setTextColor(const QColor &color); + + void setTextOn(const QString &text); + void setTextOff(const QString &text); + +// void setStep(int step); +// void setStartX(int startX); +// void setEndX(int endX); + + +}; + + + +#endif // SWITCHBUTTON_H diff --git a/SwitchButton.pro b/SwitchButton.pro new file mode 100644 index 0000000..db5b57c --- /dev/null +++ b/SwitchButton.pro @@ -0,0 +1,23 @@ +QT += widgets + +TEMPLATE = lib +DEFINES += SWITCHBUTTON_LIBRARY + +CONFIG += c++17 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + SwitchButton.cpp + +HEADERS += \ + SwitchButton_global.h \ + SwitchButton.h + +# Default rules for deployment. +unix { + target.path = /usr/lib +} +!isEmpty(target.path): INSTALLS += target diff --git a/SwitchButton_global.h b/SwitchButton_global.h new file mode 100644 index 0000000..8f41d20 --- /dev/null +++ b/SwitchButton_global.h @@ -0,0 +1,12 @@ +#ifndef SWITCHBUTTON_GLOBAL_H +#define SWITCHBUTTON_GLOBAL_H + +#include + +#if defined(SWITCHBUTTON_LIBRARY) +# define SWITCHBUTTON_EXPORT Q_DECL_EXPORT +#else +# define SWITCHBUTTON_EXPORT Q_DECL_IMPORT +#endif + +#endif // SWITCHBUTTON_GLOBAL_H diff --git a/libSwitchButton.a b/libSwitchButton.a new file mode 100644 index 0000000..f64fcd7 Binary files /dev/null and b/libSwitchButton.a differ diff --git a/switchbutton.pri b/switchbutton.pri new file mode 100644 index 0000000..bc995c6 --- /dev/null +++ b/switchbutton.pri @@ -0,0 +1,9 @@ +INCLUDEPATH += $$PWD/ + +#SOURCES += \ +# $$PWD/SwitchButton.cpp + +HEADERS += \ + $$PWD/SwitchButton.h + +LIBS += -L. SwitchButton