diff options
Diffstat (limited to 'src/plugins/styles/modernwindows/qwindows11style.cpp')
| -rw-r--r-- | src/plugins/styles/modernwindows/qwindows11style.cpp | 137 |
1 files changed, 68 insertions, 69 deletions
diff --git a/src/plugins/styles/modernwindows/qwindows11style.cpp b/src/plugins/styles/modernwindows/qwindows11style.cpp index 25142612c4f..a5c6b300253 100644 --- a/src/plugins/styles/modernwindows/qwindows11style.cpp +++ b/src/plugins/styles/modernwindows/qwindows11style.cpp @@ -44,7 +44,8 @@ using namespace Qt::StringLiterals; static constexpr int topLevelRoundingRadius = 8; //Radius for toplevel items like popups for round corners static constexpr int secondLevelRoundingRadius = 4; //Radius for second level items like hovered menu item round corners - +static constexpr int contentItemHMargin = 4; // margin between content items (e.g. text and icon) +static constexpr int contentHMargin = 2 * 3; // margin between rounded border and content (= rounded border margin * 3) namespace StyleOptionHelper { inline bool isChecked(const QStyleOption *option) @@ -923,15 +924,13 @@ void QWindows11Style::drawPrimitive(PrimitiveElement element, const QStyleOption case PE_IndicatorRadioButton: { const bool isRtl = option->direction == Qt::RightToLeft; const bool isOn = option->state & State_On; - qreal innerRadius = 7.0f; + qreal innerRadius = radioButtonInnerRadius(state); if (d->transitionsEnabled() && option->styleObject) { if (option->styleObject->property("_q_end_radius").isNull()) option->styleObject->setProperty("_q_end_radius", innerRadius); QNumberStyleAnimation *animation = qobject_cast<QNumberStyleAnimation *>(d->animation(option->styleObject)); innerRadius = animation ? animation->currentValue() : option->styleObject->property("_q_end_radius").toFloat(); option->styleObject->setProperty("_q_inner_radius", innerRadius); - } else { - innerRadius = radioButtonInnerRadius(state); } QRectF rect = isRtl ? option->rect.adjusted(0, 0, -2, 0) : option->rect.adjusted(2, 0, 0, 0); @@ -1423,80 +1422,57 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op break; #endif // QT_CONFIG(progressbar) case CE_PushButtonLabel: - if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(option)) { - QRect textRect = btn->rect; + if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(option)) { + using namespace StyleOptionHelper; + const bool isEnabled = !isDisabled(option); - int tf = Qt::AlignVCenter|Qt::TextShowMnemonic; + QRect textRect = btn->rect.marginsRemoved(QMargins(contentHMargin, 0, contentHMargin, 0)); + int tf = Qt::AlignCenter | Qt::TextShowMnemonic; if (!proxy()->styleHint(SH_UnderlineShortcut, btn, widget)) tf |= Qt::TextHideMnemonic; if (btn->features & QStyleOptionButton::HasMenu) { - int indicatorSize = proxy()->pixelMetric(PM_MenuButtonIndicator, btn, widget); - QLineF menuSplitter; - QRectF indicatorRect; - painter->save(); - painter->setFont(d->assetFont); + QPainterStateGuard psg(painter); - if (btn->direction == Qt::LeftToRight) { - indicatorRect = QRect(textRect.x() + textRect.width() - indicatorSize - 4, textRect.y(),2 * 4 + indicatorSize, textRect.height()); - indicatorRect.adjust(0.5,-0.5,0.5,0.5); - menuSplitter = QLineF(indicatorRect.topLeft(),indicatorRect.bottomLeft()); - textRect = textRect.adjusted(0, 0, -indicatorSize, 0); - } else { - indicatorRect = QRect(textRect.x(), textRect.y(), textRect.x() + indicatorSize + 4, textRect.height()); - indicatorRect.adjust(-0.5,-0.5,-0.5,0.5); - menuSplitter = QLineF(indicatorRect.topRight(),indicatorRect.bottomRight()); - textRect = textRect.adjusted(indicatorSize, 0, 0, 0); - } - painter->drawText(indicatorRect, Qt::AlignCenter, ChevronDownMed); - painter->setPen(WINUI3Colors[colorSchemeIndex][controlStrokePrimary]); - painter->drawLine(menuSplitter); - painter->restore(); + const auto indSize = proxy()->pixelMetric(PM_MenuButtonIndicator, btn, widget); + const auto indRect = QRect(btn->rect.right() - indSize - contentItemHMargin, textRect.top(), + indSize + contentItemHMargin, btn->rect.height()); + const auto vindRect = visualRect(btn->direction, btn->rect, indRect); + textRect.setWidth(textRect.width() - indSize); + + int fontSize = painter->font().pointSize(); + QFont f(d->assetFont); + f.setPointSize(qRound(fontSize * 0.9f)); // a little bit smaller + painter->setFont(f); + QColor penColor = option->palette.color(isEnabled ? QPalette::Active : QPalette::Disabled, + QPalette::Text); + if (isEnabled) + penColor.setAlpha(percentToAlpha(60.63)); // fillColorTextSecondary + painter->setPen(penColor); + painter->drawText(vindRect, Qt::AlignCenter, ChevronDownMed); } if (!btn->icon.isNull()) { //Center both icon and text - QIcon::Mode mode = btn->state & State_Enabled ? QIcon::Normal : QIcon::Disabled; + QIcon::Mode mode = isEnabled ? QIcon::Normal : QIcon::Disabled; if (mode == QIcon::Normal && btn->state & State_HasFocus) mode = QIcon::Active; - QIcon::State state = QIcon::Off; - if (btn->state & State_On) - state = QIcon::On; - - QPixmap pixmap = btn->icon.pixmap(btn->iconSize, painter->device()->devicePixelRatio(), mode, state); - int pixmapWidth = pixmap.width() / pixmap.devicePixelRatio(); - int pixmapHeight = pixmap.height() / pixmap.devicePixelRatio(); - int labelWidth = pixmapWidth; - int labelHeight = pixmapHeight; - int iconSpacing = 4;//### 4 is currently hardcoded in QPushButton::sizeHint() - if (!btn->text.isEmpty()) { - int textWidth = btn->fontMetrics.boundingRect(option->rect, tf, btn->text).width(); - labelWidth += (textWidth + iconSpacing); - } + QIcon::State state = isChecked(btn) ? QIcon::On : QIcon::Off; - QRect iconRect = QRect(textRect.x() + (textRect.width() - labelWidth) / 2, - textRect.y() + (textRect.height() - labelHeight) / 2, - pixmapWidth, pixmapHeight); + int iconSpacing = 4;//### 4 is currently hardcoded in QPushButton::sizeHint() - iconRect = visualRect(btn->direction, textRect, iconRect); + QRect iconRect = QRect(textRect.x(), textRect.y(), btn->iconSize.width(), textRect.height()); + QRect vIconRect = visualRect(btn->direction, btn->rect, iconRect); + textRect.setLeft(textRect.left() + iconRect.width() + iconSpacing); - if (btn->direction == Qt::RightToLeft) { - tf |= Qt::AlignRight; - textRect.setRight(iconRect.left() - iconSpacing / 2); - } else { - tf |= Qt::AlignLeft; //left align, we adjust the text-rect instead - textRect.setLeft(iconRect.left() + iconRect.width() + iconSpacing / 2); - } - - if (btn->state & (State_On | State_Sunken)) - iconRect.translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, option, widget), - proxy()->pixelMetric(PM_ButtonShiftVertical, option, widget)); - painter->drawPixmap(iconRect, pixmap); - } else { - tf |= Qt::AlignHCenter; + if (isChecked(btn) || isPressed(btn)) + vIconRect.translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, option, widget), + proxy()->pixelMetric(PM_ButtonShiftVertical, option, widget)); + btn->icon.paint(painter, vIconRect, Qt::AlignCenter, mode, state); } + auto vTextRect = visualRect(btn->direction, btn->rect, textRect); painter->setPen(controlTextColor(option)); - proxy()->drawItemText(painter, textRect, tf, option->palette,btn->state & State_Enabled, btn->text); + proxy()->drawItemText(painter, vTextRect, tf, option->palette, isEnabled, btn->text); } break; case CE_PushButtonBevel: @@ -1526,8 +1502,6 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op painter->setPen(defaultButton ? WINUI3Colors[colorSchemeIndex][controlStrokeOnAccentSecondary] : WINUI3Colors[colorSchemeIndex][controlStrokeSecondary]); - if (flags & State_Raised) - painter->drawLine(rect.bottomLeft() + QPointF(4.0,0.0), rect.bottomRight() + QPointF(-4,0.0)); } } break; @@ -1931,6 +1905,11 @@ QRect QWindows11Style::subElementRect(QStyle::SubElement element, const QStyleOp case QStyle::SE_HeaderArrow: ret = QCommonStyle::subElementRect(element, option, widget); break; + case SE_PushButtonContents: { + int border = proxy()->pixelMetric(PM_DefaultFrameWidth, option, widget); + ret = option->rect.marginsRemoved(QMargins(border, border, border, border)); + break; + } default: ret = QWindowsVistaStyle::subElementRect(element, option, widget); } @@ -2193,11 +2172,13 @@ QSize QWindows11Style::sizeFromContents(ContentsType type, const QStyleOption *o if (size.width() == 0) contentSize.rwidth() += 2; break; - case CT_PushButton: + case CT_PushButton: { contentSize = QWindowsVistaStyle::sizeFromContents(type, option, size, widget); - contentSize.rwidth() += 2 * 2; // the CE_PushButtonBevel draws a rounded rect with - // QMargins(2, 2, 2, 2) removed + // we want our own horizontal spacing + const int oldMargin = proxy()->pixelMetric(PM_ButtonMargin, option, widget); + contentSize.rwidth() += 2 * contentHMargin - oldMargin; break; + } default: contentSize = QWindowsVistaStyle::sizeFromContents(type, option, size, widget); break; @@ -2212,6 +2193,7 @@ QSize QWindows11Style::sizeFromContents(ContentsType type, const QStyleOption *o */ int QWindows11Style::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const { + Q_D(const QWindows11Style); int res = 0; switch (metric) { @@ -2247,6 +2229,26 @@ int QWindows11Style::pixelMetric(PixelMetric metric, const QStyleOption *option, case QStyle::PM_SubMenuOverlap: res = -1; break; + case PM_MenuButtonIndicator: { + res = contentItemHMargin; + if (widget) { + const int fontSize = widget->font().pointSize(); + QFont f(d->assetFont); + f.setPointSize(qRound(fontSize * 0.9f)); // a little bit smaller + QFontMetrics fm(f); + res += fm.horizontalAdvance(ChevronDownMed); + } else { + res += 12; + } + break; + } + case PM_DefaultFrameWidth: + res = 2; + break; + case PM_ButtonShiftHorizontal: + case PM_ButtonShiftVertical: + res = 0; + break; default: res = QWindowsVistaStyle::pixelMetric(metric, option, widget); } @@ -2452,9 +2454,6 @@ void QWindows11Style::polish(QPalette& result) SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Text, result.text().color()); SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::WindowText, result.windowText().color()); - if (highContrastTheme) - result.setColor(QPalette::Active, QPalette::HighlightedText, result.windowText().color()); - auto *d = const_cast<QWindows11StylePrivate *>(d_func()); d->m_titleBarMinIcon = QIcon(); d->m_titleBarMaxIcon = QIcon(); |
