code cleanup
[case] / src / progressbar.cpp
1 // case - file manager for N900
2 // Copyright (C) 2010 Lukas Hrazky <lukkash@email.cz>
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, either version 3 of the License, or
7 // (at your option) any later version.
8 // 
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 // 
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 #include "progressbar.h"
19
20 #include <QtGui>
21
22 #include "fileoperator.h"
23
24 #include <iostream>
25
26
27 ProgressBar::ProgressBar(const QPixmap &icon, const QPixmap &inverseIcon) :
28     bgIcon(icon),
29     fgIcon(inverseIcon),
30     paused(false),
31     contextEvent(false),
32     lastTimeUpdate(0),
33     startTime(0)
34 {
35     timeBuf[0] = 0;
36     setMaximum(1);
37     setValue(0);
38     setMinimum(0);
39     QFont barFont = font();
40     barFont.setPointSize(12);
41     setFont(barFont);
42     setMinimumHeight(44);
43     setFormat("");
44     mainText = tr("gathering information...");
45 }
46
47
48 void ProgressBar::updateProgress(int val) {
49     if (value() + val > maximum()) {
50         std::cout << "WARNING: exceeding progressbar maximum (" << maximum() << ") by " << val << std::endl;
51     }
52
53     setValue(value() + val);
54
55     time_t now = time(0);
56     if (lastTimeUpdate < now) {
57         lastTimeUpdate = now;
58
59         time_t elapsed = now - startTime;
60         time_t remaining = (time_t) ((float) elapsed / value() * (maximum() - value()));
61         struct tm *ts = gmtime(&remaining);
62         
63         if (remaining == 0) {
64             timeBuf[0] = 0;
65         } else if (remaining < 60) {
66             strftime(timeBuf, sizeof(timeBuf), "  %Ss", ts);
67         } else if (remaining < 3600) {
68             strftime(timeBuf, sizeof(timeBuf), "  %M:%S", ts);
69         } else {
70             strftime(timeBuf, sizeof(timeBuf), "  %H:%M:%S", ts);
71         }
72     }
73
74     setFormat(QString("%p%") + timeBuf);
75 }
76
77
78 void ProgressBar::updateMainText(const QString &text) {
79     mainText = text;
80     if (fromText.size()) {
81         mainText.remove(0, fromText.size() + 1);
82     }
83     repaint();
84 }
85
86
87 void ProgressBar::setBottomTexts(const QString &left, const QString &right) {
88     fromText = left;
89     toText = right;
90 }
91
92
93 void ProgressBar::setStartTime(time_t t) {
94     startTime = t;
95     lastTimeUpdate = time(0);
96     updateProgress(0);
97 }
98
99
100 void ProgressBar::pause() {
101     paused = true;
102     repaint();
103 }
104
105
106 void ProgressBar::resume(time_t stallTime) {
107     startTime += stallTime;
108     paused = false;
109     repaint();
110 }
111
112
113 void ProgressBar::showRemoveNotice() {
114     toText = "<" + tr("deleting") + ">";
115     repaint();
116 }
117
118
119 void ProgressBar::mouseReleaseEvent(QMouseEvent *) {
120     if (!contextEvent) emit togglePauseOperation(this);
121     contextEvent = false;
122 }
123
124
125 void ProgressBar::contextMenuEvent(QContextMenuEvent *) {
126     contextEvent = true;
127     emit abortOperation(this);
128 }