| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhen Sun <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "kswitchbutton.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QDebug> |
| 26 |
#include <QMouseEvent> |
| 27 |
#include <QPainterPath> |
| 28 |
#include <QStyle> |
| 29 |
#include <QStyleOption> |
| 30 |
#include <QVariantAnimation> |
| 31 |
#include <QtMath> |
| 32 |
|
| 33 |
namespace kdk |
| 34 |
{ |
| 35 |
class KSwitchButtonPrivate : public QObject, public ThemeController |
| 36 |
{ |
| 37 |
Q_OBJECT |
| 38 |
Q_DECLARE_PUBLIC(KSwitchButton) |
| 39 |
|
| 40 |
public: |
| 41 |
KSwitchButtonPrivate(KSwitchButton *parent) |
| 42 |
: q_ptr(parent) |
| 43 |
, m_animation(new QVariantAnimation(parent)) |
| 44 |
{ |
| 45 |
setParent(parent); |
| 46 |
m_animation->setDuration(200); |
| 47 |
m_animation->setStartValue(0.0); |
| 48 |
m_animation->setEndValue(1.0); |
| 49 |
m_animation->setEasingCurve(QEasingCurve::Linear); |
| 50 |
m_animation->setLoopCount(1); |
| 51 |
} |
| 52 |
|
| 53 |
protected: |
| 54 |
void changeTheme(); |
| 55 |
|
| 56 |
private: |
| 57 |
KSwitchButton *q_ptr; |
| 58 |
|
| 59 |
QVariantAnimation *m_animation; |
| 60 |
int m_space; // 滑块离背景间隔 |
| 61 |
int m_rectRadius; // 圆角角度 |
| 62 |
int m_startX; // 滑块开始X轴坐标 |
| 63 |
int m_endX; // 滑块结束X轴坐标 |
| 64 |
QColor m_sliderColor; |
| 65 |
QColor m_bkgStartColor; |
| 66 |
QColor m_bkgEndColor; |
| 67 |
QColor m_borderColor; |
| 68 |
bool m_isHoverd; |
| 69 |
bool m_isTranslucent; |
| 70 |
bool m_isTranslucentFlag; |
| 71 |
QLinearGradient m_linearGradient; |
| 72 |
void updateColor(const QStyleOptionButton &opt); |
| 73 |
void drawBackground(QPainter *painter); |
| 74 |
void drawSlider(QPainter *painter); |
| 75 |
void stepChanged(); |
| 76 |
}; |
| 77 |
|
| 78 |
KSwitchButton::KSwitchButton(QWidget *parent) |
| 79 |
: QPushButton(parent) |
| 80 |
, d_ptr(new KSwitchButtonPrivate(this)) |
| 81 |
|
| 82 |
{ |
| 83 |
Q_D(KSwitchButton); |
| 84 |
d->m_isHoverd = false; |
| 85 |
d->m_isTranslucent = false; |
| 86 |
d->m_isTranslucentFlag = false; |
| 87 |
this->setCheckable(true); |
| 88 |
|
| 89 |
d->changeTheme(); |
| 90 |
connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 91 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 92 |
{ |
| 93 |
d->changeTheme(); |
| 94 |
} |
| 95 |
}); |
| 96 |
connect(d->m_animation, &QVariantAnimation::valueChanged, d, &KSwitchButtonPrivate::stepChanged); |
| 97 |
|
| 98 |
connect(this, &KSwitchButton::toggled, this, [=](bool flag) { |
| 99 |
d->m_endX = width() > height() ? width() - height() : 0; |
| 100 |
d->m_animation->start(); |
| 101 |
Q_EMIT stateChanged(flag); |
| 102 |
}); |
| 103 |
d->m_space = 4; |
| 104 |
} |
| 105 |
|
| 106 |
KSwitchButton::~KSwitchButton() |
| 107 |
{ |
| 108 |
} |
| 109 |
|
| 110 |
void KSwitchButton::setCheckable(bool flag) |
| 111 |
{ |
| 112 |
QPushButton::setCheckable(flag); |
| 113 |
this->update(); |
| 114 |
} |
| 115 |
|
| 116 |
bool KSwitchButton::isCheckable() const |
| 117 |
{ |
| 118 |
return QPushButton::isCheckable(); |
| 119 |
} |
| 120 |
|
| 121 |
bool KSwitchButton::isChecked() const |
| 122 |
{ |
| 123 |
return QPushButton::isChecked(); |
| 124 |
} |
| 125 |
|
| 126 |
void KSwitchButton::setChecked(bool flag) |
| 127 |
{ |
| 128 |
Q_D(KSwitchButton); |
| 129 |
QPushButton::setChecked(flag); |
| 130 |
if (signalsBlocked()) { |
| 131 |
d->m_animation->start(); |
| 132 |
} |
| 133 |
this->update(); |
| 134 |
} |
| 135 |
|
| 136 |
void KSwitchButton::setTranslucent(bool flag) |
| 137 |
{ |
| 138 |
Q_D(KSwitchButton); |
| 139 |
d->m_isTranslucentFlag = flag; |
| 140 |
d->m_isTranslucent = flag; |
| 141 |
d->changeTheme(); |
| 142 |
} |
| 143 |
|
| 144 |
bool KSwitchButton::isTranslucent() |
| 145 |
{ |
| 146 |
Q_D(KSwitchButton); |
| 147 |
return d->m_isTranslucent; |
| 148 |
} |
| 149 |
|
| 150 |
void KSwitchButton::paintEvent(QPaintEvent *event) |
| 151 |
{ |
| 152 |
Q_D(KSwitchButton); |
| 153 |
|
| 154 |
d->m_linearGradient = QLinearGradient(this->width() / 2, 0, this->width() / 2, this->height()); |
| 155 |
QStyleOptionButton opt; |
| 156 |
initStyleOption(&opt); |
| 157 |
QPainter painter(this); |
| 158 |
painter.setRenderHint(QPainter::Antialiasing); |
| 159 |
d->updateColor(opt); |
| 160 |
d->drawBackground(&painter); |
| 161 |
d->drawSlider(&painter); |
| 162 |
painter.drawText(0, 0, width(), height(), Qt::AlignCenter, text()); |
| 163 |
} |
| 164 |
|
| 165 |
void KSwitchButton::resizeEvent(QResizeEvent *event) |
| 166 |
{ |
| 167 |
Q_D(KSwitchButton); |
| 168 |
QPushButton::resizeEvent(event); |
| 169 |
if (this->isChecked()) |
| 170 |
d->m_startX = width() > height() ? width() - height() : 0; |
| 171 |
else |
| 172 |
d->m_startX = 0; |
| 173 |
d->m_rectRadius = height() / 2; |
| 174 |
d->m_endX = width() > height() ? width() - height() : 0; |
| 175 |
} |
| 176 |
|
| 177 |
QSize KSwitchButton::sizeHint() const |
| 178 |
{ |
| 179 |
return QSize(50, 24); |
| 180 |
} |
| 181 |
|
| 182 |
void KSwitchButtonPrivate::changeTheme() |
| 183 |
{ |
| 184 |
Q_Q(KSwitchButton); |
| 185 |
if (m_isTranslucentFlag) { |
| 186 |
if (ThemeController::widgetTheme() == ClassicTheme) |
| 187 |
m_isTranslucent = false; |
| 188 |
else |
| 189 |
m_isTranslucent = true; |
| 190 |
} else { |
| 191 |
m_isTranslucent = false; |
| 192 |
} |
| 193 |
initThemeStyle(); |
| 194 |
q->update(); |
| 195 |
} |
| 196 |
|
| 197 |
void KSwitchButtonPrivate::updateColor(const QStyleOptionButton &opt) |
| 198 |
{ |
| 199 |
Q_Q(KSwitchButton); |
| 200 |
|
| 201 |
// 根据option状态去指定以下两个子部件的颜色 |
| 202 |
// m_sliderColor |
| 203 |
// m_linearGradient |
| 204 |
if(q->isChecked()) |
| 205 |
{ |
| 206 |
ThemeController::getGradientFromDT("kbrand-normal",m_bkgStartColor,m_bkgEndColor); |
| 207 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 208 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 209 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-normal"); |
| 210 |
m_sliderColor = ThemeController::getCustomColorFromDT("kfont-white"); |
| 211 |
if (!opt.state.testFlag(QStyle::State_Enabled)) // disable |
| 212 |
{ |
| 213 |
m_sliderColor = ThemeController::getCustomColorFromDT("kfont-white-disable"); |
| 214 |
// m_bkgStartColor=ThemeController::getCustomColorFromDT("kcomponent-disable"); |
| 215 |
ThemeController::getGradientFromDT("kbrand-disable",m_bkgStartColor,m_bkgEndColor); |
| 216 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 217 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 218 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-disable"); |
| 219 |
return; |
| 220 |
} |
| 221 |
if (opt.state.testFlag(QStyle::State_MouseOver)) |
| 222 |
{ |
| 223 |
if (opt.state & QStyle::State_Sunken) // clicked |
| 224 |
{ |
| 225 |
m_isHoverd = false; |
| 226 |
ThemeController::getGradientFromDT("kbrand-click",m_bkgStartColor,m_bkgEndColor); |
| 227 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 228 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 229 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 230 |
} |
| 231 |
else // hover |
| 232 |
{ |
| 233 |
m_isHoverd = true; |
| 234 |
ThemeController::getGradientFromDT("kbrand-hover",m_bkgStartColor,m_bkgEndColor); |
| 235 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 236 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 237 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-hover"); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
else |
| 242 |
{ |
| 243 |
if(m_isTranslucent) |
| 244 |
{ |
| 245 |
ThemeController::getGradientFromDT("kcomponent-alpha-normal",m_bkgStartColor,m_bkgEndColor); |
| 246 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 247 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 248 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-normal"); |
| 249 |
m_sliderColor = ThemeController::getCustomColorFromDT("kfont-white"); |
| 250 |
if (!opt.state.testFlag(QStyle::State_Enabled)) // disable |
| 251 |
{ |
| 252 |
m_sliderColor = ThemeController::getCustomColorFromDT("kfont-white-disable"); |
| 253 |
ThemeController::getGradientFromDT("kcomponent-alpha-disable",m_bkgStartColor,m_bkgEndColor); |
| 254 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 255 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 256 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-disable"); |
| 257 |
return; |
| 258 |
} |
| 259 |
if (opt.state.testFlag(QStyle::State_MouseOver)) |
| 260 |
{ |
| 261 |
if (opt.state & QStyle::State_Sunken) // clicked |
| 262 |
{ |
| 263 |
m_isHoverd = false; |
| 264 |
ThemeController::getGradientFromDT("kcomponent-alpha-click",m_bkgStartColor,m_bkgEndColor); |
| 265 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 266 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 267 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 268 |
} |
| 269 |
else // hover |
| 270 |
{ |
| 271 |
m_isHoverd = true; |
| 272 |
ThemeController::getGradientFromDT("kcomponent-alpha-hover",m_bkgStartColor,m_bkgEndColor); |
| 273 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 274 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 275 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-hover"); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
ThemeController::getGradientFromDT("kcomponent-normal",m_bkgStartColor,m_bkgEndColor); |
| 282 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 283 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 284 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-normal"); |
| 285 |
m_sliderColor = ThemeController::getCustomColorFromDT("kfont-white"); |
| 286 |
if (!opt.state.testFlag(QStyle::State_Enabled)) // disable |
| 287 |
{ |
| 288 |
m_sliderColor = ThemeController::getCustomColorFromDT("kfont-white-disable"); |
| 289 |
ThemeController::getGradientFromDT("kcomponent-disable",m_bkgStartColor,m_bkgEndColor); |
| 290 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 291 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 292 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-disable"); |
| 293 |
return; |
| 294 |
} |
| 295 |
if (opt.state.testFlag(QStyle::State_MouseOver)) |
| 296 |
{ |
| 297 |
if (opt.state & QStyle::State_Sunken) // clicked |
| 298 |
{ |
| 299 |
m_isHoverd = false; |
| 300 |
ThemeController::getGradientFromDT("kcomponent-click",m_bkgStartColor,m_bkgEndColor); |
| 301 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 302 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 303 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 304 |
} |
| 305 |
else // hover |
| 306 |
{ |
| 307 |
m_isHoverd = true; |
| 308 |
ThemeController::getGradientFromDT("kcomponent-hover",m_bkgStartColor,m_bkgEndColor); |
| 309 |
m_linearGradient.setColorAt(0,m_bkgStartColor); |
| 310 |
m_linearGradient.setColorAt(1,m_bkgEndColor); |
| 311 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-hover"); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
void KSwitchButtonPrivate::drawBackground(QPainter *painter) |
| 319 |
{ |
| 320 |
Q_Q(KSwitchButton); |
| 321 |
painter->save(); |
| 322 |
painter->setPen(m_borderColor); |
| 323 |
painter->setBrush(m_linearGradient); |
| 324 |
QRect rect(0, 0, q->width() , q->height() ); |
| 325 |
|
| 326 |
// 背景框两边是半圆,所以圆角的半径应该固定是矩形高度的一半 |
| 327 |
int radius = rect.height() / 2; |
| 328 |
int circleWidth = rect.height(); |
| 329 |
QPainterPath path; |
| 330 |
path.moveTo(radius, rect.top()); |
| 331 |
path.arcTo(QRectF(rect.left(), rect.top(), circleWidth, circleWidth), 90, 180); |
| 332 |
path.lineTo(rect.width() - radius, rect.height()); |
| 333 |
path.arcTo(QRectF(rect.width() - rect.height(), rect.top(), circleWidth, circleWidth), 270, 180); |
| 334 |
path.lineTo(radius, rect.top()); |
| 335 |
painter->drawPath(path); |
| 336 |
painter->restore(); |
| 337 |
} |
| 338 |
|
| 339 |
void KSwitchButtonPrivate::drawSlider(QPainter *painter) |
| 340 |
{ |
| 341 |
Q_Q(KSwitchButton); |
| 342 |
painter->save(); |
| 343 |
painter->setRenderHint(QPainter::Antialiasing); |
| 344 |
painter->setRenderHint(QPainter::TextAntialiasing); |
| 345 |
painter->setRenderHint(QPainter::SmoothPixmapTransform); |
| 346 |
|
| 347 |
painter->setPen(Qt::NoPen); |
| 348 |
painter->setBrush(m_sliderColor); |
| 349 |
// 滑块的直径 |
| 350 |
int sliderWidth = q->height() - m_space * 2; |
| 351 |
QRect sliderRect(m_startX + m_space, m_space, sliderWidth, sliderWidth); |
| 352 |
painter->drawEllipse(sliderRect); |
| 353 |
// if (!q->isEnabled()) { |
| 354 |
// // 绘制disenable状态下的小矩形条 |
| 355 |
// if (!q->isChecked()) { |
| 356 |
// int topX = q->width() - sliderWidth / 2 - 10; |
| 357 |
// int topY = (q->height() - sliderWidth / 4) / 2; |
| 358 |
// QRect lineRect(topX, topY, sliderWidth / 2, sliderWidth / 4); |
| 359 |
// int radius = lineRect.height() / 2; |
| 360 |
// painter->drawRoundedRect(lineRect, radius, radius); |
| 361 |
// } else { |
| 362 |
// int topX = 10; |
| 363 |
// int topY = (q->height() - sliderWidth / 4) / 2; |
| 364 |
// QRect lineRect(topX, topY, sliderWidth / 2, sliderWidth / 4); |
| 365 |
// int radius = lineRect.height() / 2; |
| 366 |
// painter->drawRoundedRect(lineRect, radius, radius); |
| 367 |
// } |
| 368 |
// } |
| 369 |
painter->restore(); |
| 370 |
} |
| 371 |
|
| 372 |
void KSwitchButtonPrivate::stepChanged() |
| 373 |
{ |
| 374 |
Q_Q(KSwitchButton); |
| 375 |
if (q->isChecked()) { |
| 376 |
if (m_startX < m_endX) { |
| 377 |
m_startX = qCeil(m_endX * m_animation->currentValue().toDouble()); |
| 378 |
} else { |
| 379 |
m_startX = m_endX; |
| 380 |
m_animation->stop(); |
| 381 |
} |
| 382 |
} else { |
| 383 |
if (m_startX > 0) { |
| 384 |
double percent = 1.0 - m_animation->currentValue().toDouble(); |
| 385 |
m_startX = qCeil(m_endX * percent); |
| 386 |
} else { |
| 387 |
m_startX = 0; |
| 388 |
m_animation->stop(); |
| 389 |
} |
| 390 |
} |
| 391 |
q->update(); |
| 392 |
} |
| 393 |
|
| 394 |
} |
| 395 |
#include "kswitchbutton.moc" |
| 396 |
#include "moc_kswitchbutton.cpp" |