| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/reusable/progressbarwithtext.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | |
| 7 | #include <QLocale> |
| 8 | |
| 9 | ProgressBarWithText::ProgressBarWithText(QWidget* parent) : QProgressBar(parent) {} |
| 10 | |
| 11 | QString ProgressBarWithText::text() const { |
| 12 | qint64 total_steps = qint64(maximum()) - minimum(); |
| 13 | QString result = format(); |
| 14 | QLocale locale; |
| 15 | |
| 16 | locale.setNumberOptions(locale.numberOptions() | QLocale::OmitGroupSeparator); |
| 17 | result.replace(QL1S("%m"), locale.toString(total_steps)); |
| 18 | result.replace(QL1S("%v"), locale.toString(value())); |
| 19 | |
| 20 | // If max and min are equal and we get this far, it means that the |
| 21 | // progress bar has one step and that we are on that step. Return |
| 22 | // 100% here in order to avoid division by zero further down. |
| 23 | if (total_steps == 0) { |
| 24 | result.replace(QL1S("%p"), locale.toString(100)); |
| 25 | return result; |
| 26 | } |
| 27 | |
| 28 | const auto progress = static_cast<int>((qint64(value()) - minimum()) * 100.0 / total_steps); |
| 29 | |
| 30 | result.replace(QL1S("%p"), locale.toString(progress)); |
| 31 | |
| 32 | // Now, shorten the text to fit the widget. |
| 33 | bool elide = false; |
| 34 | |
| 35 | while (fontMetrics().boundingRect(result + QSL("...")).width() > width() - 30) { |
| 36 | result.chop(1); |
| 37 | elide = true; |
| 38 | } |
| 39 | |
| 40 | return elide ? result + QSL("...") : result; |
| 41 | } |