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  COMMAND_GLYPH_SIZE( const VECTOR2D aSize ) :
224  m_size( aSize ) {};
225 
226  virtual void Execute( VIEW* aView ) const override
227  {
228  aView->GetGAL()->SetGlyphSize( m_size );
229  }
230 
232 };
233 
234 
236 {
237  COMMAND_BITMAP_TEXT( const wxString& aText, const VECTOR2D& aPosition,
238  double aRotationAngle ) :
239  m_text( aText ),
240  m_pos( aPosition ),
241  m_angle (aRotationAngle) {};
242 
243  virtual void Execute( VIEW* aView ) const override
244  {
245  aView->GetGAL()->BitmapText( m_text, m_pos, m_angle );
246  }
247 
248  wxString m_text;
250  double m_angle;
251 };
252 
254 {
255 }
256 
257 
259 {
260  releaseCommands();
261 }
262 
263 
265 {
266  for( auto cmd : m_commands )
267  delete cmd;
268 
269  m_commands.clear();
270 }
271 
272 
274 {
275  releaseCommands();
276 }
277 
278 
280 {
281  BOX2I maxBox;
282 
283  maxBox.SetMaximum();
284  return maxBox;
285 }
286 
287 
288 void VIEW_OVERLAY::ViewDraw( int aLayer, VIEW* aView ) const
289 {
290  for( const auto& cmd : m_commands )
291  cmd->Execute( aView );
292 }
293 
294 
295 void VIEW_OVERLAY::ViewGetLayers( int aLayers[], int& aCount ) const
296 {
297  aLayers[0] = LAYER_GP_OVERLAY;
298  aCount = 1;
299 }
300 
301 
302 void VIEW_OVERLAY::Line( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
303 {
304  m_commands.push_back( new COMMAND_LINE( aStartPoint, aEndPoint ) );
305 }
306 
307 
308 void VIEW_OVERLAY::Line( const SEG& aSeg )
309 {
310  Line( aSeg.A, aSeg.B );
311 }
312 
313 
314 void VIEW_OVERLAY::Segment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth )
315 {
316  SetLineWidth( aWidth );
317  Line( aStartPoint, aEndPoint );
318 }
319 
320 
321 void VIEW_OVERLAY::Polyline( const SHAPE_LINE_CHAIN& aPolyLine )
322 {
323  SetIsStroke( true );
324  SetIsFill( false );
325  for( int i = 0; i < aPolyLine.SegmentCount(); i++ )
326  {
327  Line( aPolyLine.CSegment( i ) );
328  }
329 }
330 
331 
332 void VIEW_OVERLAY::Polygon( const SHAPE_POLY_SET& aPolySet )
333 {
334  m_commands.push_back( new COMMAND_POLY_POLYGON( aPolySet ) );
335 }
336 
337 
338 void VIEW_OVERLAY::Polygon( const std::deque<VECTOR2D>& aPointList )
339 {
340  m_commands.push_back( new COMMAND_POLYGON( aPointList ) );
341 }
342 
343 
344 void VIEW_OVERLAY::Polygon( const VECTOR2D aPointList[], int aListSize )
345 {
346  m_commands.push_back( new COMMAND_POINT_POLYGON( aPointList, aListSize ) );
347 }
348 
349 
350 void VIEW_OVERLAY::Circle( const VECTOR2D& aCenterPoint, double aRadius )
351 {
352  m_commands.push_back( new COMMAND_CIRCLE( aCenterPoint, aRadius ) );
353 }
354 
355 
356 void VIEW_OVERLAY::Arc( const VECTOR2D& aCenterPoint,
357  double aRadius, double aStartAngle, double aEndAngle )
358 {
359  m_commands.push_back( new COMMAND_ARC( aCenterPoint, aRadius, aStartAngle, aEndAngle ) );
360 }
361 
362 
363 void VIEW_OVERLAY::Rectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
364 {
365  m_commands.push_back( new COMMAND_RECTANGLE( aStartPoint, aEndPoint ) );
366 }
367 
368 
369 void VIEW_OVERLAY::SetIsFill( bool aIsFillEnabled )
370 {
371  m_commands.push_back( new COMMAND_SET_FILL( aIsFillEnabled ) );
372 }
373 
374 
376 {
377  m_commands.push_back( new COMMAND_GLYPH_SIZE( aSize ) );
378 }
379 
380 
381 void VIEW_OVERLAY::BitmapText( const wxString& aText, const VECTOR2D& aPosition,
382  double aRotationAngle )
383 {
384  m_commands.push_back( new COMMAND_BITMAP_TEXT( aText, aPosition, aRotationAngle ) );
385 }
386 
387 
388 void VIEW_OVERLAY::SetIsStroke( bool aIsStrokeEnabled )
389 {
390  m_commands.push_back( new COMMAND_SET_STROKE( aIsStrokeEnabled ) );
391 }
392 
393 
394 void VIEW_OVERLAY::SetFillColor( const COLOR4D& aColor )
395 {
396  m_commands.push_back( new COMMAND_SET_COLOR( false, aColor ) );
397 }
398 
399 
401 {
402  m_commands.push_back( new COMMAND_SET_COLOR( true, aColor ) );
403 }
404 
405 void VIEW_OVERLAY::SetLineWidth( double aLineWidth )
406 {
407  m_commands.push_back( new COMMAND_SET_WIDTH( aLineWidth ) );
408 }
409 
410 void VIEW_OVERLAY::Cross( const VECTOR2D& aP, int aSize )
411 {
412  Line( aP + VECTOR2D( -aSize, -aSize ), aP + VECTOR2D( aSize, aSize ) );
413  Line( aP + VECTOR2D( aSize, -aSize ), aP + VECTOR2D( -aSize, aSize ) );
414 }
415 
416 
417 } // namespace
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
void BitmapText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
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:107
virtual void Execute(VIEW *aView) const override
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
void Segment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth)
virtual void BitmapText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draw a text using a bitmap font.
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)
virtual void Execute(VIEW *aView) const override
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:189
virtual void Execute(VIEW *aView) const override
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 Polyline(const SHAPE_LINE_CHAIN &aPolyLine)
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:622
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
COMMAND_LINE(const VECTOR2D &aP0, const VECTOR2D &aP1)
Represent a set of closed polygons.
COMMAND_BITMAP_TEXT(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
virtual void Execute(VIEW *aView) const override
void SetMaximum()
Definition: box2.h:73
void SetStrokeColor(const COLOR4D &aColor)
int SegmentCount() const
Function SegmentCount()
COMMAND_RECTANGLE(const VECTOR2D &aP0, const VECTOR2D &aP1)
COMMAND_POINT_POLYGON(const VECTOR2D aPointList[], int aListSize)
void SetIsFill(bool aIsFillEnabled)
Definition: seg.h:41
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.
void SetGlyphSize(const VECTOR2D aSize)
Set the font glyph size.
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.
const SEG CSegment(int aIndex) const
Function CSegment()
SHAPE_LINE_CHAIN.
virtual void Execute(VIEW *aView) const override
VECTOR2I A
Definition: seg.h:49
COMMAND_SET_COLOR(bool aIsStroke, const COLOR4D &aColor)
void SetGlyphSize(const VECTOR2D aSize)
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:50