Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_round_hud_button.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <QPaintEvent>
10#include <QPainter>
11
12#include "kis_global.h"
13
14
15
17{
18 Private() : isHighlighted(false) {}
19
21 QIcon onIcon;
22 QIcon offIcon;
23};
24
26 : QAbstractButton(parent),
27 m_d(new Private)
28{
29 setMouseTracking(true);
30}
31
35
36void KisRoundHudButton::setOnOffIcons(const QIcon &on, const QIcon &off)
37{
38 m_d->onIcon = on;
39 m_d->offIcon = off;
40}
41
42void KisRoundHudButton::paintEvent(QPaintEvent */*event*/)
43{
44 const int borderWidth = 3;
45 const QPointF center = QRectF(rect()).center();
46 const qreal radius = 0.5 * (center.x() + center.y()) - borderWidth;
47
48 const QPen fgPen(palette().color(m_d->isHighlighted ? QPalette::Highlight : QPalette::WindowText), borderWidth);
49 const QBrush bgBrush(palette().brush(isDown() || (isCheckable() && isChecked()) ? QPalette::Mid : QPalette::Window));
50
51 QPainter painter(this);
52 painter.setPen(fgPen);
53 painter.setBrush(bgBrush);
54 painter.setRenderHints(QPainter::Antialiasing);
55
56 painter.drawEllipse(center, radius, radius);
57
58 if (!icon().isNull()) {
59 const QIcon::Mode mode = isEnabled() ? QIcon::Normal : QIcon::Disabled;
60 const QIcon::State state = isCheckable() && isChecked() ? QIcon::On : QIcon::Off;
61 const QSize size = iconSize();
62
63 QPixmap pixmap = icon().pixmap(size, mode, state);
64
65 QPointF iconOffset(0.5 * (width() - size.width()),
66 0.5 * (height() - size.height()));
67
68 painter.drawPixmap(iconOffset, pixmap);
69 }
70
71 if (!m_d->onIcon.isNull()) {
72 const QIcon::Mode mode = isEnabled() ? QIcon::Normal : QIcon::Disabled;
73 const QIcon icon = isCheckable() && isChecked() ? m_d->onIcon : m_d->offIcon;
74 const QSize size = iconSize();
75
76 QPixmap pixmap = icon.pixmap(size, mode);
77 QPointF iconOffset(0.5 * (width() - size.width()),
78 0.5 * (height() - size.height()));
79
80 painter.drawPixmap(iconOffset, pixmap);
81 }
82}
83
84bool KisRoundHudButton::hitButton(const QPoint &pos) const
85{
86 const int borderWidth = 3;
87 const QPointF center = QRectF(rect()).center();
88 const qreal radius = 0.5 * (center.x() + center.y()) - borderWidth;
89
90 return kisDistance(center, pos) < radius;
91}
92
93void KisRoundHudButton::mouseMoveEvent(QMouseEvent *event)
94{
95 bool isOver = hitButton(event->pos());
96 if (isOver != m_d->isHighlighted) {
97 m_d->isHighlighted = isOver;
98 update();
99 }
100
101 QAbstractButton::mouseMoveEvent(event);
102}
103
105{
106 Q_UNUSED(event);
107 if (m_d->isHighlighted) {
108 m_d->isHighlighted = false;
109 update();
110 }
111
112 QAbstractButton::leaveEvent(event);
113}
int iconSize(qreal width, qreal height)
void mouseMoveEvent(QMouseEvent *event) override
bool hitButton(const QPoint &pos) const override
KisRoundHudButton(QWidget *parent)
const QScopedPointer< Private > m_d
void leaveEvent(QEvent *event) override
void setOnOffIcons(const QIcon &on, const QIcon &off)
void paintEvent(QPaintEvent *event) override
qreal kisDistance(const QPointF &pt1, const QPointF &pt2)
Definition kis_global.h:190
rgba palette[MAX_PALETTE]
Definition palette.c:35