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, [email protected]
5  * Copyright (C) 1992-2021 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 #include <wx/log.h>
34 
35 #include <wx/fdrepdlg.h>
36 
37 EDA_ITEM::EDA_ITEM( EDA_ITEM* parent, KICAD_T idType ) :
38  m_status( 0 ),
39  m_parent( parent ),
40  m_forceVisible( false ),
41  m_flags( 0 ),
42  m_structType( idType )
43 { }
44 
45 
47  m_status( 0 ),
48  m_parent( nullptr ),
49  m_forceVisible( false ),
50  m_flags( 0 ),
51  m_structType( idType )
52 { }
53 
54 
55 EDA_ITEM::EDA_ITEM( const EDA_ITEM& base ) :
56  m_Uuid( base.m_Uuid ),
57  m_status( base.m_status ),
58  m_parent( base.m_parent ),
59  m_forceVisible( base.m_forceVisible ),
60  m_flags( base.m_flags ),
61  m_structType( base.m_structType )
62 { }
63 
64 
66 {
68 
69  // If this a child object, then the parent modification state also needs to be set.
70  if( m_parent )
72 }
73 
74 
76 {
77  // return a zero-sized box per default. derived classes should override
78  // this
79  return EDA_RECT( wxPoint( 0, 0 ), wxSize( 0, 0 ) );
80 }
81 
82 
84 {
85  wxCHECK_MSG( false, nullptr, wxT( "Clone not implemented in derived class " ) + GetClass() +
86  wxT( ". Bad programmer!" ) );
87 }
88 
89 
90 // see base_struct.h
91 // many classes inherit this method, be careful:
92 //TODO (snh): Fix this to use std::set instead of C-style vector
93 SEARCH_RESULT EDA_ITEM::Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] )
94 {
95 #if 0 && defined(DEBUG)
96  std::cout << GetClass().mb_str() << ' ';
97 #endif
98 
99  if( IsType( scanTypes ) )
100  {
101  if( SEARCH_RESULT::QUIT == inspector( this, testData ) )
102  return SEARCH_RESULT::QUIT;
103  }
104 
106 }
107 
108 
109 wxString EDA_ITEM::GetSelectMenuText( EDA_UNITS aUnits ) const
110 {
111  wxFAIL_MSG( wxT( "GetSelectMenuText() was not overridden for schematic item type " ) +
112  GetClass() );
113 
114  return wxString( wxT( "Undefined menu text for " ) + GetClass() );
115 }
116 
117 
118 bool EDA_ITEM::Matches( const wxString& aText, const wxFindReplaceData& aSearchData ) const
119 {
120  wxString text = aText;
121  int flags = aSearchData.GetFlags();
122  wxString searchText = aSearchData.GetFindString();
123 
124  // Don't match if searching for replaceable item and the item doesn't support text replace.
125  if( ( flags & FR_SEARCH_REPLACE ) && !IsReplaceable() )
126  return false;
127 
128  if( !( flags & wxFR_MATCHCASE ) )
129  {
130  text.MakeUpper();
131  searchText.MakeUpper();
132  }
133 
134  if( flags & wxFR_WHOLEWORD )
135  {
136  int ii = 0;
137 
138  while( ii < (int) text.length() )
139  {
140  int next = text.find( searchText, ii );
141 
142  if( next == wxNOT_FOUND )
143  return false;
144 
145  ii = next;
146  next += searchText.length();
147 
148  bool startOK = ( ii == 0 || !wxIsalnum( text.GetChar( ii - 1 ) ) );
149  bool endOK = ( next == (int) text.length() || !wxIsalnum( text.GetChar( next ) ) );
150 
151  if( startOK && endOK )
152  return true;
153  else
154  ii++;
155  }
156 
157  return false;
158  }
159  else if( flags & FR_MATCH_WILDCARD )
160  {
161  return text.Matches( searchText );
162  }
163  else
164  {
165  return text.Find( searchText ) != wxNOT_FOUND;
166  }
167 }
168 
169 
170 bool EDA_ITEM::Replace( const wxFindReplaceData& aSearchData, wxString& aText )
171 {
172  wxString text = aText;
173  int flags = aSearchData.GetFlags();
174  wxString searchText = aSearchData.GetFindString();
175  wxString result;
176  bool replaced = false;
177 
178  if( flags & wxFR_MATCHCASE )
179  {
180  text = text.Upper();
181  searchText = searchText.Upper();
182  }
183 
184  int ii = 0;
185 
186  while( ii < (int) text.length() )
187  {
188  int next = text.find( searchText, ii );
189 
190  if( next == wxNOT_FOUND )
191  {
192  result += aText.Mid( ii, wxString::npos );
193  break;
194  }
195 
196  if( next > ii )
197  result += aText.Mid( ii, next - ii );
198 
199  ii = next;
200  next += searchText.length();
201 
202  bool startOK;
203  bool endOK;
204 
205  if( flags & wxFR_WHOLEWORD )
206  {
207  startOK = ( ii == 0 || !wxIsalnum( text.GetChar( ii - 1 ) ) );
208  endOK = ( next == (int) text.length() || !wxIsalnum( text.GetChar( next ) ) );
209  }
210  else
211  {
212  startOK = true;
213  endOK = true;
214  }
215 
216  if( startOK && endOK )
217  {
218  result += aSearchData.GetReplaceString();
219  replaced = true;
220  ii = next;
221  }
222  else
223  {
224  result += aText.GetChar( ii );
225  ii++;
226  }
227  }
228 
229  aText = result;
230  return replaced;
231 }
232 
233 
234 bool EDA_ITEM::operator<( const EDA_ITEM& aItem ) const
235 {
236  wxFAIL_MSG( wxString::Format( wxT( "Less than operator not defined for item type %s." ),
237  GetClass() ) );
238 
239  return false;
240 }
241 
242 
244 {
245  // do not call initVars()
246 
247  m_structType = aItem.m_structType;
248  m_flags = aItem.m_flags;
249  m_status = aItem.m_status;
250  m_parent = aItem.m_parent;
252 
253  return *this;
254 }
255 
256 
258 {
259  // Basic fallback
260  EDA_RECT bbox = GetBoundingBox();
261 
262  return BOX2I( bbox.GetOrigin(), bbox.GetSize() );
263 }
264 
265 
266 void EDA_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
267 {
268  // Basic fallback
269  aCount = 1;
270  aLayers[0] = 0;
271 }
272 
273 
275 {
276  return BITMAPS::dummy_item;
277 }
278 
279 
280 #if defined( DEBUG )
281 
282 void EDA_ITEM::ShowDummy( std::ostream& os ) const
283 {
284  // XML output:
285  wxString s = GetClass();
286 
287  os << '<' << s.Lower().mb_str() << ">"
288  << " Need ::Show() override for this class "
289  << "</" << s.Lower().mb_str() << ">\n";
290 }
291 
292 
293 std::ostream& EDA_ITEM::NestedSpace( int nestLevel, std::ostream& os )
294 {
295  for( int i = 0; i<nestLevel; ++i )
296  os << " ";
297 
298  // number of spaces here controls indent per nest level
299 
300  return os;
301 }
302 
303 #endif
304 
305 
306 static struct EDA_ITEM_DESC
307 {
309  {
311  .Undefined( TYPE_NOT_INIT )
312  .Map( NOT_USED, wxT( "<not used>" ) )
313  .Map( SCREEN_T, _HKI( "Screen" ) )
314 
315  .Map( PCB_FOOTPRINT_T, _HKI( "Footprint" ) )
316  .Map( PCB_PAD_T, _HKI( "Pad" ) )
317  .Map( PCB_SHAPE_T, _HKI( "Graphic" ) )
318  .Map( PCB_TEXT_T, _HKI( "Text" ) )
319  .Map( PCB_FP_TEXT_T, _HKI( "Text" ) )
320  .Map( PCB_FP_SHAPE_T, _HKI( "Graphic" ) )
321  .Map( PCB_FP_ZONE_T, _HKI( "Zone" ) )
322  .Map( PCB_TRACE_T, _HKI( "Track" ) )
323  .Map( PCB_ARC_T, _HKI( "Track" ) )
324  .Map( PCB_VIA_T, _HKI( "Via" ) )
325  .Map( PCB_MARKER_T, _HKI( "Marker" ) )
326  .Map( PCB_DIM_ALIGNED_T, _HKI( "Dimension" ) )
327  .Map( PCB_DIM_ORTHOGONAL_T, _HKI( "Dimension" ) )
328  .Map( PCB_DIM_CENTER_T, _HKI( "Dimension" ) )
329  .Map( PCB_DIM_LEADER_T, _HKI( "Leader" ) )
330  .Map( PCB_TARGET_T, _HKI( "Target" ) )
331  .Map( PCB_ZONE_T, _HKI( "Zone" ) )
332  .Map( PCB_ITEM_LIST_T, _HKI( "ItemList" ) )
333  .Map( PCB_NETINFO_T, _HKI( "NetInfo" ) )
334  .Map( PCB_GROUP_T, _HKI( "Group" ) )
335 
336  .Map( SCH_MARKER_T, _HKI( "Marker" ) )
337  .Map( SCH_JUNCTION_T, _HKI( "Junction" ) )
338  .Map( SCH_NO_CONNECT_T, _HKI( "No-Connect Flag" ) )
339  .Map( SCH_BUS_WIRE_ENTRY_T, _HKI( "Wire Entry" ) )
340  .Map( SCH_BUS_BUS_ENTRY_T, _HKI( "Bus Entry" ) )
341  .Map( SCH_LINE_T, _HKI( "Line" ) )
342  .Map( SCH_BITMAP_T, _HKI( "Bitmap" ) )
343  .Map( SCH_TEXT_T, _HKI( "Text" ) )
344  .Map( SCH_LABEL_T, _HKI( "Net Label" ) )
345  .Map( SCH_GLOBAL_LABEL_T, _HKI( "Global Label" ) )
346  .Map( SCH_HIER_LABEL_T, _HKI( "Hierarchical Label" ) )
347  .Map( SCH_FIELD_T, _HKI( "Field" ) )
348  .Map( SCH_SYMBOL_T, _HKI( "Symbol" ) )
349  .Map( SCH_PIN_T, _HKI( "Pin" ) )
350  .Map( SCH_SHEET_PIN_T, _HKI( "Sheet Pin" ) )
351  .Map( SCH_SHEET_T, _HKI( "Sheet" ) )
352 
353  // Synthetic search tokens don't need to be included...
354  //.Map( SCH_FIELD_LOCATE_REFERENCE_T, _HKI( "Field Locate Reference" ) )
355  //.Map( SCH_FIELD_LOCATE_VALUE_T, _HKI( "Field Locate Value" ) )
356  //.Map( SCH_FIELD_LOCATE_FOOTPRINT_T, _HKI( "Field Locate Footprint" ) )
357 
358  .Map( SCH_SCREEN_T, _HKI( "SCH Screen" ) )
359 
360  .Map( LIB_SYMBOL_T, _HKI( "Symbol" ) )
361  .Map( LIB_ALIAS_T, _HKI( "Alias" ) )
362  .Map( LIB_SHAPE_T, _HKI( "Graphic" ) )
363  .Map( LIB_TEXT_T, _HKI( "Text" ) )
364  .Map( LIB_PIN_T, _HKI( "Pin" ) )
365  .Map( LIB_FIELD_T, _HKI( "Symbol Field" ) )
366 
367  .Map( GERBER_LAYOUT_T, _HKI( "Gerber Layout" ) )
368  .Map( GERBER_DRAW_ITEM_T, _HKI( "Draw Item" ) )
369  .Map( GERBER_IMAGE_T, _HKI( "Image" ) );
370 
373  propMgr.AddProperty( new PROPERTY_ENUM<EDA_ITEM, KICAD_T>( "Type",
375  }
377 
CITER next(CITER it)
Definition: ptree.cpp:126
#define IS_CHANGED
Item was edited, and modified.
void SetModified()
Definition: eda_item.cpp:65
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:478
virtual SEARCH_RESULT Visit(INSPECTOR inspector, void *testData, const KICAD_T scanTypes[])
May be re-implemented for each derived class in order to handle all the types given by its member dat...
Definition: eda_item.cpp:93
BOX2< VECTOR2I > BOX2I
Definition: box2.h:506
virtual bool Matches(const wxFindReplaceData &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:364
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:65
bool operator<(const EDA_ITEM &aItem) const
Test if another item is less than this object.
Definition: eda_item.cpp:234
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:100
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:101
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:108
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:102
the 3d code uses this value
Definition: typeinfo.h:79
static ENUM_MAP< T > & Instance()
Definition: property.h:510
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:152
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:97
ENUM_TO_WXANY(KICAD_T)
EDA_ITEM_FLAGS m_status
Definition: eda_item.h:477
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
class PAD, a pad in a footprint
Definition: typeinfo.h:89
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:480
#define NO_SETTER(owner, type)
Definition: property.h:621
#define REGISTER_TYPE(x)
Definition: property_mgr.h:248
const INSPECTOR_FUNC & INSPECTOR
Definition: eda_item.h:93
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:83
not really an item, used to identify a screen
Definition: typeinfo.h:85
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Return the all the layers within the VIEW the object is painted on.
Definition: eda_item.cpp:266
const wxPoint GetOrigin() const
Definition: eda_rect.h:110
class BOARD_ITEM_LIST, a list of board items
Definition: typeinfo.h:106
wxLogTrace helper definitions.
virtual bool IsType(const KICAD_T aScanTypes[]) const
Check whether the item is one of the listed types.
Definition: eda_item.h:182
class ZONE, a copper pour area
Definition: typeinfo.h:105
static bool Replace(const wxFindReplaceData &aSearchData, wxString &aText)
Perform a text replace on aText using the find and replace criteria in aSearchData on items that supp...
Definition: eda_item.cpp:170
EDA_ITEM & operator=(const EDA_ITEM &aItem)
Assign the members of aItem to another object.
Definition: eda_item.cpp:243
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType)
Definition: eda_item.cpp:37
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:104
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
Some functions to handle hotkeys in KiCad.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
EDA_UNITS
Definition: eda_units.h:38
class PCB_MARKER, a marker used to show something
Definition: typeinfo.h:98
virtual wxString GetClass() const =0
Return the class name.
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
virtual wxString GetSelectMenuText(EDA_UNITS aUnits) const
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: eda_item.cpp:109
virtual BITMAPS GetMenuImage() const
Return a pointer to an image to be used in menus.
Definition: eda_item.cpp:274
class NETINFO_ITEM, a description of a net
Definition: typeinfo.h:107
void AddProperty(PROPERTY_BASE *aProperty)
Register a property.
class ZONE, managed by a footprint
Definition: typeinfo.h:94
virtual bool IsReplaceable() const
Override this method in any derived object that supports test find and replace.
Definition: eda_item.h:402
Handle the component boundary box.
Definition: eda_rect.h:42
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
KICAD_T m_structType
Run time identification, keep private so it can never be changed after a ctor sets it.
Definition: eda_item.h:487
static struct EDA_ITEM_DESC _EDA_ITEM_DESC
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:62
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:103
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
virtual const EDA_RECT GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition: eda_item.cpp:75
virtual const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Definition: eda_item.cpp:257
SEARCH_RESULT
Definition: eda_item.h:41
bool m_forceVisible
Definition: eda_item.h:479
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
#define _HKI(x)
const wxSize GetSize() const
Definition: eda_rect.h:100
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112