25
25
Tray::Tray(QWidget *parent)
29
dispather = new SystemDispatcher;
32
double trans_cpu = dispather->get_cpu_percent_qt();
33
cpu_value = QString::number(trans_cpu, 'f', 0);
34
used_memory = dispather->get_used_memory_qt().toDouble();
35
free_memory = dispather->get_free_memory_qt();
36
total_size = dispather->get_total_memory_qt().toDouble();
37
double size = used_memory / total_size;
38
ratio = QString::number(size, 'f', 2);
39
double trans = ratio.toDouble() * 100;
40
ratio = QString::number(trans,'f',0);
41
// dispather->get_network_flow_qt();
42
total_speed = dispather->get_network_flow_total_qt();
44
this->setWindowOpacity(1.0);
29
45
icon = QIcon(":/pixmap/image/icon.png");
30
46
this->createTray();
38
void Tray::createTray()
40
this->actionShow = new QAction(tr("隐藏/显示"), this);
47
this->setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);
48
this->setAttribute(Qt::WA_TranslucentBackground);
49
QDesktopWidget *desktop = QApplication::desktop();
50
this->move(desktop->width() - this->width(), 25);
53
frame = new SuspensionFrame;
55
connect(this, SIGNAL(sysc_data(QString, QString,QString,int,QString, QString)), frame, SLOT(get_sysc_data(QString,QString,QString,int,QString, QString)));
56
connect(frame, SIGNAL(accelerate_memory()), this, SLOT(startMemoryAccelerate()));
58
QTimer *timer = new QTimer(this);
59
timer->setInterval(3000);
60
connect(timer,SIGNAL(timeout()),this,SLOT(updateData()));
70
void Tray::updateData() {
71
QStringList current_speed = dispather->get_network_flow_total_qt();
72
double up_before = total_speed[0].toDouble();
73
double down_before = total_speed[1].toDouble();
74
double up_now = current_speed[0].toDouble();
75
double down_now = current_speed[1].toDouble();
76
total_speed = current_speed;
77
double up_final = up_now - up_before;
78
double down_final = down_now - down_before;
80
up_speed = QString::number(up_final,'f',0);
81
down_speed = QString::number(down_final,'f',0);
83
double trans_cpu = dispather->get_cpu_percent_qt();
84
cpu_value = QString::number(trans_cpu, 'f', 0);
85
used_memory = dispather->get_used_memory_qt().toDouble();
86
free_memory = dispather->get_free_memory_qt();
87
double size = used_memory / total_size;
88
ratio = QString::number(size, 'f', 2);
89
double trans = ratio.toDouble() * 100;
90
ratio = QString::number(trans,'f',0);
92
emit sysc_data(up_speed, down_speed, ratio, used_memory, free_memory, cpu_value);
93
this->uplabel->setText(up_speed + "K/s");
94
this->downlabel->setText(down_speed + "K/s");
95
this->ratiolabel->setText(ratio + "%");
96
ratio_sus = ratio.toInt();
100
void Tray::startMemoryAccelerate() {
101
dispather->cleanup_memory_qt();
105
void Tray::createTray() {
106
this->actionQml = new QAction(tr("隐藏/显示主界面"), this);
107
connect(actionQml, SIGNAL(triggered()), this, SLOT(showOrHideQml()));
108
this->actionShow = new QAction(tr("隐藏/显示监控球"), this);
41
109
connect(actionShow, SIGNAL(triggered()), this, SLOT(showOrHide()));
42
110
QShortcut *shortcut = new QShortcut(QKeySequence("Ctrl+H"), this);
43
111
connect(shortcut, SIGNAL(activated()), this, SLOT(showOrHide()));
44
this->actionQuit = new QAction(tr("E&xit"), this);
112
this->actionQuit = new QAction(tr("退出"), this);
45
113
connect(actionQuit, SIGNAL(triggered()), qApp, SLOT(quit()));
47
115
this->trayMenu = new QMenu(this);
116
this->trayMenu->addAction(actionQml);
48
117
this->trayMenu->addAction(actionShow);
49
118
this->trayMenu->addSeparator();
50
119
this->trayMenu->addAction(actionQuit);
71
void Tray::showOrHide()
138
void Tray::showOrHide() {
139
if(this->isHidden()) {
143
if(!frame->isHidden()) {
149
void Tray::showOrHideQml() {
150
emit showOrHideQmlSignal();
153
void Tray::mousePressEvent(QMouseEvent *event) {
154
// if (event->button() == Qt::LeftButton)
156
// dragPos = event->globalPos() - frameGeometry().topLeft();
159
// else if (event->button() == Qt::RightButton)
165
// if (event->button() == Qt::LeftButton) {
166
// dragPos = event->globalPos() - frameGeometry().topLeft();
167
// if(frame->isHidden())
173
if (event->button() == Qt::LeftButton) {
174
dragPos = event->globalPos() - frameGeometry().topLeft();
179
void Tray::mouseMoveEvent(QMouseEvent *event) {
180
if (event->buttons() & Qt::LeftButton ) {
181
move(event->globalPos() - dragPos);
182
setWindowOpacity(0.5);
187
void Tray::mouseReleaseEvent(QMouseEvent *event) {
188
if (event->button() == Qt::LeftButton) {
194
void Tray::mouseDoubleClickEvent(QMouseEvent *event) {
195
if(event->buttons() == Qt::RightButton) {
197
if(frame->isVisible()) {
201
if(event->buttons() == Qt::LeftButton) {
202
if(frame->isHidden()) {
211
QSize Tray::sizeHint()const {
212
return QSize(height(),height());
215
void Tray::resizeEvent(QResizeEvent *event) {
216
wheel=QImage(event->size(),QImage::Format_ARGB32_Premultiplied);
217
wheel.fill(palette().background().color());
221
void Tray::paintEvent(QPaintEvent *) {
222
QPainter painter(this);
226
background.load(":/pixmap/image/accelerate-bg0.png");
227
painter.drawPixmap(0, 0, background);
229
painter.setRenderHint(QPainter::Antialiasing); //消除锯齿
230
wheel.fill(Qt::transparent);
231
blister.load(":/pixmap/image/blister-small.png");
233
QLinearGradient linearGradient(66,1,66,31);
234
//创建了一个QLinearGradient对象实例,参数为起点和终点坐标,可作为颜色渐变的方向
235
painter.setPen(Qt::transparent);
240
color1=(ratio_sus == 100) ? "#ff2f00" : "transparent";
243
color2="transparent";
244
color3="transparent";
246
else if(ratio_sus > 80) {
249
blister.load(":/pixmap/image/blister-small.png");
254
blister.load(":/pixmap/image/blister-small.png");
257
linearGradient.setColorAt(0.0,color1);
258
linearGradient.setColorAt( 1.0 - ratio_sus * 0.01,color1);
259
linearGradient.setColorAt((ratio_sus <= 0) ? 0.0 : (1.0 - ratio_sus * 0.01 + 0.01),color2);
260
linearGradient.setColorAt(1.0,color3);
261
painter.setBrush(QBrush(linearGradient));
262
painter.drawRoundRect(51,1,30,30,5,5);
265
painter.drawImage(0,0,wheel);
267
painter.drawPixmap(0,0, blister);
268
style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);