Skip to content

Commit

Permalink
Merge pull request commontk#838 from jcfr/fix-double-spinbox-sizehint
Browse files Browse the repository at this point in the history
Fix ctkDoubleSpinBox support for sizeHintPolicy with Qt5
  • Loading branch information
jcfr authored Oct 15, 2018
2 parents 6dda0bf + cf6dd21 commit ead43bc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
53 changes: 50 additions & 3 deletions Libs/Widgets/ctkDoubleSpinBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ void ctkDoubleSpinBoxPrivate::setValue(double value, int dec)
if (this->SizeHintPolicy == ctkDoubleSpinBox::SizeHintByValue)
{
this->CachedSizeHint = QSize();
this->CachedMinimumSizeHint = QSize();
q->updateGeometry();
}
}
Expand Down Expand Up @@ -986,6 +987,7 @@ ::setSizeHintPolicy(ctkDoubleSpinBox::SizeHintPolicy newSizeHintPolicy)
}
d->SizeHintPolicy = newSizeHintPolicy;
d->CachedSizeHint = QSize();
d->CachedMinimumSizeHint = QSize();
this->updateGeometry();
}

Expand Down Expand Up @@ -1063,6 +1065,7 @@ QSize ctkDoubleSpinBox::sizeHint() const
QStyleOptionSpinBox opt;
d->SpinBox->initStyleOptionSpinBox(&opt);

#if QT_VERSION < QT_VERSION_CHECK(5,1,0)
QSize extraSize(35, 6);
opt.rect.setSize(newSizeHint + extraSize);
extraSize += newSizeHint - this->style()->subControlRect(
Expand All @@ -1074,6 +1077,7 @@ QSize ctkDoubleSpinBox::sizeHint() const
QStyle::CC_SpinBox, &opt,
QStyle::SC_SpinBoxEditField, this).size();
newSizeHint += extraSize;
#endif

opt.rect = this->rect();
d->CachedSizeHint = this->style()->sizeFromContents(
Expand All @@ -1085,9 +1089,52 @@ QSize ctkDoubleSpinBox::sizeHint() const
//----------------------------------------------------------------------------
QSize ctkDoubleSpinBox::minimumSizeHint() const
{
// For some reasons, Superclass::minimumSizeHint() returns the spinbox
// sizeHint()
return this->spinBox()->minimumSizeHint();
Q_D(const ctkDoubleSpinBox);
if (d->SizeHintPolicy == ctkDoubleSpinBox::SizeHintByMinMax)
{
// For some reasons, Superclass::minimumSizeHint() returns the spinbox
// sizeHint()
return this->spinBox()->minimumSizeHint();
}
if (!d->CachedMinimumSizeHint.isEmpty())
{
return d->CachedMinimumSizeHint;
}

QSize newSizeHint;
newSizeHint.setHeight(this->lineEdit()->minimumSizeHint().height());

QString extraString = " "; // give some room
QString s = this->text() + extraString;
s.truncate(18);
int extraWidth = 2; // cursor width

this->ensurePolished(); // ensure we are using the right font
const QFontMetrics fm(this->fontMetrics());
newSizeHint.setWidth(fm.width(s + extraString) + extraWidth);

QStyleOptionSpinBox opt;
d->SpinBox->initStyleOptionSpinBox(&opt);

#if QT_VERSION < QT_VERSION_CHECK(5,1,0)
QSize extraSize(35, 6);
opt.rect.setSize(newSizeHint + extraSize);
extraSize += newSizeHint - this->style()->subControlRect(
QStyle::CC_SpinBox, &opt,
QStyle::SC_SpinBoxEditField, this).size();
// Converging size hint...
opt.rect.setSize(newSizeHint + extraSize);
extraSize += newSizeHint - this->style()->subControlRect(
QStyle::CC_SpinBox, &opt,
QStyle::SC_SpinBoxEditField, this).size();
newSizeHint += extraSize;
#endif

opt.rect = this->rect();
d->CachedMinimumSizeHint = this->style()->sizeFromContents(
QStyle::CT_SpinBox, &opt, newSizeHint, this)
.expandedTo(QApplication::globalStrut());
return d->CachedMinimumSizeHint;
}

//-----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions Libs/Widgets/ctkDoubleSpinBox_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class ctkDoubleSpinBoxPrivate: public QObject
mutable QValidator::State CachedState;
mutable int CachedDecimals;
mutable QSize CachedSizeHint;
mutable QSize CachedMinimumSizeHint;
bool ForceInputValueUpdate;

QPointer<ctkValueProxy> Proxy;
Expand Down

0 comments on commit ead43bc

Please sign in to comment.