PulseView  0.3.0
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, 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 
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 
50 {
51  return precision_;
52 }
53 
54 void TimestampSpinBox::setPrecision(unsigned precision)
55 {
57  updateEdit();
58 }
59 
61 {
62  return stepsize_;
63 }
64 
66 {
67  stepsize_ = step;
68 }
69 
71 {
72  return value_;
73 }
74 
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 
85 {
86  if (val == value_)
87  return;
88 
89  value_ = val;
90  updateEdit();
91  Q_EMIT valueChanged(value_);
92 }
93 
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  } else {
108  // replace the malformed entered string with the old value
109  updateEdit();
110  }
111 }
112 
114 {
115  QString newtext = pv::util::format_time_si(
117  lineEdit()->setText(newtext);
118 }
119 
120 } // widgets
121 } // 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:107
TimestampSpinBox(QWidget *parent=nullptr)
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:58
const pv::util::Timestamp & value() const