-
Notifications
You must be signed in to change notification settings - Fork 15
/
WinHoverNet.cpp
520 lines (447 loc) · 16.8 KB
/
WinHoverNet.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include "WinHoverNet.h"
#include <QTimer>
#include <QDebug>
#include <QObjectList>
#include <QMessageBox>
#include <QIcon>
#include <QFile>
#include <QDate>
#include <QPalette>
#include <QMouseEvent>
#include <QDBusAbstractInterface>
#include <QDebug>
#include <climits>
/*!
* \brief WinHoverNet::WinHoverNet
* \param parent
*
* \htmlonly
* <pre style="font-family: FreeMono, Consolas, Menlo, 'Noto Mono', 'Courier New', Courier, monospace;line-height: 100%;">
* ==========================================================================
* || 上传标签 || 网速 + 单位 || CPU标签 || CPU 使用率 ||
* ==========================================================================
* || 下载标签 || 网速 + 单位 || 内存标签 || 内存 使用率 ||
* ==========================================================================
* </pre>
* \endhtmlonly
*
* \htmlonly
* <pre style="font-family: FreeMono, Consolas, Menlo, 'Noto Mono', 'Courier New', Courier, monospace;line-height: 100%;">
* =======================================
* || 上传标签 || 网速 + 单位 ||
* =======================================
* || 下载标签 || 网速 + 单位 ||
* =======================================
* || CPU标签 || CPU 使用率 ||
* =======================================
* || 内存标签 || 内存 使用率 ||
* =======================================
* </pre>
* \endhtmlonly
*/
WinHoverNet::WinHoverNet(Qt::Orientation orientation, QWidget *parent)
: WinTransparent(parent)
, m_winSetting(new WinSetting()) // 没设置父对象, null
, m_info(new MonitorInfo_x11())
, m_timer(new QTimer())
, m_modelUnit(Default)
, m_precision(0)
, m_orientation(orientation)
, m_gridLayout(new QGridLayout(this))
, m_vecLabel(8, nullptr)
{
init();
connect(m_timer, &QTimer::timeout, this, &WinHoverNet::onNet);
connect(m_timer, &QTimer::timeout, this, &WinHoverNet::onCpu);
connect(m_timer, &QTimer::timeout, this, &WinHoverNet::onMemory);
// connect(m_timer, &QTimer::timeout, this, &WinHoverNet::onSystemRunTime);
m_timer->setInterval(1000);
m_timer->start();
}
WinHoverNet *WinHoverNet::getInstance()
{
static WinHoverNet instance;
// qDebug()<<"--------------------->"<<&instance;
return &instance;
}
WinHoverNet::~WinHoverNet()
{
}
void WinHoverNet::init()
{
for (auto it = m_vecLabel.begin(); it != m_vecLabel.end(); ++it)
*it = new QLabel();
m_gridLayout->setContentsMargins(0, 0, 0, 0);
m_gridLayout->setSpacing(0);
initSigConnect();
m_winSetting->readConfig();
m_winSetting->onlyFirstEmitSig();
installEventFilter(this);
// m_winSetting->show();
if (!m_winSetting->isHorizontal())
m_orientation = Qt::Vertical;
setLabWidgetLayout(m_orientation);
m_info->netInfo(m_upload, m_down);
m_info->cpuInfo(m_vec);
// setSingleInstance(); // 注册 DBus
}
void WinHoverNet::initSigConnect()
{
//--------响应 "个性化" 发射的信号--------
connect(m_winSetting, &WinSetting::sigUnitModel, this, &WinHoverNet::onUnitModel);
connect(m_winSetting, &WinSetting::sigUnitModelIndex, this, &WinHoverNet::onUnitModelIndex);
connect(m_winSetting, &WinSetting::sigLabUploadText, this, &WinHoverNet::onLabUploadText);
connect(m_winSetting, &WinSetting::sigLabDownText, this, &WinHoverNet::onLabDownText);
connect(m_winSetting, &WinSetting::sigLabCpuText, this, &WinHoverNet::onLabCpuText);
connect(m_winSetting, &WinSetting::sigLabMemoryText, this, &WinHoverNet::onLabMemoryText);
connect(m_winSetting, &WinSetting::sigCurrentFont, this, &WinHoverNet::onCurrentFont);
connect(m_winSetting, &WinSetting::sigFontSize, this, &WinHoverNet::onFontSize);
// 此处是事件过滤器,修改颜色 3+1
// connect(m_winSetting, &WinSetting::sigTheme, this, &WinHoverNet::onTheme);
connect(m_winSetting, &WinSetting::sigWindowTop, this, &WinHoverNet::onWindowTop);
connect(m_winSetting, &WinSetting::sigLabTextColor, this, &WinHoverNet::onLabTextColor);
connect(m_winSetting, &WinSetting::sigTextColor, this, &WinHoverNet::onTextColor);
connect(m_winSetting, &WinSetting::sigBackgroundColor, this, &WinHoverNet::onBackgroundColor);
connect(m_winSetting, &WinSetting::sigBackgroundImage, this, &WinHoverNet::onBackgroundImage);
//--------响应 "常规配置" 发射的信号--------
connect(m_winSetting, &WinSetting::sigDisolayNet, this, &WinHoverNet::onDisolayNet);
connect(m_winSetting, &WinSetting::sigDisolayCPUAndMemory, this, &WinHoverNet::onDisolayCPUAndMemory);
connect(m_winSetting, &WinSetting::sigLocationExchangeNet, this, &WinHoverNet::onLocationExchangeNet);
connect(m_winSetting, &WinSetting::sigLocationExchangeCPUAndMenory, this, &WinHoverNet::onLocationExchangeCPUAndMenory);
connect(m_winSetting, &WinSetting::sigFractionalAccuracy, this, &WinHoverNet::onFractionalAccuracy);
connect(m_winSetting, &WinSetting::sigRefreshInterval, this, &WinHoverNet::onRefreshInterval);
connect(m_winSetting, &WinSetting::sigShowModel, this, &WinHoverNet::onShowModel);
// connect(m_winSetting, &WinSetting::sigBtnApplyWinMain, this, &WinHoverNet::onBtnApplyWinMain);
}
/*!
* \brief WinHoverNet::setLabWidgetLayout 设置 dde 的插件布局(水平 、垂直)
* \param[in] isHorizontal true 水平; false 垂直
*/
void WinHoverNet::setLabWidgetLayout(bool isHorizontal)
{
if (isHorizontal)
m_orientation = Qt::Horizontal;
else
m_orientation = Qt::Vertical;
setLabWidgetLayout(m_orientation);
}
/*!
* \brief WinHoverNet::setLabWidgetLayout 设置 dde 的插件布局(水平 、垂直)
* \param[in] orientation 希望设置为的布局方式
*/
void WinHoverNet::setLabWidgetLayout(Qt::Orientation orientation)
{
if (m_gridLayout == nullptr)
return;
const int nNum = m_gridLayout->children().count();
for (int i = 0; i < nNum; ++i) {
QLayoutItem *item = m_gridLayout->layout()->itemAt(i);
m_gridLayout->removeItem(item);
}
if (orientation == Qt::Horizontal && m_gridLayout->children().count() == 0) {
for (int i = 0; i < m_vecLabel.count(); i += 4) {
// // 调试布局代码
// m_vecLabel[i]->setAutoFillBackground(true);
// m_vecLabel[i + 1]->setAutoFillBackground(true);
// m_vecLabel[i + 2]->setAutoFillBackground(true);
// m_vecLabel[i + 3]->setAutoFillBackground(true);
// QPalette pa = m_vecLabel[i]->palette();
// pa.setColor(QPalette::Background, Qt::green);
// m_vecLabel[i]->setPalette(pa);
// pa.setColor(QPalette::Background, Qt::red);
// m_vecLabel[i + 1]->setPalette(pa);
// pa.setColor(QPalette::Background, Qt::blue);
// m_vecLabel[i + 2]->setPalette(pa);
// pa.setColor(QPalette::Background, Qt::gray);
// m_vecLabel[i + 3]->setPalette(pa);
int iTo2 = i % 2;
m_gridLayout->addWidget(m_vecLabel[i], 0, i / 2 + iTo2, Qt::AlignLeft | Qt::AlignBottom);
m_gridLayout->addWidget(m_vecLabel[i + 1], 0, (i + 1) / 2 + (i + 1) % 2, Qt::AlignRight | Qt::AlignBottom);
m_gridLayout->addWidget(m_vecLabel[i + 2], 1, i / 2 + iTo2, Qt::AlignLeft | Qt::AlignTop);
m_gridLayout->addWidget(m_vecLabel[i + 3], 1, (i + 1) / 2 + (i + 1) % 2, Qt::AlignRight | Qt::AlignTop);
const int space = -7;
m_vecLabel[i]->setContentsMargins(0, 0, 0, space);
m_vecLabel[i + 1]->setContentsMargins(0, 0, 0, space);
m_vecLabel[i + 2]->setContentsMargins(0, space, 0, 0);
m_vecLabel[i + 3]->setContentsMargins(0, space, 0, 0);
}
} else {
for (int i = 0; i < m_vecLabel.count(); i += 2) {
m_gridLayout->addWidget(m_vecLabel[i], i, 0, Qt::AlignLeft | Qt::AlignVCenter);
m_gridLayout->addWidget(m_vecLabel[i + 1], i, 1, Qt::AlignRight | Qt::AlignVCenter);
m_vecLabel[i]->setContentsMargins(0, 0, 0, 0);
m_vecLabel[i + 1]->setContentsMargins(0, 0, 0, 0);
}
}
QMargins margins(contentsMargins());
setContentsMargins(margins);
adjustSize();
}
/*!
* \brief WinHoverNet::setSingleInstance 若是启动,则注册 DBus 服务
* \return true 成功; false 失败
*/
bool WinHoverNet::setSingleInstance()
{
QDBusConnection sessionBus = QDBusConnection::sessionBus();
if (sessionBus.registerService("cn.ifmet.netspeed")
| sessionBus.registerObject("/", this, QDBusConnection::ExportAllContents))
return true;
else
return false;
}
/*!
* \brief WinHoverNet::onNet 定时刷新网速
*/
void WinHoverNet::onNet()
{
long upload = 0;
long down = 0;
NetUnit netUnit = Byte;
QString unit = "";
m_info->netInfo(upload, down);
double increaseUpload = m_info->netShowUnit((upload - m_upload) / (m_timer->interval() / 1000.0), netUnit);
unit = m_info->netModelUnit(netUnit, m_modelUnit);
m_vecLabel[1]->setText(QString("%1").arg(increaseUpload, 0, 'f', m_precision, QLatin1Char(' ')) + unit);
netUnit = Byte;
double increaseDown = m_info->netShowUnit((down - m_down) / (m_timer->interval() / 1000.0), netUnit);
unit = m_info->netModelUnit(netUnit, m_modelUnit);
m_vecLabel[3]->setText(QString("%1").arg(increaseDown, 0, 'f', m_precision, QLatin1Char(' ')) + unit);
m_upload = upload;
m_down = down;
}
/*!
* \brief WinHoverNet::onCpu 定时刷新 CPU
*/
void WinHoverNet::onCpu()
{
QVector<CpuInfo> vec;
m_info->cpuInfo(vec);
double valCpu = (vec.begin()->cpuWork - m_vec.begin()->cpuWork) * 100.0 / (vec.begin()->cpuAll - m_vec.begin()->cpuAll);
m_vecLabel[5]->setText(QString("%1%").arg(valCpu, 0, 'f', m_precision, QLatin1Char(' ')));
m_vec.begin()->cpuWork = vec.begin()->cpuWork;
m_vec.begin()->cpuAll = vec.begin()->cpuAll;
}
/*!
* \brief WinHoverNet::onMemory 定时刷新 内存
*/
void WinHoverNet::onMemory()
{
MemoryInfo info;
m_info->memoryInfo(info);
double mem = (info.memoryAll - info.memoryFree) * 100.0 / info.memoryAll;
m_vecLabel[7]->setText(QString("%1%").arg(mem, 0, 'f', m_precision, QLatin1Char(' ')));
}
///*!
// * \brief WinHoverNet::onSystemRunTime 定时刷新 系统开机运行时间
// */
//void WinHoverNet::onSystemRunTime()
//{
// double run = 0;
// double idle = 0;
// m_info->systemRunTime(run, idle);
// // ui->lab_32->setText(m_info->runTimeUnit(run));
//}
void WinHoverNet::onCurrentFont(const QFont &font)
{
int nCount = m_gridLayout->count();
for (int i = 0; i < nCount; ++i) {
QLayoutItem *it = m_gridLayout->itemAt(i);
QLabel * lab = static_cast<QLabel *>(it->widget());
lab->setFont(font);
}
}
void WinHoverNet::onFontSize(int size)
{
QFont font;
font.setPointSize(size);
int nCount = m_gridLayout->count();
for (int i = 0; i < nCount; ++i) {
QLayoutItem *it = m_gridLayout->itemAt(i);
QLabel * lab = static_cast<QLabel *>(it->widget());
lab->setFont(font);
}
}
void WinHoverNet::onLabTextColor(const QColor color)
{
QPalette palette;
palette.setColor(QPalette::WindowText, color);
m_vecLabel[0]->setPalette(palette);
m_vecLabel[2]->setPalette(palette);
m_vecLabel[4]->setPalette(palette);
m_vecLabel[6]->setPalette(palette);
}
void WinHoverNet::onTextColor(const QColor color)
{
QPalette palette;
palette.setColor(QPalette::WindowText, color);
m_vecLabel[1]->setPalette(palette);
m_vecLabel[3]->setPalette(palette);
m_vecLabel[5]->setPalette(palette);
m_vecLabel[7]->setPalette(palette);
}
void WinHoverNet::onBackgroundColor(const QColor color)
{
QPalette palette = this->palette();
palette.setColor(QPalette::Background, color);
setPalette(palette);
}
void WinHoverNet::onBackgroundImage(const QColor color)
{
}
/*!
* \brief WinHoverNet::onWindowTop 设置窗口是否置顶
* \param check true 置顶, false
* \see 参考:https://blog.csdn.net/GoForwardToStep/article/details/68938965
*/
void WinHoverNet::onWindowTop(bool check)
{
if (check) {
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowStaysOnTopHint);
} else {
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint); // 不是 Qt::WindowStaysOnBottomHint
}
if (!isVisible()) // 当前窗口不可见,则显示出来
setVisible(true);
}
/*!
* \brief WinHoverNet::onShowModel 插件水平还是垂直
* \param[in] check 现实模式中水平 (radioHorizontal) 控件是否被选中
* \line true 水平; false 垂直
*/
void WinHoverNet::onShowModel(bool check)
{
setLabWidgetLayout(check);
m_winSetting->changeOriePreviewUI();
}
void WinHoverNet::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton)
m_winSetting->show();
// qDebug()<<"-----------2222";
}
/*!
* \brief WinHoverNet::onUnitModel 单位显示模式
*/
void WinHoverNet::onUnitModel(const QString &text)
{
if (text == "Default")
m_modelUnit = ModelUnit::Default;
else if (text == "Upper")
m_modelUnit = ModelUnit::Upper;
else if (text == "Lower")
m_modelUnit = ModelUnit::Lower;
else if (text == "Mixed")
m_modelUnit = ModelUnit::Mixed;
}
void WinHoverNet::onUnitModelIndex(int index)
{
if (index == 0)
m_modelUnit = ModelUnit::Default;
else if (index == 1)
m_modelUnit = ModelUnit::Mixed;
else if (index == 2)
m_modelUnit = ModelUnit::Upper;
else if (index == 3)
m_modelUnit = ModelUnit::Lower;
}
void WinHoverNet::onLabUploadText(const QString &text)
{
if (m_vecLabel[0]->text() != text)
m_vecLabel[0]->setText(text);
}
void WinHoverNet::onLabDownText(const QString &text)
{
if (m_vecLabel[2]->text() != text)
m_vecLabel[2]->setText(text);
}
void WinHoverNet::onLabCpuText(const QString &text)
{
if (m_vecLabel[4]->text() != text)
m_vecLabel[4]->setText(text);
}
void WinHoverNet::onLabMemoryText(const QString &text)
{
if (m_vecLabel[6]->text() != text)
m_vecLabel[6]->setText(text);
}
void WinHoverNet::onDisolayNet(bool check)
{
if (check) {
m_vecLabel[0]->show();
m_vecLabel[1]->show();
m_vecLabel[2]->show();
m_vecLabel[3]->show();
} else {
m_vecLabel[0]->hide();
m_vecLabel[1]->hide();
m_vecLabel[2]->hide();
m_vecLabel[3]->hide();
}
}
void WinHoverNet::onDisolayCPUAndMemory(bool check)
{
if (check) {
m_vecLabel[4]->show();
m_vecLabel[5]->show();
m_vecLabel[6]->show();
m_vecLabel[7]->show();
} else {
m_vecLabel[4]->hide();
m_vecLabel[5]->hide();
m_vecLabel[6]->hide();
m_vecLabel[7]->hide();
}
}
void WinHoverNet::onLocationExchangeNet(bool check)
{
Q_UNUSED(check)
QLayoutItem* itemLabUp = m_gridLayout->itemAtPosition(0, 0);
QLayoutItem* itemUp = m_gridLayout->itemAtPosition(0, 1);
QLayoutItem* itemLabDown = m_gridLayout->itemAtPosition(1, 0);
QLayoutItem* itemDown = m_gridLayout->itemAtPosition(1, 1);
QLabel* labUp = static_cast<QLabel *>(itemLabUp->widget());
QLabel* up = static_cast<QLabel *>(itemUp->widget());
QLabel* labDown = static_cast<QLabel *>(itemLabDown->widget());
QLabel* down = static_cast<QLabel *>(itemDown->widget());
// 交换: 先移除, 后添加
m_gridLayout->removeWidget(labUp);
m_gridLayout->removeWidget(up);
m_gridLayout->removeWidget(labDown);
m_gridLayout->removeWidget(down);
m_gridLayout->addWidget(labDown, 0, 0);
m_gridLayout->addWidget(down, 0, 1);
m_gridLayout->addWidget(labUp, 1, 0);
m_gridLayout->addWidget(up, 1, 1);
}
void WinHoverNet::onLocationExchangeCPUAndMenory(bool check)
{
Q_UNUSED(check)
QLayoutItem* itemLabCpu = m_gridLayout->itemAtPosition(0, 2);
QLayoutItem* itemCpu = m_gridLayout->itemAtPosition(0, 3);
QLayoutItem* itemLabMem = m_gridLayout->itemAtPosition(1, 2);
QLayoutItem* itemMem = m_gridLayout->itemAtPosition(1, 3);
QLabel* labCpu = static_cast<QLabel *>(itemLabCpu->widget());
QLabel* cpu = static_cast<QLabel *>(itemCpu->widget());
QLabel* labMem = static_cast<QLabel *>(itemLabMem->widget());
QLabel* mem = static_cast<QLabel *>(itemMem->widget());
// 交换: 先移除, 后添加
m_gridLayout->removeWidget(labCpu);
m_gridLayout->removeWidget(cpu);
m_gridLayout->removeWidget(labMem);
m_gridLayout->removeWidget(mem);
m_gridLayout->addWidget(labMem, 0, 2);
m_gridLayout->addWidget(mem, 0, 3);
m_gridLayout->addWidget(labCpu, 1, 2);
m_gridLayout->addWidget(cpu, 1, 3);
}
void WinHoverNet::onFractionalAccuracy(int num)
{
if (m_precision != num)
m_precision = num;
}
void WinHoverNet::onRefreshInterval(int interval)
{
if (m_timer->interval() != interval)
m_timer->setInterval(interval);
}