KiCad PCB EDA Suite
view_overlay.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2013-2017 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <view/view.h>
27 #include <view/view_item.h>
28 #include <view/view_overlay.h>
30 #include <painter.h>
31 
33 #include <geometry/seg.h>
34 
35 namespace KIGFX {
36 
38 {
39  virtual ~COMMAND() {};
40  virtual void Execute( VIEW* aView ) const = 0;
41 };
42 
43 
45 {
46  COMMAND_LINE( const VECTOR2D& aP0, const VECTOR2D& aP1 ) :
47  m_p0( aP0 ),
48  m_p1( aP1 ) {}
49 
50  virtual void Execute( VIEW* aView ) const override
51  {
52  aView->GetGAL()->DrawLine( m_p0, m_p1 );
53  }
54 
57 };
58 
59 
61 {
62  COMMAND_RECTANGLE( const VECTOR2D& aP0, const VECTOR2D& aP1 ) :
63  m_p0( aP0 ), m_p1( aP1 )
64  {}
65 
66  virtual void Execute( VIEW* aView ) const override
67  {
68  aView->GetGAL()->DrawRectangle( m_p0, m_p1 );
69  }
70 
73 };
74 
75 
77 {
78  COMMAND_CIRCLE( const VECTOR2D& aCenter, double aRadius ) :
79  m_center(aCenter),
80  m_radius(aRadius) {}
81 
82  virtual void Execute( VIEW* aView ) const override
83  {
84  aView->GetGAL()->DrawCircle( m_center, m_radius );
85  }
86 
88  double m_radius;
89 };
90 
91 
93 {
94  COMMAND_ARC( const VECTOR2D& aCenter, double aRadius, double aStartAngle, double aEndAngle ) :
95  m_center( aCenter ),
96  m_radius( aRadius ),
97  m_startAngle( aStartAngle ),
98  m_endAngle( aEndAngle )
99  { }
100 
101  virtual void Execute( VIEW* aView ) const override
102  {
104  }
105 
107  double m_radius;
108  double m_startAngle;
109  double m_endAngle;
110 };
111 
112 
114 {
115  COMMAND_POLYGON( const std::deque<VECTOR2D>& aPointList ) :
116  m_pointList( aPointList ) {}
117 
118  virtual void Execute( VIEW* aView ) const override
119  {
120  aView->GetGAL()->DrawPolygon( m_pointList );
121  }
122 
123  std::deque<VECTOR2D> m_pointList;
124 };
125 
126 
128 {
130  m_polySet( aPolySet ) {}
131 
132  virtual void Execute( VIEW* aView ) const override
133  {
134  aView->GetGAL()->DrawPolygon( m_polySet );
135  }
136 
138 };
139 
140 
142 {
143  COMMAND_POINT_POLYGON( const VECTOR2D aPointList[], int aListSize )
144  {
145  m_pointList.reserve( aListSize );
146 
147  for( int ii = 0; ii < aListSize; ii++ )
148  m_pointList.push_back( aPointList[ii] );
149  }
150 
151  virtual void Execute( VIEW* aView ) const override
152  {
153  aView->GetGAL()->DrawPolygon( &m_pointList[0], (int)m_pointList.size() );
154  }
155 
156  std::vector<VECTOR2D> m_pointList;
157 };
158 
159 
161 {
162  COMMAND_SET_STROKE( bool aIsStroke ) :
163  m_isStroke( aIsStroke ) {}
164 
165  virtual void Execute( VIEW* aView ) const override
166  {
167  aView->GetGAL()->SetIsStroke( m_isStroke );
168  }
169 
171 };
172 
173 
175 {
176  COMMAND_SET_FILL( bool aIsFill ) :
177  m_isFill( aIsFill ) {}
178 
179  virtual void Execute( VIEW* aView ) const override
180  {
181  aView->GetGAL()->SetIsFill( m_isFill );
182  }
183 
184  bool m_isFill;
185 };
186 
187 
189 {
190  COMMAND_SET_COLOR( bool aIsStroke, const COLOR4D& aColor ) :
191  m_isStroke( aIsStroke ),
192  m_color( aColor ) {}
193 
194  virtual void Execute( VIEW* aView ) const override
195  {
196  if( m_isStroke )
197  aView->GetGAL()->SetStrokeColor( m_color );
198  else
199  aView->GetGAL()->SetFillColor( m_color );
200  }
201 
204 };
205 
206 
208 {
209  COMMAND_SET_WIDTH( double aWidth ) :
210  m_width( aWidth ) {}
211 
212  virtual void Execute( VIEW* aView ) const override
213  {
214  aView->GetGAL()->SetLineWidth( m_width );
215  }
216 
217  double m_width;
218 };
219 
220 
222 {
223 }
224 
225 
227 {
228  releaseCommands();
229 }
230 
231 
233 {
234  for( auto cmd : m_commands )
235  delete cmd;
236 
237  m_commands.clear();
238 }
239 
240 
242 {
243  releaseCommands();
244 }
245 
246 
248 {
249  BOX2I maxBox;
250 
251  maxBox.SetMaximum();
252  return maxBox;
253 }
254 
255 
256 void VIEW_OVERLAY::ViewDraw( int aLayer, VIEW* aView ) const
257 {
258  for( const auto& cmd : m_commands )
259  cmd->Execute( aView );
260 }
261 
262 
263 void VIEW_OVERLAY::ViewGetLayers( int aLayers[], int& aCount ) const
264 {
265  aLayers[0] = LAYER_GP_OVERLAY;
266  aCount = 1;
267 }
268 
269 
270 void VIEW_OVERLAY::Line( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
271 {
272  m_commands.push_back( new COMMAND_LINE( aStartPoint, aEndPoint ) );
273 }
274 
275 
276 void VIEW_OVERLAY::Line( const SEG& aSeg )
277 {
278  Line( aSeg.A, aSeg.B );
279 }
280 
281 
282 void VIEW_OVERLAY::Segment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth )
283 {
284  SetLineWidth( aWidth );
285  Line( aStartPoint, aEndPoint );
286 }
287 
288 
289 void VIEW_OVERLAY::Polygon( const SHAPE_POLY_SET& aPolySet )
290 {
291  m_commands.push_back( new COMMAND_POLY_POLYGON( aPolySet ) );
292 }
293 
294 
295 void VIEW_OVERLAY::Polygon( const std::deque<VECTOR2D>& aPointList )
296 {
297  m_commands.push_back( new COMMAND_POLYGON( aPointList ) );
298 }
299 
300 
301 void VIEW_OVERLAY::Polygon( const VECTOR2D aPointList[], int aListSize )
302 {
303  m_commands.push_back( new COMMAND_POINT_POLYGON( aPointList, aListSize ) );
304 }
305 
306 
307 void VIEW_OVERLAY::Circle( const VECTOR2D& aCenterPoint, double aRadius )
308 {
309  m_commands.push_back( new COMMAND_CIRCLE( aCenterPoint, aRadius ) );
310 }
311 
312 
313 void VIEW_OVERLAY::Arc( const VECTOR2D& aCenterPoint,
314  double aRadius, double aStartAngle, double aEndAngle )
315 {
316  m_commands.push_back( new COMMAND_ARC( aCenterPoint, aRadius, aStartAngle, aEndAngle ) );
317 }
318 
319 
320 void VIEW_OVERLAY::Rectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
321 {
322  m_commands.push_back( new COMMAND_RECTANGLE( aStartPoint, aEndPoint ) );
323 }
324 
325 
326 void VIEW_OVERLAY::SetIsFill( bool aIsFillEnabled )
327 {
328  m_commands.push_back( new COMMAND_SET_FILL( aIsFillEnabled ) );
329 }
330 
331 
332 void VIEW_OVERLAY::SetIsStroke( bool aIsStrokeEnabled )
333 {
334  m_commands.push_back( new COMMAND_SET_STROKE( aIsStrokeEnabled ) );
335 }
336 
337 
338 void VIEW_OVERLAY::SetFillColor( const COLOR4D& aColor )
339 {
340  m_commands.push_back( new COMMAND_SET_COLOR( false, aColor ) );
341 }
342 
343 
345 {
346  m_commands.push_back( new COMMAND_SET_COLOR( true, aColor ) );
347 }
348 
349 void VIEW_OVERLAY::SetLineWidth( double aLineWidth )
350 {
351  m_commands.push_back( new COMMAND_SET_WIDTH( aLineWidth ) );
352 }
353 
354 void VIEW_OVERLAY::Cross( const VECTOR2D& aP, int aSize )
355 {
356  Line( aP + VECTOR2D( -aSize, -aSize ), aP + VECTOR2D( aSize, aSize ) );
357  Line( aP + VECTOR2D( aSize, -aSize ), aP + VECTOR2D( -aSize, aSize ) );
358 }
359 
360 
361 } // namespace
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a rectangle.
virtual void Execute(VIEW *aView) const =0
std::vector< COMMAND * > m_commands
Definition: view_overlay.h:99
virtual void Execute(VIEW *aView) const override
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
void Segment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth)
COMMAND_POLY_POLYGON(const SHAPE_POLY_SET &aPolySet)
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle)
Draw an arc.
virtual void Execute(VIEW *aView) const override
void SetFillColor(const COLOR4D &aColor)
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:189
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Return the all the layers within the VIEW the object is painted on.
std::deque< VECTOR2D > m_pointList
virtual void Execute(VIEW *aView) const override
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
void SetIsStroke(bool aIsStrokeEnabled)
void Cross(const VECTOR2D &aP, int aSize)
virtual void SetLineWidth(float aLineWidth)
Set the line width.
COMMAND_CIRCLE(const VECTOR2D &aCenter, double aRadius)
void Line(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
void Arc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle)
void Polygon(const std::deque< VECTOR2D > &aPointList)
VIEW_ITEM class definition.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:629
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
COMMAND_LINE(const VECTOR2D &aP0, const VECTOR2D &aP1)
SHAPE_POLY_SET.
virtual void Execute(VIEW *aView) const override
void SetMaximum()
Definition: box2.h:73
void SetStrokeColor(const COLOR4D &aColor)
COMMAND_RECTANGLE(const VECTOR2D &aP0, const VECTOR2D &aP1)
COMMAND_POINT_POLYGON(const VECTOR2D aPointList[], int aListSize)
void SetIsFill(bool aIsFillEnabled)
Definition: seg.h:39
virtual const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
COMMAND_POLYGON(const std::deque< VECTOR2D > &aPointList)
virtual void Execute(VIEW *aView) const override
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
virtual void Execute(VIEW *aView) const override
virtual void Execute(VIEW *aView) const override
virtual void DrawPolygon(const std::deque< VECTOR2D > &aPointList)
Draw a polygon.
Board layer functions and definitions.
virtual void Execute(VIEW *aView) const override
void SetLineWidth(double aLineWidth)
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius)
Draw a circle using world coordinates.
virtual void Execute(VIEW *aView) const override
VECTOR2I A
Definition: seg.h:47
COMMAND_SET_COLOR(bool aIsStroke, const COLOR4D &aColor)
void Circle(const VECTOR2D &aCenterPoint, double aRadius)
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
virtual void Execute(VIEW *aView) const override
virtual void ViewDraw(int aLayer, VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
COMMAND_ARC(const VECTOR2D &aCenter, double aRadius, double aStartAngle, double aEndAngle)
void Rectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
virtual void Execute(VIEW *aView) const override
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98
VECTOR2I B
Definition: seg.h:48