]> sigrok.org Git - pulseview.git/blame - pv/widgets/timestampspinbox.cpp
Use bool literals for boolean values.
[pulseview.git] / pv / widgets / timestampspinbox.cpp
CommitLineData
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
25namespace pv {
26namespace widgets {
27
28TimestampSpinBox::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
38void TimestampSpinBox::stepBy(int steps)
39{
40 setValue(value_ + steps * stepsize_);
41}
42
43QAbstractSpinBox::StepEnabled TimestampSpinBox::stepEnabled() const
44{
45 return QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled;
46}
47
48unsigned TimestampSpinBox::precision() const
49{
50 return precision_;
51}
52
53void TimestampSpinBox::setPrecision(unsigned precision)
54{
55 precision_ = precision;
56 updateEdit();
57}
58
59const pv::util::Timestamp& TimestampSpinBox::singleStep() const
60{
61 return stepsize_;
62}
63
64void TimestampSpinBox::setSingleStep(const pv::util::Timestamp& step)
65{
66 stepsize_ = step;
67}
68
69const pv::util::Timestamp& TimestampSpinBox::value() const
70{
71 return value_;
72}
73
74QSize 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
83void TimestampSpinBox::setValue(const pv::util::Timestamp& val)
84{
85 if (val == value_)
86 return;
87
88 value_ = val;
89 updateEdit();
90 Q_EMIT valueChanged(value_);
91}
92
93void 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()));
2ad82c2e 106 } else {
806d3e1e
JS
107 // replace the malformed entered string with the old value
108 updateEdit();
109 }
110}
111
112void TimestampSpinBox::updateEdit()
113{
3ccf0f7f
JS
114 QString newtext = pv::util::format_time_si(
115 value_, pv::util::SIPrefix::none, precision_);
806d3e1e
JS
116 lineEdit()->setText(newtext);
117}
118
119} // widgets
120} // pv