forked from linuxdeepin/dde-tray-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommoniconbutton.cpp
More file actions
237 lines (204 loc) · 5.99 KB
/
Copy pathcommoniconbutton.cpp
File metadata and controls
237 lines (204 loc) · 5.99 KB
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
// SPDX-FileCopyrightText: 2019 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "commoniconbutton.h"
#include <QMouseEvent>
#include <QPainter>
#include <QTimer>
#include <DGuiApplicationHelper>
DGUI_USE_NAMESPACE
CommonIconButton::CommonIconButton(QWidget *parent)
: QWidget(parent)
, m_refreshTimer(nullptr)
, m_clickable(false)
, m_hover(false)
, m_parentHover(false)
, m_state(Default)
, m_lightThemeColor(QColor(0, 0, 0, 178))
, m_darkThemeColor(QColor(255, 255, 255, 178))
, m_activeState(false)
, m_hoverEnable(true)
, m_iconSize(QSize())
, m_rotation(0)
{
setAccessibleName("IconButton");
setFixedSize(24, 24);
if (parent)
setForegroundRole(parent->foregroundRole());
m_defaultPalette = palette();
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &CommonIconButton::refreshIcon);
}
void CommonIconButton::setStateIconMapping(QMap<State, QPair<QString, QString>> mapping)
{
m_fileMapping = mapping;
}
void CommonIconButton::setState(State state)
{
m_state = state;
if (m_fileMapping.contains(state)) {
auto pair = m_fileMapping.value(state);
setIcon(pair.first, pair.second);
}
if (!m_icon.isNull()) {
updatePalette();
}
}
void CommonIconButton::startRotate()
{
if (!m_refreshTimer) {
m_refreshTimer = new QTimer(this);
m_refreshTimer->setInterval(70);
connect(m_refreshTimer, &QTimer::timeout, this, &CommonIconButton::startRotate);
}
m_refreshTimer->start();
m_rotation += 54;
update();
}
void CommonIconButton::stopRotate()
{
m_refreshTimer->stop();
m_rotation = 0;
update();
}
void CommonIconButton::setIcon(const QIcon &icon, QColor lightThemeColor, QColor darkThemeColor)
{
m_icon = icon;
if (lightThemeColor.isValid() && darkThemeColor.isValid()) {
m_lightThemeColor = lightThemeColor;
m_darkThemeColor = darkThemeColor;
}
updatePalette();
}
void CommonIconButton::updatePalette()
{
if (isEnabled()) {
if (m_lightThemeColor.isValid() && m_darkThemeColor.isValid()) {
if (!m_activeState) {
QColor color = DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType ? m_lightThemeColor : m_darkThemeColor;
if (m_hover || m_parentHover) {
color.setAlpha(255);
}
auto pa = palette();
pa.setColor(QPalette::WindowText, color);
setPalette(pa);
}
}
} else {
setPalette(m_defaultPalette);
}
update();
}
void CommonIconButton::setActiveState(bool state)
{
m_activeState = state;
if (m_lightThemeColor.isValid() && m_darkThemeColor.isValid()) {
updatePalette();
}
setForegroundRole(state ? QPalette::Highlight : QPalette::NoRole);
}
void CommonIconButton::setHoverEnable(bool enable)
{
m_hoverEnable = enable;
}
void CommonIconButton::setParentHover(bool hover)
{
m_parentHover = hover;
updatePalette();
}
void CommonIconButton::setIcon(const QString &icon, const QString &fallback, const QString &suffix)
{
if (!m_fileMapping.contains(Default)) {
m_fileMapping.insert(Default, QPair<QString, QString>(icon, fallback));
}
QString tmp = icon;
QString tmpFallback = fallback;
static auto addDarkMark = [suffix] (QString &file) {
if (file.contains(suffix)) {
file.replace(suffix, "-dark" + suffix);
} else {
file.append("-dark");
}
};
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) {
addDarkMark(tmp);
addDarkMark(tmpFallback);
}
m_icon = QIcon::fromTheme(tmp);
if (m_icon.isNull()) {
m_icon = QIcon::fromTheme(tmpFallback);
}
if (m_icon.isNull()) {
QString defaultIcon = m_fileMapping[State::Default].first;
m_icon = QIcon::fromTheme(defaultIcon);
}
update();
}
void CommonIconButton::setHoverIcon(const QIcon &icon)
{
m_hoverIcon = icon;
}
void CommonIconButton::setClickable(bool clickable)
{
m_clickable = clickable;
}
bool CommonIconButton::event(QEvent *e)
{
switch (e->type()) {
case QEvent::Leave:
case QEvent::Enter:
m_hover = e->type() == QEvent::Enter;
updatePalette();
break;
default:
break;
}
return QWidget::event(e);
}
void CommonIconButton::paintEvent(QPaintEvent *e)
{
QWidget::paintEvent(e);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
if (m_rotation != 0) {
painter.translate(this->width() / 2, this->height() / 2);
painter.rotate(m_rotation);
painter.translate(-(this->width() / 2), -(this->height() / 2));
}
if (m_hoverEnable && m_hover && !m_hoverIcon.isNull()) {
m_hoverIcon.paint(&painter, rect());
} else if (!m_icon.isNull()) {
if (!m_iconSize.isEmpty()) {
const int left = (width() - m_iconSize.width()) / 2;
const int top = (height() - m_iconSize.height()) / 2;
m_icon.paint(&painter, rect().marginsRemoved(QMargins(left, top, left, top)));
} else {
m_icon.paint(&painter, rect());
}
}
}
void CommonIconButton::mousePressEvent(QMouseEvent *event)
{
m_pressPos = event->pos();
return QWidget::mousePressEvent(event);
}
void CommonIconButton::mouseReleaseEvent(QMouseEvent *event)
{
if (m_clickable && rect().contains(m_pressPos) && rect().contains(event->pos()) && (!m_refreshTimer || !m_refreshTimer->isActive())) {
Q_EMIT clicked();
return;
}
return QWidget::mouseReleaseEvent(event);
}
void CommonIconButton::refreshIcon()
{
setState(m_state);
}
void CommonIconButton::setIconSize(const QSize &size)
{
m_iconSize = size;
}
void CommonIconButton::setAllEnabled(bool enable)
{
setEnabled(enable);
updatePalette();
}