]> sigrok.org Git - pulseview.git/blobdiff - pv/widgets/timestampspinbox.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / widgets / timestampspinbox.cpp
index 233ff6484f562b9d723a0bd74a9e587c82fc686d..b99a640a843055fa2d2271bcae9c67d6607f63a9 100644 (file)
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "timestampspinbox.hpp"
 
 #include <QLineEdit>
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+#include <QRegularExpression>
+#else
 #include <QRegExp>
+#endif
 
 namespace pv {
 namespace widgets {
@@ -32,6 +35,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();
 }
@@ -76,7 +80,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);
 }
@@ -88,24 +92,35 @@ 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]*).*)");
+       static const auto re_pattern = R"(\s*([-+]?)\s*([0-9]+\.?[0-9]*).*)";
+
+       bool has_match;
+       QStringList captures;
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+       QRegularExpression re(re_pattern);
+       has_match = re.match(text()).hasMatch();
+       if (has_match) {
+               captures = re.match(text()).capturedTexts();
+       }
+#else
+       QRegExp re(re_pattern);
+       has_match = re.exactMatch(text());
+       if (has_match) {
+               captures = re.capturedTexts();
+       }
+#endif
 
-       if (re.exactMatch(text())) {
-               QStringList captures = re.capturedTexts();
+       if (has_match) {
                captures.removeFirst(); // remove entire match
                QString str = captures.join("");
                setValue(pv::util::Timestamp(str.toStdString()));
-       }
-       else {
+
+       else {
                // replace the malformed entered string with the old value
                updateEdit();
        }
@@ -115,8 +130,12 @@ 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);
 }
 
-} // widgets
-} // pv
+}  // namespace widgets
+}  // namespace pv