forked from linuxdeepin/dde-network-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickpanel.cpp
212 lines (178 loc) · 5.25 KB
/
quickpanel.cpp
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
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "quickpanel.h"
#include <DLabel>
#include <DFontSizeManager>
#include <DIconButton>
#include <QMouseEvent>
#include <QVBoxLayout>
#include <QApplication>
DWIDGET_USE_NAMESPACE
const QSize IconSize(24, 24); // 图标大小
class HighlightIconEngine : public QIconEngine
{
public:
explicit HighlightIconEngine(QIcon icon = QIcon())
: QIconEngine()
, m_icon(icon)
{
}
virtual void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
{
QSize pixmapSize = rect.size();
qreal scale = 1;
if (painter->device() && (!QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps)))
scale = painter->device()->devicePixelRatioF();
pixmapSize *= scale;
QPixmap pm = m_icon.pixmap(pixmapSize, mode, state);
if (pm.isNull())
return;
if (state == QIcon::On) {
QPainter pa(&pm);
pa.setCompositionMode(QPainter::CompositionMode_SourceIn);
pa.fillRect(pm.rect(), qApp->palette().highlight());
}
pm.setDevicePixelRatio(scale);
painter->drawPixmap(rect, pm);
}
virtual QIconEngine *clone() const
{
return new HighlightIconEngine(m_icon);
}
private:
QIcon m_icon;
};
QuickPanel::QuickPanel(QWidget *parent)
: QWidget(parent)
, m_iconButton(new DIconButton(this))
, m_text(new DLabel(this))
, m_description(new DLabel(this))
, m_hover(false)
{
initUi();
initConnect();
}
const QVariant &QuickPanel::userData() const
{
return m_userData;
}
void QuickPanel::setUserData(const QVariant &data)
{
m_userData = data;
}
const QString QuickPanel::text() const
{
return m_text->text();
}
const QString QuickPanel::description() const
{
return m_description->text();
}
void QuickPanel::setIcon(const QIcon &icon)
{
m_iconButton->setIcon(QIcon(new HighlightIconEngine(icon)));
}
void QuickPanel::setText(const QString &text)
{
m_text->setText(text);
}
void QuickPanel::setDescription(const QString &description)
{
m_description->setText(description);
m_description->setToolTip(description);
}
void QuickPanel::setActive(bool active)
{
m_iconButton->setChecked(active);
}
void QuickPanel::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::RenderHint::Antialiasing);
const DPalette &dp = palette();
painter.setPen(Qt::NoPen);
painter.setBrush(dp.brush(m_hover ? DPalette::ObviousBackground : DPalette::ItemBackground));
painter.drawRoundedRect(rect(), 8, 8);
}
void QuickPanel::mouseReleaseEvent(QMouseEvent *event)
{
if (!m_iconButton->rect().contains(event->pos()) && rect().contains(event->pos())) {
emit panelClicked();
}
}
void QuickPanel::enterEvent(QEvent *event)
{
setHover(true);
}
void QuickPanel::leaveEvent(QEvent *event)
{
setHover(false);
}
bool QuickPanel::eventFilter(QObject *watched, QEvent *event)
{
switch (event->type()) {
case QEvent::Enter:
setHover(false);
break;
case QEvent::Leave:
setHover(true);
break;
default:
break;
}
return QWidget::eventFilter(watched, event);
}
void QuickPanel::initUi()
{
QHBoxLayout *mainLayout = new QHBoxLayout(this);
// 文本
QWidget *labelWidget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(labelWidget);
layout->setContentsMargins(0, 10, 0, 10);
layout->setSpacing(0);
QFont nameFont = DFontSizeManager::instance()->t6();
nameFont.setBold(true);
m_text->setFont(nameFont);
m_text->setElideMode(Qt::ElideRight);
layout->addWidget(m_text);
m_description->setFont(DFontSizeManager::instance()->t10());
m_description->setElideMode(Qt::ElideRight);
layout->addWidget(m_description);
// 图标
m_iconButton->setEnabledCircle(true);
m_iconButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_iconButton->setIconSize(IconSize);
m_iconButton->installEventFilter(this);
m_iconButton->setCheckable(true);
// 进入图标
QWidget *expandWidget = new QWidget(this);
QVBoxLayout *expandLayout = new QVBoxLayout(expandWidget);
QLabel *enterIcon = new QLabel(expandWidget);
qreal ratio = devicePixelRatioF();
QSize size = QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps) ? QSize(16, 16) : QSize(16, 16) * ratio;
QPixmap enterPixmap = DStyle::standardIcon(style(), DStyle::SP_ArrowEnter).pixmap(size);
enterPixmap.setDevicePixelRatio(ratio);
enterIcon->setPixmap(enterPixmap);
expandLayout->setContentsMargins(0, 0, 0, 0);
expandLayout->setSpacing(0);
expandLayout->addWidget(enterIcon);
mainLayout->setContentsMargins(10, 0, 10, 0);
mainLayout->setSpacing(0);
mainLayout->addWidget(m_iconButton);
mainLayout->addSpacing(10);
mainLayout->addWidget(labelWidget);
mainLayout->addStretch();
mainLayout->addWidget(expandWidget);
}
void QuickPanel::initConnect()
{
connect(m_iconButton, &DIconButton::clicked, this, &QuickPanel::iconClicked);
}
void QuickPanel::setHover(bool hover)
{
if (hover == m_hover)
return;
m_hover = hover;
update();
}