PulseView  unreleased development snapshot
A Qt-based sigrok GUI
timestampspinbox.cpp
Go to the documentation of this file.
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 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
24 #include <QRegularExpression>
25 #else
26 #include <QRegExp>
27 #endif
28 
29 namespace pv {
30 namespace widgets {
31 
33  : QAbstractSpinBox(parent)
34  , precision_(9)
35  , stepsize_("1e-6")
36 {
37  connect(this, SIGNAL(editingFinished()), this, SLOT(on_editingFinished()));
38  connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(on_editingFinished()));
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 
54 {
55  return precision_;
56 }
57 
59 {
61  updateEdit();
62 }
63 
65 {
66  return stepsize_;
67 }
68 
70 {
71  stepsize_ = step;
72 }
73 
75 {
76  return value_;
77 }
78 
80 {
81  const QFontMetrics fm(fontMetrics());
82  const int l = round(value_).str().size() + precision_ + 10;
83  const int w = util::text_width(fm, QString(l, '0'));
84  const int h = lineEdit()->minimumSizeHint().height();
85  return QSize(w, h);
86 }
87 
89 {
90  if (val == value_)
91  return;
92 
93  value_ = val;
94  updateEdit();
96 }
97 
99 {
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
117 
118  if (has_match) {
119  captures.removeFirst(); // remove entire match
120  QString str = captures.join("");
121  setValue(pv::util::Timestamp(str.toStdString()));
122 
123  } else {
124  // replace the malformed entered string with the old value
125  updateEdit();
126  }
127 }
128 
130 {
131  QString newtext = pv::util::format_time_si(
133  const QSignalBlocker blocker(lineEdit());
134  // Keep cursor position
135  int cursor = lineEdit()->cursorPosition();
136  lineEdit()->setText(newtext);
137  lineEdit()->setCursorPosition(cursor);
138 }
139 
140 } // namespace widgets
141 } // namespace pv
QSize minimumSizeHint() const override
void setPrecision(unsigned precision)
QString format_time_si(const Timestamp &v, SIPrefix prefix, unsigned int precision, QString unit, bool sign)
Definition: util.cpp:132
TimestampSpinBox(QWidget *parent=nullptr)
std::streamsize text_width(const QFontMetrics &metric, const QString &string)
Definition: util.cpp:290
void valueChanged(const pv::util::Timestamp &)
void setSingleStep(const pv::util::Timestamp &step)
void stepBy(int steps) override
void setValue(const pv::util::Timestamp &val)
StepEnabled stepEnabled() const override
const pv::util::Timestamp & singleStep() const
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 24 >, boost::multiprecision::et_off > Timestamp
Timestamp type providing yoctosecond resolution.
Definition: util.hpp:67
const pv::util::Timestamp & value() const