]> sigrok.org Git - pulseview.git/blobdiff - pv/widgets/timestampspinbox.cpp
DecodeSignal: Support unitsize > 1 for logic output
[pulseview.git] / pv / widgets / timestampspinbox.cpp
index fd52c504e240d1d907d1e47cdeca0ad9d2c0ab79..01424a5b7837654e50c36de5f3f4ce27c33ec8a1 100644 (file)
@@ -31,6 +31,7 @@ TimestampSpinBox::TimestampSpinBox(QWidget* parent)
        , stepsize_("1e-6")
 {
        connect(this, SIGNAL(editingFinished()), this, SLOT(on_editingFinished()));
+       connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(on_editingFinished()));
 
        updateEdit();
 }
@@ -75,7 +76,7 @@ QSize TimestampSpinBox::minimumSizeHint() const
 {
        const QFontMetrics fm(fontMetrics());
        const int l = round(value_).str().size() + precision_ + 10;
-       const int w = fm.width(QString(l, '0'));
+       const int w = util::text_width(fm, QString(l, '0'));
        const int h = lineEdit()->minimumSizeHint().height();
        return QSize(w, h);
 }
@@ -87,15 +88,11 @@ void TimestampSpinBox::setValue(const pv::util::Timestamp& val)
 
        value_ = val;
        updateEdit();
-       Q_EMIT valueChanged(value_);
+       valueChanged(value_);
 }
 
 void TimestampSpinBox::on_editingFinished()
 {
-       if (!lineEdit()->isModified())
-               return;
-       lineEdit()->setModified(false);
-
        QRegExp re(R"(\s*([-+]?)\s*([0-9]+\.?[0-9]*).*)");
 
        if (re.exactMatch(text())) {
@@ -103,6 +100,7 @@ void TimestampSpinBox::on_editingFinished()
                captures.removeFirst(); // remove entire match
                QString str = captures.join("");
                setValue(pv::util::Timestamp(str.toStdString()));
+
        } else {
                // replace the malformed entered string with the old value
                updateEdit();
@@ -113,7 +111,11 @@ void TimestampSpinBox::updateEdit()
 {
        QString newtext = pv::util::format_time_si(
                value_, pv::util::SIPrefix::none, precision_);
+       const QSignalBlocker blocker(lineEdit());
+       // Keep cursor position
+       int cursor = lineEdit()->cursorPosition();
        lineEdit()->setText(newtext);
+       lineEdit()->setCursorPosition(cursor);
 }
 
 }  // namespace widgets