From 80515c811a4c455d7f2fb9ded751b2406b08d045 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Wed, 24 Jan 2018 17:13:24 -0800 Subject: [PATCH] Introducing SH_SpinBox_ButtonsInsideFrame This is currently only used in QMacStyle since, on macOS, the spin-box buttons are always beside the line-edit. Hence, there is no need to tamper with the line-edit frame. Change-Id: I8ccfbab547fccae89a8ddeaeb7005cb17d5951e6 Reviewed-by: Jake Petroules --- src/plugins/styles/mac/qmacstyle_mac.mm | 47 ++++++------------------ src/widgets/styles/qcommonstyle.cpp | 3 ++ src/widgets/styles/qstyle.cpp | 4 ++ src/widgets/styles/qstyle.h | 1 + src/widgets/widgets/qabstractspinbox.cpp | 8 +++- 5 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm index 8e00a8c85b..f4095cdf06 100644 --- a/src/plugins/styles/mac/qmacstyle_mac.mm +++ b/src/plugins/styles/mac/qmacstyle_mac.mm @@ -2387,14 +2387,6 @@ int QMacStyle::pixelMetric(PixelMetric metric, const QStyleOption *opt, const QW break; case PM_SpinBoxFrameWidth: ret = qt_mac_aqua_get_metric(EditTextFrameOutset); - switch (d->aquaSizeConstrain(opt, widget)) { - case QStyleHelper::SizeMini: - ret += 1; - break; - default: - ret += 2; - break; - } break; case PM_ButtonShiftHorizontal: case PM_ButtonShiftVertical: @@ -3001,6 +2993,9 @@ int QMacStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget *w case SH_ComboBox_AllowWheelScrolling: ret = false; break; + case SH_SpinBox_ButtonsInsideFrame: + ret = false; + break; default: ret = QCommonStyle::styleHint(sh, opt, w, hret); break; @@ -4157,16 +4152,7 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter const int hMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameHMargin, opt, w); const int vMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameVMargin, opt, w); if (ct == QMacStylePrivate::Box) { - auto frameRect = opt->rect; - if (ffw && ffw->inherits("QLineEdit") - && ffw->parentWidget() - && ffw->parentWidget()->inherits("QAbstractSpinBox")) { - // See CC_SpinBox case for drawComplexControl - const int frame_size = qt_mac_aqua_get_metric(EditTextFrameOutset); - frameRect = frameRect.adjusted(-frame_size, -frame_size, +frame_size, +frame_size); - } - - d->drawFocusRing(p, frameRect, hMargin, vMargin); + d->drawFocusRing(p, opt->rect, hMargin, vMargin); } else if (ffw) { const auto cs = ffw->testAttribute(Qt::WA_MacMiniSize) ? QStyleHelper::SizeMini : ffw->testAttribute(Qt::WA_MacSmallSize) ? QStyleHelper::SizeSmall : @@ -5444,9 +5430,7 @@ void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex case CC_SpinBox: if (const QStyleOptionSpinBox *sb = qstyleoption_cast(opt)) { if (sb->frame && (sb->subControls & SC_SpinBoxFrame)) { - const int frame_size = qt_mac_aqua_get_metric(EditTextFrameOutset); - const auto lineEditRect = proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxEditField, widget) - .adjusted(-frame_size, -frame_size, +frame_size, +frame_size); + const auto lineEditRect = proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxEditField, widget); QStyleOptionFrame frame; static_cast(frame) = *opt; frame.rect = lineEditRect; @@ -6175,9 +6159,9 @@ QRect QMacStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *op case CC_SpinBox: if (const QStyleOptionSpinBox *spin = qstyleoption_cast(opt)) { QStyleHelper::WidgetSizePolicy aquaSize = d->effectiveAquaSizeConstrain(spin, widget); + const auto fw = proxy()->pixelMetric(PM_SpinBoxFrameWidth, spin, widget); int spinner_w; int spinBoxSep; - int fw = proxy()->pixelMetric(PM_SpinBoxFrameWidth, spin, widget); switch (aquaSize) { case QStyleHelper::SizeLarge: spinner_w = 14; @@ -6240,16 +6224,11 @@ QRect QMacStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *op break; } case SC_SpinBoxEditField: - if (spin->buttonSymbols == QAbstractSpinBox::NoButtons) { - ret.setRect(fw, fw, - spin->rect.width() - fw * 2, - spin->rect.height() - fw * 2); - } else { - ret.setRect(fw, fw, - spin->rect.width() - fw * 2 - spinBoxSep - spinner_w, - spin->rect.height() - fw * 2); + ret = spin->rect.adjusted(fw, fw, -fw, -fw); + if (spin->buttonSymbols != QAbstractSpinBox::NoButtons) { + ret.setWidth(spin->rect.width() - spinBoxSep - spinner_w); + ret = visualRect(spin->direction, spin->rect, ret); } - ret = visualRect(spin->direction, spin->rect, ret); break; default: ret = QCommonStyle::subControlRect(cc, spin, sc, widget); @@ -6286,10 +6265,8 @@ QSize QMacStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt, #if QT_CONFIG(spinbox) case CT_SpinBox: if (const QStyleOptionSpinBox *vopt = qstyleoption_cast(opt)) { - // Add button + frame widths - int buttonWidth = 20; - int fw = proxy()->pixelMetric(PM_SpinBoxFrameWidth, vopt, widget); - sz += QSize(buttonWidth + 2*fw, 2*fw - 3); + const int buttonWidth = 20; // FIXME Use subControlRect() + sz += QSize(buttonWidth, -3); } break; #endif diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp index 1190099c16..12b60c634c 100644 --- a/src/widgets/styles/qcommonstyle.cpp +++ b/src/widgets/styles/qcommonstyle.cpp @@ -5303,6 +5303,9 @@ int QCommonStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget case SH_ComboBox_AllowWheelScrolling: ret = true; break; + case SH_SpinBox_ButtonsInsideFrame: + ret = true; + break; default: ret = 0; break; diff --git a/src/widgets/styles/qstyle.cpp b/src/widgets/styles/qstyle.cpp index c22f45d54e..73a6554f1a 100644 --- a/src/widgets/styles/qstyle.cpp +++ b/src/widgets/styles/qstyle.cpp @@ -1997,6 +1997,10 @@ void QStyle::drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, This is on by default in all styles except the Mac style. This enum value has been introduced in Qt 5.10. + \value SH_SpinBox_ButtonsInsideFrame + Determnines if the spin box buttons are inside the line edit frame. + This enum value has been introduced in Qt 5.11. + \sa styleHint() */ diff --git a/src/widgets/styles/qstyle.h b/src/widgets/styles/qstyle.h index ab61534a96..cef569d514 100644 --- a/src/widgets/styles/qstyle.h +++ b/src/widgets/styles/qstyle.h @@ -740,6 +740,7 @@ public: SH_TitleBar_ShowToolTipsOnButtons, SH_Widget_Animation_Duration, SH_ComboBox_AllowWheelScrolling, + SH_SpinBox_ButtonsInsideFrame, // Add new style hint values here SH_CustomBase = 0xf0000000 diff --git a/src/widgets/widgets/qabstractspinbox.cpp b/src/widgets/widgets/qabstractspinbox.cpp index fe2801fcbf..7ca47e9f0f 100644 --- a/src/widgets/widgets/qabstractspinbox.cpp +++ b/src/widgets/widgets/qabstractspinbox.cpp @@ -691,7 +691,7 @@ void QAbstractSpinBox::setLineEdit(QLineEdit *lineEdit) if (d->edit->parent() != this) d->edit->setParent(this); - d->edit->setFrame(false); + d->edit->setFrame(!style()->styleHint(QStyle::SH_SpinBox_ButtonsInsideFrame, nullptr, this)); d->edit->setFocusProxy(this); d->edit->setAcceptDrops(false); @@ -823,6 +823,8 @@ void QAbstractSpinBox::changeEvent(QEvent *event) d->spinClickTimerInterval = style()->styleHint(QStyle::SH_SpinBox_ClickAutoRepeatRate, 0, this); d->spinClickThresholdTimerInterval = style()->styleHint(QStyle::SH_SpinBox_ClickAutoRepeatThreshold, 0, this); + if (d->edit) + d->edit->setFrame(!style()->styleHint(QStyle::SH_SpinBox_ButtonsInsideFrame, nullptr, this)); d->reset(); d->updateEditFieldGeometry(); break; @@ -1649,7 +1651,9 @@ void QAbstractSpinBox::initStyleOption(QStyleOptionSpinBox *option) const option->initFrom(this); option->activeSubControls = QStyle::SC_None; option->buttonSymbols = d->buttonSymbols; - option->subControls = QStyle::SC_SpinBoxFrame | QStyle::SC_SpinBoxEditField; + option->subControls = QStyle::SC_SpinBoxEditField; + if (!style()->styleHint(QStyle::SH_SpinBox_ButtonsInsideFrame, nullptr, this)) + option->subControls |= QStyle::SC_SpinBoxFrame; if (d->buttonSymbols != QAbstractSpinBox::NoButtons) { option->subControls |= QStyle::SC_SpinBoxUp | QStyle::SC_SpinBoxDown; if (d->buttonState & Up) {