Skip to content

Commit

Permalink
GCS:Utils: Implement minimumSizeHint for LongLongSpinBox (#1873)
Browse files Browse the repository at this point in the history
Otherwise widgets with fixed/minimum size policies end up too short. If only Qt made QAbstractSpinBox able to take care of things like this without private headers..
  • Loading branch information
tracernz authored and mlyle committed Jul 2, 2017
1 parent 955c265 commit 6833bef
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
55 changes: 54 additions & 1 deletion ground/gcs/src/libs/utils/longlongspinbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@

#include "longlongspinbox.h"

#include <QLineEdit>
#include <QApplication>
#include <QEvent>
#include <QLineEdit>
#include <QStyle>
#include <QStyleOptionSpinBox>

#include <limits>

Expand Down Expand Up @@ -72,6 +74,7 @@ void LongLongSpinBox::setPrefix(const QString &prefix)
{
m_prefix = prefix;
updateEdit();
m_cachedMinSize = QSize();
updateGeometry();
}

Expand All @@ -84,6 +87,7 @@ void LongLongSpinBox::setSuffix(const QString &suffix)
{
m_suffix = suffix;
updateEdit();
m_cachedMinSize = QSize();
updateGeometry();
}

Expand Down Expand Up @@ -128,6 +132,7 @@ void LongLongSpinBox::setRange(qint64 min, qint64 max)
m_min = min;
m_max = max < min ? min : max;
setValue(qBound(m_min, m_value, m_max));
m_cachedMinSize = QSize();
}

int LongLongSpinBox::displayIntegerBase() const
Expand All @@ -146,6 +151,8 @@ void LongLongSpinBox::setDisplayIntegerBase(int base)
m_displayBase = base;
updateEdit();
}

m_cachedMinSize = QSize();
}

QString LongLongSpinBox::textFromValue(qint64 val) const
Expand Down Expand Up @@ -325,6 +332,52 @@ void LongLongSpinBox::lineEditChanged(const QString &t)
}
}

QSize LongLongSpinBox::minimumSizeHint() const
{
if (m_cachedMinSize.isEmpty()) {
ensurePolished();

const QFontMetrics fm(fontMetrics());
int h = lineEdit()->minimumSizeHint().height();

QString s = textFromValue(m_min);
QString fixedContent = m_prefix + QStringLiteral(" ") + m_suffix;
s.truncate(18);
s += fixedContent;
int w = fm.width(s);
s = textFromValue(m_max);
s.truncate(18);
s += fixedContent;
w = std::max(w, fm.width(s));

if (specialValueText().size())
w = qMax(w, fm.width(specialValueText()));

w += 2; // cursor blinking space

QStyleOptionSpinBox opt;
initStyleOption(&opt);
QSize hint(w, h);

m_cachedMinSize = style()->sizeFromContents(QStyle::CT_SpinBox, &opt, hint, this)
.expandedTo(QApplication::globalStrut());
}
return m_cachedMinSize;
}

bool LongLongSpinBox::event(QEvent *event)
{
switch (event->type()) {
case QEvent::FontChange:
case QEvent::StyleChange:
m_cachedMinSize = QSize();
break;
default:
break;
}
return QAbstractSpinBox::event(event);
}

/**
* @}
* @}
Expand Down
5 changes: 5 additions & 0 deletions ground/gcs/src/libs/utils/longlongspinbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class QTCREATOR_UTILS_EXPORT LongLongSpinBox : public QAbstractSpinBox
int displayIntegerBase() const;
void setDisplayIntegerBase(int base);

virtual QSize minimumSizeHint() const override;

virtual bool event(QEvent *event) override;

protected:
QValidator::State validate(QString &input, int &pos) const override;
virtual qint64 valueFromText(const QString &text);
Expand Down Expand Up @@ -95,6 +99,7 @@ protected Q_SLOTS:
QString m_prefix, m_suffix;
int m_displayBase;
bool m_showGroupSeparator;
mutable QSize m_cachedMinSize;

Q_DISABLE_COPY(LongLongSpinBox)
};
Expand Down

0 comments on commit 6833bef

Please sign in to comment.