1 // SPDX-FileCopyrightText: 2002 Dominique Devriese <devriese@kde.org>
2 
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 #include "screeninfo.h"
6 
7 #include <cmath>
8 
ScreenInfo(const Rect & docRect,const QRect & viewRect)9 ScreenInfo::ScreenInfo( const Rect& docRect, const QRect& viewRect )
10   : mkrect( docRect.normalized() ), mqrect( viewRect.normalized() )
11 {
12 }
13 
fromScreen(const QRect & r) const14 Rect ScreenInfo::fromScreen( const QRect& r ) const
15 {
16   return Rect(
17     fromScreen( r.topLeft() ),
18     fromScreen( r.bottomRight() )
19     ).normalized();
20 }
21 
fromScreen(const QPoint & p) const22 Coordinate ScreenInfo::fromScreen( const QPoint& p ) const
23 {
24   // invert the y-axis: 0 is at the bottom !
25   Coordinate t( p.x(), mqrect.height() - p.y() );
26   t *= mkrect.width();
27   t /= mqrect.width();
28   return t + mkrect.bottomLeft();
29 }
30 
toScreen(const Coordinate & p) const31 QPoint ScreenInfo::toScreen( const Coordinate& p ) const
32 {
33   Coordinate t = p - mkrect.bottomLeft();
34   t *= mqrect.width();
35   t /= mkrect.width();
36   // invert the y-axis: 0 is at the bottom !
37   return QPoint( (int) t.x, mqrect.height() - (int) t.y );
38 }
39 
toScreen(const Rect & r) const40 QRect ScreenInfo::toScreen( const Rect& r ) const
41 {
42   return QRect(
43     toScreen( r.bottomLeft() ),
44     toScreen( r.topRight() )
45     ).normalized();
46 }
47 
toScreenF(const Coordinate & p) const48 QPointF ScreenInfo::toScreenF( const Coordinate& p ) const
49 {
50   Coordinate t = p - mkrect.bottomLeft();
51   t *= mqrect.width();
52   t /= mkrect.width();
53   // invert the y-axis: 0 is at the bottom !
54   return QPointF( t.x, mqrect.height() - t.y );
55 }
56 
toScreenF(const Rect & r) const57 QRectF ScreenInfo::toScreenF( const Rect& r ) const
58 {
59   return QRectF(
60     toScreenF( r.bottomLeft() ),
61     toScreenF( r.topRight() )
62     ).normalized();
63 }
64 
pixelWidth() const65 double ScreenInfo::pixelWidth() const
66 {
67   Coordinate a = fromScreen( QPoint( 0, 0 ) );
68   Coordinate b = fromScreen( QPoint( 0, 1000 ) );
69   return std::fabs( b.y - a.y ) / 1000;
70 }
71 
shownRect() const72 const Rect& ScreenInfo::shownRect() const
73 {
74   return mkrect;
75 }
76 
setShownRect(const Rect & r)77 void ScreenInfo::setShownRect( const Rect& r )
78 {
79   mkrect = r;
80 }
81 
viewRect() const82 const QRect ScreenInfo::viewRect() const
83 {
84   return mqrect;
85 }
86 
setViewRect(const QRect & r)87 void ScreenInfo::setViewRect( const QRect& r )
88 {
89   mqrect = r;
90 }
91 
normalMiss(int width) const92 double ScreenInfo::normalMiss( int width ) const
93 {
94   int twidth = width == -1 ? 1 : width;
95   return (twidth+2)*pixelWidth();
96 }
97