-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwidget.cpp
More file actions
160 lines (137 loc) · 4.53 KB
/
Copy pathmainwidget.cpp
File metadata and controls
160 lines (137 loc) · 4.53 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
#include "mainwidget.h"
#include <QPalette>
#include <QRegularExpression>
MainWidget::MainWidget(Dock::Position position)
: m_upArrow(nullptr)
, m_upNum(nullptr)
, m_upUnit(nullptr)
, m_downArrow(nullptr)
, m_downNum(nullptr)
, m_downUnit(nullptr)
, m_layout(nullptr)
, m_arrowWidth(0)
, m_numWidth(0)
, m_scale(100)
, m_position(position)
{
m_dpi = QApplication::primaryScreen()->logicalDotsPerInch();
m_font.setFamily("Noto Mono");
applyScale();
recalcFixedWidths();
m_upArrow = new QLabel(this);
m_upNum = new QLabel(this);
m_upUnit = new QLabel(this);
m_downArrow = new QLabel(this);
m_downNum = new QLabel(this);
m_downUnit = new QLabel(this);
m_upArrow->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
m_upNum->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
m_upUnit->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
m_downArrow->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
m_downNum->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
m_downUnit->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
m_upArrow->setFont(m_font);
m_upNum->setFont(m_font);
m_upUnit->setFont(m_font);
m_downArrow->setFont(m_font);
m_downNum->setFont(m_font);
m_downUnit->setFont(m_font);
m_upArrow->setFixedWidth(m_arrowWidth);
m_downArrow->setFixedWidth(m_arrowWidth);
m_upNum->setFixedWidth(m_numWidth);
m_downNum->setFixedWidth(m_numWidth);
m_layout = new QGridLayout;
m_layout->addWidget(m_upArrow, 0, 0);
m_layout->addWidget(m_upNum, 0, 1);
m_layout->addWidget(m_upUnit, 0, 2);
m_layout->addWidget(m_downArrow, 1, 0);
m_layout->addWidget(m_downNum, 1, 1);
m_layout->addWidget(m_downUnit, 1, 2);
m_layout->setContentsMargins(4, 0, 4, 0);
m_layout->setSpacing(0);
m_layout->setHorizontalSpacing(0);
setLayout(m_layout);
setAutoFillBackground(true);
QPalette pal = palette();
pal.setColor(QPalette::Window, Qt::transparent);
setPalette(pal);
updateSpeed({0, 0, "0 B/s", "0 B/s"}, position);
}
MainWidget::~MainWidget() = default;
void MainWidget::applyScale()
{
int pixelSize = qMax(6, (m_dpi * 9 * m_scale) / (72 * 100));
m_font.setPixelSize(pixelSize);
}
void MainWidget::recalcFixedWidths()
{
QFontMetrics fm(m_font);
m_arrowWidth = fm.horizontalAdvance("↑") + 2;
m_numWidth = fm.horizontalAdvance("999.9") + 2;
if (m_upArrow) {
m_upArrow->setFixedWidth(m_arrowWidth);
m_downArrow->setFixedWidth(m_arrowWidth);
m_upNum->setFixedWidth(m_numWidth);
m_downNum->setFixedWidth(m_numWidth);
m_upArrow->setFont(m_font);
m_upNum->setFont(m_font);
m_upUnit->setFont(m_font);
m_downArrow->setFont(m_font);
m_downNum->setFont(m_font);
m_downUnit->setFont(m_font);
}
}
void MainWidget::setScale(int scale)
{
m_scale = qBound(50, scale, 200);
applyScale();
recalcFixedWidths();
updateGeometry();
}
void MainWidget::parseSpeed(const QString &speed, QString &num, QString &unit) const
{
QRegularExpression re("^([\\d.]+)\\s*(.*)$");
auto match = re.match(speed);
if (match.hasMatch()) {
num = match.captured(1);
unit = match.captured(2);
} else {
num = speed;
unit.clear();
}
}
void MainWidget::updateSpeed(const NetSpeedInfo &info, Dock::Position position)
{
m_position = position;
QColor textColor = palette().color(QPalette::WindowText);
QString colorName = textColor.name();
QString style = QString("QLabel { color: %1; }").arg(colorName);
m_upArrow->setStyleSheet(style);
m_upNum->setStyleSheet(style);
m_upUnit->setStyleSheet(style);
m_downArrow->setStyleSheet(style);
m_downNum->setStyleSheet(style);
m_downUnit->setStyleSheet(style);
QString upNum, upUnit, downNum, downUnit;
parseSpeed(info.upSpeed, upNum, upUnit);
parseSpeed(info.downSpeed, downNum, downUnit);
m_upArrow->setText("↑");
m_upNum->setText(upNum);
m_upUnit->setText(QString("%1/s").arg(upUnit));
m_downArrow->setText("↓");
m_downNum->setText(downNum);
m_downUnit->setText(QString("%1/s").arg(downUnit));
setMinimumSize(0, 0);
setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
updateGeometry();
}
QSize MainWidget::sizeHint() const
{
if (!m_upNum || !m_upUnit) {
return QSize(90, 24);
}
QFontMetrics fm(m_font);
int lineH = fm.height();
int unitW = fm.horizontalAdvance("MB/s");
return QSize(m_arrowWidth + m_numWidth + unitW + 8, lineH * 2 + 4);
}