KiCad PCB EDA Suite
marker_base.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) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
33 #include "fctsys.h"
34 #include "base_screen.h"
35 #include "common.h"
36 #include "macros.h"
37 #include "marker_base.h"
40 
41 
42 /* The graphic shape of markers is a polygon.
43  * MarkerShapeCorners contains the coordinates of corners of the polygonal default shape
44  * they are arbitrary units to make coding shape easy.
45  * internal units coordinates are these values scaled by .m_ScalingFactor
46  */
47 static const VECTOR2I MarkerShapeCorners[] =
48 {
49  VECTOR2I( 0, 0 ),
50  VECTOR2I( 8, 1 ),
51  VECTOR2I( 4, 3 ),
52  VECTOR2I( 13, 8 ),
53  VECTOR2I( 9, 9 ),
54  VECTOR2I( 8, 13 ),
55  VECTOR2I( 3, 4 ),
56  VECTOR2I( 1, 8 ),
57  VECTOR2I( 0, 0 )
58 };
60 
61 /*******************/
62 /* Classe MARKER_BASE */
63 /*******************/
64 
66 {
69  m_Color = RED;
70  const VECTOR2I* point_shape = GetShapePolygon();
71  wxPoint start( point_shape->x, point_shape->y );
72  wxPoint end = start;
73 
74  for( int ii = 1; ii < GetShapePolygonCornerCount(); ii++ )
75  {
76  ++point_shape;
77  start.x = std::min( start.x, point_shape->x);
78  start.y = std::min( start.y, point_shape->y);
79  end.x = std::max( end.x, point_shape->x);
80  end.y = std::max( end.y, point_shape->y);
81  }
82 
85 }
86 
87 
89 {
90  m_Pos = aMarker.m_Pos;
91  m_ErrorLevel = aMarker.m_ErrorLevel;
92  m_MarkerType = aMarker.m_MarkerType;
93  m_Color = aMarker.m_Color;
96 }
97 
98 
99 MARKER_BASE::MARKER_BASE( int aScalingFactor )
100 {
101  m_ScalingFactor = aScalingFactor;
102  init();
103 }
104 
105 
106 MARKER_BASE::MARKER_BASE( EDA_UNITS aUnits, int aErrorCode, const wxPoint& aMarkerPos,
107  EDA_ITEM* aItem, const wxPoint& aPos, EDA_ITEM* bItem, const wxPoint& bPos,
108  int aScalingFactor )
109 {
110  m_ScalingFactor = aScalingFactor;
111  init();
112 
113  SetData( aUnits, aErrorCode, aMarkerPos, aItem, aPos, bItem, bPos );
114 }
115 
116 
117 MARKER_BASE::MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos,
118  const wxString& aText, const wxPoint& aPos,
119  const wxString& bText, const wxPoint& bPos, int aScalingFactor )
120 {
121  m_ScalingFactor = aScalingFactor;
122  init();
123 
124  SetData( aErrorCode, aMarkerPos, aText, aPos, bText, bPos );
125 }
126 
127 
128 MARKER_BASE::MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos,
129  const wxString& aText, const wxPoint& aPos, int aScalingFactor )
130 {
131  m_ScalingFactor = aScalingFactor;
132  init();
133 
134  SetData( aErrorCode, aMarkerPos, aText, aPos );
135 }
136 
137 
139 {
140 }
141 
142 
143 void MARKER_BASE::SetData( EDA_UNITS aUnits, int aErrorCode, const wxPoint& aMarkerPos,
144  EDA_ITEM* aItem, const wxPoint& aPos, EDA_ITEM* bItem, const wxPoint& bPos )
145 {
146  m_Pos = aMarkerPos;
147  m_drc.SetData( aUnits, aErrorCode, aItem, aPos, bItem, bPos );
148  m_drc.SetParent( this );
149 }
150 
151 
152 void MARKER_BASE::SetData( int aErrorCode, const wxPoint& aMarkerPos,
153  const wxString& aText, const wxPoint& aPos,
154  const wxString& bText, const wxPoint& bPos )
155 {
156  m_Pos = aMarkerPos;
157  m_drc.SetData( aErrorCode, aText, aPos, bText, bPos );
158  m_drc.SetParent( this );
159 }
160 
161 
162 bool MARKER_BASE::HitTestMarker( const wxPoint& aHitPosition, int aAccuracy ) const
163 {
165  bbox.Inflate( aAccuracy );
166 
167  // Fast hit test using boundary box. A finer test will be made if requested
168  bool hit = bbox.Contains( aHitPosition );
169 
170  if( hit ) // Fine test
171  {
172  SHAPE_LINE_CHAIN polygon;
173  ShapeToPolygon( polygon );
174  VECTOR2I rel_pos( aHitPosition - m_Pos );
175  hit = polygon.PointInside( rel_pos, aAccuracy );
176  }
177 
178  return hit;
179 }
180 
181 
183 {
184  // Build the marker shape polygon in internal units:
185  const int ccount = GetShapePolygonCornerCount();
186 
187  for( int ii = 0; ii < ccount; ii++ )
188  aPolygon.Append( GetShapePolygonCorner( ii ) * MarkerScale() );
189 
190  // Be sure aPolygon is seen as a closed polyline:
191  aPolygon.SetClosed( true );
192 }
193 
194 
196 {
197  return MarkerShapeCorners;
198 }
199 
200 
202 {
203  return MarkerShapeCorners[aIdx];
204 }
205 
206 
208 {
209  return CORNERS_COUNT;
210 }
211 
212 
214 {
215  wxSize size_iu = m_ShapeBoundingBox.GetSize();
216  wxPoint position_iu = m_ShapeBoundingBox.GetPosition();
217  size_iu.x *= m_ScalingFactor;
218  size_iu.y *= m_ScalingFactor;
219  position_iu.x *= m_ScalingFactor;
220  position_iu.y *= m_ScalingFactor;
221  position_iu += m_Pos;
222 
223  return EDA_RECT( position_iu, size_iu );
224 }
225 
226 
227 
229 {
230  wxString msg = m_drc.ShowHtml( aFrame->GetUserUnits() );
231  DIALOG_DISPLAY_HTML_TEXT_BASE infodisplay( (wxWindow*)aFrame, wxID_ANY, _( "Marker Info" ),
232  wxGetMousePosition(), wxSize( 550, 140 ) );
233 
234  infodisplay.m_htmlWindow->SetPage( msg );
235  infodisplay.ShowModal();
236 }
237 
238 
239 void MARKER_BASE::PrintMarker( wxDC* aDC, const wxPoint& aOffset )
240 {
241  // Build the marker shape polygon in internal units:
242  const int ccount = GetShapePolygonCornerCount();
243  std::vector<wxPoint> shape;
244  shape.reserve( ccount );
245 
246  for( int ii = 0; ii < ccount; ii++ )
247  {
248  shape.emplace_back( GetShapePolygonCorner( ii ).x * MarkerScale(),
249  GetShapePolygonCorner( ii ).y * MarkerScale() );
250  }
251 
252  for( int ii = 0; ii < ccount; ii++ )
253  shape[ii] += m_Pos + aOffset;
254 
255  GRClosedPoly( nullptr, aDC, ccount, &shape[0], true, 0, m_Color, m_Color );
256 }
EDA_UNITS
Definition: common.h:72
MARKER_SEVERITY m_ErrorLevel
Specify the severity of the error.
Definition: marker_base.h:60
void SetData(EDA_UNITS aUnits, int aErrorCode, EDA_ITEM *aMainItem, const wxPoint &aMainPos, EDA_ITEM *bAuxiliaryItem=nullptr, const wxPoint &bAuxiliaryPos=wxPoint())
Function SetData initialize all data in item.
Definition: drc_item.h:103
int GetShapePolygonCornerCount() const
wxPoint m_Pos
position of the marker
Definition: marker_base.h:54
void SetParent(MARKER_BASE *aMarker)
Definition: drc_item.h:163
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
const VECTOR2I * GetShapePolygon() const
The base class for create windows for drawing purpose.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
MARKER_BASE(int aScalingFactor)
Definition: marker_base.cpp:99
bool Contains(const wxPoint &aPoint) const
Function Contains.
TYPEMARKER m_MarkerType
The type of marker (useful to filter markers)
Definition: marker_base.h:59
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
This file contains miscellaneous commonly used macros and functions.
EDA_RECT GetBoundingBoxMarker() const
Function GetBoundingBoxMarker returns the orthogonal, bounding box of this object for display purpose...
void SetClosed(bool aClosed)
Function SetClosed()
void DisplayMarkerInfo(EDA_DRAW_FRAME *aFrame)
Function DisplayMarkerInfo displays the full info of this marker, in a HTML window.
bool HitTestMarker(const wxPoint &aHitPosition, int aAccuracy) const
Tests if the given wxPoint is within the bounds of this object.
Class DIALOG_DISPLAY_HTML_TEXT_BASE.
const unsigned CORNERS_COUNT
Definition: marker_base.cpp:59
void SetEnd(int x, int y)
Definition: eda_rect.h:192
const wxPoint GetPosition() const
Definition: eda_rect.h:115
void ShapeToPolygon(SHAPE_LINE_CHAIN &aPolygon) const
Returns the shape polygon in internal units in a SHAPE_LINE_CHAIN the coordinates are relatives to th...
COLOR4D m_Color
color
Definition: marker_base.h:61
int m_ScalingFactor
Scaling factor to convert corners coordinates to internat units coordinates.
Definition: marker_base.h:57
Definition: colors.h:60
EDA_RECT m_ShapeBoundingBox
Bounding box of the graphic symbol, relative to the position of the shape, in marker shape units.
Definition: marker_base.h:62
wxString ShowHtml(EDA_UNITS aUnits) const
Function ShowHtml translates this object into a fragment of HTML suitable for the wxWidget's wxHtmlLi...
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:108
int MarkerScale() const
The scaling factor to convert polygonal shape coordinates to internal units.
Definition: marker_base.h:122
void PrintMarker(wxDC *aDC, const wxPoint &aOffset)
Function PrintMarker Prints the shape is the polygon defined in m_Corners (array of wxPoints).
#define _(s)
Definition: 3d_actions.cpp:31
SHAPE_LINE_CHAIN.
const VECTOR2I & GetShapePolygonCorner(int aIdx) const
static const VECTOR2I MarkerShapeCorners[]
Definition: marker_base.cpp:47
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:163
The common library.
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Function PointInside()
void SetData(EDA_UNITS aUnits, int aErrorCode, const wxPoint &aMarkerPos, EDA_ITEM *aItem, const wxPoint &aPos, EDA_ITEM *bItem=nullptr, const wxPoint &bPos=wxPoint())
Function SetData fills in all the reportable data associated with a MARKER.
BASE_SCREEN class implementation.
void GRClosedPoly(EDA_RECT *ClipBox, wxDC *DC, int n, const wxPoint *Points, bool Fill, COLOR4D Color, COLOR4D BgColor)
Function GRClosedPoly draws a closed polygon onto the drawing context aDC and optionally fills and/or...
Definition: gr_basic.cpp:552
DRC_ITEM m_drc
Definition: marker_base.h:64
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
const wxSize GetSize() const
Definition: eda_rect.h:103