http模块有问题,需要修改
This commit is contained in:
473
core_form/switchbutton/switchbutton.cpp
Normal file
473
core_form/switchbutton/switchbutton.cpp
Normal file
@@ -0,0 +1,473 @@
|
||||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "switchbutton.h"
|
||||
#include "qpainter.h"
|
||||
#include <QPainterPath>
|
||||
#include "qevent.h"
|
||||
#include "qtimer.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
SwitchButton::SwitchButton(QWidget *parent): QWidget(parent)
|
||||
{
|
||||
space = 2;
|
||||
rectRadius = 5;
|
||||
checked = false;
|
||||
showText = true;
|
||||
showCircle = false;
|
||||
animation = false;
|
||||
|
||||
buttonStyle = ButtonStyle_CircleIn;
|
||||
|
||||
bgColorOff = QColor(111, 122, 126);
|
||||
bgColorOn = QColor(21, 156, 119);
|
||||
|
||||
sliderColorOff = QColor(255, 255, 255);
|
||||
sliderColorOn = QColor(255, 255, 255);
|
||||
|
||||
textColorOff = QColor(250, 250, 250);
|
||||
textColorOn = QColor(255, 255, 255);
|
||||
|
||||
textOff = "关闭";
|
||||
textOn = "开启";
|
||||
|
||||
step = 0;
|
||||
startX = 0;
|
||||
endX = 0;
|
||||
|
||||
timer = new QTimer(this);
|
||||
timer->setInterval(30);
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(updateValue()));
|
||||
}
|
||||
|
||||
SwitchButton::~SwitchButton()
|
||||
{
|
||||
if (timer->isActive()) {
|
||||
timer->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::mousePressEvent(QMouseEvent *)
|
||||
{
|
||||
checked = !checked;
|
||||
emit checkedChanged(checked);
|
||||
|
||||
//每次移动的步长
|
||||
step = width() / 7;
|
||||
|
||||
//状态切换改变后自动计算终点坐标
|
||||
if (checked) {
|
||||
if (buttonStyle == ButtonStyle_Rect) {
|
||||
endX = width() - width() / 2;
|
||||
} else if (buttonStyle == ButtonStyle_CircleIn) {
|
||||
endX = width() - height();
|
||||
} else if (buttonStyle == ButtonStyle_CircleOut) {
|
||||
endX = width() - height();
|
||||
}
|
||||
} else {
|
||||
endX = 0;
|
||||
}
|
||||
|
||||
if (animation) {
|
||||
timer->start();
|
||||
} else {
|
||||
startX = endX;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::resizeEvent(QResizeEvent *)
|
||||
{
|
||||
//每次移动的步长为宽度的 50分之一
|
||||
step = width() / 50;
|
||||
|
||||
//尺寸大小改变后自动设置起点坐标为终点
|
||||
if (checked) {
|
||||
if (buttonStyle == ButtonStyle_Rect) {
|
||||
startX = width() - width() / 2;
|
||||
} else if (buttonStyle == ButtonStyle_CircleIn) {
|
||||
startX = width() - height();
|
||||
} else if (buttonStyle == ButtonStyle_CircleOut) {
|
||||
startX = width() - height();
|
||||
}
|
||||
} else {
|
||||
startX = 0;
|
||||
}
|
||||
|
||||
this->update();
|
||||
}
|
||||
|
||||
void SwitchButton::paintEvent(QPaintEvent *)
|
||||
{
|
||||
//绘制准备工作,启用反锯齿
|
||||
QPainter painter(this);
|
||||
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
|
||||
|
||||
//绘制背景
|
||||
drawBg(&painter);
|
||||
//绘制滑块
|
||||
drawSlider(&painter);
|
||||
}
|
||||
|
||||
void SwitchButton::drawBg(QPainter *painter)
|
||||
{
|
||||
painter->save();
|
||||
painter->setPen(Qt::NoPen);
|
||||
|
||||
QColor bgColor = checked ? bgColorOn : bgColorOff;
|
||||
if (!isEnabled()) {
|
||||
bgColor.setAlpha(60);
|
||||
}
|
||||
|
||||
painter->setBrush(bgColor);
|
||||
|
||||
if (buttonStyle == ButtonStyle_Rect) {
|
||||
painter->drawRoundedRect(rect(), rectRadius, rectRadius);
|
||||
} else if (buttonStyle == ButtonStyle_CircleIn) {
|
||||
QRect rect(0, 0, width(), height());
|
||||
//半径为高度的一半
|
||||
int side = qMin(rect.width(), rect.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, rect.height());
|
||||
|
||||
QPainterPath path;
|
||||
path = path3 + path1 + path2;
|
||||
painter->drawPath(path);
|
||||
} else if (buttonStyle == ButtonStyle_CircleOut) {
|
||||
QRect rect(height() / 2, space, width() - height(), height() - space * 2);
|
||||
painter->drawRoundedRect(rect, rectRadius, rectRadius);
|
||||
}
|
||||
|
||||
if (buttonStyle == ButtonStyle_Rect || buttonStyle == ButtonStyle_CircleIn) {
|
||||
//绘制文本和小圆,互斥
|
||||
if (showText) {
|
||||
int sliderWidth = qMin(width(), height()) - space * 2;
|
||||
if (buttonStyle == ButtonStyle_Rect) {
|
||||
sliderWidth = width() / 2 - 5;
|
||||
} else if (buttonStyle == ButtonStyle_CircleIn) {
|
||||
sliderWidth -= 5;
|
||||
}
|
||||
|
||||
if (checked) {
|
||||
QRect textRect(0, 0, width() - sliderWidth, height());
|
||||
painter->setPen(textColorOn);
|
||||
painter->drawText(textRect, Qt::AlignCenter, textOn);
|
||||
} else {
|
||||
QRect textRect(sliderWidth, 0, width() - sliderWidth, height());
|
||||
painter->setPen(textColorOff);
|
||||
painter->drawText(textRect, Qt::AlignCenter, textOff);
|
||||
}
|
||||
} else if (showCircle) {
|
||||
int side = qMin(width(), height()) / 2;
|
||||
int y = (height() - side) / 2;
|
||||
|
||||
if (checked) {
|
||||
QRect circleRect(side / 2, y, side, side);
|
||||
QPen pen(textColorOn, 2);
|
||||
painter->setPen(pen);
|
||||
painter->setBrush(Qt::NoBrush);
|
||||
painter->drawEllipse(circleRect);
|
||||
} else {
|
||||
QRect circleRect(width() - (side * 1.5), y, side, side);
|
||||
QPen pen(textColorOff, 2);
|
||||
painter->setPen(pen);
|
||||
painter->setBrush(Qt::NoBrush);
|
||||
painter->drawEllipse(circleRect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
void SwitchButton::drawSlider(QPainter *painter)
|
||||
{
|
||||
painter->save();
|
||||
painter->setPen(Qt::NoPen);
|
||||
|
||||
if (!checked) {
|
||||
painter->setBrush(sliderColorOff);
|
||||
} else {
|
||||
painter->setBrush(sliderColorOn);
|
||||
}
|
||||
|
||||
if (buttonStyle == ButtonStyle_Rect) {
|
||||
int sliderWidth = width() / 2 - space * 2;
|
||||
int sliderHeight = height() - space * 2;
|
||||
QRect sliderRect(startX + space, space, sliderWidth , sliderHeight);
|
||||
painter->drawRoundedRect(sliderRect, rectRadius, rectRadius);
|
||||
} else if (buttonStyle == ButtonStyle_CircleIn) {
|
||||
QRect rect(0, 0, width(), height());
|
||||
int sliderWidth = qMin(rect.width(), rect.height()) - space * 2;
|
||||
QRect sliderRect(startX + space, space, sliderWidth, sliderWidth);
|
||||
painter->drawEllipse(sliderRect);
|
||||
} else if (buttonStyle == ButtonStyle_CircleOut) {
|
||||
int sliderWidth = this->height();
|
||||
QRect sliderRect(startX, 0, sliderWidth, sliderWidth);
|
||||
|
||||
QColor color1 = (checked ? Qt::white : bgColorOff);
|
||||
QColor color2 = (checked ? sliderColorOn : sliderColorOff);
|
||||
|
||||
QRadialGradient radialGradient(sliderRect.center(), sliderWidth / 2);
|
||||
radialGradient.setColorAt(0, checked ? color1 : color2);
|
||||
radialGradient.setColorAt(0.5, checked ? color1 : color2);
|
||||
radialGradient.setColorAt(0.6, checked ? color2 : color1);
|
||||
radialGradient.setColorAt(1.0, checked ? color2 : color1);
|
||||
painter->setBrush(radialGradient);
|
||||
|
||||
painter->drawEllipse(sliderRect);
|
||||
}
|
||||
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
void SwitchButton::change()
|
||||
{
|
||||
mousePressEvent(NULL);
|
||||
}
|
||||
|
||||
void SwitchButton::updateValue()
|
||||
{
|
||||
if (checked) {
|
||||
if (startX < endX) {
|
||||
startX = startX + step;
|
||||
} else {
|
||||
startX = endX;
|
||||
timer->stop();
|
||||
}
|
||||
} else {
|
||||
if (startX > endX) {
|
||||
startX = startX - step;
|
||||
} else {
|
||||
startX = endX;
|
||||
timer->stop();
|
||||
}
|
||||
}
|
||||
|
||||
this->update();
|
||||
}
|
||||
|
||||
int SwitchButton::getSpace() const
|
||||
{
|
||||
return this->space;
|
||||
}
|
||||
|
||||
int SwitchButton::getRectRadius() const
|
||||
{
|
||||
return this->rectRadius;
|
||||
}
|
||||
|
||||
bool SwitchButton::getChecked() const
|
||||
{
|
||||
return this->checked;
|
||||
}
|
||||
|
||||
bool SwitchButton::getShowText() const
|
||||
{
|
||||
return this->showText;
|
||||
}
|
||||
|
||||
bool SwitchButton::getShowCircle() const
|
||||
{
|
||||
return this->showCircle;
|
||||
}
|
||||
|
||||
bool SwitchButton::getAnimation() const
|
||||
{
|
||||
return this->animation;
|
||||
}
|
||||
|
||||
SwitchButton::ButtonStyle SwitchButton::getButtonStyle() const
|
||||
{
|
||||
return this->buttonStyle;
|
||||
}
|
||||
|
||||
QColor SwitchButton::getBgColorOff() const
|
||||
{
|
||||
return bgColorOff;
|
||||
}
|
||||
|
||||
QColor SwitchButton::getBgColorOn() const
|
||||
{
|
||||
return this->bgColorOn;
|
||||
}
|
||||
|
||||
QColor SwitchButton::getSliderColorOff() const
|
||||
{
|
||||
return this->sliderColorOff;
|
||||
}
|
||||
|
||||
QColor SwitchButton::getSliderColorOn() const
|
||||
{
|
||||
return this->sliderColorOn;
|
||||
}
|
||||
|
||||
QColor SwitchButton::getTextColorOff() const
|
||||
{
|
||||
return this->textColorOff;
|
||||
}
|
||||
|
||||
QColor SwitchButton::getTextColorOn() const
|
||||
{
|
||||
return this->textColorOn;
|
||||
}
|
||||
|
||||
QString SwitchButton::getTextOff() const
|
||||
{
|
||||
return this->textOff;
|
||||
}
|
||||
|
||||
QString SwitchButton::getTextOn() const
|
||||
{
|
||||
return this->textOn;
|
||||
}
|
||||
|
||||
QSize SwitchButton::sizeHint() const
|
||||
{
|
||||
return QSize(70, 30);
|
||||
}
|
||||
|
||||
QSize SwitchButton::minimumSizeHint() const
|
||||
{
|
||||
return QSize(10, 5);
|
||||
}
|
||||
|
||||
void SwitchButton::setSpace(int space)
|
||||
{
|
||||
if (this->space != space) {
|
||||
this->space = space;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setRectRadius(int rectRadius)
|
||||
{
|
||||
if (this->rectRadius != rectRadius) {
|
||||
this->rectRadius = rectRadius;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setChecked(bool checked)
|
||||
{
|
||||
//如果刚刚初始化完成的属性改变则延时处理
|
||||
if (this->checked != checked) {
|
||||
if (step == 0) {
|
||||
QTimer::singleShot(10, this, SLOT(change()));
|
||||
} else {
|
||||
mousePressEvent(NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setShowText(bool showText)
|
||||
{
|
||||
if (this->showText != showText) {
|
||||
this->showText = showText;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setShowCircle(bool showCircle)
|
||||
{
|
||||
if (this->showCircle != showCircle) {
|
||||
this->showCircle = showCircle;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setAnimation(bool animation)
|
||||
{
|
||||
if (this->animation != animation) {
|
||||
this->animation = animation;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setButtonStyle(const SwitchButton::ButtonStyle &buttonStyle)
|
||||
{
|
||||
if (this->buttonStyle != buttonStyle) {
|
||||
this->buttonStyle = buttonStyle;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setBgColorOff(const QColor &bgColorOff)
|
||||
{
|
||||
if (this->bgColorOff != bgColorOff) {
|
||||
this->bgColorOff = bgColorOff;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setBgColorOn(const QColor &bgColorOn)
|
||||
{
|
||||
if (this->bgColorOn != bgColorOn) {
|
||||
this->bgColorOn = bgColorOn;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setSliderColorOff(const QColor &sliderColorOff)
|
||||
{
|
||||
if (this->sliderColorOff != sliderColorOff) {
|
||||
this->sliderColorOff = sliderColorOff;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setSliderColorOn(const QColor &sliderColorOn)
|
||||
{
|
||||
if (this->sliderColorOn != sliderColorOn) {
|
||||
this->sliderColorOn = sliderColorOn;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setTextColorOff(const QColor &textColorOff)
|
||||
{
|
||||
if (this->textColorOff != textColorOff) {
|
||||
this->textColorOff = textColorOff;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setTextColorOn(const QColor &textColorOn)
|
||||
{
|
||||
if (this->textColorOn != textColorOn) {
|
||||
this->textColorOn = textColorOn;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::enableText(bool enable)
|
||||
{
|
||||
if (!enable) {
|
||||
this->textOn = "";
|
||||
this->textOff = "";
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setTextOff(const QString &textOff)
|
||||
{
|
||||
if (this->textOff != textOff) {
|
||||
this->textOff = textOff;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchButton::setTextOn(const QString &textOn)
|
||||
{
|
||||
if (this->textOn != textOn) {
|
||||
this->textOn = textOn;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
159
core_form/switchbutton/switchbutton.h
Normal file
159
core_form/switchbutton/switchbutton.h
Normal file
@@ -0,0 +1,159 @@
|
||||
#ifndef SWITCHBUTTON_H
|
||||
#define SWITCHBUTTON_H
|
||||
|
||||
/**
|
||||
* 开关按钮控件 作者:feiyangqingyun(QQ:517216493) 2016-11-6
|
||||
* 1:可设置开关按钮的样式 圆角矩形/内圆形/外圆形
|
||||
* 2:可设置选中和未选中时的背景颜色
|
||||
* 3:可设置选中和未选中时的滑块颜色
|
||||
* 4:可设置显示的文本
|
||||
* 5:可设置滑块离背景的间隔
|
||||
* 6:可设置圆角角度
|
||||
* 7:可设置是否显示动画过渡效果
|
||||
*/
|
||||
|
||||
#include <QWidget>
|
||||
#include <QPainterPath>
|
||||
|
||||
#ifdef quc
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
|
||||
#include <QtDesigner/QDesignerExportWidget>
|
||||
#else
|
||||
#include <QtUiPlugin/QDesignerExportWidget>
|
||||
#endif
|
||||
|
||||
class QDESIGNER_WIDGET_EXPORT SwitchButton : public QWidget
|
||||
#else
|
||||
class SwitchButton : public QWidget
|
||||
#endif
|
||||
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_ENUMS(ButtonStyle)
|
||||
|
||||
Q_PROPERTY(int space READ getSpace WRITE setSpace)
|
||||
Q_PROPERTY(int rectRadius READ getRectRadius WRITE setRectRadius)
|
||||
Q_PROPERTY(bool checked READ getChecked WRITE setChecked)
|
||||
Q_PROPERTY(bool showText READ getShowText WRITE setShowText)
|
||||
Q_PROPERTY(bool showCircle READ getShowCircle WRITE setShowCircle)
|
||||
Q_PROPERTY(bool animation READ getAnimation WRITE setAnimation)
|
||||
Q_PROPERTY(ButtonStyle buttonStyle READ getButtonStyle WRITE setButtonStyle)
|
||||
|
||||
Q_PROPERTY(QColor bgColorOff READ getBgColorOff WRITE setBgColorOff)
|
||||
Q_PROPERTY(QColor bgColorOn READ getBgColorOn WRITE setBgColorOn)
|
||||
Q_PROPERTY(QColor sliderColorOff READ getSliderColorOff WRITE setSliderColorOff)
|
||||
Q_PROPERTY(QColor sliderColorOn READ getSliderColorOn WRITE setSliderColorOn)
|
||||
Q_PROPERTY(QColor textColorOff READ getTextColorOff WRITE setTextColorOff)
|
||||
Q_PROPERTY(QColor textColorOn READ getTextColorOn WRITE setTextColorOn)
|
||||
|
||||
Q_PROPERTY(QString textOff READ getTextOff WRITE setTextOff)
|
||||
Q_PROPERTY(QString textOn READ getTextOn WRITE setTextOn)
|
||||
|
||||
public:
|
||||
enum ButtonStyle {
|
||||
ButtonStyle_Rect = 0, //圆角矩形
|
||||
ButtonStyle_CircleIn = 1, //内圆形
|
||||
ButtonStyle_CircleOut = 2 //外圆形
|
||||
};
|
||||
|
||||
SwitchButton(QWidget *parent = 0);
|
||||
~SwitchButton();
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *);
|
||||
void resizeEvent(QResizeEvent *);
|
||||
void paintEvent(QPaintEvent *);
|
||||
void drawBg(QPainter *painter);
|
||||
void drawSlider(QPainter *painter);
|
||||
|
||||
private:
|
||||
int space; //滑块离背景间隔
|
||||
int rectRadius; //圆角角度
|
||||
bool checked; //是否选中
|
||||
bool showText; //显示文字
|
||||
bool showCircle; //显示小圆
|
||||
bool animation; //动画过渡
|
||||
|
||||
ButtonStyle buttonStyle; //开关按钮样式
|
||||
|
||||
QColor bgColorOff; //关闭时背景颜色
|
||||
QColor bgColorOn; //打开时背景颜色
|
||||
QColor sliderColorOff; //关闭时滑块颜色
|
||||
QColor sliderColorOn; //打开时滑块颜色
|
||||
QColor textColorOff; //关闭时文字颜色
|
||||
QColor textColorOn; //打开时文字颜色
|
||||
|
||||
QString textOff; //关闭时显示的文字
|
||||
QString textOn; //打开时显示的文字
|
||||
|
||||
int step; //每次移动的步长
|
||||
int startX; //滑块开始X轴坐标
|
||||
int endX; //滑块结束X轴坐标
|
||||
QTimer *timer; //定时器绘制
|
||||
|
||||
private slots:
|
||||
void change();
|
||||
void updateValue();
|
||||
|
||||
public:
|
||||
int getSpace() const;
|
||||
int getRectRadius() const;
|
||||
bool getChecked() const;
|
||||
bool getShowText() const;
|
||||
bool getShowCircle() const;
|
||||
bool getAnimation() const;
|
||||
|
||||
ButtonStyle getButtonStyle() const;
|
||||
|
||||
QColor getBgColorOff() const;
|
||||
QColor getBgColorOn() const;
|
||||
QColor getSliderColorOff() const;
|
||||
QColor getSliderColorOn() const;
|
||||
QColor getTextColorOff() const;
|
||||
QColor getTextColorOn() const;
|
||||
|
||||
QString getTextOff() const;
|
||||
QString getTextOn() const;
|
||||
|
||||
QSize sizeHint() const;
|
||||
QSize minimumSizeHint() const;
|
||||
|
||||
public Q_SLOTS:
|
||||
//设置间隔
|
||||
void setSpace(int space);
|
||||
//设置圆角角度
|
||||
void setRectRadius(int rectRadius);
|
||||
//设置是否选中
|
||||
void setChecked(bool checked);
|
||||
//设置是否显示文字
|
||||
void setShowText(bool showText);
|
||||
//设置是否显示小圆
|
||||
void setShowCircle(bool showCircle);
|
||||
//设置是否动画过渡
|
||||
void setAnimation(bool animation);
|
||||
|
||||
//设置风格样式
|
||||
void setButtonStyle(const ButtonStyle &buttonStyle);
|
||||
|
||||
//设置背景颜色
|
||||
void setBgColorOff(const QColor &bgColorOff);
|
||||
void setBgColorOn(const QColor &bgColorOn);
|
||||
|
||||
//设置滑块颜色
|
||||
void setSliderColorOff(const QColor &sliderColorOff);
|
||||
void setSliderColorOn(const QColor &sliderColorOn);
|
||||
|
||||
//设置文字颜色
|
||||
void setTextColorOff(const QColor &textColorOff);
|
||||
void setTextColorOn(const QColor &textColorOn);
|
||||
void enableText(bool enable);
|
||||
|
||||
//设置文字
|
||||
void setTextOff(const QString &textOff);
|
||||
void setTextOn(const QString &textOn);
|
||||
|
||||
Q_SIGNALS:
|
||||
void checkedChanged(bool checked);
|
||||
};
|
||||
|
||||
#endif // SWITCHBUTTON_H
|
||||
5
core_form/switchbutton/switchbutton.pri
Normal file
5
core_form/switchbutton/switchbutton.pri
Normal file
@@ -0,0 +1,5 @@
|
||||
HEADERS += \
|
||||
$$PWD/switchbutton.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/switchbutton.cpp
|
||||
Reference in New Issue
Block a user