#include <iostream>
-ProgressBar::ProgressBar(FileManipulatorThread *thread, QWidget *parent) :
- QProgressBar(parent),
+
+ProgressBar::ProgressBar(const QPixmap &icon, const QPixmap &inverseIcon) :
+ bgIcon(icon),
+ fgIcon(inverseIcon),
paused(false),
contextEvent(false),
- thread(thread)
+ lastTimeUpdate(0),
+ startTime(0)
{
+ timeBuf[0] = 0;
setMaximum(1);
setValue(0);
setMinimum(0);
}
-void ProgressBar::setIcons(const QPixmap &icon, const QPixmap &inverseIcon) {
- bgIcon = icon;
- fgIcon = inverseIcon;
+void ProgressBar::updateProgress(int val) {
+ if (value() + val > maximum()) {
+ std::cout << "WARNING: exceeding progressbar maximum (" << maximum() << ") by " << val << std::endl;
+ }
+
+ setValue(value() + val);
+
+ time_t now = time(0);
+ if (lastTimeUpdate < now) {
+ lastTimeUpdate = now;
+
+ time_t elapsed = now - startTime;
+ time_t remaining = (time_t) ((float) elapsed / value() * (maximum() - value()));
+ struct tm *ts = gmtime(&remaining);
+
+ if (remaining == 0) {
+ timeBuf[0] = 0;
+ } else if (remaining < 60) {
+ strftime(timeBuf, sizeof(timeBuf), " %Ss", ts);
+ } else if (remaining < 3600) {
+ strftime(timeBuf, sizeof(timeBuf), " %M:%S", ts);
+ } else {
+ strftime(timeBuf, sizeof(timeBuf), " %H:%M:%S", ts);
+ }
+ }
+
+ setFormat(QString("%p%") + timeBuf);
+}
+
+
+void ProgressBar::updateMainText(const QString &text) {
+ mainText = text;
+ if (fromText.size()) {
+ mainText.remove(0, fromText.size() + 1);
+ }
+ repaint();
+}
+
+
+void ProgressBar::setBottomTexts(const QString &left, const QString &right) {
+ fromText = left;
+ toText = right;
+}
+
+
+void ProgressBar::setStartTime(time_t t) {
+ startTime = t;
+ lastTimeUpdate = time(0);
+ updateProgress(0);
+}
+
+
+void ProgressBar::pause() {
+ paused = true;
+ repaint();
+}
+
+
+void ProgressBar::resume(time_t stallTime) {
+ startTime += stallTime;
+ paused = false;
+ repaint();
+}
+
+
+void ProgressBar::showRemoveNotice() {
+ toText = "<" + tr("deleting") + ">";
+ repaint();
}
void ProgressBar::mouseReleaseEvent(QMouseEvent *) {
- if (!contextEvent) emit togglePauseOperation(thread);
+ if (!contextEvent) emit togglePauseOperation(this);
contextEvent = false;
}
void ProgressBar::contextMenuEvent(QContextMenuEvent *) {
contextEvent = true;
- emit abortOperation(thread);
+ emit abortOperation(this);
}