diff options
author | Christian Ehrlicher <[email protected]> | 2024-12-16 17:03:33 +0100 |
---|---|---|
committer | Christian Ehrlicher <[email protected]> | 2025-03-05 02:35:56 +0100 |
commit | 611031e4f069aea6a0312b5c459247094204d5eb (patch) | |
tree | 419bcff5f376b2b2bb5a12ea5d4d7294096d3432 | |
parent | 98653cca453fe3d88807d1a314ae17efadd7c958 (diff) |
QDrawUtil: Cleanup qDrawPlainRoundedRect/qDrawPlainRect
Cleanup the drawing code to avoid useless drawings by only calling
drawRect once with the correct parameters. This also removes rendering
artifacts due to e.g. rounding errors with a dpr != 1.
Fixes: QTBUG-132187
Pick-to: 6.9 6.8
Change-Id: Ib5eff89c8bdcbd0c86954e78dadaad83346c92ff
Reviewed-by: Volker Hilsheimer <[email protected]>
-rw-r--r-- | src/widgets/styles/qdrawutil.cpp | 63 |
1 files changed, 18 insertions, 45 deletions
diff --git a/src/widgets/styles/qdrawutil.cpp b/src/widgets/styles/qdrawutil.cpp index 1210390da80..29d8101f719 100644 --- a/src/widgets/styles/qdrawutil.cpp +++ b/src/widgets/styles/qdrawutil.cpp @@ -540,27 +540,15 @@ void qDrawPlainRect(QPainter *p, int x, int y, int w, int h, const QColor &c, } QPainterStateGuard painterGuard(p); - const qreal devicePixelRatio = QStyleHelper::getDpr(p); - if (!qFuzzyCompare(devicePixelRatio, qreal(1))) { - const qreal inverseScale = qreal(1) / devicePixelRatio; - p->scale(inverseScale, inverseScale); - x = qRound(devicePixelRatio * x); - y = qRound(devicePixelRatio * y); - w = devicePixelRatio * w; - h = devicePixelRatio * h; - lineWidth = qRound(devicePixelRatio * lineWidth); - p->translate(0.5, 0.5); - } - - p->setPen(c); - p->setBrush(Qt::NoBrush); - for (int i=0; i<lineWidth; i++) - p->drawRect(x+i, y+i, w-i*2 - 1, h-i*2 - 1); - if (fill) { // fill with fill color - p->setPen(Qt::NoPen); - p->setBrush(*fill); - p->drawRect(x+lineWidth, y+lineWidth, w-lineWidth*2, h-lineWidth*2); - } + if (lineWidth == 0 && !fill) + return; + if (lineWidth > 0) + p->setPen(QPen(c, lineWidth, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin)); + p->setBrush(fill ? *fill : Qt::NoBrush); + const QRectF r(x, y, w, h); + const auto lw2 = lineWidth / 2.; + const QRectF rect = r.marginsRemoved(QMarginsF(lw2, lw2, lw2, lw2)); + p->drawRect(rect); } /*! @@ -603,30 +591,15 @@ void qDrawPlainRoundedRect(QPainter *p, int x, int y, int w, int h, } QPainterStateGuard painterGuard(p); - const qreal devicePixelRatio = QStyleHelper::getDpr(p); - if (!qFuzzyCompare(devicePixelRatio, qreal(1))) { - const qreal inverseScale = qreal(1) / devicePixelRatio; - p->scale(inverseScale, inverseScale); - x = qRound(devicePixelRatio * x); - y = qRound(devicePixelRatio * y); - w = devicePixelRatio * w; - h = devicePixelRatio * h; - lineWidth = qRound(devicePixelRatio * lineWidth); - p->translate(0.5, 0.5); - } - - p->setPen(c); - p->setBrush(Qt::NoBrush); - for (int i=0; i<lineWidth; i++) { - QRectF rect(x+i, y+i, w-i*2 - 1, h-i*2 - 1); - rect.marginsRemoved(QMarginsF(0.5,0.5,0.5,0.5)); - p->drawRoundedRect(rect, rx, ry); - } - if (fill) { // fill with fill color - p->setPen(Qt::NoPen); - p->setBrush(*fill); - p->drawRoundedRect(x+lineWidth, y+lineWidth, w-lineWidth*2, h-lineWidth*2, rx, ry); - } + if (lineWidth == 0 && !fill) + return; + if (lineWidth > 0) + p->setPen(QPen(c, lineWidth)); + p->setBrush(fill ? *fill : Qt::NoBrush); + const QRectF r(x, y, w, h); + const auto lw2 = lineWidth / 2.; + const QRectF rect = r.marginsRemoved(QMarginsF(lw2, lw2, lw2, lw2)); + p->drawRoundedRect(rect, rx, ry); } /***************************************************************************** |