]> sigrok.org Git - pulseview.git/blobdiff - pv/widgets/timestampspinbox.cpp
Accept new position on enter press in cursor popups
[pulseview.git] / pv / widgets / timestampspinbox.cpp
index 463a846277d814bcebed936335e18d73a6314f55..fea8175e8e00b74b21f6690cf9d726e3a9cd418c 100644 (file)
@@ -14,8 +14,7 @@
  * 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"
@@ -32,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();
 }
@@ -88,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())) {
@@ -104,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();
@@ -114,8 +111,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