From 8e1d9aa4808a6e4bb19197848238037a6790bb95 Mon Sep 17 00:00:00 2001 From: Oliver Sander Date: Sat, 13 Jul 2019 07:57:59 +0200 Subject: [PATCH] Fix page boundary gradients on scaled displays The code that draws a thin gradient at the right and bottom boundaries of a page implicitly assumed that the display was not scaled, i.e., that logical pixel where identical to physical pixels. Nobody really noticed because that gradient was so thin. Anyway, this patch fixes the gradient for scaled displays. I considered using a QLinearGradient, but decided against it: The code would have been longer, and the manual code in the patch can exploit the fact that all lines are horizontal or vertical. Therefore, expensive scan-conversion can be avoided. --- ui/pageview.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/ui/pageview.cpp b/ui/pageview.cpp index 45437b0ac..77b4f3245 100644 --- a/ui/pageview.cpp +++ b/ui/pageview.cpp @@ -3549,6 +3549,15 @@ void PageView::drawDocumentOnPainter( const QRect & contentsRect, QPainter * p ) QRect checkRect = contentsRect; checkRect.adjust( -3, -3, 1, 1 ); + // Method to linearly interpolate between black (=(0,0,0), omitted) and the background color + auto interpolateColor = [&backColor]( double t ) + { + return QColor( t*backColor.red(), t*backColor.green(), t*backColor.blue() ); + }; + + // width of the shadow in device pixels + static const int shadowWidth = 2*dpr; + // iterate over all items painting a black outline and a simple bottom/right gradient for ( const PageViewItem * item : qAsConst( d->items ) ) { @@ -3563,7 +3572,7 @@ void PageView::drawDocumentOnPainter( const QRect & contentsRect, QPainter * p ) p->save(); p->translate( itemGeometry.left(), itemGeometry.top() ); - // draw the page outline (black border and 2px bottom-right shadow) + // draw the page outline (black border and bottom-right shadow) if ( !itemGeometry.contains( contentsRect ) ) { int itemWidth = itemGeometry.width(); @@ -3577,15 +3586,15 @@ void PageView::drawDocumentOnPainter( const QRect & contentsRect, QPainter * p ) p->drawRect( outline ); // draw bottom/right gradient - static const int levels = 2; - int r = backColor.red() / (levels + 2) + 6, - g = backColor.green() / (levels + 2) + 6, - b = backColor.blue() / (levels + 2) + 6; - for ( int i = 0; i < levels; i++ ) + for ( int i = 1; i <= shadowWidth; i++ ) { - p->setPen( QColor( r * (i+2), g * (i+2), b * (i+2) ) ); - p->drawLine( i, i + itemHeight + 1, i + itemWidth + 1, i + itemHeight + 1 ); - p->drawLine( i + itemWidth + 1, i, i + itemWidth + 1, i + itemHeight ); + pen.setColor( interpolateColor( double(i)/( shadowWidth+1 ) ) ); + p->setPen( pen ); + QPointF left( (i-1)/dpr, itemHeight + i/dpr ); + QPointF up( itemWidth + i/dpr, (i-1)/dpr ); + QPointF corner( itemWidth + i/dpr, itemHeight + i/dpr); + p->drawLine( left, corner ); + p->drawLine( up, corner ); } } -- GitLab