From 683afd5cbb89ac064ec1e00a7865abd2ce60785e Mon Sep 17 00:00:00 2001 From: zeljan1 Date: Tue, 11 Feb 2025 14:44:05 +0100 Subject: [PATCH] Qt5,Qt6: use floating point functions for primitives drawing on scaled displays. issue #41422 --- lcl/interfaces/qt5/qtobjects.pas | 30 ++++++++++++++++++++++++++++++ lcl/interfaces/qt5/qtwinapi.inc | 19 +++++++++++++------ lcl/interfaces/qt6/qtobjects.pas | 20 ++++++++++++++++++++ lcl/interfaces/qt6/qtwinapi.inc | 19 +++++++++++++------ 4 files changed, 76 insertions(+), 12 deletions(-) diff --git a/lcl/interfaces/qt5/qtobjects.pas b/lcl/interfaces/qt5/qtobjects.pas index 878ce36a6a..19422a22b4 100644 --- a/lcl/interfaces/qt5/qtobjects.pas +++ b/lcl/interfaces/qt5/qtobjects.pas @@ -465,6 +465,7 @@ type function getDeviceSize: TPoint; function getRegionType(ARegion: QRegionH): integer; function getClipRegion: TQtRegion; + function preferFloatingPointDrawingFunctions: boolean; {when screen is scaled we use drawLineF, drawRectF etc.} procedure setClipping(const AValue: Boolean); procedure setClipRect(const ARect: TRect); procedure setClipRegion(ARegion: QRegionH; AOperation: QtClipOperation = QtReplaceClip); @@ -3398,6 +3399,35 @@ begin Result := vRegion; end; +function TQtDeviceContext.preferFloatingPointDrawingFunctions: boolean; +const + AEpsilon: double = 0.01; +var + APixelRatio: QReal; + AWindow: QWindowH; + AWidget: QWidgetH; + AScreen: QScreenH; +begin + Result := False; + if Assigned(Parent) then + begin + AWindow := QWidget_windowHandle(Parent); + if AWindow = nil then + begin + AWidget := QWidget_topLevelWidget(Parent); + AWindow := QWidget_windowHandle(AWidget); + if AWindow = nil then + exit; + end; + AScreen := QWindow_screen(AWindow); + if AScreen = nil then + exit; + APixelRatio := QScreen_devicePixelRatio(AScreen); + if Abs(APixelRatio - 1.00) > AEpsilon then + exit(True); + end; +end; + procedure TQtDeviceContext.setClipping(const AValue: Boolean); begin QPainter_setClipping(Widget, AValue); diff --git a/lcl/interfaces/qt5/qtwinapi.inc b/lcl/interfaces/qt5/qtwinapi.inc index 29e29cd9ee..3f29e6b8d2 100644 --- a/lcl/interfaces/qt5/qtwinapi.inc +++ b/lcl/interfaces/qt5/qtwinapi.inc @@ -4896,7 +4896,11 @@ begin Pt := TQtCustomControl(AHandle).viewport.ScrolledOffset; Types.OffsetRect(Rect^, -Pt.X, -Pt.Y); end; - + if (Rect^.Width <= 0) or (Rect^.Height <= 0) then + begin + //DebugLn('WARNING: TQtWidgetSet.InvalidateRect() Rect is null ',dbgs(Rect^),' LCL=',dbgsName(TQtWidget(AHandle).LCLObject)); + exit(True); + end; // no need to handle bErase. Qt automatically erase rect on paint event according to docs TQtWidget(aHandle).Update(Rect); end else @@ -4966,8 +4970,9 @@ begin OldBkMode := SetBkMode(DC, TRANSPARENT); if TQtDeviceContext(DC).pen.getCosmetic then LastPos := TQtDeviceContext(DC).GetLineLastPixelPos(PenPos, LastPos); - if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) then - TQtDeviceContext(DC).drawLineF(PenPos.X, PenPos.Y, LastPos.X, LastPos.Y) + if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) or + TQtDeviceContext(DC).preferFloatingPointDrawingFunctions then + TQtDeviceContext(DC).drawLineF(PenPos.X, PenPos.Y, LastPos.X, LastPos.Y) else TQtDeviceContext(DC).drawLine(PenPos.X, PenPos.Y, LastPos.X, LastPos.Y); @@ -5321,7 +5326,8 @@ begin Result := IsValidDC(DC); if Result then begin - if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) then + if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) or + TQtDeviceContext(DC).preferFloatingPointDrawingFunctions then begin GetMem(QtPointsF, NumPts * SizeOf(TQtPointF)); for i := 0 to NumPts - 1 do @@ -5407,8 +5413,9 @@ begin R := NormalizeRect(Rect(X1, Y1, X2, Y2)); if IsRectEmpty(R) then Exit(True); - if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) then - TQtDeviceContext(DC).drawRectF(R.Left, R.Top, R.Right - R.Left - 1, R.Bottom - R.Top - 1) + if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) or + TQtDeviceContext(DC).preferFloatingPointDrawingFunctions then + TQtDeviceContext(DC).drawRectF(R.Left, R.Top, R.Right - R.Left - 1, R.Bottom - R.Top - 1) else TQtDeviceContext(DC).drawRect(R.Left, R.Top, R.Right - R.Left - 1, R.Bottom - R.Top - 1); Result := True; diff --git a/lcl/interfaces/qt6/qtobjects.pas b/lcl/interfaces/qt6/qtobjects.pas index 08d63d49c2..5337679530 100644 --- a/lcl/interfaces/qt6/qtobjects.pas +++ b/lcl/interfaces/qt6/qtobjects.pas @@ -472,6 +472,7 @@ type function getDeviceSize: TPoint; function getRegionType(ARegion: QRegionH): integer; function getClipRegion: TQtRegion; + function preferFloatingPointDrawingFunctions: boolean; {when screen is scaled we use drawLineF, drawRectF etc.} procedure setClipping(const AValue: Boolean); procedure setClipRect(const ARect: TRect); procedure setClipRegion(ARegion: QRegionH; AOperation: QtClipOperation = QtReplaceClip); @@ -3568,6 +3569,25 @@ begin Result := vRegion; end; +function TQtDeviceContext.preferFloatingPointDrawingFunctions: boolean; +const + AEpsilon: double = 0.01; +var + APixelRatio: QReal; + AScreen: QScreenH; +begin + Result := False; + if Assigned(Parent) then + begin + AScreen := QWidget_screen(Parent); + if AScreen = nil then + exit; + APixelRatio := QScreen_devicePixelRatio(AScreen); + if Abs(APixelRatio - 1.00) > AEpsilon then + exit(True); + end; +end; + procedure TQtDeviceContext.setClipping(const AValue: Boolean); begin QPainter_setClipping(Widget, AValue); diff --git a/lcl/interfaces/qt6/qtwinapi.inc b/lcl/interfaces/qt6/qtwinapi.inc index e0939080ad..1c360073b3 100644 --- a/lcl/interfaces/qt6/qtwinapi.inc +++ b/lcl/interfaces/qt6/qtwinapi.inc @@ -4924,7 +4924,11 @@ begin Pt := TQtCustomControl(AHandle).viewport.ScrolledOffset; Types.OffsetRect(Rect^, -Pt.X, -Pt.Y); end; - + if (Rect^.Width <= 0) or (Rect^.Height <= 0) then + begin + //DebugLn('WARNING: TQtWidgetSet.InvalidateRect() Rect is null ',dbgs(Rect^),' LCL=',dbgsName(TQtWidget(AHandle).LCLObject)); + exit(True); + end; // no need to handle bErase. Qt automatically erase rect on paint event according to docs TQtWidget(aHandle).Update(Rect); end else @@ -4994,8 +4998,9 @@ begin OldBkMode := SetBkMode(DC, TRANSPARENT); if TQtDeviceContext(DC).pen.getCosmetic then LastPos := TQtDeviceContext(DC).GetLineLastPixelPos(PenPos, LastPos); - if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) then - TQtDeviceContext(DC).drawLineF(PenPos.X, PenPos.Y, LastPos.X, LastPos.Y) + if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) or + TQtDeviceContext(DC).preferFloatingPointDrawingFunctions then + TQtDeviceContext(DC).drawLineF(PenPos.X, PenPos.Y, LastPos.X, LastPos.Y) else TQtDeviceContext(DC).drawLine(PenPos.X, PenPos.Y, LastPos.X, LastPos.Y); if TQtDeviceContext(DC).pen.getStyle = QtCustomDashLine then @@ -5393,7 +5398,8 @@ begin Result := IsValidDC(DC); if Result then begin - if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) then + if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) or + TQtDeviceContext(DC).preferFloatingPointDrawingFunctions then begin GetMem(QtPointsF, NumPts * SizeOf(TQtPointF)); for i := 0 to NumPts - 1 do @@ -5479,8 +5485,9 @@ begin R := NormalizeRect(Rect(X1, Y1, X2, Y2)); if IsRectEmpty(R) then Exit(True); - if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) then - TQtDeviceContext(DC).drawRectF(R.Left, R.Top, R.Right - R.Left - 1, R.Bottom - R.Top - 1) + if QPainter_testRenderHint(TQtDeviceContext(DC).Widget, QPainterAntialiasing) or + TQtDeviceContext(DC).preferFloatingPointDrawingFunctions then + TQtDeviceContext(DC).drawRectF(R.Left, R.Top, R.Right - R.Left - 1, R.Bottom - R.Top - 1) else TQtDeviceContext(DC).drawRect(R.Left, R.Top, R.Right - R.Left - 1, R.Bottom - R.Top - 1); Result := True;