Debian Codegraph @

src/librssguard/gui/reusable/timespinbox.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/reusable/timespinbox.h"
4
5 #include "definitions/definitions.h"
6
7 #include <QStringList>
8
9 TimeSpinBox::TimeSpinBox(QWidget* parent) : QDoubleSpinBox(parent) {
10 setAccelerated(true);
11 setMinimum(1.0);
12 setMaximum(10000000.0);
13 setMode(TimeSpinBox::Mode::HoursMinutes);
14 }
15
16 double TimeSpinBox::valueFromText(const QString& text) const {
17 bool ok;
18 double value = text.toDouble(&ok);
19
20 if (ok) {
21 return value;
22 }
23 else {
24 static QRegularExpression rx(QSL("\\b[0-9]{1,}\\b"));
25 QStringList numbers;
26 QRegularExpressionMatchIterator i = rx.globalMatch(text);
27
28 while (i.hasNext()) {
29 numbers.append(i.next().captured());
30 }
31
32 if (numbers.size() == 2) {
33 switch (m_mode) {
34 case TimeSpinBox::Mode::DaysHours:
35 return (numbers.at(0).toDouble() * 24.0) + numbers.at(1).toDouble();
36
37 default:
38 case TimeSpinBox::Mode::HoursMinutes:
39 case TimeSpinBox::Mode::MinutesSeconds:
40 return (numbers.at(0).toDouble() * 60.0) + numbers.at(1).toDouble();
41 }
42 }
43 else {
44 return -1.0;
45 }
46 }
47 }
48
49 QString TimeSpinBox::textFromValue(double val) const {
50 switch (m_mode) {
51 case TimeSpinBox::Mode::MinutesSeconds: {
52 // "val" is number of seconds.
53 int seconds_val = int(val);
54 int minutes_total = seconds_val / 60;
55 int seconds_total = seconds_val - (minutes_total * 60);
56 QString seconds = tr("%n second(s)", nullptr, seconds_total);
57 QString minutes = tr("%n minute(s)", nullptr, minutes_total);
58
59 return minutes + tr(" and ") + seconds;
60 }
61
62 case TimeSpinBox::Mode::DaysHours: {
63 // "val" is number of hours.
64 int hours_val = int(val);
65 int days_total = hours_val / 24;
66 int hours_total = hours_val - (days_total * 24);
67 QString hours = tr("%n hour(s)", nullptr, hours_total);
68 QString days = tr("%n day(s)", nullptr, days_total);
69
70 return days + tr(" and ") + hours;
71 }
72
73 default:
74 case TimeSpinBox::Mode::HoursMinutes: {
75 // "val" is number of minutes.
76 int minutes_total = int(val);
77 int minutes_val = minutes_total % 60;
78 int hours_val = (minutes_total - minutes_val) / 60;
79 QString hours = tr("%n hour(s)", nullptr, hours_val);
80 QString minutes = tr("%n minute(s)", nullptr, minutes_val);
81
82 return hours + tr(" and ") + minutes;
83 }
84 }
85 }
86
87 void TimeSpinBox::fixup(QString& input) const {
88 bool ok;
89 double value = input.toDouble(&ok);
90
91 if (ok) {
92 input = textFromValue(value);
93 }
94 }
95
96 QValidator::State TimeSpinBox::validate(QString& input, int& pos) const {
97 Q_UNUSED(pos)
98 return (valueFromText(input) != -1.0) ? QValidator::State::Acceptable : QValidator::State::Intermediate;
99 }
100
101 TimeSpinBox::Mode TimeSpinBox::mode() const {
102 return m_mode;
103 }
104
105 void TimeSpinBox::setMode(TimeSpinBox::Mode mode) {
106 m_mode = mode;
107
108 setValue(value());
109 }