~ubuntu-branches/ubuntu/vivid/youker-assistant/vivid

« back to all changes in this revision

Viewing changes to src/tray.cpp

  • Committer: Package Import Robot
  • Author(s): Kobe Lee (kylinkobe)
  • Date: 2013-09-18 16:22:14 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130918162214-6nqyjyf3cd3ynqky
Tags: 0.2.1-0ubuntu1
* Modify the mouse events of MonitorBall.
* Add reset button for clear pages.
* Add policykit for apt clear in sudodbus.
* Fixed the bug about software status and add masklayer.
* Rewrite the code of system information.
* Add some signals and slots.
* Fixed the bug about Software signals(LP: #1226389).
* Modify KThread and add base.py.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 */
22
22
 
23
23
#include "tray.h"
24
 
 
 
24
#include <QDebug>
25
25
Tray::Tray(QWidget *parent)
26
26
    : QWidget(parent)
27
27
{
28
28
    setupUi(this);
 
29
    dispather = new SystemDispatcher;
 
30
 
 
31
    ratio_sus = 0;
 
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();
 
43
 
 
44
    this->setWindowOpacity(1.0);
29
45
    icon = QIcon(":/pixmap/image/icon.png");
30
46
    this->createTray();
31
 
}
32
 
 
33
 
Tray::~Tray()
34
 
{
35
 
 
36
 
}
37
 
 
38
 
void Tray::createTray()
39
 
{
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);
 
51
    this->show();
 
52
 
 
53
    frame = new SuspensionFrame;
 
54
    frame->hide();
 
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()));
 
57
 
 
58
    QTimer *timer = new QTimer(this);
 
59
    timer->setInterval(3000);
 
60
    connect(timer,SIGNAL(timeout()),this,SLOT(updateData()));
 
61
    timer->start();
 
62
}
 
63
 
 
64
Tray::~Tray() {
 
65
    if(frame) {
 
66
        delete frame;
 
67
    }
 
68
}
 
69
 
 
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;
 
79
 
 
80
    up_speed = QString::number(up_final,'f',0);
 
81
    down_speed = QString::number(down_final,'f',0);
 
82
 
 
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);
 
91
 
 
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();
 
97
    update();
 
98
}
 
99
 
 
100
void Tray::startMemoryAccelerate() {
 
101
    dispather->cleanup_memory_qt();
 
102
    updateData();
 
103
}
 
104
 
 
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()));
46
114
 
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);
56
125
    this->trayIcon->show();
57
126
}
58
127
 
59
 
void Tray::handle_trayIcon_activated(QSystemTrayIcon::ActivationReason reason)
60
 
{
61
 
    switch(reason)
62
 
    {
 
128
void Tray::handle_trayIcon_activated(QSystemTrayIcon::ActivationReason reason) {
 
129
    switch(reason) {
63
130
        case QSystemTrayIcon::Trigger:
64
131
            this->showOrHide();
65
132
            break;
68
135
    }
69
136
}
70
137
 
71
 
void Tray::showOrHide()
72
 
{
73
 
    emit showFloat();
 
138
void Tray::showOrHide() {
 
139
    if(this->isHidden()) {
 
140
        this->show();
 
141
    }else {
 
142
        this->hide();
 
143
        if(!frame->isHidden()) {
 
144
            frame->hide();
 
145
        }
 
146
    }
 
147
}
 
148
 
 
149
void Tray::showOrHideQml() {
 
150
    emit showOrHideQmlSignal();
 
151
}
 
152
 
 
153
void Tray::mousePressEvent(QMouseEvent *event) {
 
154
//    if (event->button() == Qt::LeftButton)
 
155
//    {
 
156
//        dragPos = event->globalPos() - frameGeometry().topLeft();
 
157
//        event->accept();
 
158
//    }
 
159
//    else if (event->button() == Qt::RightButton)
 
160
//    {
 
161
//        this->hide();
 
162
//        event->accept();
 
163
//    }
 
164
 
 
165
//    if (event->button() == Qt::LeftButton) {
 
166
//        dragPos = event->globalPos() - frameGeometry().topLeft();
 
167
//        if(frame->isHidden())
 
168
//            frame->show();
 
169
//        else
 
170
//            frame->hide();
 
171
//        event->accept();
 
172
//    }
 
173
    if (event->button() == Qt::LeftButton) {
 
174
        dragPos = event->globalPos() - frameGeometry().topLeft();
 
175
        event->accept();
 
176
    }
 
177
}
 
178
 
 
179
void Tray::mouseMoveEvent(QMouseEvent *event) {
 
180
    if (event->buttons() & Qt::LeftButton ) {
 
181
        move(event->globalPos() - dragPos);
 
182
        setWindowOpacity(0.5);
 
183
    }
 
184
    event->accept();
 
185
}
 
186
 
 
187
void Tray::mouseReleaseEvent(QMouseEvent *event) {
 
188
    if (event->button() == Qt::LeftButton) {
 
189
        setWindowOpacity(1);
 
190
    }
 
191
    event->accept();
 
192
}
 
193
 
 
194
void Tray::mouseDoubleClickEvent(QMouseEvent *event) {
 
195
    if(event->buttons() == Qt::RightButton) {
 
196
        this->hide();
 
197
        if(frame->isVisible()) {
 
198
            frame->hide();
 
199
        }
 
200
    }
 
201
    if(event->buttons() == Qt::LeftButton) {
 
202
        if(frame->isHidden()) {
 
203
            frame->show();
 
204
        }
 
205
        else {
 
206
            frame->hide();
 
207
        }
 
208
    }
 
209
}
 
210
 
 
211
QSize Tray::sizeHint()const {
 
212
    return QSize(height(),height());
 
213
}
 
214
 
 
215
void Tray::resizeEvent(QResizeEvent *event) {
 
216
    wheel=QImage(event->size(),QImage::Format_ARGB32_Premultiplied);
 
217
    wheel.fill(palette().background().color());
 
218
    update();
 
219
}
 
220
 
 
221
void Tray::paintEvent(QPaintEvent *) {
 
222
    QPainter painter(this);
 
223
    QStyleOption opt;
 
224
 
 
225
    QPixmap background;
 
226
    background.load(":/pixmap/image/accelerate-bg0.png");
 
227
    painter.drawPixmap(0, 0, background);
 
228
 
 
229
    painter.setRenderHint(QPainter::Antialiasing);  //消除锯齿
 
230
    wheel.fill(Qt::transparent);
 
231
    blister.load(":/pixmap/image/blister-small.png");
 
232
    //线性渐变
 
233
    QLinearGradient linearGradient(66,1,66,31);
 
234
    //创建了一个QLinearGradient对象实例,参数为起点和终点坐标,可作为颜色渐变的方向
 
235
    painter.setPen(Qt::transparent);
 
236
    QString color1;
 
237
    QString color2;
 
238
    QString color3;
 
239
 
 
240
    color1=(ratio_sus == 100) ? "#ff2f00" : "transparent";
 
241
    if (ratio_sus == 0)
 
242
    {
 
243
        color2="transparent";
 
244
        color3="transparent";
 
245
    }
 
246
    else if(ratio_sus > 80) {
 
247
        color2="#d13625";
 
248
        color3="#d13625";
 
249
        blister.load(":/pixmap/image/blister-small.png");
 
250
    }
 
251
    else {
 
252
        color2="#00b0ff";
 
253
        color3="#006eff";
 
254
        blister.load(":/pixmap/image/blister-small.png");
 
255
    }
 
256
 
 
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);
 
263
 
 
264
    opt.init(this);
 
265
    painter.drawImage(0,0,wheel);
 
266
 
 
267
    painter.drawPixmap(0,0, blister);
 
268
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
74
269
}