Gui: improve usability of QuantitySpinBox when used as menu item

This commit is contained in:
wmayer
2022-11-12 18:24:27 +01:00
parent 4774c432c5
commit a2a285b7c9
2 changed files with 25 additions and 3 deletions

View File

@@ -363,7 +363,7 @@ void QuantitySpinBox::evaluateExpression()
void Gui::QuantitySpinBox::setNumberExpression(App::NumberExpression* expr)
{
lineEdit()->setText(getUserString(expr->getQuantity()));
updateEdit(getUserString(expr->getQuantity()));
handlePendingEmit();
}
@@ -403,10 +403,31 @@ void QuantitySpinBox::updateText(const Quantity &quant)
double dFactor;
QString txt = getUserString(quant, dFactor, d->unitStr);
d->unitValue = quant.getValue()/dFactor;
lineEdit()->setText(txt);
updateEdit(txt);
handlePendingEmit();
}
void QuantitySpinBox::updateEdit(const QString& text)
{
Q_D(QuantitySpinBox);
QLineEdit* edit = lineEdit();
bool empty = edit->text().isEmpty();
int cursor = edit->cursorPosition();
int selsize = edit->selectedText().size();
edit->setText(text);
cursor = qBound(0, cursor, edit->displayText().size() - d->unitStr.size());
if (selsize > 0) {
edit->setSelection(0, cursor);
}
else {
edit->setCursorPosition(empty ? 0 : cursor);
}
}
void QuantitySpinBox::validateInput()
{
Q_D(QuantitySpinBox);
@@ -416,7 +437,7 @@ void QuantitySpinBox::validateInput()
const App::ObjectIdentifier & path = getPath();
d->validateAndInterpret(text, state, path);
if (state != QValidator::Acceptable) {
lineEdit()->setText(d->validStr);
updateEdit(d->validStr);
}
handlePendingEmit();

View File

@@ -160,6 +160,7 @@ protected:
private:
void validateInput() override;
void updateText(const Base::Quantity&);
void updateEdit(const QString& text);
void updateFromCache(bool notify, bool updateUnit = true);
QString getUserString(const Base::Quantity& val, double& factor, QString& unitString) const;
QString getUserString(const Base::Quantity& val) const;