SIUnitEdit behavior improvement
- ignore mousewheel event when cursor in front of first digit - keep text if value is changed while editing
This commit is contained in:
parent
26dc68fb73
commit
642995cec4
@ -49,10 +49,14 @@ static char swapUpperLower(char c) {
|
|||||||
|
|
||||||
bool SIUnitEdit::eventFilter(QObject *, QEvent *event)
|
bool SIUnitEdit::eventFilter(QObject *, QEvent *event)
|
||||||
{
|
{
|
||||||
|
if(value() == 1000000) {
|
||||||
|
qDebug() << "Event: " << event->type();
|
||||||
|
}
|
||||||
if (event->type() == QEvent::KeyPress) {
|
if (event->type() == QEvent::KeyPress) {
|
||||||
int key = static_cast<QKeyEvent *>(event)->key();
|
int key = static_cast<QKeyEvent *>(event)->key();
|
||||||
if(key == Qt::Key_Escape) {
|
if(key == Qt::Key_Escape) {
|
||||||
// abort editing process and set old value
|
// abort editing process and set old value
|
||||||
|
clear();
|
||||||
setValueQuiet(_value);
|
setValueQuiet(_value);
|
||||||
emit editingAborted();
|
emit editingAborted();
|
||||||
clearFocus();
|
clearFocus();
|
||||||
@ -91,7 +95,7 @@ bool SIUnitEdit::eventFilter(QObject *, QEvent *event)
|
|||||||
} else if(event->type() == QEvent::Wheel) {
|
} else if(event->type() == QEvent::Wheel) {
|
||||||
if(_value == 0.0) {
|
if(_value == 0.0) {
|
||||||
// can't figure out step size with zero value
|
// can't figure out step size with zero value
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
auto wheel = static_cast<QWheelEvent*>(event);
|
auto wheel = static_cast<QWheelEvent*>(event);
|
||||||
// most mousewheel have 15 degree increments, the reported delta is in 1/8th degree -> 120
|
// most mousewheel have 15 degree increments, the reported delta is in 1/8th degree -> 120
|
||||||
@ -101,18 +105,12 @@ bool SIUnitEdit::eventFilter(QObject *, QEvent *event)
|
|||||||
int sign = increment > 0 ? 1 : -1;
|
int sign = increment > 0 ? 1 : -1;
|
||||||
// figure out step increment
|
// figure out step increment
|
||||||
auto newVal = _value;
|
auto newVal = _value;
|
||||||
auto cursor = cursorPosition();
|
if(hasFocus()) {
|
||||||
if(cursor == 0) {
|
auto cursor = cursorPosition();
|
||||||
// no active cursor, use default digit
|
if(cursor == 0) {
|
||||||
constexpr int nthDigit = 3;
|
// cursor in front of first digit, do nothing (too big of a change, probably over/underflows)
|
||||||
while(steps > 0) {
|
return true;
|
||||||
// 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
|
// change the digit at the current cursor
|
||||||
int nthDigit = cursor;
|
int nthDigit = cursor;
|
||||||
// account for decimal point/leading zero/sign
|
// account for decimal point/leading zero/sign
|
||||||
@ -131,6 +129,16 @@ bool SIUnitEdit::eventFilter(QObject *, QEvent *event)
|
|||||||
setValue(newVal);
|
setValue(newVal);
|
||||||
setText(placeholderText());
|
setText(placeholderText());
|
||||||
setCursorPosition(cursor);
|
setCursorPosition(cursor);
|
||||||
|
} else {
|
||||||
|
// default to the third 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);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -140,7 +148,6 @@ bool SIUnitEdit::eventFilter(QObject *, QEvent *event)
|
|||||||
void SIUnitEdit::setValueQuiet(double value)
|
void SIUnitEdit::setValueQuiet(double value)
|
||||||
{
|
{
|
||||||
_value = value;
|
_value = value;
|
||||||
clear();
|
|
||||||
setPlaceholderText(Unit::ToString(value, unit, prefixes, precision));
|
setPlaceholderText(Unit::ToString(value, unit, prefixes, precision));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,12 +170,12 @@ void SIUnitEdit::parseNewValue(double factor)
|
|||||||
// remaining input should only contain numbers
|
// remaining input should only contain numbers
|
||||||
bool conversion_ok;
|
bool conversion_ok;
|
||||||
auto v = input.toDouble(&conversion_ok);
|
auto v = input.toDouble(&conversion_ok);
|
||||||
|
clear();
|
||||||
if(conversion_ok) {
|
if(conversion_ok) {
|
||||||
setValue(v * factor);
|
setValue(v * factor);
|
||||||
} else {
|
} else {
|
||||||
qWarning() << "SIUnit conversion failure:" << input;
|
qWarning() << "SIUnit conversion failure:" << input;
|
||||||
}
|
}
|
||||||
clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user