2 * This file is part of the PulseView project.
4 * Copyright (C) 2015 Jens Steinhauser <jens.steinhauser@gmail.com>
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.
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.
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/>.
20 #include "timestampspinbox.hpp"
28 TimestampSpinBox::TimestampSpinBox(QWidget* parent)
29 : QAbstractSpinBox(parent)
33 connect(this, SIGNAL(editingFinished()), this, SLOT(on_editingFinished()));
38 void TimestampSpinBox::stepBy(int steps)
40 setValue(value_ + steps * stepsize_);
43 QAbstractSpinBox::StepEnabled TimestampSpinBox::stepEnabled() const
45 return QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled;
48 unsigned TimestampSpinBox::precision() const
53 void TimestampSpinBox::setPrecision(unsigned precision)
55 precision_ = precision;
59 const pv::util::Timestamp& TimestampSpinBox::singleStep() const
64 void TimestampSpinBox::setSingleStep(const pv::util::Timestamp& step)
69 const pv::util::Timestamp& TimestampSpinBox::value() const
74 QSize TimestampSpinBox::minimumSizeHint() const
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();
83 void TimestampSpinBox::setValue(const pv::util::Timestamp& val)
90 Q_EMIT valueChanged(value_);
93 void TimestampSpinBox::on_editingFinished()
95 if (!lineEdit()->isModified())
97 lineEdit()->setModified(false);
99 QRegExp re(R"(\s*([-+]?)\s*([0-9]+\.?[0-9]*).*)");
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()));
107 // replace the malformed entered string with the old value
112 void TimestampSpinBox::updateEdit()
114 QString newtext = pv::util::format_time_si(
115 value_, pv::util::SIPrefix::none, precision_);
116 lineEdit()->setText(newtext);
119 } // namespace widgets