From 80650e0e873ddb5b95fc8111981ea25258736be1 Mon Sep 17 00:00:00 2001 From: Noah Davis Date: Sat, 14 Dec 2019 13:56:28 -0500 Subject: [PATCH] Fix rubberband selection outline position Summary: Before this patch, the outline of the rubberband selection control was down and to the right half a pixel too far. This wasn't visible at 1x scaling because of the lack of antialiasing, but resulted in the top/left being too thin and bottom/right being too thick on high DPI displays. To fix the issue, I enabled antialiasing and moved all sides inwards by half a pixel so that the outline would be pixel perfect. I also added a function to get a QRectF with the correct size and position for rectangles with pen strokes. Test Plan: - Open dolphin and make a selection with the mouse - Go to a Folder View Plasma widget and make a selection with the mouse 2x scaling before: {F7818498, size=full} after: {F7818500, size=full} 3x scaling before: {F7818484} after: {F7818483} Reviewers: #vdg, #breeze, #plasma, hpereiradacosta, broulik Reviewed By: hpereiradacosta Subscribers: broulik, plasma-devel Tags: #plasma, #breeze, #vdg Differential Revision: https://phabricator.kde.org/D26001 --- kstyle/breezehelper.cpp | 21 ++++++++++++++++++++- kstyle/breezehelper.h | 6 ++++++ kstyle/breezestyle.cpp | 14 +++++++++----- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/kstyle/breezehelper.cpp b/kstyle/breezehelper.cpp index 0c13e006..24c4607b 100644 --- a/kstyle/breezehelper.cpp +++ b/kstyle/breezehelper.cpp @@ -1436,10 +1436,29 @@ namespace Breeze #endif } + //______________________________________________________________________________ + QRectF Helper::strokedRect( const QRect &rect, const int penWidth ) const + { + /* With a pen stroke width of 1, the rectangle should have each of its + * sides moved inwards by half a pixel. This allows the stroke to be + * pixel perfect instead of blurry from sitting between pixels and + * prevents the rectangle with a stroke from becoming larger than the + * original size of the rectangle. + */ + qreal adjustment = 0.5 * penWidth; + return QRectF( rect ).adjusted( adjustment, adjustment, -adjustment, -adjustment ); + } + + QRectF Helper::strokedRect( const QRectF &rect, const int penWidth ) const + { + qreal adjustment = 0.5 * penWidth; + return rect.adjusted( adjustment, adjustment, -adjustment, -adjustment ); + } + //______________________________________________________________________________ QRectF Helper::shadowRect( const QRectF& rect ) const { return rect.adjusted( 0.5, 0.5, -0.5, -0.5 ).translated( 0.5, 0.5 ); } - + //______________________________________________________________________________ QPainterPath Helper::roundedPath( const QRectF& rect, Corners corners, qreal radius ) const { diff --git a/kstyle/breezehelper.h b/kstyle/breezehelper.h index 290f689b..2367ee70 100644 --- a/kstyle/breezehelper.h +++ b/kstyle/breezehelper.h @@ -307,6 +307,12 @@ namespace Breeze //* frame radius qreal frameRadius( qreal bias = 0 ) const { return qMax( qreal( Metrics::Frame_FrameRadius ) - 0.5 + bias, 0.0 ); } + + //* return a QRectF with the appropriate size for a rectangle with a pen stroke + QRectF strokedRect( const QRect &rect, const int penWidth = 1 ) const; + + //* return a QRectF with the appropriate size for a rectangle with a pen stroke + QRectF strokedRect( const QRectF &rect, const int penWidth = 1 ) const; protected: diff --git a/kstyle/breezestyle.cpp b/kstyle/breezestyle.cpp index 9fef8a41..3554758b 100644 --- a/kstyle/breezestyle.cpp +++ b/kstyle/breezestyle.cpp @@ -5388,15 +5388,19 @@ namespace Breeze bool Style::drawRubberBandControl( const QStyleOption* option, QPainter* painter, const QWidget* ) const { - const auto& palette( option->palette ); - const auto rect( option->rect ); + painter->save(); + painter->setRenderHints( QPainter::Antialiasing ); + const auto& palette( option->palette ); auto color = palette.color( QPalette::Highlight ); - painter->setPen( KColorUtils::mix( color, palette.color( QPalette::Active, QPalette::WindowText ) ) ); + QPen pen = KColorUtils::mix( color, palette.color( QPalette::Active, QPalette::WindowText ) ); + pen.setJoinStyle(Qt::RoundJoin); + painter->setPen( pen ); color.setAlpha( 50 ); painter->setBrush( color ); - painter->setClipRegion( rect ); - painter->drawRect( rect.adjusted( 0, 0, -1, -1 ) ); + painter->drawRect( _helper->strokedRect( option->rect ) ); + + painter->restore(); return true; }