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 
33 #include <layer_ids.h>
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 ),
65  m_p1( aP1 )
66  { }
67 
68  virtual void Execute( VIEW* aView ) const override
69  {
70  aView->GetGAL()->DrawRectangle( m_p0, m_p1 );
71  }
72 
75 };
76 
77 
79 {
80  COMMAND_CIRCLE( const VECTOR2D& aCenter, double aRadius ) :
81  m_center(aCenter),
82  m_radius(aRadius)
83  { }
84 
85  virtual void Execute( VIEW* aView ) const override
86  {
87  aView->GetGAL()->DrawCircle( m_center, m_radius );
88  }
89 
91  double m_radius;
92 };
93 
94 
96 {
97  COMMAND_ARC( const VECTOR2D& aCenter, double aRadius, double aStartAngle, double aEndAngle ) :
98  m_center( aCenter ),
99  m_radius( aRadius ),
100  m_startAngle( aStartAngle ),
101  m_endAngle( aEndAngle )
102  { }
103 
104  virtual void Execute( VIEW* aView ) const override
105  {
107  }
108 
110  double m_radius;
111  double m_startAngle;
112  double m_endAngle;
113 };
114 
115 
117 {
118  COMMAND_POLYGON( const std::deque<VECTOR2D>& aPointList ) :
119  m_pointList( aPointList )
120  { }
121 
122  virtual void Execute( VIEW* aView ) const override
123  {
124  aView->GetGAL()->DrawPolygon( m_pointList );
125  }
126 
127  std::deque<VECTOR2D> m_pointList;
128 };
129 
130 
132 {
134  m_polySet( aPolySet )
135  { }
136 
137  virtual void Execute( VIEW* aView ) const override
138  {
139  aView->GetGAL()->DrawPolygon( m_polySet );
140  }
141 
143 };
144 
145 
147 {
148  COMMAND_POINT_POLYGON( const VECTOR2D aPointList[], int aListSize )
149  {
150  m_pointList.reserve( aListSize );
151 
152  for( int ii = 0; ii < aListSize; ii++ )
153  m_pointList.push_back( aPointList[ii] );
154  }
155 
156  virtual void Execute( VIEW* aView ) const override
157  {
158  aView->GetGAL()->DrawPolygon( &m_pointList[0], (int)m_pointList.size() );
159  }
160 
161  std::vector<VECTOR2D> m_pointList;
162 };
163 
164 
166 {
167  COMMAND_SET_STROKE( bool aIsStroke ) :
168  m_isStroke( aIsStroke )
169  { }
170 
171  virtual void Execute( VIEW* aView ) const override
172  {
173  aView->GetGAL()->SetIsStroke( m_isStroke );
174  }
175 
177 };
178 
179 
181 {
182  COMMAND_SET_FILL( bool aIsFill ) :
183  m_isFill( aIsFill )
184  { }
185 
186  virtual void Execute( VIEW* aView ) const override
187  {
188  aView->GetGAL()->SetIsFill( m_isFill );
189  }
190 
191  bool m_isFill;
192 };
193 
194 
196 {
197  COMMAND_SET_COLOR( bool aIsStroke, const COLOR4D& aColor ) :
198  m_isStroke( aIsStroke ),
199  m_color( aColor )
200  { }
201 
202  virtual void Execute( VIEW* aView ) const override
203  {
204  if( m_isStroke )
205  aView->GetGAL()->SetStrokeColor( m_color );
206  else
207  aView->GetGAL()->SetFillColor( m_color );
208  }
209 
212 };
213 
214 
216 {
217  COMMAND_SET_WIDTH( double aWidth ) :
218  m_width( aWidth )
219  { }
220 
221  virtual void Execute( VIEW* aView ) const override
222  {
223  aView->GetGAL()->SetLineWidth( m_width );
224  }
225 
226  double m_width;
227 };
228 
229 
231 {
232  COMMAND_GLYPH_SIZE( const VECTOR2D aSize ) :
233  m_size( aSize )
234  { };
235 
236  virtual void Execute( VIEW* aView ) const override
237  {
238  aView->GetGAL()->SetGlyphSize( m_size );
239  }
240 
242 };
243 
244 
246 {
247  COMMAND_BITMAP_TEXT( const wxString& aText, const VECTOR2D& aPosition, double aRotationAngle ) :
248  m_text( aText ),
249  m_pos( aPosition ),
250  m_angle (aRotationAngle)
251  { }
252 
253  virtual void Execute( VIEW* aView ) const override
254  {
255  aView->GetGAL()->BitmapText( m_text, m_pos, m_angle );
256  }
257 
258  wxString m_text;
260  double m_angle;
261 };
262 
264 {
265 }
266 
267 
269 {
270  releaseCommands();
271 }
272 
273 
275 {
276  for( VIEW_OVERLAY::COMMAND* cmd : m_commands )
277  delete cmd;
278 
279  m_commands.clear();
280 }
281 
282 
284 {
285  releaseCommands();
286 }
287 
288 
290 {
291  BOX2I maxBox;
292 
293  maxBox.SetMaximum();
294  return maxBox;
295 }
296 
297 
298 void VIEW_OVERLAY::ViewDraw( int aLayer, VIEW* aView ) const
299 {
300  for( const VIEW_OVERLAY::COMMAND* cmd : m_commands )
301  cmd->Execute( aView );
302 }
303 
304 
305 void VIEW_OVERLAY::ViewGetLayers( int aLayers[], int& aCount ) const
306 {
307  aLayers[0] = LAYER_GP_OVERLAY;
308  aCount = 1;
309 }
310 
311 
312 void VIEW_OVERLAY::Line( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
313 {
314  m_commands.push_back( new COMMAND_LINE( aStartPoint, aEndPoint ) );
315 }
316 
317 
318 void VIEW_OVERLAY::Line( const SEG& aSeg )
319 {
320  Line( aSeg.A, aSeg.B );
321 }
322 
323 
324 void VIEW_OVERLAY::Segment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth )
325 {
326  SetLineWidth( aWidth );
327  Line( aStartPoint, aEndPoint );
328 }
329 
330 
331 void VIEW_OVERLAY::Polyline( const SHAPE_LINE_CHAIN& aPolyLine )
332 {
333  SetIsStroke( true );
334  SetIsFill( false );
335 
336  for( int i = 0; i < aPolyLine.SegmentCount(); i++ )
337  Line( aPolyLine.CSegment( i ) );
338 }
339 
340 
341 void VIEW_OVERLAY::Polygon( const SHAPE_POLY_SET& aPolySet )
342 {
343  m_commands.push_back( new COMMAND_POLY_POLYGON( aPolySet ) );
344 }
345 
346 
347 void VIEW_OVERLAY::Polygon( const std::deque<VECTOR2D>& aPointList )
348 {
349  m_commands.push_back( new COMMAND_POLYGON( aPointList ) );
350 }
351 
352 
353 void VIEW_OVERLAY::Polygon( const VECTOR2D aPointList[], int aListSize )
354 {
355  m_commands.push_back( new COMMAND_POINT_POLYGON( aPointList, aListSize ) );
356 }
357 
358 
359 void VIEW_OVERLAY::Circle( const VECTOR2D& aCenterPoint, double aRadius )
360 {
361  m_commands.push_back( new COMMAND_CIRCLE( aCenterPoint, aRadius ) );
362 }
363 
364 
365 void VIEW_OVERLAY::Arc( const VECTOR2D& aCenterPoint, double aRadius, double aStartAngle,
366  double aEndAngle )
367 {
368  m_commands.push_back( new COMMAND_ARC( aCenterPoint, aRadius, aStartAngle, aEndAngle ) );
369 }
370 
371 
372 void VIEW_OVERLAY::Rectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
373 {
374  m_commands.push_back( new COMMAND_RECTANGLE( aStartPoint, aEndPoint ) );
375 }
376 
377 
378 void VIEW_OVERLAY::SetIsFill( bool aIsFillEnabled )
379 {
380  m_commands.push_back( new COMMAND_SET_FILL( aIsFillEnabled ) );
381 }
382 
383 
385 {
386  m_commands.push_back( new COMMAND_GLYPH_SIZE( aSize ) );
387 }
388 
389 
390 void VIEW_OVERLAY::BitmapText( const wxString& aText, const VECTOR2D& aPosition,
391  double aRotationAngle )
392 {
393  m_commands.push_back( new COMMAND_BITMAP_TEXT( aText, aPosition, aRotationAngle ) );
394 }
395 
396 
397 void VIEW_OVERLAY::SetIsStroke( bool aIsStrokeEnabled )
398 {
399  m_commands.push_back( new COMMAND_SET_STROKE( aIsStrokeEnabled ) );
400 }
401 
402 
403 void VIEW_OVERLAY::SetFillColor( const COLOR4D& aColor )
404 {
405  m_fillColor = aColor;
406  m_commands.push_back( new COMMAND_SET_COLOR( false, aColor ) );
407 }
408 
409 
411 {
412  m_strokeColor = aColor;
413  m_commands.push_back( new COMMAND_SET_COLOR( true, aColor ) );
414 }
415 
416 void VIEW_OVERLAY::SetLineWidth( double aLineWidth )
417 {
418  m_commands.push_back( new COMMAND_SET_WIDTH( aLineWidth ) );
419 }
420 
421 void VIEW_OVERLAY::Cross( const VECTOR2D& aP, int aSize )
422 {
423  Line( aP + VECTOR2D( -aSize, -aSize ), aP + VECTOR2D( aSize, aSize ) );
424  Line( aP + VECTOR2D( aSize, -aSize ), aP + VECTOR2D( -aSize, aSize ) );
425 }
426 
427 
428 } // 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:236
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:190
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)
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.
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.
general purpose overlay
Definition: layer_ids.h:214
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:68
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