Gui: implement size hint for quantity spin box

This commit is contained in:
wmayer
2019-12-22 11:52:59 +01:00
parent 7008cf92de
commit ebf585c0c7
2 changed files with 65 additions and 3 deletions

View File

@@ -26,6 +26,7 @@
# include <QDebug>
# include <QLineEdit>
# include <QFocusEvent>
# include <QFontMetrics>
# include <QHBoxLayout>
# include <QLabel>
# include <QStyle>
@@ -477,10 +478,8 @@ void Gui::QuantitySpinBox::keyPressEvent(QKeyEvent *event)
{
if (event->text() == QString::fromUtf8("=") && isBound())
openFormulaDialog();
else {
if (!hasExpression())
else if (!hasExpression())
QAbstractSpinBox::keyPressEvent(event);
}
}
@@ -769,6 +768,67 @@ void QuantitySpinBox::stepBy(int steps)
selectNumber();
}
QSize QuantitySpinBox::sizeHint() const
{
Q_D(const QuantitySpinBox);
ensurePolished();
const QFontMetrics fm(fontMetrics());
int h = lineEdit()->sizeHint().height();
int w = 0;
QString s;
QString fixedContent = QLatin1String(" ");
Base::Quantity q(d->quantity);
q.setValue(d->maximum);
s = textFromValue(q);
s.truncate(18);
s += fixedContent;
w = qMax(w, fm.width(s));
w += 2; // cursor blinking space
w += iconHeight;
QStyleOptionSpinBox opt;
initStyleOption(&opt);
QSize hint(w, h);
QSize size = style()->sizeFromContents(QStyle::CT_SpinBox, &opt, hint, this)
.expandedTo(QApplication::globalStrut());
return size;
}
QSize QuantitySpinBox::minimumSizeHint() const
{
Q_D(const QuantitySpinBox);
ensurePolished();
const QFontMetrics fm(fontMetrics());
int h = lineEdit()->minimumSizeHint().height();
int w = 0;
QString s;
QString fixedContent = QLatin1String(" ");
Base::Quantity q(d->quantity);
q.setValue(d->maximum);
s = textFromValue(q);
s.truncate(18);
s += fixedContent;
w = qMax(w, fm.width(s));
w += 2; // cursor blinking space
w += iconHeight;
QStyleOptionSpinBox opt;
initStyleOption(&opt);
QSize hint(w, h);
QSize size = style()->sizeFromContents(QStyle::CT_SpinBox, &opt, hint, this)
.expandedTo(QApplication::globalStrut());
return size;
}
void QuantitySpinBox::showEvent(QShowEvent * event)
{
Q_D(QuantitySpinBox);

View File

@@ -124,6 +124,8 @@ public:
virtual QValidator::State validate(QString &input, int &pos) const;
virtual void fixup(QString &str) const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
bool event(QEvent *event);
void setExpression(boost::shared_ptr<App::Expression> expr);