KiCad PCB EDA Suite
eda_item.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 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 1992-2020 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 
25 #include <algorithm>
26 
27 #include <bitmaps.h>
28 #include <eda_item.h>
29 #include <eda_rect.h>
30 #include <trace_helpers.h>
31 #include <trigo.h>
32 #include <i18n_utility.h>
33 
34 
35 static const unsigned char dummy_png[] = {
36  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
37  0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0xf3, 0xff,
38  0x61, 0x00, 0x00, 0x00, 0x5f, 0x49, 0x44, 0x41, 0x54, 0x38, 0xcb, 0x63, 0xf8, 0xff, 0xff, 0x3f,
39  0x03, 0x25, 0x98, 0x61, 0x68, 0x1a, 0x00, 0x04, 0x46, 0x40, 0xfc, 0x02, 0x88, 0x45, 0x41, 0x1c,
40  0x76, 0x20, 0xfe, 0x01, 0xc4, 0xbe, 0x24, 0x18, 0x60, 0x01, 0xc4, 0x20, 0x86, 0x04, 0x88, 0xc3,
41  0x01, 0xe5, 0x04, 0x0c, 0xb8, 0x01, 0x37, 0x81, 0xf8, 0x04, 0x91, 0xf8, 0x0a, 0x54, 0x8f, 0x06,
42  0xb2, 0x01, 0x9b, 0x81, 0x78, 0x02, 0x91, 0x78, 0x05, 0x54, 0x8f, 0xca, 0xe0, 0x08, 0x03, 0x36,
43  0xa8, 0xbf, 0xec, 0xc8, 0x32, 0x80, 0xcc, 0x84, 0x04, 0x0a, 0xbc, 0x1d, 0x40, 0x2c, 0xc8, 0x30,
44  0xf4, 0x33, 0x13, 0x00, 0x6b, 0x1a, 0x46, 0x7b, 0x68, 0xe7, 0x0f, 0x0b, 0x00, 0x00, 0x00, 0x00,
45  0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
46 };
47 
48 static const BITMAP_OPAQUE dummy_xpm[1] = {{ dummy_png, sizeof( dummy_png ), "dummy_xpm" }};
49 
50 
51 EDA_ITEM::EDA_ITEM( EDA_ITEM* parent, KICAD_T idType ) :
52  m_structType( idType ),
53  m_status( 0 ),
54  m_parent( parent ),
55  m_forceVisible( false ),
56  m_flags( 0 )
57 { }
58 
59 
61  m_structType( idType ),
62  m_status( 0 ),
63  m_parent( nullptr ),
64  m_forceVisible( false ),
65  m_flags( 0 )
66 { }
67 
68 
69 EDA_ITEM::EDA_ITEM( const EDA_ITEM& base ) :
70  m_Uuid( base.m_Uuid ),
71  m_structType( base.m_structType ),
72  m_status( base.m_status ),
73  m_parent( base.m_parent ),
74  m_forceVisible( base.m_forceVisible ),
75  m_flags( base.m_flags )
76 { }
77 
78 
80 {
82 
83  // If this a child object, then the parent modification state also needs to be set.
84  if( m_parent )
86 }
87 
88 
90 {
91  // return a zero-sized box per default. derived classes should override
92  // this
93  return EDA_RECT( wxPoint( 0, 0 ), wxSize( 0, 0 ) );
94 }
95 
96 
98 {
99  wxCHECK_MSG( false, NULL, wxT( "Clone not implemented in derived class " ) + GetClass() +
100  wxT( ". Bad programmer!" ) );
101 }
102 
103 
104 // see base_struct.h
105 // many classes inherit this method, be careful:
106 //TODO (snh): Fix this to use std::set instead of C-style vector
107 SEARCH_RESULT EDA_ITEM::Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] )
108 {
109 #if 0 && defined(DEBUG)
110  std::cout << GetClass().mb_str() << ' ';
111 #endif
112 
113  if( IsType( scanTypes ) )
114  {
115  if( SEARCH_RESULT::QUIT == inspector( this, testData ) )
116  return SEARCH_RESULT::QUIT;
117  }
118 
120 }
121 
122 
123 wxString EDA_ITEM::GetSelectMenuText( EDA_UNITS aUnits ) const
124 {
125  wxFAIL_MSG( wxT( "GetSelectMenuText() was not overridden for schematic item type " ) +
126  GetClass() );
127 
128  return wxString( wxT( "Undefined menu text for " ) + GetClass() );
129 }
130 
131 
132 bool EDA_ITEM::Matches( const wxString& aText, wxFindReplaceData& aSearchData )
133 {
134  wxString text = aText;
135  wxString searchText = aSearchData.GetFindString();
136 
137  // Don't match if searching for replaceable item and the item doesn't support text replace.
138  if( (aSearchData.GetFlags() & FR_SEARCH_REPLACE) && !IsReplaceable() )
139  return false;
140 
141  if( aSearchData.GetFlags() & wxFR_WHOLEWORD )
142  return aText.IsSameAs( searchText, aSearchData.GetFlags() & wxFR_MATCHCASE );
143 
144  if( aSearchData.GetFlags() & FR_MATCH_WILDCARD )
145  {
146  if( aSearchData.GetFlags() & wxFR_MATCHCASE )
147  return text.Matches( searchText );
148 
149  return text.MakeUpper().Matches( searchText.MakeUpper() );
150  }
151 
152  if( aSearchData.GetFlags() & wxFR_MATCHCASE )
153  return aText.Find( searchText ) != wxNOT_FOUND;
154 
155  return text.MakeUpper().Find( searchText.MakeUpper() ) != wxNOT_FOUND;
156 }
157 
158 
159 bool EDA_ITEM::Replace( wxFindReplaceData& aSearchData, wxString& aText )
160 {
161  wxString searchString = (aSearchData.GetFlags() & wxFR_MATCHCASE) ? aText : aText.Upper();
162 
163  int result = searchString.Find( (aSearchData.GetFlags() & wxFR_MATCHCASE) ?
164  aSearchData.GetFindString() :
165  aSearchData.GetFindString().Upper() );
166 
167  if( result == wxNOT_FOUND )
168  return false;
169 
170  wxString prefix = aText.Left( result );
171  wxString suffix;
172 
173  if( aSearchData.GetFindString().length() + result < aText.length() )
174  suffix = aText.Right( aText.length() - ( aSearchData.GetFindString().length() + result ) );
175 
176  wxLogTrace( traceFindReplace, wxT( "Replacing '%s', prefix '%s', replace '%s', suffix '%s'." ),
177  aText, prefix, aSearchData.GetReplaceString(), suffix );
178 
179  aText = prefix + aSearchData.GetReplaceString() + suffix;
180 
181  return true;
182 }
183 
184 
185 bool EDA_ITEM::operator<( const EDA_ITEM& aItem ) const
186 {
187  wxFAIL_MSG( wxString::Format( wxT( "Less than operator not defined for item type %s." ),
188  GetClass() ) );
189 
190  return false;
191 }
192 
194 {
195  // do not call initVars()
196 
197  m_structType = aItem.m_structType;
198  m_flags = aItem.m_flags;
199  m_status = aItem.m_status;
200  m_parent = aItem.m_parent;
202 
203  return *this;
204 }
205 
207 {
208  // Basic fallback
209  return BOX2I( VECTOR2I( GetBoundingBox().GetOrigin() ),
210  VECTOR2I( GetBoundingBox().GetSize() ) );
211 }
212 
213 
214 void EDA_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
215 {
216  // Basic fallback
217  aCount = 1;
218  aLayers[0] = 0;
219 }
220 
222 {
223  return dummy_xpm;
224 }
225 
226 #if defined(DEBUG)
227 
228 void EDA_ITEM::ShowDummy( std::ostream& os ) const
229 {
230  // XML output:
231  wxString s = GetClass();
232 
233  os << '<' << s.Lower().mb_str() << ">"
234  << " Need ::Show() override for this class "
235  << "</" << s.Lower().mb_str() << ">\n";
236 }
237 
238 
239 std::ostream& EDA_ITEM::NestedSpace( int nestLevel, std::ostream& os )
240 {
241  for( int i = 0; i<nestLevel; ++i )
242  os << " ";
243 
244  // number of spaces here controls indent per nest level
245 
246  return os;
247 }
248 
249 #endif
250 
251 
252 static struct EDA_ITEM_DESC
253 {
255  {
257  .Undefined( TYPE_NOT_INIT )
258  .Map( NOT_USED, wxT( "<not used>" ) )
259  .Map( SCREEN_T, _HKI( "Screen" ) )
260 
261  .Map( PCB_FOOTPRINT_T, _HKI( "Footprint" ) )
262  .Map( PCB_PAD_T, _HKI( "Pad" ) )
263  .Map( PCB_SHAPE_T, _HKI( "Graphic Shape" ) )
264  .Map( PCB_TEXT_T, _HKI( "Board Text" ) )
265  .Map( PCB_FP_TEXT_T, _HKI( "Footprint Text" ) )
266  .Map( PCB_FP_SHAPE_T, _HKI( "Graphic Shape" ) )
267  .Map( PCB_FP_ZONE_T, _HKI( "Zone" ) )
268  .Map( PCB_TRACE_T, _HKI( "Track" ) )
269  .Map( PCB_VIA_T, _HKI( "Via" ) )
270  .Map( PCB_MARKER_T, _HKI( "Board Marker" ) )
271  .Map( PCB_DIM_ALIGNED_T, _HKI( "Aligned Dimension" ) )
272  .Map( PCB_DIM_ORTHOGONAL_T, _HKI( "Orthogonal Dimension" ) )
273  .Map( PCB_DIM_CENTER_T, _HKI( "Center Dimension" ) )
274  .Map( PCB_DIM_LEADER_T, _HKI( "Leader" ) )
275  .Map( PCB_TARGET_T, _HKI( "Target" ) )
276  .Map( PCB_ZONE_T, _HKI( "Zone" ) )
277  .Map( PCB_ITEM_LIST_T, _HKI( "Item List" ) )
278  .Map( PCB_NETINFO_T, _HKI( "Net Info" ) )
279  .Map( PCB_GROUP_T, _HKI( "Group" ) )
280 
281  .Map( SCH_MARKER_T, _HKI( "Schematic Marker" ) )
282  .Map( SCH_JUNCTION_T, _HKI( "Junction" ) )
283  .Map( SCH_NO_CONNECT_T, _HKI( "No-Connect Flag" ) )
284  .Map( SCH_BUS_WIRE_ENTRY_T, _HKI( "Wire Entry" ) )
285  .Map( SCH_BUS_BUS_ENTRY_T, _HKI( "Bus Entry" ) )
286  .Map( SCH_LINE_T, _HKI( "Graphic Line" ) )
287  .Map( SCH_BITMAP_T, _HKI( "Bitmap" ) )
288  .Map( SCH_TEXT_T, _HKI( "Schematic Text" ) )
289  .Map( SCH_LABEL_T, _HKI( "Net Label" ) )
290  .Map( SCH_GLOBAL_LABEL_T, _HKI( "Global Label" ) )
291  .Map( SCH_HIER_LABEL_T, _HKI( "Hierarchical Label" ) )
292  .Map( SCH_FIELD_T, _HKI( "Schematic Field" ) )
293  .Map( SCH_COMPONENT_T, _HKI( "Component" ) )
294  .Map( SCH_SHEET_PIN_T, _HKI( "Sheet Pin" ) )
295  .Map( SCH_SHEET_T, _HKI( "Sheet" ) )
296 
297  // Synthetic search tokens don't need to be included...
298  //.Map( SCH_FIELD_LOCATE_REFERENCE_T, _HKI( "Field Locate Reference" ) )
299  //.Map( SCH_FIELD_LOCATE_VALUE_T, _HKI( "Field Locate Value" ) )
300  //.Map( SCH_FIELD_LOCATE_FOOTPRINT_T, _HKI( "Field Locate Footprint" ) )
301 
302  .Map( SCH_SCREEN_T, _HKI( "SCH Screen" ) )
303 
304  .Map( LIB_PART_T, _HKI( "Symbol" ) )
305  .Map( LIB_ALIAS_T, _HKI( "Alias" ) )
306  .Map( LIB_ARC_T, _HKI( "Arc" ) )
307  .Map( LIB_CIRCLE_T, _HKI( "Circle" ) )
308  .Map( LIB_TEXT_T, _HKI( "Symbol Text" ) )
309  .Map( LIB_RECTANGLE_T, _HKI( "Rectangle" ) )
310  .Map( LIB_POLYLINE_T, _HKI( "Polyline" ) )
311  .Map( LIB_BEZIER_T, _HKI( "Bezier" ) )
312  .Map( LIB_PIN_T, _HKI( "Pin" ) )
313  .Map( LIB_FIELD_T, _HKI( "Symbol Field" ) )
314 
315  .Map( GERBER_LAYOUT_T, _HKI( "Gerber Layout" ) )
316  .Map( GERBER_DRAW_ITEM_T, _HKI( "Draw Item" ) )
317  .Map( GERBER_IMAGE_T, _HKI( "Image" ) );
318 
321  propMgr.AddProperty( new PROPERTY_ENUM<EDA_ITEM, KICAD_T>( "Type",
323  }
325 
void SetModified()
Definition: eda_item.cpp:79
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:162
virtual SEARCH_RESULT Visit(INSPECTOR inspector, void *testData, const KICAD_T scanTypes[])
Function Visit may be re-implemented for each derived class in order to handle all the types given by...
Definition: eda_item.cpp:107
#define IS_CHANGED
std::function passed to nested users by ref, avoids copying std::function
Definition: eda_item.h:102
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:64
bool operator<(const EDA_ITEM &aItem) const
Test if another item is less than this object.
Definition: eda_item.cpp:185
class ALIGNED_DIMENSION, a linear dimension (graphic item)
Definition: typeinfo.h:101
class LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
const wxChar *const traceFindReplace
Flag to enable find and replace debug tracing.
class FP_TEXT, text in a footprint
Definition: typeinfo.h:93
PNG memory record (file in memory).
Definition: bitmap_def.h:29
static bool Replace(wxFindReplaceData &aSearchData, wxString &aText)
Helper function used in search and replace dialog Function Replace performs a text replace on aText u...
Definition: eda_item.cpp:159
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:109
class CENTER_DIMENSION, a center point marking (graphic item)
Definition: typeinfo.h:103
the 3d code uses this value
Definition: typeinfo.h:80
static ENUM_MAP< T > & Instance()
Definition: property.h:519
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
ENUM_TO_WXANY(KICAD_T)
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:94
class PAD, a pad in a footprint
Definition: typeinfo.h:90
static const unsigned char dummy_png[]
Definition: eda_item.cpp:35
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
#define NO_SETTER(owner, type)
Macro to define read-only fields (no setter method available)
Definition: property.h:613
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names
Definition: property_mgr.h:247
const INSPECTOR_FUNC & INSPECTOR
Definition: eda_item.h:94
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
virtual EDA_ITEM * Clone() const
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:97
not really an item, used to identify a screen
Definition: typeinfo.h:86
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
STATUS_FLAGS m_status
Definition: eda_item.h:161
void SetFlags(STATUS_FLAGS aMask)
Definition: eda_item.h:220
#define NULL
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Function ViewGetLayers() Returns the all the layers within the VIEW the object is painted on.
Definition: eda_item.cpp:214
class BOARD_ITEM_LIST, a list of board items
Definition: typeinfo.h:107
static const BITMAP_OPAQUE dummy_xpm[1]
Definition: eda_item.cpp:48
wxLogTrace helper definitions.
virtual bool IsType(const KICAD_T aScanTypes[]) const
Function IsType Checks whether the item is one of the listed types.
Definition: eda_item.h:250
class ZONE, a copper pour area
Definition: typeinfo.h:106
EDA_ITEM & operator=(const EDA_ITEM &aItem)
Operator assignment is used to assign the members of aItem to another object.
Definition: eda_item.cpp:193
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType)
Definition: eda_item.cpp:51
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:105
class FOOTPRINT, a footprint
Definition: typeinfo.h:89
Some functions to handle hotkeys in KiCad.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
EDA_UNITS
Definition: eda_units.h:38
class MARKER_PCB, a marker used to show something
Definition: typeinfo.h:99
virtual wxString GetClass() const =0
Function GetClass returns the class name.
virtual wxString GetSelectMenuText(EDA_UNITS aUnits) const
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
Definition: eda_item.cpp:123
class NETINFO_ITEM, a description of a net
Definition: typeinfo.h:108
void AddProperty(PROPERTY_BASE *aProperty)
Registers a property.
class ZONE, managed by a footprint
Definition: typeinfo.h:95
virtual bool IsReplaceable() const
Function IsReplaceable.
Definition: eda_item.h:490
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: eda_item.h:148
KICAD_T m_structType
Run time identification, keep private so it can never be changed after a ctor sets it.
Definition: eda_item.h:158
static struct EDA_ITEM_DESC _EDA_ITEM_DESC
Provides class metadata.
Definition: property_mgr.h:61
class ORTHOGONAL_DIMENSION, a linear dimension constrained to x/y
Definition: typeinfo.h:104
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
virtual const EDA_RECT GetBoundingBox() const
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Definition: eda_item.cpp:89
STATUS_FLAGS m_flags
Definition: eda_item.h:164
virtual const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers.
Definition: eda_item.cpp:206
SEARCH_RESULT
Definition: eda_item.h:40
bool m_forceVisible
Definition: eda_item.h:163
virtual BITMAP_DEF GetMenuImage() const
Function GetMenuImage returns a pointer to an image to be used in menus.
Definition: eda_item.cpp:221
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:91
#define _HKI(x)
virtual bool Matches(wxFindReplaceData &aSearchData, void *aAuxData)
Function Matches compares the item against the search criteria in aSearchData.
Definition: eda_item.h:446
KICAD_T Type() const
Function Type()
Definition: eda_item.h:181