KiCad PCB EDA Suite
origin_viewitem.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) 2015 CERN
5  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
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 <array>
27 #include <eda_draw_frame.h>
30 #include <origin_viewitem.h>
31 
32 using namespace KIGFX;
33 
34 ORIGIN_VIEWITEM::ORIGIN_VIEWITEM( const COLOR4D& aColor, MARKER_STYLE aStyle, int aSize, const VECTOR2D& aPosition ) :
35  BOARD_ITEM( nullptr, NOT_USED ), // this item is never added to a BOARD so it needs no type
36  m_position( aPosition ),
37  m_size( aSize ),
38  m_color( aColor ),
39  m_style( aStyle ),
40  m_drawAtZero( false )
41 {
42 }
43 
44 
46  BOARD_ITEM( nullptr, NOT_USED ), // this item is never added to a BOARD so it needs no type
47  m_position( aPosition ),
48  m_size( NOT_USED ),
49  m_color( UNSPECIFIED_COLOR ),
50  m_style( NO_GRAPHIC ),
51  m_drawAtZero( false )
52 {
53  SetFlags( flags );
54 }
55 
56 
58 {
60 }
61 
62 
64 {
65  BOX2I bbox;
66  bbox.SetMaximum();
67  return bbox;
68 }
69 
70 void ORIGIN_VIEWITEM::ViewDraw( int, VIEW* aView ) const
71 {
72  auto gal = aView->GetGAL();
73 
74  // Nothing to do if the target shouldn't be drawn at 0,0 and that's where the target is.
75  if( !m_drawAtZero && ( m_position.x == 0 ) && ( m_position.y == 0 ) )
76  return;
77 
78  gal->SetIsStroke( true );
79  gal->SetIsFill( false );
80  gal->SetLineWidth( 1 );
81  gal->SetStrokeColor( m_color );
82  VECTOR2D scaledSize = aView->ToWorld( VECTOR2D( m_size, m_size ), false );
83 
84  // Draw a circle around the marker's centre point if the style demands it
85  if( ( m_style == CIRCLE_CROSS ) || ( m_style == CIRCLE_DOT ) || ( m_style == CIRCLE_X ) )
86  gal->DrawCircle( m_position, fabs( scaledSize.x ) );
87 
88  switch( m_style )
89  {
90  case NO_GRAPHIC:
91  break;
92 
93  case CROSS:
94  case CIRCLE_CROSS:
95  gal->DrawLine( m_position - VECTOR2D( scaledSize.x, 0 ),
96  m_position + VECTOR2D( scaledSize.x, 0 ) );
97  gal->DrawLine( m_position - VECTOR2D( 0, scaledSize.y ),
98  m_position + VECTOR2D( 0, scaledSize.y ) );
99  break;
100 
101  case DASH_LINE:
102  {
103  gal->DrawCircle( m_position, scaledSize.x / 4 );
104 
105  VECTOR2D start( m_position );
106  VECTOR2D end( m_end );
107  EDA_RECT clip( wxPoint( start ), wxSize( end.x - start.x, end.y - start.y ) );
108  clip.Normalize();
109 
110  double theta = atan2( end.y - start.y, end.x - start.x );
111  std::array<double,2> strokes = { DASH_MARK_LEN( 1 ), DASH_GAP_LEN( 1 ) };
112 
113  for( size_t i = 0; i < 10000; ++i )
114  {
115  VECTOR2D next( start.x + strokes[ i % 2 ] * cos( theta ),
116  start.y + strokes[ i % 2 ] * sin( theta ) );
117 
118  // Drawing each segment can be done rounded to ints.
119  wxPoint segStart( KiROUND( start.x ), KiROUND( start.y ) );
120  wxPoint segEnd( KiROUND( next.x ), KiROUND( next.y ) );
121 
122  if( ClipLine( &clip, segStart.x, segStart.y, segEnd.x, segEnd.y ) )
123  break;
124  else if( i % 2 == 0 )
125  gal->DrawLine( segStart, segEnd );
126 
127  start = next;
128  }
129 
130  gal->DrawLine( m_position, m_end );
131  gal->DrawCircle( m_end, scaledSize.x / 4 );
132  break;
133  }
134 
135  case X:
136  case CIRCLE_X:
137  gal->DrawLine( m_position - scaledSize, m_position + scaledSize );
138  scaledSize.y = -scaledSize.y;
139  gal->DrawLine( m_position - scaledSize, m_position + scaledSize );
140  break;
141 
142  case DOT:
143  case CIRCLE_DOT:
144  gal->DrawCircle( m_position, scaledSize.x / 4 );
145  break;
146  }
147 }
CITER next(CITER it)
Definition: ptree.cpp:130
VECTOR2D m_end
Marker end position for markers that stretch between points
ORIGIN_VIEWITEM(const COLOR4D &aColor=COLOR4D(1.0, 1.0, 1.0, 1.0), MARKER_STYLE aStyle=CIRCLE_X, int aSize=16, const VECTOR2D &aPosition=VECTOR2D(0, 0))
COLOR4D m_color
Marker color.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Function ToWorld() Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:475
the 3d code uses this value
Definition: typeinfo.h:80
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:180
#define DASH_MARK_LEN(aLineWidth)
VECTOR2D m_position
Marker coordinates.
bool ClipLine(const EDA_RECT *aClipBox, int &x1, int &y1, int &x2, int &y2)
Test if any part of a line falls within the bounds of a rectangle.
void ViewDraw(int aLayer, VIEW *aView) const override
Function ViewDraw() Draws the parts of the object belonging to layer aLayer.
MARKER_STYLE m_style
Marker symbol.
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:232
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
void SetMaximum()
Definition: box2.h:72
unsigned STATUS_FLAGS
Definition: base_struct.h:152
const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers.
a few functions useful in geometry calculations.
void Normalize()
Function Normalize ensures that the height ant width are positive.
int m_size
Marker size (in pixels).
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
#define DASH_GAP_LEN(aLineWidth)
bool m_drawAtZero
If set, the marker will be drawn even if its position is 0,0
VIEW.
Definition: view.h:61
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
MARKER_STYLE
Marker symbol styles
ORIGIN_VIEWITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99