]>
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> | |
23 | #include <QRegExp> | |
24 | ||
25 | namespace pv { | |
26 | namespace widgets { | |
27 | ||
28 | TimestampSpinBox::TimestampSpinBox(QWidget* parent) | |
29 | : QAbstractSpinBox(parent) | |
30 | , precision_(9) | |
31 | , stepsize_("1e-6") | |
32 | { | |
33 | connect(this, SIGNAL(editingFinished()), this, SLOT(on_editingFinished())); | |
ee7fd68c | 34 | connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(on_editingFinished())); |
806d3e1e JS |
35 | |
36 | updateEdit(); | |
37 | } | |
38 | ||
39 | void TimestampSpinBox::stepBy(int steps) | |
40 | { | |
41 | setValue(value_ + steps * stepsize_); | |
42 | } | |
43 | ||
44 | QAbstractSpinBox::StepEnabled TimestampSpinBox::stepEnabled() const | |
45 | { | |
46 | return QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled; | |
47 | } | |
48 | ||
49 | unsigned TimestampSpinBox::precision() const | |
50 | { | |
51 | return precision_; | |
52 | } | |
53 | ||
54 | void TimestampSpinBox::setPrecision(unsigned precision) | |
55 | { | |
56 | precision_ = precision; | |
57 | updateEdit(); | |
58 | } | |
59 | ||
60 | const pv::util::Timestamp& TimestampSpinBox::singleStep() const | |
61 | { | |
62 | return stepsize_; | |
63 | } | |
64 | ||
65 | void TimestampSpinBox::setSingleStep(const pv::util::Timestamp& step) | |
66 | { | |
67 | stepsize_ = step; | |
68 | } | |
69 | ||
70 | const pv::util::Timestamp& TimestampSpinBox::value() const | |
71 | { | |
72 | return value_; | |
73 | } | |
74 | ||
75 | QSize TimestampSpinBox::minimumSizeHint() const | |
76 | { | |
77 | const QFontMetrics fm(fontMetrics()); | |
78 | const int l = round(value_).str().size() + precision_ + 10; | |
ae726b70 | 79 | const int w = util::text_width(fm, QString(l, '0')); |
806d3e1e JS |
80 | const int h = lineEdit()->minimumSizeHint().height(); |
81 | return QSize(w, h); | |
82 | } | |
83 | ||
84 | void TimestampSpinBox::setValue(const pv::util::Timestamp& val) | |
85 | { | |
86 | if (val == value_) | |
87 | return; | |
88 | ||
89 | value_ = val; | |
90 | updateEdit(); | |
25e734c8 | 91 | valueChanged(value_); |
806d3e1e JS |
92 | } |
93 | ||
94 | void TimestampSpinBox::on_editingFinished() | |
95 | { | |
806d3e1e JS |
96 | QRegExp re(R"(\s*([-+]?)\s*([0-9]+\.?[0-9]*).*)"); |
97 | ||
98 | if (re.exactMatch(text())) { | |
99 | QStringList captures = re.capturedTexts(); | |
100 | captures.removeFirst(); // remove entire match | |
101 | QString str = captures.join(""); | |
102 | setValue(pv::util::Timestamp(str.toStdString())); | |
ee7fd68c | 103 | |
2ad82c2e | 104 | } else { |
806d3e1e JS |
105 | // replace the malformed entered string with the old value |
106 | updateEdit(); | |
107 | } | |
108 | } | |
109 | ||
110 | void TimestampSpinBox::updateEdit() | |
111 | { | |
3ccf0f7f JS |
112 | QString newtext = pv::util::format_time_si( |
113 | value_, pv::util::SIPrefix::none, precision_); | |
ee7fd68c | 114 | const QSignalBlocker blocker(lineEdit()); |
115 | // Keep cursor position | |
116 | int cursor = lineEdit()->cursorPosition(); | |
806d3e1e | 117 | lineEdit()->setText(newtext); |
ee7fd68c | 118 | lineEdit()->setCursorPosition(cursor); |
806d3e1e JS |
119 | } |
120 | ||
870ea3db UH |
121 | } // namespace widgets |
122 | } // namespace pv |