macOS: Use more of QStyleOption and less of QWidget

This is a step in the direction of reducing QWidget usage in the macOS style.

Change-Id: I247f39c2ba46c7fa5be51e01cc41fd8b8a93cea7
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Alberto Mardegan 2019-07-21 15:00:50 +02:00
parent 6d29264e3e
commit 57c2c37715

View File

@ -854,7 +854,8 @@ static inline int qt_mac_aqua_get_metric(QAquaMetric m)
return qt_mac_aqua_metrics[m]; return qt_mac_aqua_metrics[m];
} }
static QSize qt_aqua_get_known_size(QStyle::ContentsType ct, const QWidget *widg, QSize szHint, static QSize qt_aqua_get_known_size(QStyle::ContentsType ct, const QStyleOption *opt,
const QWidget *widg, QSize szHint,
QStyleHelper::WidgetSizePolicy sz) QStyleHelper::WidgetSizePolicy sz)
{ {
QSize ret(-1, -1); QSize ret(-1, -1);
@ -918,12 +919,9 @@ static QSize qt_aqua_get_known_size(QStyle::ContentsType ct, const QWidget *widg
switch (ct) { switch (ct) {
#if QT_CONFIG(pushbutton) #if QT_CONFIG(pushbutton)
case QStyle::CT_PushButton: { case QStyle::CT_PushButton: {
const QPushButton *psh = qobject_cast<const QPushButton *>(widg); const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt);
// If this comparison is false, then the widget was not a push button. if (btn) {
// This is bad and there's very little we can do since we were requested to find a QString buttonText = qt_mac_removeMnemonics(btn->text);
// sensible size for a widget that pretends to be a QPushButton but is not.
if(psh) {
QString buttonText = qt_mac_removeMnemonics(psh->text());
if (buttonText.contains(QLatin1Char('\n'))) if (buttonText.contains(QLatin1Char('\n')))
ret = QSize(-1, -1); ret = QSize(-1, -1);
else if (sz == QStyleHelper::SizeLarge) else if (sz == QStyleHelper::SizeLarge)
@ -933,11 +931,11 @@ static QSize qt_aqua_get_known_size(QStyle::ContentsType ct, const QWidget *widg
else if (sz == QStyleHelper::SizeMini) else if (sz == QStyleHelper::SizeMini)
ret = QSize(-1, qt_mac_aqua_get_metric(MiniPushButtonHeight)); ret = QSize(-1, qt_mac_aqua_get_metric(MiniPushButtonHeight));
if (!psh->icon().isNull()){ if (!btn->icon.isNull()){
// If the button got an icon, and the icon is larger than the // If the button got an icon, and the icon is larger than the
// button, we can't decide on a default size // button, we can't decide on a default size
ret.setWidth(-1); ret.setWidth(-1);
if (ret.height() < psh->iconSize().height()) if (ret.height() < btn->iconSize.height())
ret.setHeight(-1); ret.setHeight(-1);
} }
else if (buttonText == QLatin1String("OK") || buttonText == QLatin1String("Cancel")){ else if (buttonText == QLatin1String("OK") || buttonText == QLatin1String("Cancel")){
@ -951,17 +949,6 @@ static QSize qt_aqua_get_known_size(QStyle::ContentsType ct, const QWidget *widg
// or accept button (i.e., rightmost) and cancel button have the same width. // or accept button (i.e., rightmost) and cancel button have the same width.
ret.setWidth(69); ret.setWidth(69);
} }
} else {
// The only sensible thing to do is to return whatever the style suggests...
if (sz == QStyleHelper::SizeLarge)
ret = QSize(-1, qt_mac_aqua_get_metric(PushButtonHeight));
else if (sz == QStyleHelper::SizeSmall)
ret = QSize(-1, qt_mac_aqua_get_metric(SmallPushButtonHeight));
else if (sz == QStyleHelper::SizeMini)
ret = QSize(-1, qt_mac_aqua_get_metric(MiniPushButtonHeight));
else
// Since there's no default size we return the large size...
ret = QSize(-1, qt_mac_aqua_get_metric(PushButtonHeight));
} }
#endif #endif
#if 0 //Not sure we are applying the rules correctly for RadioButtons/CheckBoxes --Sam #if 0 //Not sure we are applying the rules correctly for RadioButtons/CheckBoxes --Sam
@ -1018,19 +1005,19 @@ static QSize qt_aqua_get_known_size(QStyle::ContentsType ct, const QWidget *widg
int width = 0, height = 0; int width = 0, height = 0;
if (szHint == QSize(-1, -1)) { //just 'guess'.. if (szHint == QSize(-1, -1)) { //just 'guess'..
#if QT_CONFIG(toolbutton) #if QT_CONFIG(toolbutton)
const QToolButton *bt = qobject_cast<const QToolButton *>(widg); const QStyleOptionToolButton *bt = qstyleoption_cast<const QStyleOptionToolButton *>(opt);
// If this conversion fails then the widget was not what it claimed to be. // If this conversion fails then the widget was not what it claimed to be.
if(bt) { if(bt) {
if (!bt->icon().isNull()) { if (!bt->icon.isNull()) {
QSize iconSize = bt->iconSize(); QSize iconSize = bt->iconSize;
QSize pmSize = bt->icon().actualSize(QSize(32, 32), QIcon::Normal); QSize pmSize = bt->icon.actualSize(QSize(32, 32), QIcon::Normal);
width = qMax(width, qMax(iconSize.width(), pmSize.width())); width = qMax(width, qMax(iconSize.width(), pmSize.width()));
height = qMax(height, qMax(iconSize.height(), pmSize.height())); height = qMax(height, qMax(iconSize.height(), pmSize.height()));
} }
if (!bt->text().isNull() && bt->toolButtonStyle() != Qt::ToolButtonIconOnly) { if (!bt->text.isNull() && bt->toolButtonStyle != Qt::ToolButtonIconOnly) {
int text_width = bt->fontMetrics().horizontalAdvance(bt->text()), int text_width = bt->fontMetrics.horizontalAdvance(bt->text),
text_height = bt->fontMetrics().height(); text_height = bt->fontMetrics.height();
if (bt->toolButtonStyle() == Qt::ToolButtonTextUnderIcon) { if (bt->toolButtonStyle == Qt::ToolButtonTextUnderIcon) {
width = qMax(width, text_width); width = qMax(width, text_width);
height += text_height; height += text_height;
} else { } else {
@ -1056,37 +1043,37 @@ static QSize qt_aqua_get_known_size(QStyle::ContentsType ct, const QWidget *widg
break; break;
case QStyle::CT_Slider: { case QStyle::CT_Slider: {
int w = -1; int w = -1;
const QSlider *sld = qobject_cast<const QSlider *>(widg); const QStyleOptionSlider *sld = qstyleoption_cast<const QStyleOptionSlider *>(opt);
// If this conversion fails then the widget was not what it claimed to be. // If this conversion fails then the widget was not what it claimed to be.
if(sld) { if(sld) {
if (sz == QStyleHelper::SizeLarge) { if (sz == QStyleHelper::SizeLarge) {
if (sld->orientation() == Qt::Horizontal) { if (sld->orientation == Qt::Horizontal) {
w = qt_mac_aqua_get_metric(HSliderHeight); w = qt_mac_aqua_get_metric(HSliderHeight);
if (sld->tickPosition() != QSlider::NoTicks) if (sld->tickPosition != QSlider::NoTicks)
w += qt_mac_aqua_get_metric(HSliderTickHeight); w += qt_mac_aqua_get_metric(HSliderTickHeight);
} else { } else {
w = qt_mac_aqua_get_metric(VSliderWidth); w = qt_mac_aqua_get_metric(VSliderWidth);
if (sld->tickPosition() != QSlider::NoTicks) if (sld->tickPosition != QSlider::NoTicks)
w += qt_mac_aqua_get_metric(VSliderTickWidth); w += qt_mac_aqua_get_metric(VSliderTickWidth);
} }
} else if (sz == QStyleHelper::SizeSmall) { } else if (sz == QStyleHelper::SizeSmall) {
if (sld->orientation() == Qt::Horizontal) { if (sld->orientation == Qt::Horizontal) {
w = qt_mac_aqua_get_metric(SmallHSliderHeight); w = qt_mac_aqua_get_metric(SmallHSliderHeight);
if (sld->tickPosition() != QSlider::NoTicks) if (sld->tickPosition != QSlider::NoTicks)
w += qt_mac_aqua_get_metric(SmallHSliderTickHeight); w += qt_mac_aqua_get_metric(SmallHSliderTickHeight);
} else { } else {
w = qt_mac_aqua_get_metric(SmallVSliderWidth); w = qt_mac_aqua_get_metric(SmallVSliderWidth);
if (sld->tickPosition() != QSlider::NoTicks) if (sld->tickPosition != QSlider::NoTicks)
w += qt_mac_aqua_get_metric(SmallVSliderTickWidth); w += qt_mac_aqua_get_metric(SmallVSliderTickWidth);
} }
} else if (sz == QStyleHelper::SizeMini) { } else if (sz == QStyleHelper::SizeMini) {
if (sld->orientation() == Qt::Horizontal) { if (sld->orientation == Qt::Horizontal) {
w = qt_mac_aqua_get_metric(MiniHSliderHeight); w = qt_mac_aqua_get_metric(MiniHSliderHeight);
if (sld->tickPosition() != QSlider::NoTicks) if (sld->tickPosition != QSlider::NoTicks)
w += qt_mac_aqua_get_metric(MiniHSliderTickHeight); w += qt_mac_aqua_get_metric(MiniHSliderTickHeight);
} else { } else {
w = qt_mac_aqua_get_metric(MiniVSliderWidth); w = qt_mac_aqua_get_metric(MiniVSliderWidth);
if (sld->tickPosition() != QSlider::NoTicks) if (sld->tickPosition != QSlider::NoTicks)
w += qt_mac_aqua_get_metric(MiniVSliderTickWidth); w += qt_mac_aqua_get_metric(MiniVSliderTickWidth);
} }
} }
@ -1098,7 +1085,7 @@ static QSize qt_aqua_get_known_size(QStyle::ContentsType ct, const QWidget *widg
w = qt_mac_aqua_get_metric(HSliderHeight); w = qt_mac_aqua_get_metric(HSliderHeight);
w += qt_mac_aqua_get_metric(HSliderTickHeight); w += qt_mac_aqua_get_metric(HSliderTickHeight);
} }
if (sld->orientation() == Qt::Horizontal) if (sld->orientation == Qt::Horizontal)
ret.setHeight(w); ret.setHeight(w);
else else
ret.setWidth(w); ret.setWidth(w);
@ -1496,9 +1483,9 @@ QStyleHelper::WidgetSizePolicy QMacStylePrivate::aquaSizeConstrain(const QStyleO
return QStyleHelper::SizeDefault; return QStyleHelper::SizeDefault;
} }
QSize large = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeLarge), QSize large = qt_aqua_get_known_size(ct, option, widg, szHint, QStyleHelper::SizeLarge),
small = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeSmall), small = qt_aqua_get_known_size(ct, option, widg, szHint, QStyleHelper::SizeSmall),
mini = qt_aqua_get_known_size(ct, widg, szHint, QStyleHelper::SizeMini); mini = qt_aqua_get_known_size(ct, option, widg, szHint, QStyleHelper::SizeMini);
bool guess_size = false; bool guess_size = false;
QStyleHelper::WidgetSizePolicy ret = QStyleHelper::SizeDefault; QStyleHelper::WidgetSizePolicy ret = QStyleHelper::SizeDefault;
QStyleHelper::WidgetSizePolicy wsp = QStyleHelper::widgetSizePolicy(widg); QStyleHelper::WidgetSizePolicy wsp = QStyleHelper::widgetSizePolicy(widg);
@ -2489,7 +2476,7 @@ int QMacStyle::pixelMetric(PixelMetric metric, const QStyleOption *opt, const QW
aSize = QStyleHelper::SizeSmall; aSize = QStyleHelper::SizeSmall;
else else
aSize = QStyleHelper::SizeLarge; aSize = QStyleHelper::SizeLarge;
const QSize size = qt_aqua_get_known_size(CT_SizeGrip, widget, QSize(), aSize); const QSize size = qt_aqua_get_known_size(CT_SizeGrip, opt, widget, QSize(), aSize);
ret = size.width(); ret = size.width();
break; } break; }
case PM_MdiSubWindowFrameWidth: case PM_MdiSubWindowFrameWidth: