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  wxString searchText = aSearchData.GetFindString();
122 
123  // Don't match if searching for replaceable item and the item doesn't support text replace.
124  if( (aSearchData.GetFlags() & FR_SEARCH_REPLACE) && !IsReplaceable() )
125  return false;
126 
127  if( aSearchData.GetFlags() & wxFR_WHOLEWORD )
128  return aText.IsSameAs( searchText, aSearchData.GetFlags() & wxFR_MATCHCASE );
129 
130  if( aSearchData.GetFlags() & FR_MATCH_WILDCARD )
131  {
132  if( aSearchData.GetFlags() & wxFR_MATCHCASE )
133  return text.Matches( searchText );
134 
135  return text.MakeUpper().Matches( searchText.MakeUpper() );
136  }
137 
138  if( aSearchData.GetFlags() & wxFR_MATCHCASE )
139  return aText.Find( searchText ) != wxNOT_FOUND;
140 
141  return text.MakeUpper().Find( searchText.MakeUpper() ) != wxNOT_FOUND;
142 }
143 
144 
145 bool EDA_ITEM::Replace( const wxFindReplaceData& aSearchData, wxString& aText )
146 {
147  wxString searchString = (aSearchData.GetFlags() & wxFR_MATCHCASE) ? aText : aText.Upper();
148 
149  int result = searchString.Find( ( aSearchData.GetFlags() & wxFR_MATCHCASE ) ?
150  aSearchData.GetFindString() :
151  aSearchData.GetFindString().Upper() );
152 
153  if( result == wxNOT_FOUND )
154  return false;
155 
156  wxString prefix = aText.Left( result );
157  wxString suffix;
158 
159  if( aSearchData.GetFindString().length() + result < aText.length() )
160  suffix = aText.Right( aText.length() - ( aSearchData.GetFindString().length() + result ) );
161 
162  wxLogTrace( traceFindReplace, wxT( "Replacing '%s', prefix '%s', replace '%s', suffix '%s'." ),
163  aText, prefix, aSearchData.GetReplaceString(), suffix );
164 
165  aText = prefix + aSearchData.GetReplaceString() + suffix;
166 
167  return true;
168 }
169 
170 
171 bool EDA_ITEM::operator<( const EDA_ITEM& aItem ) const
172 {
173  wxFAIL_MSG( wxString::Format( wxT( "Less than operator not defined for item type %s." ),
174  GetClass() ) );
175 
176  return false;
177 }
178 
179 
181 {
182  // do not call initVars()
183 
184  m_structType = aItem.m_structType;
185  m_flags = aItem.m_flags;
186  m_status = aItem.m_status;
187  m_parent = aItem.m_parent;
189 
190  return *this;
191 }
192 
193 
195 {
196  // Basic fallback
197  EDA_RECT bbox = GetBoundingBox();
198 
199  return BOX2I( bbox.GetOrigin(), bbox.GetSize() );
200 }
201 
202 
203 void EDA_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
204 {
205  // Basic fallback
206  aCount = 1;
207  aLayers[0] = 0;
208 }
209 
210 
212 {
213  return BITMAPS::dummy_item;
214 }
215 
216 
217 #if defined( DEBUG )
218 
219 void EDA_ITEM::ShowDummy( std::ostream& os ) const
220 {
221  // XML output:
222  wxString s = GetClass();
223 
224  os << '<' << s.Lower().mb_str() << ">"
225  << " Need ::Show() override for this class "
226  << "</" << s.Lower().mb_str() << ">\n";
227 }
228 
229 
230 std::ostream& EDA_ITEM::NestedSpace( int nestLevel, std::ostream& os )
231 {
232  for( int i = 0; i<nestLevel; ++i )
233  os << " ";
234 
235  // number of spaces here controls indent per nest level
236 
237  return os;
238 }
239 
240 #endif
241 
242 
243 static struct EDA_ITEM_DESC
244 {
246  {
248  .Undefined( TYPE_NOT_INIT )
249  .Map( NOT_USED, wxT( "<not used>" ) )
250  .Map( SCREEN_T, _HKI( "Screen" ) )
251 
252  .Map( PCB_FOOTPRINT_T, _HKI( "Footprint" ) )
253  .Map( PCB_PAD_T, _HKI( "Pad" ) )
254  .Map( PCB_SHAPE_T, _HKI( "Graphic" ) )
255  .Map( PCB_TEXT_T, _HKI( "Text" ) )
256  .Map( PCB_FP_TEXT_T, _HKI( "Text" ) )
257  .Map( PCB_FP_SHAPE_T, _HKI( "Graphic" ) )
258  .Map( PCB_FP_ZONE_T, _HKI( "Zone" ) )
259  .Map( PCB_TRACE_T, _HKI( "Track" ) )
260  .Map( PCB_VIA_T, _HKI( "Via" ) )
261  .Map( PCB_MARKER_T, _HKI( "Marker" ) )
262  .Map( PCB_DIM_ALIGNED_T, _HKI( "Dimension" ) )
263  .Map( PCB_DIM_ORTHOGONAL_T, _HKI( "Dimension" ) )
264  .Map( PCB_DIM_CENTER_T, _HKI( "Dimension" ) )
265  .Map( PCB_DIM_LEADER_T, _HKI( "Leader" ) )
266  .Map( PCB_TARGET_T, _HKI( "Target" ) )
267  .Map( PCB_ZONE_T, _HKI( "Zone" ) )
268  .Map( PCB_ITEM_LIST_T, _HKI( "ItemList" ) )
269  .Map( PCB_NETINFO_T, _HKI( "NetInfo" ) )
270  .Map( PCB_GROUP_T, _HKI( "Group" ) )
271 
272  .Map( SCH_MARKER_T, _HKI( "Marker" ) )
273  .Map( SCH_JUNCTION_T, _HKI( "Junction" ) )
274  .Map( SCH_NO_CONNECT_T, _HKI( "No-Connect Flag" ) )
275  .Map( SCH_BUS_WIRE_ENTRY_T, _HKI( "Wire Entry" ) )
276  .Map( SCH_BUS_BUS_ENTRY_T, _HKI( "Bus Entry" ) )
277  .Map( SCH_LINE_T, _HKI( "Line" ) )
278  .Map( SCH_BITMAP_T, _HKI( "Bitmap" ) )
279  .Map( SCH_TEXT_T, _HKI( "Text" ) )
280  .Map( SCH_LABEL_T, _HKI( "Net Label" ) )
281  .Map( SCH_GLOBAL_LABEL_T, _HKI( "Global Label" ) )
282  .Map( SCH_HIER_LABEL_T, _HKI( "Hierarchical Label" ) )
283  .Map( SCH_FIELD_T, _HKI( "Field" ) )
284  .Map( SCH_SYMBOL_T, _HKI( "Symbol" ) )
285  .Map( SCH_PIN_T, _HKI( "Pin" ) )
286  .Map( SCH_SHEET_PIN_T, _HKI( "Sheet Pin" ) )
287  .Map( SCH_SHEET_T, _HKI( "Sheet" ) )
288 
289  // Synthetic search tokens don't need to be included...
290  //.Map( SCH_FIELD_LOCATE_REFERENCE_T, _HKI( "Field Locate Reference" ) )
291  //.Map( SCH_FIELD_LOCATE_VALUE_T, _HKI( "Field Locate Value" ) )
292  //.Map( SCH_FIELD_LOCATE_FOOTPRINT_T, _HKI( "Field Locate Footprint" ) )
293 
294  .Map( SCH_SCREEN_T, _HKI( "SCH Screen" ) )
295 
296  .Map( LIB_SYMBOL_T, _HKI( "Symbol" ) )
297  .Map( LIB_ALIAS_T, _HKI( "Alias" ) )
298  .Map( LIB_SHAPE_T, _HKI( "Graphic" ) )
299  .Map( LIB_TEXT_T, _HKI( "Text" ) )
300  .Map( LIB_PIN_T, _HKI( "Pin" ) )
301  .Map( LIB_FIELD_T, _HKI( "Symbol Field" ) )
302 
303  .Map( GERBER_LAYOUT_T, _HKI( "Gerber Layout" ) )
304  .Map( GERBER_DRAW_ITEM_T, _HKI( "Draw Item" ) )
305  .Map( GERBER_IMAGE_T, _HKI( "Image" ) );
306 
309  propMgr.AddProperty( new PROPERTY_ENUM<EDA_ITEM, KICAD_T>( "Type",
311  }
313 
#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:479
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:365
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:171
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
const wxChar *const traceFindReplace
Flag to enable find and replace debug tracing.
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:153
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
ENUM_TO_WXANY(KICAD_T)
EDA_ITEM_FLAGS m_status
Definition: eda_item.h:478
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:481
#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:94
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:203
const wxPoint GetOrigin() const
Definition: eda_rect.h:101
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:183
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:145
EDA_ITEM & operator=(const EDA_ITEM &aItem)
Assign the members of aItem to another object.
Definition: eda_item.cpp:180
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:211
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:403
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:100
KICAD_T m_structType
Run time identification, keep private so it can never be changed after a ctor sets it.
Definition: eda_item.h:488
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:194
SEARCH_RESULT
Definition: eda_item.h:41
bool m_forceVisible
Definition: eda_item.h:480
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
#define _HKI(x)
const wxSize GetSize() const
Definition: eda_rect.h:91
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113