-
Notifications
You must be signed in to change notification settings - Fork 28
/
cmduwidget.cpp
62 lines (55 loc) · 1.6 KB
/
cmduwidget.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
#include "cmduwidget.h"
#include "dde-dock/constants.h"
#include <QApplication>
#include <QPainter>
#include <QDebug>
#include <QMouseEvent>
#define PLUGIN_STATE_KEY "enable"
CMDUWidget::CMDUWidget(QWidget *parent)
: QWidget(parent),
m_settings("deepin", "dde-dock-cmdu")
{
text = " ↑0.00KB/s \n ↓0.00KB/s ";
mp = 0;
cp = 0;
}
bool CMDUWidget::enabled()
{
return m_settings.value(PLUGIN_STATE_KEY, true).toBool();
}
void CMDUWidget::setEnabled(const bool b)
{
m_settings.setValue(PLUGIN_STATE_KEY, b);
}
QSize CMDUWidget::sizeHint() const
{
QFont font = qApp->font();
font.setFamily("Noto Mono");
QFontMetrics FM(font);
return FM.boundingRect(" ↑0.00KB/s ").size() + QSize(0, FM.boundingRect(" ↓0.00KB/s ").height());
}
void CMDUWidget::resizeEvent(QResizeEvent *e)
{
QWidget::resizeEvent(e);
}
void CMDUWidget::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::white);
QFont font = qApp->font();
font.setFamily("Noto Mono");
painter.setFont(font);
painter.drawText(rect().adjusted(2,0,0,0), Qt::AlignLeft | Qt::AlignVCenter, text);
if (mp < 90) {
painter.fillRect(0, height()*(100-mp)/100, 2, height()*mp/100, Qt::white);
} else {
painter.fillRect(0, height()*(100-mp)/100, 2, height()*mp/100, Qt::red);
}
if (cp < 90) {
painter.fillRect(width()-2, height()*(100-cp)/100, 2, height()*cp/100, Qt::white);
} else {
painter.fillRect(width()-2, height()*(100-cp)/100, 2, height()*cp/100, Qt::red);
}
}