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  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <view/view.h>
28 #include <view/view_item.h>
29 #include <view/view_overlay.h>
31 #include <painter.h>
32 
34 #include <geometry/seg.h>
35 
36 namespace KIGFX {
37 
39 {
40  virtual ~COMMAND() {};
41  virtual void Execute( VIEW* aView ) const = 0;
42 };
43 
44 
46 {
47  COMMAND_LINE( const VECTOR2D& aP0, const VECTOR2D& aP1 ) :
48  m_p0( aP0 ),
49  m_p1( aP1 ) {}
50 
51  virtual void Execute( VIEW* aView ) const override
52  {
53  aView->GetGAL()->DrawLine( m_p0, m_p1 );
54  }
55 
58 };
59 
60 
62 {
63  COMMAND_RECTANGLE( const VECTOR2D& aP0, const VECTOR2D& aP1 ) :
64  m_p0( aP0 ), m_p1( aP1 )
65  {}
66 
67  virtual void Execute( VIEW* aView ) const override
68  {
69  aView->GetGAL()->DrawRectangle( m_p0, m_p1 );
70  }
71 
74 };
75 
76 
78 {
79  COMMAND_CIRCLE( const VECTOR2D& aCenter, double aRadius ) :
80  m_center(aCenter),
81  m_radius(aRadius) {}
82 
83  virtual void Execute( VIEW* aView ) const override
84  {
85  aView->GetGAL()->DrawCircle( m_center, m_radius );
86  }
87 
89  double m_radius;
90 };
91 
92 
94 {
95  COMMAND_ARC( const VECTOR2D& aCenter, double aRadius, double aStartAngle, double aEndAngle ) :
96  m_center( aCenter ),
97  m_radius( aRadius ),
98  m_startAngle( aStartAngle ),
99  m_endAngle( aEndAngle )
100  { }
101 
102  virtual void Execute( VIEW* aView ) const override
103  {
105  }
106 
108  double m_radius;
109  double m_startAngle;
110  double m_endAngle;
111 };
112 
113 
115 {
116  COMMAND_POLYGON( const std::deque<VECTOR2D>& aPointList ) :
117  m_pointList( aPointList ) {}
118 
119  virtual void Execute( VIEW* aView ) const override
120  {
121  aView->GetGAL()->DrawPolygon( m_pointList );
122  }
123 
124  std::deque<VECTOR2D> m_pointList;
125 };
126 
127 
129 {
131  m_polySet( aPolySet ) {}
132 
133  virtual void Execute( VIEW* aView ) const override
134  {
135  aView->GetGAL()->DrawPolygon( m_polySet );
136  }
137 
139 };
140 
141 
143 {
144  COMMAND_POINT_POLYGON( const VECTOR2D aPointList[], int aListSize )
145  {
146  m_pointList.reserve( aListSize );
147 
148  for( int ii = 0; ii < aListSize; ii++ )
149  m_pointList.push_back( aPointList[ii] );
150  }
151 
152  virtual void Execute( VIEW* aView ) const override
153  {
154  aView->GetGAL()->DrawPolygon( &m_pointList[0], (int)m_pointList.size() );
155  }
156 
157  std::vector<VECTOR2D> m_pointList;
158 };
159 
160 
162 {
163  COMMAND_SET_STROKE( bool aIsStroke ) :
164  m_isStroke( aIsStroke ) {}
165 
166  virtual void Execute( VIEW* aView ) const override
167  {
168  aView->GetGAL()->SetIsStroke( m_isStroke );
169  }
170 
172 };
173 
174 
176 {
177  COMMAND_SET_FILL( bool aIsFill ) :
178  m_isFill( aIsFill ) {}
179 
180  virtual void Execute( VIEW* aView ) const override
181  {
182  aView->GetGAL()->SetIsFill( m_isFill );
183  }
184 
185  bool m_isFill;
186 };
187 
188 
190 {
191  COMMAND_SET_COLOR( bool aIsStroke, const COLOR4D& aColor ) :
192  m_isStroke( aIsStroke ),
193  m_color( aColor ) {}
194 
195  virtual void Execute( VIEW* aView ) const override
196  {
197  if( m_isStroke )
198  aView->GetGAL()->SetStrokeColor( m_color );
199  else
200  aView->GetGAL()->SetFillColor( m_color );
201  }
202 
205 };
206 
207 
209 {
210  COMMAND_SET_WIDTH( double aWidth ) :
211  m_width( aWidth ) {}
212 
213  virtual void Execute( VIEW* aView ) const override
214  {
215  aView->GetGAL()->SetLineWidth( m_width );
216  }
217 
218  double m_width;
219 };
220 
221 
223 {
224  COMMAND_GLYPH_SIZE( const VECTOR2D aSize ) :
225  m_size( aSize ) {};
226 
227  virtual void Execute( VIEW* aView ) const override
228  {
229  aView->GetGAL()->SetGlyphSize( m_size );
230  }
231 
233 };
234 
235 
237 {
238  COMMAND_BITMAP_TEXT( const wxString& aText, const VECTOR2D& aPosition,
239  double aRotationAngle ) :
240  m_text( aText ),
241  m_pos( aPosition ),
242  m_angle (aRotationAngle) {};
243 
244  virtual void Execute( VIEW* aView ) const override
245  {
246  aView->GetGAL()->BitmapText( m_text, m_pos, m_angle );
247  }
248 
249  wxString m_text;
251  double m_angle;
252 };
253 
255 {
256 }
257 
258 
260 {
261  releaseCommands();
262 }
263 
264 
266 {
267  for( auto cmd : m_commands )
268  delete cmd;
269 
270  m_commands.clear();
271 }
272 
273 
275 {
276  releaseCommands();
277 }
278 
279 
281 {
282  BOX2I maxBox;
283 
284  maxBox.SetMaximum();
285  return maxBox;
286 }
287 
288 
289 void VIEW_OVERLAY::ViewDraw( int aLayer, VIEW* aView ) const
290 {
291  for( const auto& cmd : m_commands )
292  cmd->Execute( aView );
293 }
294 
295 
296 void VIEW_OVERLAY::ViewGetLayers( int aLayers[], int& aCount ) const
297 {
298  aLayers[0] = LAYER_GP_OVERLAY;
299  aCount = 1;
300 }
301 
302 
303 void VIEW_OVERLAY::Line( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
304 {
305  m_commands.push_back( new COMMAND_LINE( aStartPoint, aEndPoint ) );
306 }
307 
308 
309 void VIEW_OVERLAY::Line( const SEG& aSeg )
310 {
311  Line( aSeg.A, aSeg.B );
312 }
313 
314 
315 void VIEW_OVERLAY::Segment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth )
316 {
317  SetLineWidth( aWidth );
318  Line( aStartPoint, aEndPoint );
319 }
320 
321 
322 void VIEW_OVERLAY::Polyline( const SHAPE_LINE_CHAIN& aPolyLine )
323 {
324  SetIsStroke( true );
325  SetIsFill( false );
326  for( int i = 0; i < aPolyLine.SegmentCount(); i++ )
327  {
328  Line( aPolyLine.CSegment( i ) );
329  }
330 }
331 
332 
333 void VIEW_OVERLAY::Polygon( const SHAPE_POLY_SET& aPolySet )
334 {
335  m_commands.push_back( new COMMAND_POLY_POLYGON( aPolySet ) );
336 }
337 
338 
339 void VIEW_OVERLAY::Polygon( const std::deque<VECTOR2D>& aPointList )
340 {
341  m_commands.push_back( new COMMAND_POLYGON( aPointList ) );
342 }
343 
344 
345 void VIEW_OVERLAY::Polygon( const VECTOR2D aPointList[], int aListSize )
346 {
347  m_commands.push_back( new COMMAND_POINT_POLYGON( aPointList, aListSize ) );
348 }
349 
350 
351 void VIEW_OVERLAY::Circle( const VECTOR2D& aCenterPoint, double aRadius )
352 {
353  m_commands.push_back( new COMMAND_CIRCLE( aCenterPoint, aRadius ) );
354 }
355 
356 
357 void VIEW_OVERLAY::Arc( const VECTOR2D& aCenterPoint,
358  double aRadius, double aStartAngle, double aEndAngle )
359 {
360  m_commands.push_back( new COMMAND_ARC( aCenterPoint, aRadius, aStartAngle, aEndAngle ) );
361 }
362 
363 
364 void VIEW_OVERLAY::Rectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
365 {
366  m_commands.push_back( new COMMAND_RECTANGLE( aStartPoint, aEndPoint ) );
367 }
368 
369 
370 void VIEW_OVERLAY::SetIsFill( bool aIsFillEnabled )
371 {
372  m_commands.push_back( new COMMAND_SET_FILL( aIsFillEnabled ) );
373 }
374 
375 
377 {
378  m_commands.push_back( new COMMAND_GLYPH_SIZE( aSize ) );
379 }
380 
381 
382 void VIEW_OVERLAY::BitmapText( const wxString& aText, const VECTOR2D& aPosition,
383  double aRotationAngle )
384 {
385  m_commands.push_back( new COMMAND_BITMAP_TEXT( aText, aPosition, aRotationAngle ) );
386 }
387 
388 
389 void VIEW_OVERLAY::SetIsStroke( bool aIsStrokeEnabled )
390 {
391  m_commands.push_back( new COMMAND_SET_STROKE( aIsStrokeEnabled ) );
392 }
393 
394 
395 void VIEW_OVERLAY::SetFillColor( const COLOR4D& aColor )
396 {
397  m_fillColor = aColor;
398  m_commands.push_back( new COMMAND_SET_COLOR( false, aColor ) );
399 }
400 
401 
403 {
404  m_strokeColor = aColor;
405  m_commands.push_back( new COMMAND_SET_COLOR( true, aColor ) );
406 }
407 
408 void VIEW_OVERLAY::SetLineWidth( double aLineWidth )
409 {
410  m_commands.push_back( new COMMAND_SET_WIDTH( aLineWidth ) );
411 }
412 
413 void VIEW_OVERLAY::Cross( const VECTOR2D& aP, int aSize )
414 {
415  Line( aP + VECTOR2D( -aSize, -aSize ), aP + VECTOR2D( aSize, aSize ) );
416  Line( aP + VECTOR2D( aSize, -aSize ), aP + VECTOR2D( -aSize, aSize ) );
417 }
418 
419 
420 } // 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:112
virtual void Execute(VIEW *aView) const override
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:194
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.
void SetGlyphSize(const VECTOR2D &aSize)
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:57
void SetStrokeColor(const COLOR4D &aColor)
int SegmentCount() const
Return the number of segments in this line chain.
void SetGlyphSize(const VECTOR2D &aSize)
Set the font glyph size.
COMMAND_RECTANGLE(const VECTOR2D &aP0, const VECTOR2D &aP1)
COMMAND_POINT_POLYGON(const VECTOR2D aPointList[], int aListSize)
void SetIsFill(bool aIsFillEnabled)
Definition: seg.h:40
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.
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
Represent a polyline (an zero-thickness chain of connected line segments).
virtual void Execute(VIEW *aView) const override
VECTOR2I A
Definition: seg.h:48
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:103
VECTOR2I B
Definition: seg.h:49