]> sigrok.org Git - pulseview.git/blob - pv/widgets/timestampspinbox.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / widgets / timestampspinbox.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2015 Jens Steinhauser <jens.steinhauser@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "timestampspinbox.hpp"
21
22 #include <QLineEdit>
23 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
24 #include <QRegularExpression>
25 #else
26 #include <QRegExp>
27 #endif
28
29 namespace pv {
30 namespace widgets {
31
32 TimestampSpinBox::TimestampSpinBox(QWidget* parent)
33         : QAbstractSpinBox(parent)
34         , precision_(9)
35         , stepsize_("1e-6")
36 {
37         connect(this, SIGNAL(editingFinished()), this, SLOT(on_editingFinished()));
38         connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(on_editingFinished()));
39
40         updateEdit();
41 }
42
43 void TimestampSpinBox::stepBy(int steps)
44 {
45         setValue(value_ + steps * stepsize_);
46 }
47
48 QAbstractSpinBox::StepEnabled TimestampSpinBox::stepEnabled() const
49 {
50         return QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled;
51 }
52
53 unsigned TimestampSpinBox::precision() const
54 {
55         return precision_;
56 }
57
58 void TimestampSpinBox::setPrecision(unsigned precision)
59 {
60         precision_ = precision;
61         updateEdit();
62 }
63
64 const pv::util::Timestamp& TimestampSpinBox::singleStep() const
65 {
66         return stepsize_;
67 }
68
69 void TimestampSpinBox::setSingleStep(const pv::util::Timestamp& step)
70 {
71         stepsize_ = step;
72 }
73
74 const pv::util::Timestamp& TimestampSpinBox::value() const
75 {
76         return value_;
77 }
78
79 QSize TimestampSpinBox::minimumSizeHint() const
80 {
81         const QFontMetrics fm(fontMetrics());
82         const int l = round(value_).str().size() + precision_ + 10;
83         const int w = util::text_width(fm, QString(l, '0'));
84         const int h = lineEdit()->minimumSizeHint().height();
85         return QSize(w, h);
86 }
87
88 void TimestampSpinBox::setValue(const pv::util::Timestamp& val)
89 {
90         if (val == value_)
91                 return;
92
93         value_ = val;
94         updateEdit();
95         valueChanged(value_);
96 }
97
98 void TimestampSpinBox::on_editingFinished()
99 {
100         static const auto re_pattern = R"(\s*([-+]?)\s*([0-9]+\.?[0-9]*).*)";
101
102         bool has_match;
103         QStringList captures;
104 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
105         QRegularExpression re(re_pattern);
106         has_match = re.match(text()).hasMatch();
107         if (has_match) {
108                 captures = re.match(text()).capturedTexts();
109         }
110 #else
111         QRegExp re(re_pattern);
112         has_match = re.exactMatch(text());
113         if (has_match) {
114                 captures = re.capturedTexts();
115         }
116 #endif
117
118         if (has_match) {
119                 captures.removeFirst(); // remove entire match
120                 QString str = captures.join("");
121                 setValue(pv::util::Timestamp(str.toStdString()));
122
123         } else {
124                 // replace the malformed entered string with the old value
125                 updateEdit();
126         }
127 }
128
129 void TimestampSpinBox::updateEdit()
130 {
131         QString newtext = pv::util::format_time_si(
132                 value_, pv::util::SIPrefix::none, precision_);
133         const QSignalBlocker blocker(lineEdit());
134         // Keep cursor position
135         int cursor = lineEdit()->cursorPosition();
136         lineEdit()->setText(newtext);
137         lineEdit()->setCursorPosition(cursor);
138 }
139
140 }  // namespace widgets
141 }  // namespace pv