KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
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
32using namespace KIGFX;
33
34ORIGIN_VIEWITEM::ORIGIN_VIEWITEM( const COLOR4D& aColor, MARKER_STYLE aStyle, int aSize, const VECTOR2D& aPosition ) :
35 EDA_ITEM( nullptr, NOT_USED ), // this item is never added to a BOARD/SCHEMATIC 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 EDA_ITEM( nullptr, NOT_USED ), // this item is never added to a BOARD/SCHEMATIC 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
70void ORIGIN_VIEWITEM::ViewDraw( int, VIEW* aView ) const
71{
72 GAL* 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 BOX2I clip( VECTOR2I( start ), VECTOR2I( 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 = { scaledSize.x, scaledSize.x / 2 };
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 VECTOR2I segStart( KiROUND( start.x ), KiROUND( start.y ) );
120 VECTOR2I 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->DrawCircle( m_end, scaledSize.x / 4 );
131 break;
132 }
133
134 case X:
135 case CIRCLE_X:
136 gal->DrawLine( m_position - scaledSize, m_position + scaledSize );
137 scaledSize.y = -scaledSize.y;
138 gal->DrawLine( m_position - scaledSize, m_position + scaledSize );
139 break;
140
141 case DOT:
142 case CIRCLE_DOT:
143 gal->DrawCircle( m_position, scaledSize.x / 4 );
144 break;
145 }
146}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
constexpr void SetMaximum()
Definition: box2.h:80
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition: box2.h:146
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:127
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Abstract interface for drawing on a 2D-surface.
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius)
Draw a circle using world coordinates.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
ORIGIN_VIEWITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
COLOR4D m_color
Marker symbol.
VECTOR2D m_position
< Marker coordinates.
void ViewDraw(int aLayer, VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
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))
MARKER_STYLE m_style
If set, the marker will be drawn even if its position is 0,0.
VECTOR2D m_end
Marker size (in pixels).
MARKER_STYLE
< Marker symbol styles
int m_size
Marker color.
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:199
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:459
@ UNSPECIFIED_COLOR
Definition: color4d.h:43
std::uint32_t EDA_ITEM_FLAGS
a few functions useful in geometry calculations.
bool ClipLine(const BOX2I *aClipBox, int &x1, int &y1, int &x2, int &y2)
Test if any part of a line falls within the bounds of a rectangle.
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
CITER next(CITER it)
Definition: ptree.cpp:126
@ NOT_USED
the 3d code uses this value
Definition: typeinfo.h:79
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690