]>
Commit | Line | Data |
---|---|---|
806d3e1e JS |
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
806d3e1e JS |
18 | */ |
19 | ||
20 | #include "timestampspinbox.hpp" | |
21 | ||
22 | #include <QLineEdit> | |
1ed73ebd VPP |
23 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
24 | #include <QRegularExpression> | |
25 | #else | |
806d3e1e | 26 | #include <QRegExp> |
1ed73ebd | 27 | #endif |
806d3e1e JS |
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())); | |
ee7fd68c | 38 | connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(on_editingFinished())); |
806d3e1e JS |
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; | |
ae726b70 | 83 | const int w = util::text_width(fm, QString(l, '0')); |
806d3e1e JS |
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(); | |
25e734c8 | 95 | valueChanged(value_); |
806d3e1e JS |
96 | } |
97 | ||
98 | void TimestampSpinBox::on_editingFinished() | |
99 | { | |
1ed73ebd VPP |
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 | |
806d3e1e | 117 | |
1ed73ebd | 118 | if (has_match) { |
806d3e1e JS |
119 | captures.removeFirst(); // remove entire match |
120 | QString str = captures.join(""); | |
121 | setValue(pv::util::Timestamp(str.toStdString())); | |
ee7fd68c | 122 | |
2ad82c2e | 123 | } else { |
806d3e1e JS |
124 | // replace the malformed entered string with the old value |
125 | updateEdit(); | |
126 | } | |
127 | } | |
128 | ||
129 | void TimestampSpinBox::updateEdit() | |
130 | { | |
3ccf0f7f JS |
131 | QString newtext = pv::util::format_time_si( |
132 | value_, pv::util::SIPrefix::none, precision_); | |
ee7fd68c | 133 | const QSignalBlocker blocker(lineEdit()); |
134 | // Keep cursor position | |
135 | int cursor = lineEdit()->cursorPosition(); | |
806d3e1e | 136 | lineEdit()->setText(newtext); |
ee7fd68c | 137 | lineEdit()->setCursorPosition(cursor); |
806d3e1e JS |
138 | } |
139 | ||
870ea3db UH |
140 | } // namespace widgets |
141 | } // namespace pv |