1 // SPDX-FileCopyrightText: 2003 Dominique Devriese <devriese@kde.org>
2 
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 #include "object_drawer.h"
6 
7 #include "object_imp.h"
8 #include "../misc/kigpainter.h"
9 
10 #include <QPen>
11 #include <qnamespace.h>
12 #include <cassert>
13 
14 #include <QDebug>
15 
draw(const ObjectImp & imp,KigPainter & p,bool sel) const16 void ObjectDrawer::draw( const ObjectImp& imp, KigPainter& p, bool sel ) const
17 {
18   bool nv = p.getNightVision( );
19   if ( mshown || nv )
20   {
21     p.setBrushStyle( Qt::NoBrush );
22     p.setBrushColor( sel ? Qt::red : ( mshown?mcolor:Qt::gray ) );
23     p.setPen( QPen ( sel ? Qt::red : ( mshown?mcolor:Qt::gray ),  1) );
24     p.setWidth( mwidth );
25     p.setStyle( mstyle );
26     p.setPointStyle( mpointstyle );
27     p.setFont( mfont );
28     p.setSelected( sel );
29     imp.draw( p );
30   }
31 }
32 
contains(const ObjectImp & imp,const Coordinate & pt,const KigWidget & w,bool nv) const33 bool ObjectDrawer::contains( const ObjectImp& imp, const Coordinate& pt, const KigWidget& w, bool nv ) const
34 {
35   bool shownornv = mshown || nv;
36   return shownornv && imp.contains( pt, mwidth, w );
37 }
38 
shown() const39 bool ObjectDrawer::shown( ) const
40 {
41   return mshown;
42 }
43 
color() const44 QColor ObjectDrawer::color() const
45 {
46   return mcolor;
47 }
48 
getCopyShown(bool s) const49 ObjectDrawer* ObjectDrawer::getCopyShown( bool s ) const
50 {
51   ObjectDrawer* ret = new ObjectDrawer;
52   ret->mcolor = mcolor;
53   ret->mshown = s;
54   ret->mwidth = mwidth;
55   ret->mstyle = mstyle;
56   ret->mpointstyle = mpointstyle;
57   ret->mfont = mfont;
58   return ret;
59 }
60 
getCopyColor(const QColor & c) const61 ObjectDrawer* ObjectDrawer::getCopyColor( const QColor& c ) const
62 {
63   ObjectDrawer* ret = new ObjectDrawer;
64   ret->mcolor = c;
65   ret->mshown = mshown;
66   ret->mwidth = mwidth;
67   ret->mstyle = mstyle;
68   ret->mpointstyle = mpointstyle;
69   ret->mfont = mfont;
70   return ret;
71 }
72 
getCopyWidth(int w) const73 ObjectDrawer* ObjectDrawer::getCopyWidth( int w ) const
74 {
75   ObjectDrawer* ret = new ObjectDrawer;
76   ret->mcolor = mcolor;
77   ret->mshown = mshown;
78   ret->mwidth = w;
79   ret->mstyle = mstyle;
80   ret->mpointstyle = mpointstyle;
81   ret->mfont = mfont;
82   return ret;
83 }
84 
getCopyStyle(Qt::PenStyle s) const85 ObjectDrawer* ObjectDrawer::getCopyStyle( Qt::PenStyle s ) const
86 {
87   ObjectDrawer* ret = new ObjectDrawer;
88   ret->mcolor = mcolor;
89   ret->mshown = mshown;
90   ret->mwidth = mwidth;
91   ret->mstyle = s;
92   ret->mpointstyle = mpointstyle;
93   ret->mfont = mfont;
94   return ret;
95 }
96 
getCopyPointStyle(Kig::PointStyle p) const97 ObjectDrawer* ObjectDrawer::getCopyPointStyle( Kig::PointStyle p ) const
98 {
99   ObjectDrawer* ret = new ObjectDrawer;
100   ret->mcolor = mcolor;
101   ret->mshown = mshown;
102   ret->mwidth = mwidth;
103   ret->mstyle = mstyle;
104   ret->mpointstyle = p;
105   ret->mfont = mfont;
106   return ret;
107 }
108 
getCopyFont(const QFont & f) const109 ObjectDrawer* ObjectDrawer::getCopyFont( const QFont& f ) const
110 {
111   ObjectDrawer* ret = new ObjectDrawer;
112   ret->mcolor = mcolor;
113   ret->mshown = mshown;
114   ret->mwidth = mwidth;
115   ret->mstyle = mstyle;
116   ret->mpointstyle = mpointstyle;
117   ret->mfont = f;
118   return ret;
119 }
120 
width() const121 int ObjectDrawer::width() const
122 {
123   return mwidth;
124 }
125 
style() const126 Qt::PenStyle ObjectDrawer::style() const
127 {
128   return mstyle;
129 }
130 
pointStyle() const131 Kig::PointStyle ObjectDrawer::pointStyle() const
132 {
133   return mpointstyle;
134 }
135 
font() const136 QFont ObjectDrawer::font() const
137 {
138   return mfont;
139 }
140 
ObjectDrawer(const QColor & color,int width,bool shown,Qt::PenStyle style,Kig::PointStyle pointStyle,const QFont & f)141 ObjectDrawer::ObjectDrawer( const QColor& color, int width, bool shown, Qt::PenStyle style, Kig::PointStyle pointStyle, const QFont& f )
142   : mcolor( color ), mshown( shown ), mwidth( width ), mstyle( style ), mpointstyle( pointStyle ), mfont( f )
143 {
144 }
145 
ObjectDrawer()146 ObjectDrawer::ObjectDrawer()
147   : mcolor( Qt::blue ), mshown( true ), mwidth( -1 ), mstyle( Qt::SolidLine ), mpointstyle( Kig::Round ), mfont( QFont() )
148 {
149 }
150 
inRect(const ObjectImp & imp,const Rect & r,const KigWidget & w) const151 bool ObjectDrawer::inRect( const ObjectImp& imp, const Rect& r, const KigWidget& w ) const
152 {
153   return mshown && imp.inRect( r, mwidth, w );
154 }
155 
styleFromString(const QString & style)156 Qt::PenStyle ObjectDrawer::styleFromString( const QString& style )
157 {
158   if ( style == QLatin1String("SolidLine") )
159     return Qt::SolidLine;
160   else if ( style == QLatin1String("DashLine") )
161     return Qt::DashLine;
162   else if ( style == QLatin1String("DotLine") )
163     return Qt::DotLine;
164   else if ( style == QLatin1String("DashDotLine") )
165     return Qt::DashDotLine;
166   else if ( style == QLatin1String("DashDotDotLine") )
167     return Qt::DashDotDotLine;
168   else return Qt::SolidLine;
169 }
170 
styleToString() const171 QString ObjectDrawer::styleToString() const
172 {
173   if ( mstyle == Qt::SolidLine )
174     return QStringLiteral("SolidLine");
175   else if ( mstyle == Qt::DashLine )
176     return QStringLiteral("DashLine");
177   else if ( mstyle == Qt::DotLine )
178     return QStringLiteral("DotLine");
179   else if ( mstyle == Qt::DashDotLine )
180     return QStringLiteral("DashDotLine");
181   else if ( mstyle == Qt::DashDotDotLine )
182     return QStringLiteral("DashDotDotLine");
183   return QStringLiteral("SolidLine");
184 }
185