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()));
34 connect(lineEdit(), 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 = util::text_width(fm, QString(l, '0'));
80 const int h = lineEdit()->minimumSizeHint().height();
84 void TimestampSpinBox::setValue(const pv::util::Timestamp& val)
94 void TimestampSpinBox::on_editingFinished()
96 QRegExp re(R"(\s*([-+]?)\s*([0-9]+\.?[0-9]*).*)");
98 if (re.exactMatch(text())) {
99 QStringList captures = re.capturedTexts();
100 captures.removeFirst(); // remove entire match
101 QString str = captures.join("");
102 setValue(pv::util::Timestamp(str.toStdString()));
105 // replace the malformed entered string with the old value
110 void TimestampSpinBox::updateEdit()
112 QString newtext = pv::util::format_time_si(
113 value_, pv::util::SIPrefix::none, precision_);
114 const QSignalBlocker blocker(lineEdit());
115 // Keep cursor position
116 int cursor = lineEdit()->cursorPosition();
117 lineEdit()->setText(newtext);
118 lineEdit()->setCursorPosition(cursor);
121 } // namespace widgets