Skip to content

Commit

Permalink
QCommonStyle: use QPainterStateGuard
Browse files Browse the repository at this point in the history
Replace the internal QPainterStateSaver class with the now public
QPainterStateGuard.

Pick-to: 6.9
Change-Id: I56285cb469cec43071320e87ac378674fd99c06b
Reviewed-by: Volker Hilsheimer <[email protected]>
  • Loading branch information
chehrlic committed Dec 21, 2024
1 parent 6844c70 commit 7690b62
Showing 1 changed file with 38 additions and 50 deletions.
88 changes: 38 additions & 50 deletions src/widgets/styles/qcommonstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <qpainter.h>
#include <qpaintengine.h>
#include <qpainterpath.h>
#include <qpainterstateguard.h>
#if QT_CONFIG(slider)
#include <qslider.h>
#endif
Expand Down Expand Up @@ -91,31 +92,6 @@ Q_STATIC_LOGGING_CATEGORY(lcCommonStyle, "qt.widgets.commonstyle");

using namespace Qt::StringLiterals;

struct QPainterStateSaver
{
QPainterStateSaver(QPainter *p, bool bSaveRestore = true)
: m_painter(p)
, m_bSaveRestore(bSaveRestore)
{
if (m_bSaveRestore)
m_painter->save();
}
~QPainterStateSaver()
{
restore();
}
void restore()
{
if (m_bSaveRestore) {
m_bSaveRestore = false;
m_painter->restore();
}
}
private:
QPainter *m_painter;
bool m_bSaveRestore;
};

/*!
\class QCommonStyle
\brief The QCommonStyle class encapsulates the common Look and Feel of a GUI.
Expand Down Expand Up @@ -248,7 +224,7 @@ void QCommonStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, Q
--yy;
}
if (!(opt->state & State_Enabled) && !(opt->state & State_On)) {
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
p->translate(1, 1);
p->setPen(opt->palette.light().color());
p->drawLines(a);
Expand Down Expand Up @@ -373,7 +349,7 @@ void QCommonStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, Q
case PE_FrameTabBarBase:
if (const QStyleOptionTabBarBase *tbb
= qstyleoption_cast<const QStyleOptionTabBarBase *>(opt)) {
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
switch (tbb->shape) {
case QTabBar::RoundedNorth:
case QTabBar::TriangularNorth:
Expand Down Expand Up @@ -460,7 +436,7 @@ void QCommonStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, Q
#endif // QT_CONFIG(dockwidget)
#if QT_CONFIG(toolbar)
case PE_IndicatorToolBarHandle: {
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
p->translate(opt->rect.x(), opt->rect.y());
if (opt->state & State_Horizontal) {
int x = opt->rect.width() / 3;
Expand Down Expand Up @@ -507,7 +483,7 @@ void QCommonStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, Q
int y = br.y();
int w = br.width();
int h = br.height();
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
const qreal devicePixelRatio = QStyleHelper::getDpr(p);
if (!qFuzzyCompare(devicePixelRatio, qreal(1))) {
const qreal inverseScale = qreal(1) / devicePixelRatio;
Expand Down Expand Up @@ -544,7 +520,7 @@ void QCommonStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, Q
int y = r.y();
int w = r.width();
int h = r.height();
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
const qreal devicePixelRatio = QStyleHelper::getDpr(p);
if (!qFuzzyCompare(devicePixelRatio, qreal(1))) {
const qreal inverseScale = qreal(1) / devicePixelRatio;
Expand Down Expand Up @@ -642,7 +618,7 @@ void QCommonStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, Q
case PE_IndicatorColumnViewArrow: {
if (const QStyleOptionViewItem *viewOpt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
bool reverse = (viewOpt->direction == Qt::RightToLeft);
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
QPainterPath path;
int x = viewOpt->rect.x() + 1;
int offset = (viewOpt->rect.height() / 3);
Expand Down Expand Up @@ -1841,7 +1817,7 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,
break;
case CE_TabBarTabShape:
if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
QRect rect(tab->rect);
bool selected = tab->state & State_Selected;
bool onlyOne = tab->position == QStyleOptionTab::OnlyOneTab;
Expand Down Expand Up @@ -2002,8 +1978,9 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,
if (!proxy()->styleHint(SH_UnderlineShortcut, opt, widget))
alignment |= Qt::TextHideMnemonic;

QPainterStateSaver pss(p, verticalTabs);
QPainterStateGuard psg(p, QPainterStateGuard::InitialState::NoSave);
if (verticalTabs) {
psg.save();
int newX, newY, newRot;
if (tab->shape == QTabBar::RoundedEast || tab->shape == QTabBar::TriangularEast) {
newX = tr.width() + tr.x();
Expand Down Expand Up @@ -2036,7 +2013,8 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,

proxy()->drawItemText(p, tr, alignment, tab->palette, tab->state & State_Enabled, tab->text,
widget ? widget->foregroundRole() : QPalette::WindowText);
pss.restore();
if (verticalTabs)
psg.restore();

if (tab->state & State_HasFocus) {
const int OFFSET = 1 + pixelMetric(PM_DefaultFrameWidth, opt, widget);
Expand All @@ -2056,7 +2034,7 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,
#endif // QT_CONFIG(tabbar)
#if QT_CONFIG(sizegrip)
case CE_SizeGrip: {
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
int x, y, w, h;
opt->rect.getRect(&x, &y, &w, &h);

Expand Down Expand Up @@ -2141,7 +2119,7 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,
// ### workaround for borked XRENDER
tiledPixmap = QPixmap::fromImage(tiledPixmap.toImage());

QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
QRect r = opt->rect;
QStyleHintReturnMask mask;
if (proxy()->styleHint(QStyle::SH_RubberBand_Mask, opt, widget, &mask))
Expand All @@ -2167,8 +2145,9 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,
if (!dwOpt->title.isEmpty()) {
const bool verticalTitleBar = dwOpt->verticalTitleBar;

QPainterStateSaver pss(p, verticalTitleBar);
QPainterStateGuard psg(p, QPainterStateGuard::InitialState::NoSave);
if (verticalTitleBar) {
psg.save();
r = r.transposed();

p->translate(r.left(), r.top() + r.width());
Expand Down Expand Up @@ -2222,7 +2201,7 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,
case CE_ComboBoxLabel:
if (const QStyleOptionComboBox *cb = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
QRect editRect = proxy()->subControlRect(CC_ComboBox, cb, SC_ComboBoxEditField, widget);
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
p->setClipRect(editRect);
if (!cb->currentIcon.isNull()) {
QIcon::Mode mode = cb->state & State_Enabled ? QIcon::Normal
Expand Down Expand Up @@ -2293,7 +2272,7 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,
#if QT_CONFIG(itemviews)
case CE_ItemViewItem:
if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
// the style calling this might want to clip, so respect any region already set
const QRegion clipRegion = p->hasClipping() ? (p->clipRegion() & opt->rect) : opt->rect;
p->setClipRegion(clipRegion);
Expand Down Expand Up @@ -3288,7 +3267,7 @@ void QCommonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionCompl
int fudge = len / 2;
int pos;
// Since there is no subrect for tickmarks do a translation here.
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
p->translate(slider->rect.x(), slider->rect.y());
p->setPen(slider->palette.windowText().color());
int v = slider->minimum;
Expand Down Expand Up @@ -3582,7 +3561,8 @@ void QCommonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionCompl
tool.state = down ? State_Sunken : State_Raised;
proxy()->drawPrimitive(PE_PanelButtonTool, &tool, p, widget);

QPainterStateSaver pss(p, down);
QPainterStateGuard psg(p, down ? QPainterStateGuard::InitialState::Save
: QPainterStateGuard::InitialState::NoSave);
if (down)
p->translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, tb, widget),
proxy()->pixelMetric(PM_ButtonShiftVertical, tb, widget));
Expand All @@ -3600,7 +3580,8 @@ void QCommonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionCompl
tool.state = down ? State_Sunken : State_Raised;
proxy()->drawPrimitive(PE_PanelButtonTool, &tool, p, widget);

QPainterStateSaver pss(p, down);
QPainterStateGuard psg(p, down ? QPainterStateGuard::InitialState::Save
: QPainterStateGuard::InitialState::NoSave);
if (down)
p->translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, tb, widget),
proxy()->pixelMetric(PM_ButtonShiftVertical, tb, widget));
Expand All @@ -3617,7 +3598,8 @@ void QCommonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionCompl
tool.state = down ? State_Sunken : State_Raised;
proxy()->drawPrimitive(PE_PanelButtonTool, &tool, p, widget);

QPainterStateSaver pss(p, down);
QPainterStateGuard psg(p, down ? QPainterStateGuard::InitialState::Save
: QPainterStateGuard::InitialState::NoSave);
if (down)
p->translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, tb, widget),
proxy()->pixelMetric(PM_ButtonShiftVertical, tb, widget));
Expand All @@ -3638,7 +3620,8 @@ void QCommonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionCompl
tool.state = down ? State_Sunken : State_Raised;
proxy()->drawPrimitive(PE_PanelButtonTool, &tool, p, widget);

QPainterStateSaver pss(p, down);
QPainterStateGuard psg(p, down ? QPainterStateGuard::InitialState::Save
: QPainterStateGuard::InitialState::NoSave);
if (down)
p->translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, tb, widget),
proxy()->pixelMetric(PM_ButtonShiftVertical, tb, widget));
Expand All @@ -3654,7 +3637,9 @@ void QCommonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionCompl
tool.rect = ir;
tool.state = down ? State_Sunken : State_Raised;
proxy()->drawPrimitive(PE_PanelButtonTool, &tool, p, widget);
QPainterStateSaver pss(p, down);

QPainterStateGuard psg(p, down ? QPainterStateGuard::InitialState::Save
: QPainterStateGuard::InitialState::NoSave);
if (down)
p->translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, tb, widget),
proxy()->pixelMetric(PM_ButtonShiftVertical, tb, widget));
Expand All @@ -3671,7 +3656,9 @@ void QCommonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionCompl
tool.rect = ir;
tool.state = down ? State_Sunken : State_Raised;
proxy()->drawPrimitive(PE_PanelButtonTool, &tool, p, widget);
QPainterStateSaver pss(p, down);

QPainterStateGuard psg(p, down ? QPainterStateGuard::InitialState::Save
: QPainterStateGuard::InitialState::NoSave);
if (down)
p->translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, tb, widget),
proxy()->pixelMetric(PM_ButtonShiftVertical, tb, widget));
Expand All @@ -3686,7 +3673,9 @@ void QCommonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionCompl
tool.rect = ir;
tool.state = down ? State_Sunken : State_Raised;
proxy()->drawPrimitive(PE_PanelButtonTool, &tool, p, widget);
QPainterStateSaver pss(p, down);

QPainterStateGuard psg(p, down ? QPainterStateGuard::InitialState::Save
: QPainterStateGuard::InitialState::NoSave);
if (down)
p->translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, tb, widget),
proxy()->pixelMetric(PM_ButtonShiftVertical, tb, widget));
Expand All @@ -3700,7 +3689,6 @@ void QCommonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionCompl
int iconSize = proxy()->pixelMetric(PM_SmallIconSize, tb, widget);
pm = proxy()->standardIcon(SP_TitleBarMenuButton, &tool, widget).pixmap(QSize(iconSize, iconSize), QStyleHelper::getDpr(p));
tool.rect = ir;
QPainterStateSaver pss(p);
proxy()->drawItemPixmap(p, ir, Qt::AlignCenter, pm);
}
}
Expand All @@ -3710,7 +3698,7 @@ void QCommonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionCompl
case CC_Dial:
if (const QStyleOptionSlider *dial = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
// OK, this is more a port of things over
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);

// avoid dithering
if (p->paintEngine()->hasFeature(QPaintEngine::Antialiasing))
Expand Down Expand Up @@ -3811,7 +3799,7 @@ void QCommonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionCompl
frame.lineWidth = groupBox->lineWidth;
frame.midLineWidth = groupBox->midLineWidth;
frame.rect = proxy()->subControlRect(CC_GroupBox, opt, SC_GroupBoxFrame, widget);
QPainterStateSaver pss(p);
QPainterStateGuard psg(p);
QRegion region(groupBox->rect);
if (!groupBox->text.isEmpty()) {
bool ltr = groupBox->direction == Qt::LeftToRight;
Expand Down

0 comments on commit 7690b62

Please sign in to comment.