| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "timedprogressdialog.h" |
| 7 |
|
| 8 |
namespace Avogadro::QtGui { |
| 9 |
|
| 10 |
TimedProgressDialog::TimedProgressDialog(QWidget* parent) |
| 11 |
: QProgressDialog(parent) |
| 12 |
{ |
| 13 |
} |
| 14 |
|
| 15 |
TimedProgressDialog::TimedProgressDialog(const QString& labelText, |
| 16 |
const QString& cancelButtonText, |
| 17 |
int minimum, int maximum, |
| 18 |
QWidget* parent) |
| 19 |
: QProgressDialog(labelText, cancelButtonText, minimum, maximum, parent), |
| 20 |
originalLabelText(labelText) |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
void TimedProgressDialog::show() |
| 25 |
{ |
| 26 |
elapsedTimer.start(); |
| 27 |
QProgressDialog::show(); |
| 28 |
} |
| 29 |
|
| 30 |
void TimedProgressDialog::setValue(int value) |
| 31 |
{ |
| 32 |
QProgressDialog::setValue(value); |
| 33 |
|
| 34 |
if (!elapsedTimer.isValid()) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
// Calculate progress percentage |
| 39 |
float progress = |
| 40 |
static_cast<float>(value - minimum()) / (maximum() - minimum()); |
| 41 |
|
| 42 |
// Avoid division by zero |
| 43 |
if (progress <= 0) { |
| 44 |
return; |
| 45 |
} |
| 46 |
// Ignore early progress (e.g., unreliable progress bar) |
| 47 |
if (progress < 0.1) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// Calculate elapsed time in seconds |
| 52 |
float elapsedSeconds = elapsedTimer.elapsed() / 1000.0f; |
| 53 |
|
| 54 |
// Estimate total time and remaining time |
| 55 |
float estimatedTotal = elapsedSeconds / progress; |
| 56 |
float remainingSeconds = estimatedTotal - elapsedSeconds; |
| 57 |
|
| 58 |
// Format the time string |
| 59 |
QString remaining = formatTime(remainingSeconds); |
| 60 |
|
| 61 |
// Update label with original text and time remaining |
| 62 |
QString labelText = originalLabelText; |
| 63 |
if (!labelText.isEmpty()) { |
| 64 |
labelText += "\n" + remaining; |
| 65 |
} else { |
| 66 |
labelText = remaining; |
| 67 |
} |
| 68 |
|
| 69 |
QProgressDialog::setLabelText(labelText); |
| 70 |
} |
| 71 |
|
| 72 |
QString TimedProgressDialog::formatTime(double seconds) |
| 73 |
{ |
| 74 |
if (seconds < 0) { |
| 75 |
return tr("Calculating…"); |
| 76 |
} |
| 77 |
|
| 78 |
if (seconds < 1) { |
| 79 |
return tr("Less than a second remaining"); |
| 80 |
} else if (seconds < 60) { |
| 81 |
int sec = static_cast<int>(seconds); |
| 82 |
return tr("%n second(s) remaining", "", sec); |
| 83 |
} else if (seconds < 3600) { |
| 84 |
int minutes = static_cast<int>(seconds / 60); |
| 85 |
return tr("%n minute(s) remaining", "", minutes); |
| 86 |
} else { |
| 87 |
// hopefully we don't have anything this slow |
| 88 |
int hours = static_cast<int>(seconds / 3600); |
| 89 |
int minutes = static_cast<int>((static_cast<int>(seconds) % 3600) / 60); |
| 90 |
if (hours == 1 && minutes > 0) { |
| 91 |
// give a message for under 2 hours, e.g. |
| 92 |
// "1 hour 10 minutes remaining" |
| 93 |
return tr("1 hour %n minute(s) remaining", "", minutes); |
| 94 |
} else { |
| 95 |
// one hour or 2 hours, etc. |
| 96 |
return tr("%n hour(s) remaining", "", hours); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
} // namespace Avogadro::QtGui |