]>
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 | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include "timestampspinbox.hpp" | |
22 | ||
23 | #include <QLineEdit> | |
24 | #include <QRegExp> | |
25 | ||
26 | namespace pv { | |
27 | namespace widgets { | |
28 | ||
29 | TimestampSpinBox::TimestampSpinBox(QWidget* parent) | |
30 | : QAbstractSpinBox(parent) | |
31 | , precision_(9) | |
32 | , stepsize_("1e-6") | |
33 | { | |
34 | connect(this, SIGNAL(editingFinished()), this, SLOT(on_editingFinished())); | |
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; | |
79 | const int w = fm.width(QString(l, '0')); | |
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(); | |
91 | Q_EMIT valueChanged(value_); | |
92 | } | |
93 | ||
94 | void TimestampSpinBox::on_editingFinished() | |
95 | { | |
96 | if (!lineEdit()->isModified()) | |
97 | return; | |
98 | lineEdit()->setModified(false); | |
99 | ||
100 | QRegExp re(R"(\s*([-+]?)\s*([0-9]+\.?[0-9]*).*)"); | |
101 | ||
102 | if (re.exactMatch(text())) { | |
103 | QStringList captures = re.capturedTexts(); | |
104 | captures.removeFirst(); // remove entire match | |
105 | QString str = captures.join(""); | |
106 | setValue(pv::util::Timestamp(str.toStdString())); | |
107 | } | |
108 | else { | |
109 | // replace the malformed entered string with the old value | |
110 | updateEdit(); | |
111 | } | |
112 | } | |
113 | ||
114 | void TimestampSpinBox::updateEdit() | |
115 | { | |
116 | QString newtext = pv::util::format_si_value( | |
117 | value_, "s", 8, precision_); | |
118 | lineEdit()->setText(newtext); | |
119 | } | |
120 | ||
121 | } // widgets | |
122 | } // pv |