Move widget size policy methods to QStyleHelper

While they're relatively Mac-specific, conceivably multiple style
implementations could use them, and it breaks a tight coupling between
QStyleOption and QMacStyle.

Change-Id: I5915c519129538a9ac39a34d5cfc3aeb838145d6
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@qt.io>
This commit is contained in:
Jake Petroules 2017-02-27 20:08:29 -08:00
parent f892318898
commit 7b796116ff
5 changed files with 52 additions and 58 deletions

View File

@ -439,10 +439,10 @@ void drawTabBase(QPainter *p, const QStyleOptionTabBarBase *tbb, const QWidget *
static int getControlSize(const QStyleOption *option, const QWidget *widget)
{
switch (QMacStyle::widgetSizePolicy(widget, option)) {
case QMacStyle::SizeSmall:
switch (QStyleHelper::widgetSizePolicy(widget, option)) {
case QStyleHelper::SizeSmall:
return QAquaSizeSmall;
case QMacStyle::SizeMini:
case QStyleHelper::SizeMini:
return QAquaSizeMini;
default:
break;
@ -1128,20 +1128,19 @@ QAquaWidgetSize QMacStylePrivate::aquaSizeConstrain(const QStyleOption *option,
return QAquaSizeUnknown;
}
Q_Q(const QMacStyle);
QSize large = qt_aqua_get_known_size(ct, widg, szHint, QAquaSizeLarge),
small = qt_aqua_get_known_size(ct, widg, szHint, QAquaSizeSmall),
mini = qt_aqua_get_known_size(ct, widg, szHint, QAquaSizeMini);
bool guess_size = false;
QAquaWidgetSize ret = QAquaSizeUnknown;
QMacStyle::WidgetSizePolicy wsp = q->widgetSizePolicy(widg);
if (wsp == QMacStyle::SizeDefault)
QStyleHelper::WidgetSizePolicy wsp = QStyleHelper::widgetSizePolicy(widg);
if (wsp == QStyleHelper::SizeDefault)
guess_size = true;
else if (wsp == QMacStyle::SizeMini)
else if (wsp == QStyleHelper::SizeMini)
ret = QAquaSizeMini;
else if (wsp == QMacStyle::SizeSmall)
else if (wsp == QStyleHelper::SizeSmall)
ret = QAquaSizeSmall;
else if (wsp == QMacStyle::SizeLarge)
else if (wsp == QStyleHelper::SizeLarge)
ret = QAquaSizeLarge;
if (guess_size)
ret = qt_aqua_guess_size(widg, large, small, mini);
@ -1606,13 +1605,13 @@ void QMacStylePrivate::drawTableHeader(const HIRect &outerBounds,
scrollButtonsCutoff is the smallest size where the up/down buttons is drawn.
*/
enum ScrollBarCutoffType { thumbIndicatorCutoff = 0, scrollButtonsCutoff = 1 };
static int scrollButtonsCutoffSize(ScrollBarCutoffType cutoffType, QMacStyle::WidgetSizePolicy widgetSize)
static int scrollButtonsCutoffSize(ScrollBarCutoffType cutoffType, QStyleHelper::WidgetSizePolicy widgetSize)
{
// Mini scroll bars do not exist as of version 10.4.
if (widgetSize == QMacStyle::SizeMini)
if (widgetSize == QStyleHelper::SizeMini)
return 0;
const int sizeIndex = (widgetSize == QMacStyle::SizeSmall) ? 1 : 0;
const int sizeIndex = (widgetSize == QStyleHelper::SizeSmall) ? 1 : 0;
static const int sizeTable[2][2] = { { 61, 56 }, { 49, 44 } };
return sizeTable[sizeIndex][cutoffType];
}
@ -3089,35 +3088,6 @@ QPixmap QMacStyle::standardPixmap(StandardPixmap standardPixmap, const QStyleOpt
return icon.pixmap(qt_getWindow(widget), QSize(size, size));
}
void QMacStyle::setWidgetSizePolicy(const QWidget *widget, WidgetSizePolicy policy)
{
QWidget *wadget = const_cast<QWidget *>(widget);
wadget->setAttribute(Qt::WA_MacNormalSize, policy == SizeLarge);
wadget->setAttribute(Qt::WA_MacSmallSize, policy == SizeSmall);
wadget->setAttribute(Qt::WA_MacMiniSize, policy == SizeMini);
}
QMacStyle::WidgetSizePolicy QMacStyle::widgetSizePolicy(const QWidget *widget, const QStyleOption *opt)
{
while (widget) {
if (widget->testAttribute(Qt::WA_MacMiniSize)) {
return SizeMini;
} else if (widget->testAttribute(Qt::WA_MacSmallSize)) {
return SizeSmall;
} else if (widget->testAttribute(Qt::WA_MacNormalSize)) {
return SizeLarge;
}
widget = widget->parentWidget();
}
if (opt && opt->state & State_Mini)
return SizeMini;
else if (opt && opt->state & State_Small)
return SizeSmall;
return SizeDefault;
}
void QMacStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p,
const QWidget *w) const
{
@ -5275,7 +5245,7 @@ void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex
QMacStylePrivate::scrollBars.append(QPointer<QObject>(opt->styleObject));
const int scrollBarLength = (slider->orientation == Qt::Horizontal)
? slider->rect.width() : slider->rect.height();
const QMacStyle::WidgetSizePolicy sizePolicy = widgetSizePolicy(widget, opt);
const QStyleHelper::WidgetSizePolicy sizePolicy = QStyleHelper::widgetSizePolicy(widget, opt);
if (scrollBarLength < scrollButtonsCutoffSize(thumbIndicatorCutoff, sizePolicy))
tdi.attributes &= ~kThemeTrackShowThumb;
if (scrollBarLength < scrollButtonsCutoffSize(scrollButtonsCutoff, sizePolicy))
@ -6010,7 +5980,7 @@ QStyle::SubControl QMacStyle::hitTestComplexControl(ComplexControl cc,
// exclude them from the hit test.
const int scrollBarLength = (sb->orientation == Qt::Horizontal)
? sb->rect.width() : sb->rect.height();
if (scrollBarLength < scrollButtonsCutoffSize(scrollButtonsCutoff, widgetSizePolicy(widget, opt)))
if (scrollBarLength < scrollButtonsCutoffSize(scrollButtonsCutoff, QStyleHelper::widgetSizePolicy(widget, opt)))
sbi.enableState = kThemeTrackNothingToScroll;
sbi.viewsize = sb->pageStep;
@ -6680,7 +6650,7 @@ QSize QMacStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt,
case CT_ScrollBar :
// Make sure that the scroll bar is large enough to display the thumb indicator.
if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
const int minimumSize = scrollButtonsCutoffSize(thumbIndicatorCutoff, widgetSizePolicy(widget, opt));
const int minimumSize = scrollButtonsCutoffSize(thumbIndicatorCutoff, QStyleHelper::widgetSizePolicy(widget, opt));
if (slider->orientation == Qt::Horizontal)
sz = sz.expandedTo(QSize(minimumSize, sz.height()));
else

View File

@ -100,12 +100,6 @@ public:
virtual int styleHint(StyleHint sh, const QStyleOption *opt = 0, const QWidget *w = 0,
QStyleHintReturn *shret = 0) const;
enum WidgetSizePolicy { SizeSmall, SizeLarge, SizeMini, SizeDefault
};
static void setWidgetSizePolicy(const QWidget *w, WidgetSizePolicy policy);
static WidgetSizePolicy widgetSizePolicy(const QWidget *w, const QStyleOption *opt = 0);
QPixmap standardPixmap(StandardPixmap sp, const QStyleOption *opt,
const QWidget *widget = 0) const;

View File

@ -427,5 +427,34 @@ QWindow *styleObjectWindow(QObject *so)
return 0;
}
void setWidgetSizePolicy(const QWidget *widget, WidgetSizePolicy policy)
{
QWidget *wadget = const_cast<QWidget *>(widget);
wadget->setAttribute(Qt::WA_MacNormalSize, policy == SizeLarge);
wadget->setAttribute(Qt::WA_MacSmallSize, policy == SizeSmall);
wadget->setAttribute(Qt::WA_MacMiniSize, policy == SizeMini);
}
WidgetSizePolicy widgetSizePolicy(const QWidget *widget, const QStyleOption *opt)
{
while (widget) {
if (widget->testAttribute(Qt::WA_MacMiniSize)) {
return SizeMini;
} else if (widget->testAttribute(Qt::WA_MacSmallSize)) {
return SizeSmall;
} else if (widget->testAttribute(Qt::WA_MacNormalSize)) {
return SizeLarge;
}
widget = widget->parentWidget();
}
if (opt && opt->state & QStyle::State_Mini)
return SizeMini;
else if (opt && opt->state & QStyle::State_Small)
return SizeSmall;
return SizeDefault;
}
}
QT_END_NAMESPACE

View File

@ -87,6 +87,11 @@ namespace QStyleHelper
#endif
QColor backgroundColor(const QPalette &pal, const QWidget* widget = 0);
QWindow *styleObjectWindow(QObject *so);
enum WidgetSizePolicy { SizeSmall, SizeLarge, SizeMini, SizeDefault };
void setWidgetSizePolicy(const QWidget *w, WidgetSizePolicy policy);
WidgetSizePolicy widgetSizePolicy(const QWidget *w, const QStyleOption *opt = 0);
}

View File

@ -38,11 +38,9 @@
****************************************************************************/
#include <QtWidgets/private/qtwidgetsglobal_p.h>
#include "private/qstylehelper_p.h"
#include "qstyleoption.h"
#include "qapplication.h"
#if QT_CONFIG(style_mac)
# include "qmacstyle_mac_p.h"
#endif
#include <qdebug.h>
#include <QtCore/qmath.h>
@ -205,18 +203,16 @@ void QStyleOption::init(const QWidget *widget)
if (!(state & QStyle::State_Active) && !qt_mac_can_clickThrough(widget))
state &= ~QStyle::State_Enabled;
#endif
#if QT_CONFIG(style_mac)
switch (QMacStyle::widgetSizePolicy(widget)) {
case QMacStyle::SizeSmall:
switch (QStyleHelper::widgetSizePolicy(widget)) {
case QStyleHelper::SizeSmall:
state |= QStyle::State_Small;
break;
case QMacStyle::SizeMini:
case QStyleHelper::SizeMini:
state |= QStyle::State_Mini;
break;
default:
;
}
#endif
#ifdef QT_KEYPAD_NAVIGATION
if (widget->hasEditFocus())
state |= QStyle::State_HasEditFocus;