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

« back to all changes in this revision

Viewing changes to src/progressdialog.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:
 
1
/*
 
2
 * Copyright (C) 2013 National University of Defense Technology(NUDT) & Kylin Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
#include "progressdialog.h"
 
17
#include "ui_progressdialog.h"
 
18
#include <QDebug>
 
19
#include <QMouseEvent>
 
20
#include <stdio.h>
 
21
extern bool progress_flag;
 
22
ProgressDialog::ProgressDialog(QWidget *parent) :
 
23
    QDialog(parent),
 
24
    ui(new Ui::ProgressDialog)
 
25
{
 
26
    ui->setupUi(this);
 
27
    progress_flag = false;
 
28
    this->setAttribute(Qt::WA_DeleteOnClose);
 
29
    this->setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);
 
30
    this->setAttribute(Qt::WA_TranslucentBackground);
 
31
    ui->label->setStyleSheet("QLabel {color: green; font-size: 12px}");
 
32
//    ui->label_2->setStyleSheet("QLabel {color: black; font-size: 12px}");
 
33
//    ui->progressBar->setStyleSheet("QProgressBar {border: 1px solid grey;border-radius: 2px;text-align: center;}"
 
34
//                                    "QProgressBar::chunk {background-color: #6be2fa;width: 10px;}");
 
35
    QDesktopWidget* desktop = QApplication::desktop();
 
36
    ratio_sus=1;
 
37
    move((desktop->width() - this->width())/2, (desktop->height() - this->height())/2);
 
38
}
 
39
 
 
40
ProgressDialog::~ProgressDialog() {
 
41
    delete ui;
 
42
}
 
43
 
 
44
void ProgressDialog::reset_status() {
 
45
    ratio_sus=0;
 
46
    ui->label_2->setText(tr("%1").arg(ratio_sus)+"%");
 
47
    update();
 
48
    ui->label->setText("开始");
 
49
    this->hide();
 
50
}
 
51
 
 
52
void ProgressDialog::setDynamicSoftwareProgress(QString type, QString msg) {
 
53
    if(this->isHidden()) {
 
54
        this->show();
 
55
    }
 
56
    if(type == "down_start") {
 
57
        ui->label->setText("开始下载");
 
58
//        ui->progressBar->setValue(0);
 
59
        ui->label_2->setText(tr("%1").arg(ratio_sus)+"%");
 
60
        ratio_sus=1;
 
61
    }
 
62
    else if(type == "down_pulse"){
 
63
        if(!msg.isEmpty()) {
 
64
            if(msg.contains("download_bytes") && msg.contains("total_bytes")) {
 
65
                QStringList process_value = msg.split(",");
 
66
                if (process_value.size() == 4) {
 
67
                    QStringList download_bytes = process_value.at(0).split(":");
 
68
                    double download_bytes_value = download_bytes.at(1).toDouble();
 
69
                    QStringList total_bytes = process_value.at(1).split(":");
 
70
                    double total_bytes_value = total_bytes.at(1).toDouble();
 
71
                    double percent = download_bytes_value / total_bytes_value;
 
72
                    QString ratio = QString::number(percent, 'f', 2);
 
73
                    double trans = ratio.toDouble() * 100;
 
74
                    ratio = QString::number(trans,'f',0);
 
75
//                    ui->progressBar->setValue(ratio.toInt());
 
76
                    ratio_sus=ratio.toInt();
 
77
                    if(progress_flag) {
 
78
                        this->hide();
 
79
                        softwareSourceUpdateProgressToSudoDispather(ratio);
 
80
                    }
 
81
                    else {
 
82
                        ui->label->setText("正在下载安装包...");
 
83
                        ui->label_2->setText(tr("%1").arg(ratio_sus)+"%");
 
84
                    }
 
85
                }
 
86
            }
 
87
        }
 
88
    }
 
89
    else if(type == "down_stop") {
 
90
        if(progress_flag) {
 
91
            this->hide();
 
92
        }
 
93
        else {
 
94
            ui->label->setText("下载完成");
 
95
            ratio_sus=0;
 
96
            ui->label_2->setText(tr("%1").arg(ratio_sus)+"%");
 
97
        }
 
98
    }
 
99
    else if(type == "apt_start"){
 
100
        if(this->isHidden()) {
 
101
            this->show();
 
102
        }
 
103
        ui->label->setText("开始");
 
104
//        ui->progressBar->setValue(0);
 
105
        ui->label_2->setText(tr("%1").arg(ratio_sus)+"%");
 
106
        ratio_sus=1;
 
107
 
 
108
    }
 
109
    else if(type == "apt_pulse"){
 
110
        if(!msg.isEmpty()) {
 
111
            if(msg.contains(",")) {
 
112
                QStringList process_value = msg.split(",");
 
113
                if (process_value.size() == 2) {
 
114
                    QStringList status_value = process_value.at(0).split(":");
 
115
                    int value = status_value.at(1).toInt();
 
116
                    QStringList action_value = process_value.at(1).split(":");
 
117
                    QString act = action_value.at(1);
 
118
                    ui->label->setText("正在进行:" + act);
 
119
//                    ui->progressBar->setValue(value);
 
120
                    ratio_sus=value;
 
121
                    ui->label_2->setText(tr("%1").arg(ratio_sus)+"%");
 
122
                }
 
123
            }
 
124
        }
 
125
    }
 
126
    else if(type == "apt_stop") {
 
127
        ui->label->setText("完成");
 
128
//        ui->progressBar->setValue(0);
 
129
        ratio_sus=100;
 
130
        ui->label_2->setText(tr("%1").arg(ratio_sus)+"%");
 
131
        update();
 
132
//        sleep(2000);
 
133
        QTimer *timer = new QTimer(this);
 
134
        timer->setInterval(2000);
 
135
        connect(timer,SIGNAL(timeout()),this,SLOT(reset_status()));
 
136
        timer->start();
 
137
        ratio_sus=100;
 
138
        update();
 
139
    }
 
140
    update();
 
141
}
 
142
 
 
143
 
 
144
void ProgressDialog::mousePressEvent(QMouseEvent *event) {
 
145
    if (event->button() == Qt::LeftButton) {
 
146
        dragPos = event->globalPos() - frameGeometry().topLeft();
 
147
        event->accept();
 
148
    }
 
149
}
 
150
 
 
151
void ProgressDialog::mouseMoveEvent(QMouseEvent *event) {
 
152
    if (event->buttons() & Qt::LeftButton ) {
 
153
        move(event->globalPos() - dragPos);
 
154
        setWindowOpacity(0.5);
 
155
    }
 
156
    event->accept();
 
157
}
 
158
 
 
159
void ProgressDialog::mouseReleaseEvent(QMouseEvent *event) {
 
160
    if (event->button() == Qt::LeftButton) {
 
161
        setWindowOpacity(1);
 
162
    }
 
163
    event->accept();
 
164
}
 
165
 
 
166
 
 
167
QSize ProgressDialog::sizeHint()const {
 
168
    return QSize(height(),height());
 
169
}
 
170
 
 
171
void ProgressDialog::resizeEvent(QResizeEvent *) {
 
172
    update();
 
173
}
 
174
 
 
175
void ProgressDialog::paintEvent(QPaintEvent *) {
 
176
    QPainter painter(this);
 
177
    QStyleOption opt;
 
178
    QPixmap progress_bar1;
 
179
    QPixmap progress_bar2;
 
180
    QPixmap background;
 
181
    background.load(":/pixmap/image/progress-bg.png");
 
182
    progress_bar1.load(":/pixmap/image/progress-bar1.png");
 
183
    progress_bar2.load(":/pixmap/image/progress-bar2.png");
 
184
    painter.drawPixmap(0,0, background);
 
185
    painter.setPen(Qt::transparent);
 
186
    opt.init(this);
 
187
    painter.drawPixmap(10,30,progress_bar1);
 
188
//    painter.drawPixmap(10,30,progress_bar2,0,0,progress_bar2.width()*(ratio_sus*0.01),progress_bar2.height());
 
189
    painter.drawPixmap(10,30,progress_bar2.width()*(ratio_sus*0.01),progress_bar2.height(),progress_bar2);
 
190
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
 
191
}