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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "timestampspinbox.hpp"
29 TimestampSpinBox::TimestampSpinBox(QWidget* parent)
30 : QAbstractSpinBox(parent)
34 connect(this, SIGNAL(editingFinished()), this, SLOT(on_editingFinished()));
39 void TimestampSpinBox::stepBy(int steps)
41 setValue(value_ + steps * stepsize_);
44 QAbstractSpinBox::StepEnabled TimestampSpinBox::stepEnabled() const
46 return QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled;
49 unsigned TimestampSpinBox::precision() const
54 void TimestampSpinBox::setPrecision(unsigned precision)
56 precision_ = precision;
60 const pv::util::Timestamp& TimestampSpinBox::singleStep() const
65 void TimestampSpinBox::setSingleStep(const pv::util::Timestamp& step)
70 const pv::util::Timestamp& TimestampSpinBox::value() const
75 QSize TimestampSpinBox::minimumSizeHint() const
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();
84 void TimestampSpinBox::setValue(const pv::util::Timestamp& val)
91 Q_EMIT valueChanged(value_);
94 void TimestampSpinBox::on_editingFinished()
96 if (!lineEdit()->isModified())
98 lineEdit()->setModified(false);
100 QRegExp re(R"(\s*([-+]?)\s*([0-9]+\.?[0-9]*).*)");
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()));
108 // replace the malformed entered string with the old value
113 void TimestampSpinBox::updateEdit()
115 QString newtext = pv::util::format_time_si(
116 value_, pv::util::SIPrefix::none, precision_);
117 lineEdit()->setText(newtext);