Skip to content

Commit

Permalink
Gui: [skip ci] make QuantitySpinBox to handle disabled keyboard tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jun 1, 2020
1 parent 28222fe commit 15c9644
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 31 deletions.
111 changes: 81 additions & 30 deletions src/Gui/QuantitySpinBox.cpp
Expand Up @@ -63,6 +63,7 @@ class QuantitySpinBoxPrivate
public:
QuantitySpinBoxPrivate() :
validInput(true),
pendingEmit(false),
unitValue(0),
maximum(DOUBLE_MAX),
minimum(-DOUBLE_MAX),
Expand All @@ -83,6 +84,34 @@ class QuantitySpinBoxPrivate
return text;
}

bool validate(QString& input, Base::Quantity& result) const
{
bool success = false;
QString tmp = input;
int pos = 0;
QValidator::State state;
Base::Quantity res = validateAndInterpret(tmp, pos, state);
res.setFormat(quantity.getFormat());
if (state == QValidator::Acceptable) {
success = true;
result = res;
input = tmp;
}
else if (state == QValidator::Intermediate) {
tmp = tmp.trimmed();
tmp += QLatin1Char(' ');
tmp += unitStr;
Base::Quantity res2 = validateAndInterpret(tmp, pos, state);
res2.setFormat(quantity.getFormat());
if (state == QValidator::Acceptable) {
success = true;
result = res2;
input = tmp;
}
}

return success;
}
Base::Quantity validateAndInterpret(QString& input, int& pos, QValidator::State& state) const
{
Base::Quantity res;
Expand Down Expand Up @@ -224,8 +253,10 @@ class QuantitySpinBoxPrivate

QLocale locale;
bool validInput;
bool pendingEmit;
QString validStr;
Base::Quantity quantity;
Base::Quantity cached;
Base::Unit unit;
double unitValue;
QString unitStr;
Expand All @@ -245,6 +276,8 @@ QuantitySpinBox::QuantitySpinBox(QWidget *parent)
this->setContextMenuPolicy(Qt::DefaultContextMenu);
QObject::connect(lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(userInput(QString)));
QObject::connect(this, SIGNAL(editingFinished()),

This comment has been minimized.

Copy link
@donovaly

donovaly Jun 1, 2020

Member

why editingFinished()? according to the Qt docs keyboardTracking only affects valueChanged():
https://doc.qt.io/archives/qt-4.8/qabstractspinbox.html#keyboardTracking-prop

this, SLOT(handlePendingEmit()));

defaultPalette = lineEdit()->palette();

Expand Down Expand Up @@ -378,6 +411,8 @@ void Gui::QuantitySpinBox::onChange()
s << value->getValue();

lineEdit()->setText(getUserString(value->getQuantity()));
handlePendingEmit();

setReadOnly(true);
QPixmap pixmap = getIcon(":/icons/bound-expression.svg", QSize(iconHeight, iconHeight));
iconLabel->setPixmap(pixmap);
Expand Down Expand Up @@ -490,6 +525,7 @@ void QuantitySpinBox::updateText(const Quantity &quant)
QString txt = getUserString(quant, dFactor, d->unitStr);
d->unitValue = quant.getValue()/dFactor;
lineEdit()->setText(txt);
handlePendingEmit();
}

Base::Quantity QuantitySpinBox::value() const
Expand Down Expand Up @@ -536,44 +572,26 @@ void QuantitySpinBox::userInput(const QString & text)
{
Q_D(QuantitySpinBox);

d->pendingEmit = true;

QString tmp = text;
int pos = 0;
QValidator::State state;
Base::Quantity res = d->validateAndInterpret(tmp, pos, state);
res.setFormat(d->quantity.getFormat());
if (state == QValidator::Acceptable) {
Base::Quantity res;
if (d->validate(tmp, res)) {
d->validStr = tmp;
d->validInput = true;
d->validStr = text;
}
else if (state == QValidator::Intermediate) {
tmp = tmp.trimmed();
tmp += QLatin1Char(' ');
tmp += d->unitStr;
Base::Quantity res2 = d->validateAndInterpret(tmp, pos, state);
res2.setFormat(d->quantity.getFormat());
if (state == QValidator::Acceptable) {
d->validInput = true;
d->validStr = tmp;
res = res2;
}
else {
d->validInput = false;
return;
}
}
else {
d->validInput = false;
return;
}

double factor;
getUserString(res, factor, d->unitStr);
d->unitValue = res.getValue()/factor;
d->quantity = res;

// signaling
valueChanged(res);
valueChanged(res.getValue());
if (keyboardTracking()) {
d->cached = res;
handlePendingEmit();
}
else {
d->cached = res;
}
}

void QuantitySpinBox::openFormulaDialog()
Expand Down Expand Up @@ -610,6 +628,23 @@ void QuantitySpinBox::finishFormulaDialog()
Q_EMIT showFormulaDialog(false);
}

void QuantitySpinBox::handlePendingEmit()
{
Q_D(QuantitySpinBox);
if (d->pendingEmit) {
double factor;
const Base::Quantity& res = d->cached;
getUserString(res, factor, d->unitStr);
d->unitValue = res.getValue() / factor;
d->quantity = res;

// signaling
valueChanged(res);
valueChanged(res.getValue());
d->pendingEmit = false;
}
}

Base::Unit QuantitySpinBox::unit() const
{
Q_D(const QuantitySpinBox);
Expand Down Expand Up @@ -765,6 +800,7 @@ void QuantitySpinBox::stepBy(int steps)
val = d->minimum;

lineEdit()->setText(QString::fromUtf8("%L1 %2").arg(val).arg(d->unitStr));
handlePendingEmit();
update();
selectNumber();
}
Expand Down Expand Up @@ -842,6 +878,18 @@ void QuantitySpinBox::showEvent(QShowEvent * event)
selectNumber();
}

void QuantitySpinBox::hideEvent(QHideEvent * event)
{
handlePendingEmit();
QAbstractSpinBox::hideEvent(event);
}

void QuantitySpinBox::closeEvent(QCloseEvent * event)
{
handlePendingEmit();
QAbstractSpinBox::closeEvent(event);
}

bool QuantitySpinBox::event(QEvent * event)
{
// issue #0004059: Tooltips for Gui::QuantitySpinBox not showing
Expand Down Expand Up @@ -900,6 +948,9 @@ void QuantitySpinBox::focusOutEvent(QFocusEvent * event)
if (state != QValidator::Acceptable) {
lineEdit()->setText(d->validStr);
}

handlePendingEmit();

QToolTip::hideText();
QAbstractSpinBox::focusOutEvent(event);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Gui/QuantitySpinBox.h
Expand Up @@ -142,13 +142,16 @@ protected Q_SLOTS:
void userInput(const QString & text);
void openFormulaDialog();
void finishFormulaDialog();

void handlePendingEmit();

//get notified on expression change
virtual void onChange();

protected:
virtual StepEnabled stepEnabled() const;
virtual void showEvent(QShowEvent * event);
virtual void hideEvent(QHideEvent * event);
virtual void closeEvent(QCloseEvent * event);
virtual void focusInEvent(QFocusEvent * event);
virtual void focusOutEvent(QFocusEvent * event);
virtual void keyPressEvent(QKeyEvent *event);
Expand Down

0 comments on commit 15c9644

Please sign in to comment.