forked from linuxdeepin/dde-tray-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickpanelwidget.cpp
More file actions
216 lines (185 loc) · 6.3 KB
/
Copy pathquickpanelwidget.cpp
File metadata and controls
216 lines (185 loc) · 6.3 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
// Copyright (C) 2022 ~ 2022 Deepin Technology Co., Ltd.
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "quickpanelwidget.h"
#include <DFontSizeManager>
#include <DStyle>
#include <DGuiApplicationHelper>
#include <QHBoxLayout>
#include <QLabel>
#include <QMouseEvent>
#include <QEnterEvent>
#include <QPainter>
#include <QPainterPath>
#include <DToolTip>
DGUI_USE_NAMESPACE
DWIDGET_USE_NAMESPACE
const QSize IconSize(22, 22); // 图标大小
class QuickButton : public DFloatingButton
{
public:
QuickButton(QWidget *parent = nullptr)
: DFloatingButton(parent)
, m_parentHover(false)
{
}
void setParentHover(bool hover) {
if (m_parentHover == hover)
return;
m_parentHover = hover;
update();
}
protected:
void initStyleOption(DStyleOptionButton *option) const override
{
DFloatingButton::initStyleOption(option);
QColor bgColor = option->dpalette.color(backgroundRole());
QColor textColor = option->dpalette.color(foregroundRole());
if (backgroundRole() == QPalette::Highlight) {
textColor = Qt::white;
if (!option->state.testFlag(QStyle::State_Raised)) { // press
bgColor.setHslF(bgColor.hslHueF(), bgColor.hslSaturationF(), bgColor.lightnessF() * 0.9);
textColor.setAlphaF(0.8);
} else if (option->state.testFlag(QStyle::State_MouseOver)) { // hover
bgColor.setHslF(bgColor.hslHueF(), bgColor.hslSaturationF(), bgColor.lightnessF() * 1.1);
}
} else {
textColor.setAlphaF(m_parentHover ? 1.0 : 0.7);
if (!option->state.testFlag(QStyle::State_Raised)) { // press
bgColor.setAlphaF(0.2);
} else if (option->state.testFlag(QStyle::State_MouseOver) || m_parentHover) { // hover
bgColor.setAlphaF(0.15);
} else { // normal
bgColor.setAlphaF(0.1);
}
}
option->palette.setBrush(QPalette::Button, bgColor);
option->palette.setBrush(QPalette::ButtonText, textColor);
option->state.setFlag(QStyle::State_MouseOver, false);
option->state.setFlag(QStyle::State_Sunken, false);
option->state.setFlag(QStyle::State_Raised, true);
}
private:
bool m_parentHover;
};
QuickPanelWidget::QuickPanelWidget(QWidget *parent)
: QWidget(parent)
, m_iconWidget(new QuickButton(this))
, m_nameLabel(new DLabel(this))
, m_stateLabel(new DLabel(this))
, m_expandLabel(new DIconButton(this))
, m_hover(false)
{
initUi();
initConnection();
}
QuickPanelWidget::~QuickPanelWidget() { }
void QuickPanelWidget::setIcon(const QIcon &icon)
{
m_iconWidget->setIcon(icon);
}
void QuickPanelWidget::setText(const QString &text)
{
m_nameLabel->setText(text);
}
void QuickPanelWidget::setDescription(const QString &description)
{
m_stateLabel->setText(description);
}
void QuickPanelWidget::setActive(bool active)
{
m_iconWidget->setBackgroundRole(active ? QPalette::Highlight : QPalette::BrightText);
}
void QuickPanelWidget::mousePressEvent(QMouseEvent *event)
{
switch (event->button()) {
case Qt::LeftButton:
m_clickPoint = event->pos();
break;
default:
break;
}
QWidget::mousePressEvent(event);
}
void QuickPanelWidget::mouseReleaseEvent(QMouseEvent *event)
{
switch (event->button()) {
case Qt::LeftButton:
if (m_clickPoint == event->pos())
Q_EMIT panelClicked();
break;
default:
break;
}
m_clickPoint = QPoint();
return QWidget::mouseReleaseEvent(event);
}
void QuickPanelWidget::initUi()
{
QWidget *labelWidget = new QWidget(this);
DFontSizeManager::instance()->bind(m_nameLabel, DFontSizeManager::T6, QFont::Normal);
DToolTip::setToolTipShowMode(m_nameLabel, DToolTip::ShowWhenElided);
m_nameLabel->setElideMode(Qt::ElideRight);
m_nameLabel->setContentsMargins(0, 2, 0, 0);
m_nameLabel->setForegroundRole(QPalette::BrightText);
DFontSizeManager::instance()->bind(m_stateLabel, DFontSizeManager::T10);
DToolTip::setToolTipShowMode(m_stateLabel, DToolTip::ShowWhenElided);
m_stateLabel->setElideMode(Qt::ElideRight);
QVBoxLayout *layout = new QVBoxLayout(labelWidget);
layout->setContentsMargins(0, 8, 0, 8);
layout->setSpacing(0);
layout->addWidget(m_nameLabel);
layout->addWidget(m_stateLabel);
// 图标
m_iconWidget->setEnabledCircle(true);
m_iconWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_iconWidget->setIconSize(IconSize);
m_iconWidget->setCheckable(false);
m_iconWidget->setFixedSize(QSize(40, 40));
m_iconWidget->setFocusPolicy(Qt::NoFocus);
// 进入图标
m_expandLabel->setIcon(DStyle::standardIcon(style(), DStyle::SP_ArrowEnter));
m_expandLabel->setFlat(true);
m_expandLabel->setFocusPolicy(Qt::NoFocus);
m_expandLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setContentsMargins(10, 0, 10, 0);
mainLayout->setSpacing(0);
mainLayout->addWidget(m_iconWidget);
mainLayout->addSpacing(10);
mainLayout->addWidget(labelWidget);
mainLayout->addStretch(1);
mainLayout->addWidget(m_expandLabel);
setActive(true);
setFixedSize(150, 60);
}
void QuickPanelWidget::initConnection()
{
connect(m_iconWidget, &DFloatingButton::clicked, this, &QuickPanelWidget::iconClicked);
}
void QuickPanelWidget::enterEvent(QEnterEvent *event)
{
m_hover = true;
m_iconWidget->setParentHover(true);
updateTextColor(true);
QWidget::enterEvent(event);
}
void QuickPanelWidget::leaveEvent(QEvent *event)
{
m_hover = false;
m_iconWidget->setParentHover(false);
updateTextColor(false);
QWidget::leaveEvent(event);
}
void QuickPanelWidget::updateTextColor(bool hover)
{
bool isLight = DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType;
QColor color = isLight ? QColor(0, 0, 0) : QColor(255, 255, 255);
color.setAlphaF(hover ? 1.0 : 0.7);
QPalette pa = m_nameLabel->palette();
pa.setColor(QPalette::BrightText, color);
pa.setColor(QPalette::WindowText, color);
m_nameLabel->setPalette(pa);
m_stateLabel->setPalette(pa);
}