diff options
author | Alessandro Portale <alessandro.portale@qt.io> | 2025-04-15 17:47:26 +0200 |
---|---|---|
committer | Alessandro Portale <alessandro.portale@qt.io> | 2025-04-23 10:03:07 +0000 |
commit | d9d27c2ba86b39e0a4f0d7cc2e2ff85fd50c88a1 (patch) | |
tree | 494c228d90aa7948fcfac970060defd8cda2ee2f | |
parent | 29f4743dd5ced5646271e524ec13aa1b47f18402 (diff) |
Utils: Move drawCardBg from WelcomePageHelpers to StyleHelper
Conveniently draw smooth rounded rects from anywhere.
Change-Id: Ifb2710c23b88eb21a0bd669882e0ca65dd85114e
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
-rw-r--r-- | src/libs/utils/stylehelper.cpp | 19 | ||||
-rw-r--r-- | src/libs/utils/stylehelper.h | 5 | ||||
-rw-r--r-- | src/plugins/coreplugin/welcomepagehelper.cpp | 43 | ||||
-rw-r--r-- | src/plugins/coreplugin/welcomepagehelper.h | 4 | ||||
-rw-r--r-- | src/plugins/extensionmanager/extensionsbrowser.cpp | 12 | ||||
-rw-r--r-- | src/plugins/projectexplorer/projectwelcomepage.cpp | 6 |
6 files changed, 45 insertions, 44 deletions
diff --git a/src/libs/utils/stylehelper.cpp b/src/libs/utils/stylehelper.cpp index a63b7105759..acbec0c71d6 100644 --- a/src/libs/utils/stylehelper.cpp +++ b/src/libs/utils/stylehelper.cpp @@ -498,6 +498,25 @@ void StyleHelper::drawPanelBgRect(QPainter *painter, const QRectF &rect, const Q } } +void StyleHelper::drawCardBg(QPainter *painter, const QRectF &rect, + const QBrush &fill, const QPen &pen, qreal rounding) +{ + const qreal strokeWidth = pen.style() == Qt::NoPen ? 0 : pen.widthF(); + const qreal strokeShrink = strokeWidth / 2; + const QRectF itemRectAdjusted = rect.adjusted(strokeShrink, strokeShrink, + -strokeShrink, -strokeShrink); + const qreal roundingAdjusted = rounding - strokeShrink; + QPainterPath itemOutlinePath; + itemOutlinePath.addRoundedRect(itemRectAdjusted, roundingAdjusted, roundingAdjusted); + + painter->save(); + painter->setRenderHint(QPainter::Antialiasing); + painter->setBrush(fill); + painter->setPen(pen); + painter->drawPath(itemOutlinePath); + painter->restore(); +} + void StyleHelper::menuGradient(QPainter *painter, const QRect &spanRect, const QRect &clipRect) { if (StyleHelper::usePixmapCache()) { diff --git a/src/libs/utils/stylehelper.h b/src/libs/utils/stylehelper.h index 1e3d3636eb5..cb1ca4b1418 100644 --- a/src/libs/utils/stylehelper.h +++ b/src/libs/utils/stylehelper.h @@ -5,6 +5,7 @@ #include "utils_global.h" +#include <QPen> #include <QStyle> QT_BEGIN_NAMESPACE @@ -22,6 +23,7 @@ namespace Utils::StyleHelper { const unsigned int DEFAULT_BASE_COLOR = 0x666666; const int progressFadeAnimationDuration = 600; +constexpr qreal defaultCardBgRounding = 3.75; constexpr char C_ALIGN_ARROW[] = "alignarrow"; constexpr char C_DRAW_LEFT_BORDER[] = "drawleftborder"; @@ -140,6 +142,9 @@ QTCREATOR_UTILS_EXPORT void drawMinimalArrow(QStyle::PrimitiveElement element, Q QTCREATOR_UTILS_EXPORT void drawPanelBgRect(QPainter *painter, const QRectF &rect, const QBrush &brush); +QTCREATOR_UTILS_EXPORT void drawCardBg(QPainter *painter, const QRectF &rect, const QBrush &fill, + const QPen &pen = QPen(Qt::NoPen), + qreal rounding = defaultCardBgRounding); // Gradients used for panels QTCREATOR_UTILS_EXPORT void horizontalGradient(QPainter *painter, const QRect &spanRect, diff --git a/src/plugins/coreplugin/welcomepagehelper.cpp b/src/plugins/coreplugin/welcomepagehelper.cpp index a93ec63919e..29cc054fe2d 100644 --- a/src/plugins/coreplugin/welcomepagehelper.cpp +++ b/src/plugins/coreplugin/welcomepagehelper.cpp @@ -60,25 +60,6 @@ void setBackgroundColor(QWidget *widget, Theme::Color colorRole) widget->setAutoFillBackground(true); } -void drawCardBackground(QPainter *painter, const QRectF &rect, - const QBrush &fill, const QPen &pen, qreal rounding) -{ - const qreal strokeWidth = pen.style() == Qt::NoPen ? 0 : pen.widthF(); - const qreal strokeShrink = strokeWidth / 2; - const QRectF itemRectAdjusted = rect.adjusted(strokeShrink, strokeShrink, - -strokeShrink, -strokeShrink); - const qreal roundingAdjusted = rounding - strokeShrink; - QPainterPath itemOutlinePath; - itemOutlinePath.addRoundedRect(itemRectAdjusted, roundingAdjusted, roundingAdjusted); - - painter->save(); - painter->setRenderHint(QPainter::Antialiasing); - painter->setBrush(fill); - painter->setPen(pen); - painter->drawPath(itemOutlinePath); - painter->restore(); -} - QWidget *createRule(Qt::Orientation orientation, QWidget *parent) { auto rule = new QWidget(parent); @@ -227,7 +208,7 @@ void Button::paintEvent(QPaintEvent *event) : Theme::Token_Accent_Default)) : Theme::Token_Foreground_Subtle; const QBrush fill(creatorColor(color)); - drawCardBackground(&p, bgR, fill, QPen(Qt::NoPen), brRectRounding); + StyleHelper::drawCardBg(&p, bgR, fill, QPen(Qt::NoPen), brRectRounding); break; } case LargeSecondary: @@ -236,7 +217,7 @@ void Button::paintEvent(QPaintEvent *event) : Theme::Token_Stroke_Subtle; const qreal width = hovered ? 2.0 : 1.0; const QPen outline(creatorColor(color), width); - drawCardBackground(&p, bgR, QBrush(Qt::NoBrush), outline, brRectRounding); + StyleHelper::drawCardBg(&p, bgR, QBrush(Qt::NoBrush), outline, brRectRounding); break; } case LargeTertiary: @@ -247,14 +228,14 @@ void Button::paintEvent(QPaintEvent *event) : (hovered ? Theme::Token_Foreground_Muted : Theme::Token_Foreground_Subtle)) : Theme::Token_Foreground_Subtle; - drawCardBackground(&p, bgR, creatorColor(bg), creatorColor(border), brRectRounding); + StyleHelper::drawCardBg(&p, bgR, creatorColor(bg), creatorColor(border), brRectRounding); break; } case SmallList: { if (isChecked() || hovered) { const QBrush fill(creatorColor(isChecked() ? Theme::Token_Foreground_Muted : Theme::Token_Foreground_Subtle)); - drawCardBackground(&p, bgR, fill, QPen(Qt::NoPen), brRectRounding); + StyleHelper::drawCardBg(&p, bgR, fill, QPen(Qt::NoPen), brRectRounding); } break; } @@ -264,7 +245,7 @@ void Button::paintEvent(QPaintEvent *event) const QBrush fill(hovered ? creatorColor(Theme::Token_Foreground_Subtle) : QBrush(Qt::NoBrush)); const QPen outline(hovered ? QPen(Qt::NoPen) : creatorColor(Theme::Token_Stroke_Subtle)); - drawCardBackground(&p, bgR, fill, outline, brRectRounding); + StyleHelper::drawCardBg(&p, bgR, fill, outline, brRectRounding); break; } } @@ -393,7 +374,7 @@ static void paintCommonBackground(QPainter *p, const QRectF &rect, const QWidget : Theme::Token_Stroke_Subtle)) : Theme::Token_Foreground_Subtle; const QPen pen(creatorColor(c)); - drawCardBackground(p, rect, fill, pen); + StyleHelper::drawCardBg(p, rect, fill, pen); } void SearchBox::paintEvent(QPaintEvent *event) @@ -536,7 +517,7 @@ void Switch::paintEvent([[maybe_unused]] QPaintEvent *event) const QPen outline = checkedEnabled ? QPen(Qt::NoPen) : creatorColor(hovered ? Theme::Token_Stroke_Muted : Theme::Token_Stroke_Subtle); - drawCardBackground(&p, trackR, fill, outline, trackRounding); + StyleHelper::drawCardBg(&p, trackR, fill, outline, trackRounding); } { // track label const QColor color = creatorColor(isEnabled() ? (isChecked() ? Theme::Token_Basic_White @@ -550,7 +531,7 @@ void Switch::paintEvent([[maybe_unused]] QPaintEvent *event) } else { const QRect offLabelR(trackX + switchTrackS.width() - trackRounding - labelS / 2 - 1, labelY, labelS, labelS); - drawCardBackground(&p, offLabelR, Qt::NoBrush, color, labelS / 2); + StyleHelper::drawCardBg(&p, offLabelR, Qt::NoBrush, color, labelS / 2); } } { // knob @@ -566,7 +547,7 @@ void Switch::paintEvent([[maybe_unused]] QPaintEvent *event) : Theme::Token_Foreground_Default); const QPen outline = checkedEnabled ? QPen(Qt::NoPen) : creatorColor(Theme::Token_Stroke_Subtle); - drawCardBackground(&p, thumbR, fill, outline, thumbRounding); + StyleHelper::drawCardBg(&p, thumbR, fill, outline, thumbRounding); } { // switch text label const int switchAndGapWidth = switchTrackS.width() + HGapS; @@ -1013,7 +994,7 @@ void ListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti const QColor fill(themeColor(hovered ? cardHoverBackground : cardDefaultBackground)); const QPen pen(themeColor(hovered ? cardHoverStroke : cardDefaultStroke), itemOutlineWidth); - WelcomePageHelpers::drawCardBackground(painter, bgR, fill, pen, itemCornerRounding); + StyleHelper::drawCardBg(painter, bgR, fill, pen, itemCornerRounding); const int shiftY = thumbnailAreaR.bottom(); int offset = 0; @@ -1086,8 +1067,8 @@ void ListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti QPainter maskPainter(&mask); const QRect maskR = bgR.translated(filterMargin, filterMargin) .adjusted(1, 1, -1, -1); - WelcomePageHelpers::drawCardBackground(&maskPainter, maskR, - Qt::white, Qt::NoPen, itemCornerRounding); + StyleHelper::drawCardBg(&maskPainter, maskR, Qt::white, Qt::NoPen, + itemCornerRounding); thumbnail.setAlphaChannel(mask); m_blurredThumbnail = QPixmap::fromImage( diff --git a/src/plugins/coreplugin/welcomepagehelper.h b/src/plugins/coreplugin/welcomepagehelper.h index a95c2cef9ea..f08b94b359b 100644 --- a/src/plugins/coreplugin/welcomepagehelper.h +++ b/src/plugins/coreplugin/welcomepagehelper.h @@ -58,14 +58,10 @@ public: }; CORE_EXPORT void setBackgroundColor(QWidget *widget, Utils::Theme::Color colorRole); -constexpr qreal defaultCardBackgroundRounding = 3.75; constexpr Utils::Theme::Color cardDefaultBackground = Utils::Theme::Token_Background_Muted; constexpr Utils::Theme::Color cardDefaultStroke = Utils::Theme::Token_Stroke_Subtle; constexpr Utils::Theme::Color cardHoverBackground = Utils::Theme::Token_Background_Subtle; constexpr Utils::Theme::Color cardHoverStroke = cardDefaultStroke; -CORE_EXPORT void drawCardBackground(QPainter *painter, const QRectF &rect, - const QBrush &fill, const QPen &pen = QPen(Qt::NoPen), - qreal rounding = defaultCardBackgroundRounding); CORE_EXPORT QWidget *createRule(Qt::Orientation orientation, QWidget *parent = nullptr); CORE_EXPORT void applyTf(QLabel *label, const TextFormat &tf, bool singleLine = true); diff --git a/src/plugins/extensionmanager/extensionsbrowser.cpp b/src/plugins/extensionmanager/extensionsbrowser.cpp index 2fc67230454..7f2bd97442e 100644 --- a/src/plugins/extensionmanager/extensionsbrowser.cpp +++ b/src/plugins/extensionmanager/extensionsbrowser.cpp @@ -333,7 +333,7 @@ public: creatorColor(selected ? Theme::Token_Stroke_Strong : hovered ? WelcomePageHelpers::cardHoverStroke : WelcomePageHelpers::cardDefaultStroke); - WelcomePageHelpers::drawCardBackground(painter, bgR, fillColor, strokeColor); + StyleHelper::drawCardBg(painter, bgR, fillColor, strokeColor); } render(painter, bgR.topLeft(), {}, QWidget::DrawChildren); @@ -353,7 +353,8 @@ public: circleSize, circleSize); const QColor fillColor = creatorColor(Theme::Token_Foreground_Muted); const QColor strokeColor = creatorColor(Theme::Token_Stroke_Subtle); - drawCardBackground(painter, smallCircle, fillColor, strokeColor, circleSize / 2); + StyleHelper::drawCardBg(painter, smallCircle, fillColor, strokeColor, + circleSize / 2); painter->setFont(countTF.font()); painter->setPen(countTF.color()); @@ -913,7 +914,7 @@ QPixmap itemIcon(const QModelIndex &index, Size size) gradient.setStops(gradientStops); if (!isEnabled) p.setOpacity(iconOpacityDisabled); - WelcomePageHelpers::drawCardBackground(&p, iconBgR, gradient, Qt::NoPen, iconRectRounding); + StyleHelper::drawCardBg(&p, iconBgR, gradient, Qt::NoPen, iconRectRounding); icon.paint(&p, iconBgR); return pixmap; @@ -939,9 +940,8 @@ QPixmap itemBadge(const QModelIndex &index, [[maybe_unused]] Size size) pixmap.setDevicePixelRatio(dpr); QPainter p(&pixmap); - WelcomePageHelpers::drawCardBackground(&p, badgeR, - creatorColor(Theme::Token_Notification_Neutral_Default), - Qt::NoPen, iconRectRounding); + StyleHelper::drawCardBg(&p, badgeR, creatorColor(Theme::Token_Notification_Neutral_Default), + Qt::NoPen, iconRectRounding); p.setFont(font); p.setPen(badgeTF.color()); p.drawText(badgeR, Qt::AlignCenter, badgeText); diff --git a/src/plugins/projectexplorer/projectwelcomepage.cpp b/src/plugins/projectexplorer/projectwelcomepage.cpp index ae0e2a78080..2aab103da72 100644 --- a/src/plugins/projectexplorer/projectwelcomepage.cpp +++ b/src/plugins/projectexplorer/projectwelcomepage.cpp @@ -228,7 +228,7 @@ static void drawBackgroundRect(QPainter *painter, const QRectF &rect, bool hover const QColor fill(creatorColor(hovered ? cardHoverBackground : cardDefaultBackground)); const QPen pen(creatorColor(hovered ? cardHoverStroke : cardDefaultStroke)); - WelcomePageHelpers::drawCardBackground(painter, rect, fill, pen, defaultCardBackgroundRounding); + StyleHelper::drawCardBg(painter, rect, fill, pen, StyleHelper::defaultCardBgRounding); } class BaseDelegate : public QAbstractItemDelegate @@ -281,8 +281,8 @@ public: void paintEvent([[maybe_unused]] QPaintEvent *event) override { QPainter painter(this); - const QRect bgR = rect().adjusted(-defaultCardBackgroundRounding, 0, - 0, isChecked() ? defaultCardBackgroundRounding : 0); + const QRect bgR = rect().adjusted(-StyleHelper::defaultCardBgRounding, 0, 0, + isChecked() ? StyleHelper::defaultCardBgRounding : 0); drawBackgroundRect(&painter, bgR, underMouse()); static const QPixmap arrowDown = |