From 40793d2e7dddc415295e81f70486a47d24199302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Mon, 30 Nov 2020 21:33:41 +0100 Subject: [PATCH] Selectable digit for scrollwheel increment --- .../CustomWidgets/siunitedit.cpp | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/Software/PC_Application/CustomWidgets/siunitedit.cpp b/Software/PC_Application/CustomWidgets/siunitedit.cpp index 5e95822..96e649b 100644 --- a/Software/PC_Application/CustomWidgets/siunitedit.cpp +++ b/Software/PC_Application/CustomWidgets/siunitedit.cpp @@ -101,16 +101,34 @@ bool SIUnitEdit::eventFilter(QObject *, QEvent *event) int sign = increment > 0 ? 1 : -1; // figure out step increment auto newVal = _value; - while(steps > 0) { - // do update in multiple steps because the step size could change inbetween + auto cursor = cursorPosition(); + if(cursor == 0) { + // no active cursor, use default digit constexpr int nthDigit = 3; + while(steps > 0) { + // do update in multiple steps because the step size could change inbetween + auto step_size = pow(10, floor(log10(std::abs(newVal))) - nthDigit + 1); + newVal += step_size * sign; + steps--; + } + setValue(newVal); + } else { + // change the digit at the current cursor + int nthDigit = cursor; + if(_value < 0) { + // account for sign + nthDigit--; + } + auto dotPos = text().indexOf('.'); + if(dotPos >= 0 && dotPos < nthDigit) { + nthDigit--; + } auto step_size = pow(10, floor(log10(std::abs(newVal))) - nthDigit + 1); - newVal += step_size * sign; - steps--; + newVal += step_size * steps * sign; + setValue(newVal); + setText(placeholderText()); + setCursorPosition(cursor); } - setValue(newVal); - continueEditing(); - setFocus(); return true; } return false;