KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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 <trace_helpers.h>
30#include <trigo.h>
31#include <i18n_utility.h>
32#include <wx/log.h>
33
34#include <wx/fdrepdlg.h>
35#include <eda_pattern_match.h>
36
37EDA_ITEM::EDA_ITEM( EDA_ITEM* parent, KICAD_T idType, bool isSCH_ITEM, bool isBOARD_ITEM ) :
38 KIGFX::VIEW_ITEM( isSCH_ITEM, isBOARD_ITEM ),
39 m_structType( idType ),
40 m_flags( 0 ),
41 m_parent( parent ),
42 m_forceVisible( false )
43{ }
44
45
46EDA_ITEM::EDA_ITEM( KICAD_T idType, bool isSCH_ITEM, bool isBOARD_ITEM ) :
47 KIGFX::VIEW_ITEM( isSCH_ITEM, isBOARD_ITEM ),
48 m_structType( idType ),
49 m_flags( 0 ),
50 m_parent( nullptr ),
51 m_forceVisible( false )
52{ }
53
54
56 KIGFX::VIEW_ITEM( base.IsSCH_ITEM(), base.IsBOARD_ITEM() ),
57 m_Uuid( base.m_Uuid ),
58 m_structType( base.m_structType ),
59 m_flags( base.m_flags ),
60 m_parent( base.m_parent ),
61 m_forceVisible( base.m_forceVisible )
62{
64}
65
66
68{
70
71 // If this a child object, then the parent modification state also needs to be set.
72 if( m_parent )
74}
75
76
78{
79 // return a zero-sized box per default. derived classes should override
80 // this
81 return BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( 0, 0 ) );
82}
83
84
86{
87 wxCHECK_MSG( false, nullptr, wxT( "Clone not implemented in derived class " ) + GetClass() +
88 wxT( ". Bad programmer!" ) );
89}
90
91
92// see base_struct.h
93// many classes inherit this method, be careful:
94INSPECT_RESULT EDA_ITEM::Visit( INSPECTOR inspector, void* testData,
95 const std::vector<KICAD_T>& aScanTypes )
96{
97#if 0 && defined(DEBUG)
98 std::cout << GetClass().mb_str() << ' ';
99#endif
100
101 if( IsType( aScanTypes ) )
102 {
103 if( INSPECT_RESULT::QUIT == inspector( this, testData ) )
104 return INSPECT_RESULT::QUIT;
105 }
106
107 return INSPECT_RESULT::CONTINUE;
108}
109
110
111wxString EDA_ITEM::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
112{
113 wxFAIL_MSG( wxT( "GetItemDescription() was not overridden for schematic item type " ) +
114 GetClass() );
115
116 return wxString( wxT( "Undefined item description for " ) + GetClass() );
117}
118
119
120bool isWordChar( const wxUniChar& c )
121{
122 return wxIsalnum( c ) || c == '_';
123}
124
125
126bool EDA_ITEM::Matches( const wxString& aText, const EDA_SEARCH_DATA& aSearchData ) const
127{
128 wxString text = aText;
129 wxString searchText = aSearchData.findString;
130
131 // Don't match if searching for replaceable item and the item doesn't support text replace.
132 if( aSearchData.searchAndReplace && !IsReplaceable() )
133 return false;
134
135 if( !aSearchData.matchCase )
136 {
137 text.MakeUpper();
138 searchText.MakeUpper();
139 }
140
141 auto isWordChar =
142 []( const wxUniChar& c )
143 {
144 return wxIsalnum( c ) || c == '_';
145 };
146
147 if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::PERMISSIVE )
148 {
149 EDA_COMBINED_MATCHER matcher( searchText, CTX_SEARCH );
150
151 return matcher.Find( text );
152 }
153 else if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::WHOLEWORD )
154 {
155 int ii = 0;
156
157 while( ii < (int) text.length() )
158 {
159 int next = text.find( searchText, ii );
160
161 if( next == wxNOT_FOUND )
162 return false;
163
164 ii = next;
165 next += searchText.length();
166
167 bool startOK = ( ii == 0 || !isWordChar( text.GetChar( ii - 1 ) ) );
168 bool endOK = ( next == (int) text.length() || !isWordChar( text.GetChar( next ) ) );
169
170 if( startOK && endOK )
171 return true;
172 else
173 ii++;
174 }
175
176 return false;
177 }
178 else if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::WILDCARD )
179 {
180 return text.Matches( searchText );
181 }
182 else
183 {
184 return text.Find( searchText ) != wxNOT_FOUND;
185 }
186}
187
188
189bool EDA_ITEM::Replace( const EDA_SEARCH_DATA& aSearchData, wxString& aText )
190{
191 wxString text = aText;
192 wxString searchText = aSearchData.findString;
193 wxString result;
194 bool replaced = false;
195
196 if( !aSearchData.matchCase )
197 {
198 text = text.Upper();
199 searchText = searchText.Upper();
200 }
201
202 int ii = 0;
203
204 while( ii < (int) text.length() )
205 {
206 int next = text.find( searchText, ii );
207
208 if( next == wxNOT_FOUND )
209 {
210 result += aText.Mid( ii, wxString::npos );
211 break;
212 }
213
214 if( next > ii )
215 result += aText.Mid( ii, next - ii );
216
217 ii = next;
218 next += searchText.length();
219
220 bool startOK;
221 bool endOK;
222
223 if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::WHOLEWORD )
224 {
225 startOK = ( ii == 0 || !isWordChar( text.GetChar( ii - 1 ) ) );
226 endOK = ( next == (int) text.length() || !isWordChar( text.GetChar( next ) ) );
227 }
228 else
229 {
230 startOK = true;
231 endOK = true;
232 }
233
234 if( startOK && endOK )
235 {
236 result += aSearchData.replaceString;
237 replaced = true;
238 ii = next;
239 }
240 else
241 {
242 result += aText.GetChar( ii );
243 ii++;
244 }
245 }
246
247 aText = result;
248 return replaced;
249}
250
251
252bool EDA_ITEM::operator<( const EDA_ITEM& aItem ) const
253{
254 wxFAIL_MSG( wxString::Format( wxT( "Less than operator not defined for item type %s." ),
255 GetClass() ) );
256
257 return false;
258}
259
260
262{
263 // do not call initVars()
264
266 m_flags = aItem.m_flags;
267 m_parent = aItem.m_parent;
269
271
272 return *this;
273}
274
275
277{
278 // Basic fallback
279 return GetBoundingBox();
280}
281
282
283void EDA_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
284{
285 // Basic fallback
286 aCount = 1;
287 aLayers[0] = 0;
288}
289
290
292{
293 return BITMAPS::dummy_item;
294}
295
296
297#if defined( DEBUG )
298
299void EDA_ITEM::ShowDummy( std::ostream& os ) const
300{
301 // XML output:
302 wxString s = GetClass();
303
304 os << '<' << s.Lower().mb_str() << ">"
305 << " Need ::Show() override for this class "
306 << "</" << s.Lower().mb_str() << ">\n";
307}
308
309
310std::ostream& EDA_ITEM::NestedSpace( int nestLevel, std::ostream& os )
311{
312 for( int i = 0; i<nestLevel; ++i )
313 os << " ";
314
315 // number of spaces here controls indent per nest level
316
317 return os;
318}
319
320#endif
321
322
323wxString EDA_ITEM::GetTypeDesc() const
324{
325 //@see EDA_ITEM_DESC for definition of ENUM_MAP<KICAD_T>
326 wxString typeDescr = ENUM_MAP<KICAD_T>::Instance().ToString( Type() );
327
328 return wxGetTranslation( typeDescr );
329}
330
331
333{
334 return GetTypeDesc();
335}
336
337
338static struct EDA_ITEM_DESC
339{
341 {
343 .Undefined( TYPE_NOT_INIT )
344 .Map( NOT_USED, wxT( "<not used>" ) )
345 .Map( SCREEN_T, _HKI( "Screen" ) )
346
347 .Map( PCB_FOOTPRINT_T, _HKI( "Footprint" ) )
348 .Map( PCB_PAD_T, _HKI( "Pad" ) )
349 .Map( PCB_SHAPE_T, _HKI( "Graphic" ) )
350 .Map( PCB_REFERENCE_IMAGE_T, _HKI( "Reference Image" ) )
351 .Map( PCB_GENERATOR_T, _HKI( "Generator" ) )
352 .Map( PCB_FIELD_T, _HKI( "Text" ) )
353 .Map( PCB_TEXT_T, _HKI( "Text" ) )
354 .Map( PCB_TEXTBOX_T, _HKI( "Text Box" ) )
355 .Map( PCB_TABLE_T, _HKI( "Table" ) )
356 .Map( PCB_TABLECELL_T, _HKI( "Table Cell" ) )
357 .Map( PCB_TRACE_T, _HKI( "Track" ) )
358 .Map( PCB_ARC_T, _HKI( "Track" ) )
359 .Map( PCB_VIA_T, _HKI( "Via" ) )
360 .Map( PCB_MARKER_T, _HKI( "Marker" ) )
361 .Map( PCB_DIM_ALIGNED_T, _HKI( "Dimension" ) )
362 .Map( PCB_DIM_ORTHOGONAL_T, _HKI( "Dimension" ) )
363 .Map( PCB_DIM_CENTER_T, _HKI( "Dimension" ) )
364 .Map( PCB_DIM_RADIAL_T, _HKI( "Dimension" ) )
365 .Map( PCB_DIM_LEADER_T, _HKI( "Leader" ) )
366 .Map( PCB_TARGET_T, _HKI( "Target" ) )
367 .Map( PCB_ZONE_T, _HKI( "Zone" ) )
368 .Map( PCB_ITEM_LIST_T, _HKI( "ItemList" ) )
369 .Map( PCB_NETINFO_T, _HKI( "NetInfo" ) )
370 .Map( PCB_GROUP_T, _HKI( "Group" ) )
371
372 .Map( SCH_MARKER_T, _HKI( "Marker" ) )
373 .Map( SCH_JUNCTION_T, _HKI( "Junction" ) )
374 .Map( SCH_NO_CONNECT_T, _HKI( "No-Connect Flag" ) )
375 .Map( SCH_BUS_WIRE_ENTRY_T, _HKI( "Wire Entry" ) )
376 .Map( SCH_BUS_BUS_ENTRY_T, _HKI( "Bus Entry" ) )
377 .Map( SCH_LINE_T, _HKI( "Line" ) )
378 .Map( SCH_BITMAP_T, _HKI( "Bitmap" ) )
379 .Map( SCH_SHAPE_T, _HKI( "Graphic" ) )
380 .Map( SCH_RULE_AREA_T, _HKI( "Rule Area" ) )
381 .Map( SCH_TEXT_T, _HKI( "Text" ) )
382 .Map( SCH_TEXTBOX_T, _HKI( "Text Box" ) )
383 .Map( SCH_TABLE_T, _HKI( "Table" ) )
384 .Map( SCH_TABLECELL_T, _HKI( "Table Cell" ) )
385 .Map( SCH_LABEL_T, _HKI( "Net Label" ) )
386 .Map( SCH_DIRECTIVE_LABEL_T, _HKI( "Directive Label" ) )
387 .Map( SCH_GLOBAL_LABEL_T, _HKI( "Global Label" ) )
388 .Map( SCH_HIER_LABEL_T, _HKI( "Hierarchical Label" ) )
389 .Map( SCH_FIELD_T, _HKI( "Field" ) )
390 .Map( SCH_SYMBOL_T, _HKI( "Symbol" ) )
391 .Map( SCH_PIN_T, _HKI( "Pin" ) )
392 .Map( SCH_SHEET_PIN_T, _HKI( "Sheet Pin" ) )
393 .Map( SCH_SHEET_T, _HKI( "Sheet" ) )
394
395 // Synthetic search tokens don't need to be included...
396 //.Map( SCH_FIELD_LOCATE_REFERENCE_T, _HKI( "Field Locate Reference" ) )
397 //.Map( SCH_FIELD_LOCATE_VALUE_T, _HKI( "Field Locate Value" ) )
398 //.Map( SCH_FIELD_LOCATE_FOOTPRINT_T, _HKI( "Field Locate Footprint" ) )
399
400 .Map( SCH_SCREEN_T, _HKI( "SCH Screen" ) )
401
402 .Map( LIB_SYMBOL_T, _HKI( "Symbol" ) )
403
404 .Map( GERBER_LAYOUT_T, _HKI( "Gerber Layout" ) )
405 .Map( GERBER_DRAW_ITEM_T, _HKI( "Draw Item" ) )
406 .Map( GERBER_IMAGE_T, _HKI( "Image" ) );
407
410
411 propMgr.AddProperty( new PROPERTY_ENUM<EDA_ITEM, KICAD_T>( wxS( "Type" ),
414 }
416
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
BOX2< VECTOR2I > BOX2I
Definition: box2.h:922
bool Find(const wxString &aTerm, int &aMatchersTriggered, int &aPosition)
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
EDA_ITEM & operator=(const EDA_ITEM &aItem)
Assign the members of aItem to another object.
Definition: eda_item.cpp:261
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition: eda_item.cpp:77
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:283
wxString GetTypeDesc() const
Return a translated description of the type for this EDA_ITEM for display in user facing messages.
Definition: eda_item.cpp:323
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:127
virtual wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const
Return a user-visible description string of this item.
Definition: eda_item.cpp:111
void SetModified()
Definition: eda_item.cpp:67
bool m_forceVisible
Definition: eda_item.h:501
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
bool operator<(const EDA_ITEM &aItem) const
Test if another item is less than this object.
Definition: eda_item.cpp:252
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:499
virtual bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:377
virtual bool IsType(const std::vector< KICAD_T > &aScanTypes) const
Check whether the item is one of the listed types.
Definition: eda_item.h:176
virtual INSPECT_RESULT Visit(INSPECTOR inspector, void *testData, const std::vector< KICAD_T > &aScanTypes)
May be re-implemented for each derived class in order to handle all the types given by its member dat...
Definition: eda_item.cpp:94
virtual wxString GetFriendlyName() const
Definition: eda_item.cpp:332
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:85
virtual wxString GetClass() const =0
Return the class name.
virtual const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Definition: eda_item.cpp:276
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:500
static bool Replace(const EDA_SEARCH_DATA &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:189
KICAD_T m_structType
Run time identification, keep private so it can never be changed after a ctor sets it.
Definition: eda_item.h:496
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition: eda_item.cpp:37
virtual bool IsReplaceable() const
Override this method in any derived object that supports test find and replace.
Definition: eda_item.h:415
virtual BITMAPS GetMenuImage() const
Return a pointer to an image to be used in menus.
Definition: eda_item.cpp:291
static ENUM_MAP< T > & Instance()
Definition: property.h:663
void SetForcedTransparency(double aForcedTransparency)
Definition: view_item.h:156
double GetForcedTransparency() const
Definition: view_item.h:161
PROPERTY_BASE & SetIsHiddenFromPropertiesManager(bool aHide=true)
Definition: property.h:300
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:85
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
#define _HKI(x)
static struct EDA_ITEM_DESC _EDA_ITEM_DESC
bool isWordChar(const wxUniChar &c)
Definition: eda_item.cpp:120
INSPECT_RESULT
Definition: eda_item.h:43
const INSPECTOR_FUNC & INSPECTOR
Definition: eda_item.h:82
#define IS_CHANGED
Item was edited, and modified.
Abstract pattern-matching tool and implementations.
@ CTX_SEARCH
Some functions to handle hotkeys in KiCad.
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
#define NO_SETTER(owner, type)
Definition: property.h:774
#define ENUM_TO_WXANY(type)
Macro to define read-only fields (no setter method available)
Definition: property.h:765
#define REGISTER_TYPE(x)
Definition: property_mgr.h:371
CITER next(CITER it)
Definition: ptree.cpp:126
EDA_SEARCH_MATCH_MODE matchMode
wxString replaceString
wxLogTrace helper definitions.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ SCH_TABLE_T
Definition: typeinfo.h:165
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:105
@ SCH_LINE_T
Definition: typeinfo.h:163
@ LIB_SYMBOL_T
Definition: typeinfo.h:148
@ SCH_NO_CONNECT_T
Definition: typeinfo.h:160
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition: typeinfo.h:91
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ TYPE_NOT_INIT
Definition: typeinfo.h:81
@ SCH_SYMBOL_T
Definition: typeinfo.h:172
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:103
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:110
@ SCH_TABLECELL_T
Definition: typeinfo.h:166
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:107
@ SCH_FIELD_T
Definition: typeinfo.h:150
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ SCH_DIRECTIVE_LABEL_T
Definition: typeinfo.h:171
@ SCH_LABEL_T
Definition: typeinfo.h:167
@ SCH_SHEET_T
Definition: typeinfo.h:174
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition: typeinfo.h:89
@ SCH_MARKER_T
Definition: typeinfo.h:158
@ PCB_ITEM_LIST_T
class BOARD_ITEM_LIST, a list of board items
Definition: typeinfo.h:108
@ SCH_SHAPE_T
Definition: typeinfo.h:149
@ SCH_RULE_AREA_T
Definition: typeinfo.h:170
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90
@ GERBER_DRAW_ITEM_T
Definition: typeinfo.h:209
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:169
@ NOT_USED
the 3d code uses this value
Definition: typeinfo.h:79
@ PCB_MARKER_T
class PCB_MARKER, a marker used to show something
Definition: typeinfo.h:99
@ SCH_BUS_BUS_ENTRY_T
Definition: typeinfo.h:162
@ SCH_SCREEN_T
Definition: typeinfo.h:201
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:106
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition: typeinfo.h:95
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ SCH_SHEET_PIN_T
Definition: typeinfo.h:173
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:101
@ SCH_TEXT_T
Definition: typeinfo.h:151
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ GERBER_IMAGE_T
Definition: typeinfo.h:210
@ SCH_BUS_WIRE_ENTRY_T
Definition: typeinfo.h:161
@ GERBER_LAYOUT_T
Definition: typeinfo.h:208
@ SCREEN_T
not really an item, used to identify a screen
Definition: typeinfo.h:83
@ SCH_BITMAP_T
Definition: typeinfo.h:164
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition: typeinfo.h:94
@ PCB_NETINFO_T
class NETINFO_ITEM, a description of a net
Definition: typeinfo.h:109
@ SCH_TEXTBOX_T
Definition: typeinfo.h:152
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:104
@ SCH_GLOBAL_LABEL_T
Definition: typeinfo.h:168
@ SCH_JUNCTION_T
Definition: typeinfo.h:159
@ SCH_PIN_T
Definition: typeinfo.h:153
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691