1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
|
#include <QEventTransition>
#include <QFontDatabase>
#include <QIcon>
#include <QMouseEvent>
#include <QPainterPath>
#include <QResizeEvent>
#include <QSignalTransition>
#include "FlatButton.h"
#include "Ripple.h"
#include "RippleOverlay.h"
#include "ThemeManager.h"
// The ampersand is automatically set in QPushButton or QCheckbx
// by KDEPlatformTheme plugin in Qt5.
// [https://bugs.kde.org/show_bug.cgi?id=337491]
//
// A workaroud is to add
//
// [Development]
// AutoCheckAccelerators=false
//
// to ~/.config/kdeglobals
static QString
removeKDEAccelerators(QString text)
{
return text.remove(QChar('&'));
}
void
FlatButton::init()
{
ripple_overlay_ = new RippleOverlay(this);
state_machine_ = new FlatButtonStateMachine(this);
role_ = ui::Role::Default;
ripple_style_ = ui::RippleStyle::PositionedRipple;
icon_placement_ = ui::ButtonIconPlacement::LeftIcon;
overlay_style_ = ui::OverlayStyle::GrayOverlay;
bg_mode_ = Qt::TransparentMode;
fixed_ripple_radius_ = 64;
corner_radius_ = 3;
base_opacity_ = 0.13;
font_size_ = 10; // 10.5;
use_fixed_ripple_radius_ = false;
setStyle(&ThemeManager::instance());
setAttribute(Qt::WA_Hover);
setMouseTracking(true);
setCursor(QCursor(Qt::PointingHandCursor));
QPainterPath path;
path.addRoundedRect(rect(), corner_radius_, corner_radius_);
ripple_overlay_->setClipPath(path);
ripple_overlay_->setClipping(true);
state_machine_->setupProperties();
state_machine_->startAnimations();
}
FlatButton::FlatButton(QWidget *parent, ui::ButtonPreset preset)
: QPushButton(parent)
{
init();
applyPreset(preset);
}
FlatButton::FlatButton(const QString &text, QWidget *parent, ui::ButtonPreset preset)
: QPushButton(text, parent)
{
init();
applyPreset(preset);
}
FlatButton::FlatButton(const QString &text, ui::Role role, QWidget *parent, ui::ButtonPreset preset)
: QPushButton(text, parent)
{
init();
applyPreset(preset);
setRole(role);
}
FlatButton::~FlatButton() {}
void
FlatButton::applyPreset(ui::ButtonPreset preset)
{
switch (preset) {
case ui::ButtonPreset::FlatPreset:
setOverlayStyle(ui::OverlayStyle::NoOverlay);
break;
case ui::ButtonPreset::CheckablePreset:
setOverlayStyle(ui::OverlayStyle::NoOverlay);
setCheckable(true);
break;
default:
break;
}
}
void
FlatButton::setRole(ui::Role role)
{
role_ = role;
state_machine_->setupProperties();
}
ui::Role
FlatButton::role() const
{
return role_;
}
void
FlatButton::setForegroundColor(const QColor &color)
{
foreground_color_ = color;
}
QColor
FlatButton::foregroundColor() const
{
if (!foreground_color_.isValid()) {
if (bg_mode_ == Qt::OpaqueMode) {
return ThemeManager::instance().themeColor("BrightWhite");
}
switch (role_) {
case ui::Role::Primary:
return ThemeManager::instance().themeColor("Blue");
case ui::Role::Secondary:
return ThemeManager::instance().themeColor("Gray");
case ui::Role::Default:
default:
return ThemeManager::instance().themeColor("Black");
}
}
return foreground_color_;
}
void
FlatButton::setBackgroundColor(const QColor &color)
{
background_color_ = color;
}
QColor
FlatButton::backgroundColor() const
{
if (!background_color_.isValid()) {
switch (role_) {
case ui::Role::Primary:
return ThemeManager::instance().themeColor("Blue");
case ui::Role::Secondary:
return ThemeManager::instance().themeColor("Gray");
case ui::Role::Default:
default:
return ThemeManager::instance().themeColor("Black");
}
}
return background_color_;
}
void
FlatButton::setOverlayColor(const QColor &color)
{
overlay_color_ = color;
setOverlayStyle(ui::OverlayStyle::TintedOverlay);
}
QColor
FlatButton::overlayColor() const
{
if (!overlay_color_.isValid()) {
return foregroundColor();
}
return overlay_color_;
}
void
FlatButton::setDisabledForegroundColor(const QColor &color)
{
disabled_color_ = color;
}
QColor
FlatButton::disabledForegroundColor() const
{
if (!disabled_color_.isValid()) {
return ThemeManager::instance().themeColor("FadedWhite");
}
return disabled_color_;
}
void
FlatButton::setDisabledBackgroundColor(const QColor &color)
{
disabled_background_color_ = color;
}
QColor
FlatButton::disabledBackgroundColor() const
{
if (!disabled_background_color_.isValid()) {
return ThemeManager::instance().themeColor("FadedWhite");
}
return disabled_background_color_;
}
void
FlatButton::setFontSize(qreal size)
{
font_size_ = size;
QFont f(font());
f.setPixelSize(size);
setFont(f);
update();
}
qreal
FlatButton::fontSize() const
{
return font_size_;
}
void
FlatButton::setOverlayStyle(ui::OverlayStyle style)
{
overlay_style_ = style;
update();
}
ui::OverlayStyle
FlatButton::overlayStyle() const
{
return overlay_style_;
}
void
FlatButton::setRippleStyle(ui::RippleStyle style)
{
ripple_style_ = style;
}
ui::RippleStyle
FlatButton::rippleStyle() const
{
return ripple_style_;
}
void
FlatButton::setIconPlacement(ui::ButtonIconPlacement placement)
{
icon_placement_ = placement;
update();
}
ui::ButtonIconPlacement
FlatButton::iconPlacement() const
{
return icon_placement_;
}
void
FlatButton::setCornerRadius(qreal radius)
{
corner_radius_ = radius;
updateClipPath();
update();
}
qreal
FlatButton::cornerRadius() const
{
return corner_radius_;
}
void
FlatButton::setBackgroundMode(Qt::BGMode mode)
{
bg_mode_ = mode;
state_machine_->setupProperties();
}
Qt::BGMode
FlatButton::backgroundMode() const
{
return bg_mode_;
}
void
FlatButton::setBaseOpacity(qreal opacity)
{
base_opacity_ = opacity;
state_machine_->setupProperties();
}
qreal
FlatButton::baseOpacity() const
{
return base_opacity_;
}
void
FlatButton::setCheckable(bool value)
{
state_machine_->updateCheckedStatus();
state_machine_->setCheckedOverlayProgress(0);
QPushButton::setCheckable(value);
}
void
FlatButton::setHasFixedRippleRadius(bool value)
{
use_fixed_ripple_radius_ = value;
}
bool
FlatButton::hasFixedRippleRadius() const
{
return use_fixed_ripple_radius_;
}
void
FlatButton::setFixedRippleRadius(qreal radius)
{
fixed_ripple_radius_ = radius;
setHasFixedRippleRadius(true);
}
QSize
FlatButton::sizeHint() const
{
ensurePolished();
QSize label(fontMetrics().size(Qt::TextSingleLine, removeKDEAccelerators(text())));
int w = 20 + label.width();
int h = label.height();
if (!icon().isNull()) {
w += iconSize().width() + FlatButton::IconPadding;
h = qMax(h, iconSize().height());
}
return QSize(w, 20 + h);
}
void
FlatButton::checkStateSet()
{
state_machine_->updateCheckedStatus();
QPushButton::checkStateSet();
}
void
FlatButton::mousePressEvent(QMouseEvent *event)
{
if (ui::RippleStyle::NoRipple != ripple_style_) {
QPoint pos;
qreal radiusEndValue;
if (ui::RippleStyle::CenteredRipple == ripple_style_) {
pos = rect().center();
} else {
pos = event->pos();
}
if (use_fixed_ripple_radius_) {
radiusEndValue = fixed_ripple_radius_;
} else {
radiusEndValue = static_cast<qreal>(width()) / 2;
}
Ripple *ripple = new Ripple(pos);
ripple->setRadiusEndValue(radiusEndValue);
ripple->setOpacityStartValue(0.35);
ripple->setColor(foregroundColor());
ripple->radiusAnimation()->setDuration(250);
ripple->opacityAnimation()->setDuration(250);
ripple_overlay_->addRipple(ripple);
}
QPushButton::mousePressEvent(event);
}
void
FlatButton::mouseReleaseEvent(QMouseEvent *event)
{
QPushButton::mouseReleaseEvent(event);
state_machine_->updateCheckedStatus();
}
void
FlatButton::resizeEvent(QResizeEvent *event)
{
QPushButton::resizeEvent(event);
updateClipPath();
}
void
FlatButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
const qreal cr = corner_radius_;
if (cr > 0) {
QPainterPath path;
path.addRoundedRect(rect(), cr, cr);
painter.setClipPath(path);
painter.setClipping(true);
}
paintBackground(&painter);
painter.setOpacity(1);
painter.setClipping(false);
paintForeground(&painter);
}
void
FlatButton::paintBackground(QPainter *painter)
{
const qreal overlayOpacity = state_machine_->overlayOpacity();
const qreal checkedProgress = state_machine_->checkedOverlayProgress();
if (Qt::OpaqueMode == bg_mode_) {
QBrush brush;
brush.setStyle(Qt::SolidPattern);
if (isEnabled()) {
brush.setColor(backgroundColor());
} else {
brush.setColor(disabledBackgroundColor());
}
painter->setOpacity(1);
painter->setBrush(brush);
painter->setPen(Qt::NoPen);
painter->drawRect(rect());
}
QBrush brush;
brush.setStyle(Qt::SolidPattern);
painter->setPen(Qt::NoPen);
if (!isEnabled()) {
return;
}
if ((ui::OverlayStyle::NoOverlay != overlay_style_) && (overlayOpacity > 0)) {
if (ui::OverlayStyle::TintedOverlay == overlay_style_) {
brush.setColor(overlayColor());
} else {
brush.setColor(Qt::gray);
}
painter->setOpacity(overlayOpacity);
painter->setBrush(brush);
painter->drawRect(rect());
}
if (isCheckable() && checkedProgress > 0) {
const qreal q = Qt::TransparentMode == bg_mode_ ? 0.45 : 0.7;
brush.setColor(foregroundColor());
painter->setOpacity(q * checkedProgress);
painter->setBrush(brush);
QRect r(rect());
r.setHeight(static_cast<qreal>(r.height()) * checkedProgress);
painter->drawRect(r);
}
}
#define COLOR_INTERPOLATE(CH) (1 - progress) * source.CH() + progress *dest.CH()
void
FlatButton::paintForeground(QPainter *painter)
{
if (isEnabled()) {
painter->setPen(foregroundColor());
const qreal progress = state_machine_->checkedOverlayProgress();
if (isCheckable() && progress > 0) {
QColor source = foregroundColor();
QColor dest =
Qt::TransparentMode == bg_mode_ ? Qt::white : backgroundColor();
if (qFuzzyCompare(1, progress)) {
painter->setPen(dest);
} else {
painter->setPen(QColor(COLOR_INTERPOLATE(red),
COLOR_INTERPOLATE(green),
COLOR_INTERPOLATE(blue),
COLOR_INTERPOLATE(alpha)));
}
}
} else {
painter->setPen(disabledForegroundColor());
}
if (icon().isNull()) {
painter->drawText(rect(), Qt::AlignCenter, removeKDEAccelerators(text()));
return;
}
QSize textSize(fontMetrics().size(Qt::TextSingleLine, removeKDEAccelerators(text())));
QSize base(size() - textSize);
const int iw = iconSize().width() + IconPadding;
QPoint pos((base.width() - iw) / 2, 0);
QRect textGeometry(pos + QPoint(0, base.height() / 2), textSize);
QRect iconGeometry(pos + QPoint(0, (height() - iconSize().height()) / 2), iconSize());
/* if (ui::LeftIcon == icon_placement_) { */
/* textGeometry.translate(iw, 0); */
/* } else { */
/* iconGeometry.translate(textSize.width() + IconPadding, 0); */
/* } */
painter->drawText(textGeometry, Qt::AlignCenter, removeKDEAccelerators(text()));
QPixmap pixmap = icon().pixmap(iconSize());
QPainter icon(&pixmap);
icon.setCompositionMode(QPainter::CompositionMode_SourceIn);
icon.fillRect(pixmap.rect(), painter->pen().color());
painter->drawPixmap(iconGeometry, pixmap);
}
void
FlatButton::updateClipPath()
{
const qreal radius = corner_radius_;
QPainterPath path;
path.addRoundedRect(rect(), radius, radius);
ripple_overlay_->setClipPath(path);
}
FlatButtonStateMachine::FlatButtonStateMachine(FlatButton *parent)
: QStateMachine(parent)
, button_(parent)
, top_level_state_(new QState(QState::ParallelStates))
, config_state_(new QState(top_level_state_))
, checkable_state_(new QState(top_level_state_))
, checked_state_(new QState(checkable_state_))
, unchecked_state_(new QState(checkable_state_))
, neutral_state_(new QState(config_state_))
, neutral_focused_state_(new QState(config_state_))
, hovered_state_(new QState(config_state_))
, hovered_focused_state_(new QState(config_state_))
, pressed_state_(new QState(config_state_))
, overlay_opacity_(0)
, checked_overlay_progress_(parent->isChecked() ? 1 : 0)
, was_checked_(false)
{
Q_ASSERT(parent);
parent->installEventFilter(this);
config_state_->setInitialState(neutral_state_);
addState(top_level_state_);
setInitialState(top_level_state_);
checkable_state_->setInitialState(parent->isChecked() ? checked_state_ : unchecked_state_);
QSignalTransition *transition;
QPropertyAnimation *animation;
transition = new QSignalTransition(this, SIGNAL(buttonChecked()));
transition->setTargetState(checked_state_);
unchecked_state_->addTransition(transition);
animation = new QPropertyAnimation(this, "checkedOverlayProgress", this);
animation->setDuration(200);
transition->addAnimation(animation);
transition = new QSignalTransition(this, SIGNAL(buttonUnchecked()));
transition->setTargetState(unchecked_state_);
checked_state_->addTransition(transition);
animation = new QPropertyAnimation(this, "checkedOverlayProgress", this);
animation->setDuration(200);
transition->addAnimation(animation);
addTransition(button_, QEvent::FocusIn, neutral_state_, neutral_focused_state_);
addTransition(button_, QEvent::FocusOut, neutral_focused_state_, neutral_state_);
addTransition(button_, QEvent::Enter, neutral_state_, hovered_state_);
addTransition(button_, QEvent::Leave, hovered_state_, neutral_state_);
addTransition(button_, QEvent::Enter, neutral_focused_state_, hovered_focused_state_);
addTransition(button_, QEvent::Leave, hovered_focused_state_, neutral_focused_state_);
addTransition(button_, QEvent::FocusIn, hovered_state_, hovered_focused_state_);
addTransition(button_, QEvent::FocusOut, hovered_focused_state_, hovered_state_);
addTransition(this, SIGNAL(buttonPressed()), hovered_state_, pressed_state_);
addTransition(button_, QEvent::Leave, pressed_state_, neutral_focused_state_);
addTransition(button_, QEvent::FocusOut, pressed_state_, hovered_state_);
}
FlatButtonStateMachine::~FlatButtonStateMachine() {}
void
FlatButtonStateMachine::setOverlayOpacity(qreal opacity)
{
overlay_opacity_ = opacity;
button_->update();
}
void
FlatButtonStateMachine::setCheckedOverlayProgress(qreal opacity)
{
checked_overlay_progress_ = opacity;
button_->update();
}
void
FlatButtonStateMachine::startAnimations()
{
start();
}
void
FlatButtonStateMachine::setupProperties()
{
QColor overlayColor;
if (Qt::TransparentMode == button_->backgroundMode()) {
overlayColor = button_->backgroundColor();
} else {
overlayColor = button_->foregroundColor();
}
const qreal baseOpacity = button_->baseOpacity();
neutral_state_->assignProperty(this, "overlayOpacity", 0);
neutral_focused_state_->assignProperty(this, "overlayOpacity", 0);
hovered_state_->assignProperty(this, "overlayOpacity", baseOpacity);
hovered_focused_state_->assignProperty(this, "overlayOpacity", baseOpacity);
pressed_state_->assignProperty(this, "overlayOpacity", baseOpacity);
checked_state_->assignProperty(this, "checkedOverlayProgress", 1);
unchecked_state_->assignProperty(this, "checkedOverlayProgress", 0);
button_->update();
}
void
FlatButtonStateMachine::updateCheckedStatus()
{
const bool checked = button_->isChecked();
if (was_checked_ != checked) {
was_checked_ = checked;
if (checked) {
emit buttonChecked();
} else {
emit buttonUnchecked();
}
}
}
bool
FlatButtonStateMachine::eventFilter(QObject *watched, QEvent *event)
{
if (QEvent::FocusIn == event->type()) {
QFocusEvent *focusEvent = static_cast<QFocusEvent *>(event);
if (focusEvent && Qt::MouseFocusReason == focusEvent->reason()) {
emit buttonPressed();
return true;
}
}
return QStateMachine::eventFilter(watched, event);
}
void
FlatButtonStateMachine::addTransition(QObject *object,
const char *signal,
QState *fromState,
QState *toState)
{
addTransition(new QSignalTransition(object, signal), fromState, toState);
}
void
FlatButtonStateMachine::addTransition(QObject *object,
QEvent::Type eventType,
QState *fromState,
QState *toState)
{
addTransition(new QEventTransition(object, eventType), fromState, toState);
}
void
FlatButtonStateMachine::addTransition(QAbstractTransition *transition,
QState *fromState,
QState *toState)
{
transition->setTargetState(toState);
QPropertyAnimation *animation;
animation = new QPropertyAnimation(this, "overlayOpacity", this);
animation->setDuration(150);
transition->addAnimation(animation);
fromState->addTransition(transition);
}
|