2024-01-15 16:03:34 +08:00
|
|
|
/*
|
2024-01-19 14:12:25 +08:00
|
|
|
* Copyright (C) 2023, KylinSoft Co., Ltd.
|
2022-06-02 16:31:24 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2024-01-15 16:03:34 +08:00
|
|
|
* the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
* any later version.
|
2022-06-02 16:31:24 +08:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2024-01-15 16:03:34 +08:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2022-06-02 16:31:24 +08:00
|
|
|
*
|
2024-01-15 16:03:34 +08:00
|
|
|
**/
|
2022-06-02 16:31:24 +08:00
|
|
|
|
|
|
|
#include "checkbutton.h"
|
|
|
|
|
|
|
|
checkButton::checkButton(QWidget *parent)
|
|
|
|
:QFrame(parent)
|
|
|
|
{
|
|
|
|
this->setFixedSize(QSize(52, 24));
|
|
|
|
checked = false;
|
|
|
|
|
|
|
|
borderColorOff = QColor("#cccccc");
|
|
|
|
|
|
|
|
bgColorOff = QColor("#ffffff");
|
|
|
|
bgColorOn = QColor("#0078d7");
|
|
|
|
|
|
|
|
sliderColorOff = QColor("#cccccc");
|
|
|
|
sliderColorOn = QColor("#ffffff");
|
|
|
|
|
|
|
|
space = 4;
|
|
|
|
|
|
|
|
|
|
|
|
step = width() / 50;
|
|
|
|
startX = 0;
|
|
|
|
endX= 0;
|
|
|
|
|
|
|
|
timer = new QTimer(this);
|
|
|
|
timer->setInterval(5);
|
|
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(updatevalue()));
|
|
|
|
setWindowFlags(Qt::FramelessWindowHint);
|
|
|
|
setAttribute(Qt::WA_TranslucentBackground, true);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkButton::paintEvent(QPaintEvent *){
|
|
|
|
//启用反锯齿
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
|
|
|
|
drawBg(&painter);
|
|
|
|
drawSlider(&painter);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void checkButton::drawBg(QPainter *painter){
|
|
|
|
painter->save();
|
|
|
|
|
|
|
|
if (!checked){
|
|
|
|
painter->setPen(QColor(255,255,255,30));
|
|
|
|
painter->setBrush(QColor(255,255,255,30));
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
painter->setPen(Qt::NoPen);
|
|
|
|
painter->setBrush(bgColorOn);
|
|
|
|
}
|
|
|
|
|
|
|
|
//circle in
|
|
|
|
QRect rect(0, 0, width(), height());
|
|
|
|
//半径为高度的一半
|
|
|
|
int radius = rect.height() / 2;
|
|
|
|
//圆的宽度为高度
|
|
|
|
int circleWidth = rect.height();
|
|
|
|
|
|
|
|
QPainterPath path;
|
|
|
|
path.moveTo(radius, rect.left());
|
|
|
|
path.arcTo(QRectF(rect.left(), rect.top(), circleWidth, circleWidth), 90, 180);
|
|
|
|
path.lineTo(rect.width() - radius, rect.height());
|
|
|
|
path.arcTo(QRectF(rect.width() - rect.height(), rect.top(), circleWidth, circleWidth), 270, 180);
|
|
|
|
path.lineTo(radius, rect.top());
|
|
|
|
|
|
|
|
painter->drawPath(path);
|
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void checkButton::drawSlider(QPainter *painter){
|
|
|
|
painter->save();
|
|
|
|
painter->setPen(Qt::NoPen);
|
|
|
|
|
|
|
|
if (!checked){
|
|
|
|
painter->setBrush(sliderColorOff);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
painter->setBrush(sliderColorOn);
|
|
|
|
|
|
|
|
//circle in
|
|
|
|
QRect rect(0, 0, width(), height());
|
|
|
|
int sliderWidth = rect.height() - space * 2;
|
|
|
|
QRect sliderRect(startX + space, space, sliderWidth, sliderWidth);
|
|
|
|
painter->drawEllipse(sliderRect);
|
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkButton::mousePressEvent(QMouseEvent *){
|
|
|
|
checked = !checked;
|
|
|
|
emit checkedChanged(checked);
|
|
|
|
|
|
|
|
step = width() / 50;
|
|
|
|
|
|
|
|
if (checked){
|
|
|
|
|
|
|
|
endX = width() - height();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
endX = 0;
|
|
|
|
}
|
|
|
|
timer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkButton::resizeEvent(QResizeEvent *){
|
|
|
|
step = width() / 50;
|
|
|
|
|
|
|
|
if (checked){
|
|
|
|
startX = width() - height();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
startX = 0;
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkButton::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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void checkButton::setChecked(bool checked){
|
|
|
|
if (this->checked != checked){
|
|
|
|
this->checked = checked;
|
|
|
|
emit checkedChanged(checked);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
step = width() / 50;
|
|
|
|
|
|
|
|
if (checked){
|
|
|
|
endX = width() - height();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
endX = 0;
|
|
|
|
}
|
|
|
|
timer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkButton::isChecked(){
|
|
|
|
return this->checked;
|
|
|
|
}
|
|
|
|
|