]> sigrok.org Git - pulseview.git/blob - pv/widgets/timestampspinbox.cpp
Session: Query the device's sample rate when needed, not sooner
[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 #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()));
34
35         updateEdit();
36 }
37
38 void TimestampSpinBox::stepBy(int steps)
39 {
40         setValue(value_ + steps * stepsize_);
41 }
42
43 QAbstractSpinBox::StepEnabled TimestampSpinBox::stepEnabled() const
44 {
45         return QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled;
46 }
47
48 unsigned TimestampSpinBox::precision() const
49 {
50         return precision_;
51 }
52
53 void TimestampSpinBox::setPrecision(unsigned precision)
54 {
55         precision_ = precision;
56         updateEdit();
57 }
58
59 const pv::util::Timestamp& TimestampSpinBox::singleStep() const
60 {
61         return stepsize_;
62 }
63
64 void TimestampSpinBox::setSingleStep(const pv::util::Timestamp& step)
65 {
66         stepsize_ = step;
67 }
68
69 const pv::util::Timestamp& TimestampSpinBox::value() const
70 {
71         return value_;
72 }
73
74 QSize TimestampSpinBox::minimumSizeHint() const
75 {
76         const QFontMetrics fm(fontMetrics());
77         const int l = round(value_).str().size() + precision_ + 10;
78         const int w = fm.width(QString(l, '0'));
79         const int h = lineEdit()->minimumSizeHint().height();
80         return QSize(w, h);
81 }
82
83 void TimestampSpinBox::setValue(const pv::util::Timestamp& val)
84 {
85         if (val == value_)
86                 return;
87
88         value_ = val;
89         updateEdit();
90         valueChanged(value_);
91 }
92
93 void TimestampSpinBox::on_editingFinished()
94 {
95         if (!lineEdit()->isModified())
96                 return;
97         lineEdit()->setModified(false);
98
99         QRegExp re(R"(\s*([-+]?)\s*([0-9]+\.?[0-9]*).*)");
100
101         if (re.exactMatch(text())) {
102                 QStringList captures = re.capturedTexts();
103                 captures.removeFirst(); // remove entire match
104                 QString str = captures.join("");
105                 setValue(pv::util::Timestamp(str.toStdString()));
106         } else {
107                 // replace the malformed entered string with the old value
108                 updateEdit();
109         }
110 }
111
112 void TimestampSpinBox::updateEdit()
113 {
114         QString newtext = pv::util::format_time_si(
115                 value_, pv::util::SIPrefix::none, precision_);
116         lineEdit()->setText(newtext);
117 }
118
119 }  // namespace widgets
120 }  // namespace pv