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
38 m_parent( parent ),
39 m_forceVisible( false ),
40 m_flags( 0 ),
41 m_structType( idType )
42{ }
43
44
46 m_parent( nullptr ),
47 m_forceVisible( false ),
48 m_flags( 0 ),
49 m_structType( idType )
50{ }
51
52
54 m_Uuid( base.m_Uuid ),
55 m_parent( base.m_parent ),
56 m_forceVisible( base.m_forceVisible ),
57 m_flags( base.m_flags ),
58 m_structType( base.m_structType )
59{
61}
62
63
65{
67
68 // If this a child object, then the parent modification state also needs to be set.
69 if( m_parent )
71}
72
73
75{
76 // return a zero-sized box per default. derived classes should override
77 // this
78 return BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( 0, 0 ) );
79}
80
81
83{
84 wxCHECK_MSG( false, nullptr, wxT( "Clone not implemented in derived class " ) + GetClass() +
85 wxT( ". Bad programmer!" ) );
86}
87
88
89// see base_struct.h
90// many classes inherit this method, be careful:
91INSPECT_RESULT EDA_ITEM::Visit( INSPECTOR inspector, void* testData,
92 const std::vector<KICAD_T>& aScanTypes )
93{
94#if 0 && defined(DEBUG)
95 std::cout << GetClass().mb_str() << ' ';
96#endif
97
98 if( IsType( aScanTypes ) )
99 {
100 if( INSPECT_RESULT::QUIT == inspector( this, testData ) )
101 return INSPECT_RESULT::QUIT;
102 }
103
104 return INSPECT_RESULT::CONTINUE;
105}
106
107
108wxString EDA_ITEM::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
109{
110 wxFAIL_MSG( wxT( "GetItemDescription() was not overridden for schematic item type " ) +
111 GetClass() );
112
113 return wxString( wxT( "Undefined item description for " ) + GetClass() );
114}
115
116
117bool isWordChar( const wxUniChar& c )
118{
119 return wxIsalnum( c ) || c == '_';
120}
121
122
123bool EDA_ITEM::Matches( const wxString& aText, const EDA_SEARCH_DATA& aSearchData ) const
124{
125 wxString text = aText;
126 wxString searchText = aSearchData.findString;
127
128 // Don't match if searching for replaceable item and the item doesn't support text replace.
129 if( aSearchData.searchAndReplace && !IsReplaceable() )
130 return false;
131
132 if( !aSearchData.matchCase )
133 {
134 text.MakeUpper();
135 searchText.MakeUpper();
136 }
137
138 auto isWordChar =
139 []( const wxUniChar& c )
140 {
141 return wxIsalnum( c ) || c == '_';
142 };
143
144 if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::PERMISSIVE )
145 {
146 EDA_COMBINED_MATCHER matcher( searchText, CTX_SEARCH );
147
148 return matcher.Find( text );
149 }
150 else if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::WHOLEWORD )
151 {
152 int ii = 0;
153
154 while( ii < (int) text.length() )
155 {
156 int next = text.find( searchText, ii );
157
158 if( next == wxNOT_FOUND )
159 return false;
160
161 ii = next;
162 next += searchText.length();
163
164 bool startOK = ( ii == 0 || !isWordChar( text.GetChar( ii - 1 ) ) );
165 bool endOK = ( next == (int) text.length() || !isWordChar( text.GetChar( next ) ) );
166
167 if( startOK && endOK )
168 return true;
169 else
170 ii++;
171 }
172
173 return false;
174 }
175 else if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::WILDCARD )
176 {
177 return text.Matches( searchText );
178 }
179 else
180 {
181 return text.Find( searchText ) != wxNOT_FOUND;
182 }
183}
184
185
186bool EDA_ITEM::Replace( const EDA_SEARCH_DATA& aSearchData, wxString& aText )
187{
188 wxString text = aText;
189 wxString searchText = aSearchData.findString;
190 wxString result;
191 bool replaced = false;
192
193 if( !aSearchData.matchCase )
194 {
195 text = text.Upper();
196 searchText = searchText.Upper();
197 }
198
199 int ii = 0;
200
201 while( ii < (int) text.length() )
202 {
203 int next = text.find( searchText, ii );
204
205 if( next == wxNOT_FOUND )
206 {
207 result += aText.Mid( ii, wxString::npos );
208 break;
209 }
210
211 if( next > ii )
212 result += aText.Mid( ii, next - ii );
213
214 ii = next;
215 next += searchText.length();
216
217 bool startOK;
218 bool endOK;
219
220 if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::WHOLEWORD )
221 {
222 startOK = ( ii == 0 || !isWordChar( text.GetChar( ii - 1 ) ) );
223 endOK = ( next == (int) text.length() || !isWordChar( text.GetChar( next ) ) );
224 }
225 else
226 {
227 startOK = true;
228 endOK = true;
229 }
230
231 if( startOK && endOK )
232 {
233 result += aSearchData.replaceString;
234 replaced = true;
235 ii = next;
236 }
237 else
238 {
239 result += aText.GetChar( ii );
240 ii++;
241 }
242 }
243
244 aText = result;
245 return replaced;
246}
247
248
249bool EDA_ITEM::operator<( const EDA_ITEM& aItem ) const
250{
251 wxFAIL_MSG( wxString::Format( wxT( "Less than operator not defined for item type %s." ),
252 GetClass() ) );
253
254 return false;
255}
256
257
259{
260 // do not call initVars()
261
263 m_flags = aItem.m_flags;
264 m_parent = aItem.m_parent;
266
268
269 return *this;
270}
271
272
274{
275 // Basic fallback
276 return GetBoundingBox();
277}
278
279
280void EDA_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
281{
282 // Basic fallback
283 aCount = 1;
284 aLayers[0] = 0;
285}
286
287
289{
290 return BITMAPS::dummy_item;
291}
292
293
294#if defined( DEBUG )
295
296void EDA_ITEM::ShowDummy( std::ostream& os ) const
297{
298 // XML output:
299 wxString s = GetClass();
300
301 os << '<' << s.Lower().mb_str() << ">"
302 << " Need ::Show() override for this class "
303 << "</" << s.Lower().mb_str() << ">\n";
304}
305
306
307std::ostream& EDA_ITEM::NestedSpace( int nestLevel, std::ostream& os )
308{
309 for( int i = 0; i<nestLevel; ++i )
310 os << " ";
311
312 // number of spaces here controls indent per nest level
313
314 return os;
315}
316
317#endif
318
319
320wxString EDA_ITEM::GetTypeDesc() const
321{
322 //@see EDA_ITEM_DESC for definition of ENUM_MAP<KICAD_T>
323 wxString typeDescr = ENUM_MAP<KICAD_T>::Instance().ToString( Type() );
324
325 return wxGetTranslation( typeDescr );
326}
327
328
330{
331 return GetTypeDesc();
332}
333
334
335static struct EDA_ITEM_DESC
336{
338 {
340 .Undefined( TYPE_NOT_INIT )
341 .Map( NOT_USED, wxT( "<not used>" ) )
342 .Map( SCREEN_T, _HKI( "Screen" ) )
343
344 .Map( PCB_FOOTPRINT_T, _HKI( "Footprint" ) )
345 .Map( PCB_PAD_T, _HKI( "Pad" ) )
346 .Map( PCB_SHAPE_T, _HKI( "Graphic" ) )
347 .Map( PCB_REFERENCE_IMAGE_T, _HKI( "Reference Image" ) )
348 .Map( PCB_GENERATOR_T, _HKI( "Generator" ) )
349 .Map( PCB_FIELD_T, _HKI( "Text" ) )
350 .Map( PCB_TEXT_T, _HKI( "Text" ) )
351 .Map( PCB_TEXTBOX_T, _HKI( "Text Box" ) )
352 .Map( PCB_TABLE_T, _HKI( "Table" ) )
353 .Map( PCB_TABLECELL_T, _HKI( "Table Cell" ) )
354 .Map( PCB_TRACE_T, _HKI( "Track" ) )
355 .Map( PCB_ARC_T, _HKI( "Track" ) )
356 .Map( PCB_VIA_T, _HKI( "Via" ) )
357 .Map( PCB_MARKER_T, _HKI( "Marker" ) )
358 .Map( PCB_DIM_ALIGNED_T, _HKI( "Dimension" ) )
359 .Map( PCB_DIM_ORTHOGONAL_T, _HKI( "Dimension" ) )
360 .Map( PCB_DIM_CENTER_T, _HKI( "Dimension" ) )
361 .Map( PCB_DIM_RADIAL_T, _HKI( "Dimension" ) )
362 .Map( PCB_DIM_LEADER_T, _HKI( "Leader" ) )
363 .Map( PCB_TARGET_T, _HKI( "Target" ) )
364 .Map( PCB_ZONE_T, _HKI( "Zone" ) )
365 .Map( PCB_ITEM_LIST_T, _HKI( "ItemList" ) )
366 .Map( PCB_NETINFO_T, _HKI( "NetInfo" ) )
367 .Map( PCB_GROUP_T, _HKI( "Group" ) )
368
369 .Map( SCH_MARKER_T, _HKI( "Marker" ) )
370 .Map( SCH_JUNCTION_T, _HKI( "Junction" ) )
371 .Map( SCH_NO_CONNECT_T, _HKI( "No-Connect Flag" ) )
372 .Map( SCH_BUS_WIRE_ENTRY_T, _HKI( "Wire Entry" ) )
373 .Map( SCH_BUS_BUS_ENTRY_T, _HKI( "Bus Entry" ) )
374 .Map( SCH_LINE_T, _HKI( "Line" ) )
375 .Map( SCH_BITMAP_T, _HKI( "Bitmap" ) )
376 .Map( SCH_SHAPE_T, _HKI( "Graphic" ) )
377 .Map( SCH_TEXT_T, _HKI( "Text" ) )
378 .Map( SCH_TEXTBOX_T, _HKI( "Text Box" ) )
379 .Map( SCH_TABLE_T, _HKI( "Table" ) )
380 .Map( SCH_TABLECELL_T, _HKI( "Table Cell" ) )
381 .Map( SCH_LABEL_T, _HKI( "Net Label" ) )
382 .Map( SCH_DIRECTIVE_LABEL_T, _HKI( "Directive Label" ) )
383 .Map( SCH_GLOBAL_LABEL_T, _HKI( "Global Label" ) )
384 .Map( SCH_HIER_LABEL_T, _HKI( "Hierarchical Label" ) )
385 .Map( SCH_FIELD_T, _HKI( "Field" ) )
386 .Map( SCH_SYMBOL_T, _HKI( "Symbol" ) )
387 .Map( SCH_PIN_T, _HKI( "Pin" ) )
388 .Map( SCH_SHEET_PIN_T, _HKI( "Sheet Pin" ) )
389 .Map( SCH_SHEET_T, _HKI( "Sheet" ) )
390
391 // Synthetic search tokens don't need to be included...
392 //.Map( SCH_FIELD_LOCATE_REFERENCE_T, _HKI( "Field Locate Reference" ) )
393 //.Map( SCH_FIELD_LOCATE_VALUE_T, _HKI( "Field Locate Value" ) )
394 //.Map( SCH_FIELD_LOCATE_FOOTPRINT_T, _HKI( "Field Locate Footprint" ) )
395
396 .Map( SCH_SCREEN_T, _HKI( "SCH Screen" ) )
397
398 .Map( LIB_SYMBOL_T, _HKI( "Symbol" ) )
399 .Map( LIB_SHAPE_T, _HKI( "Graphic" ) )
400 .Map( LIB_TEXT_T, _HKI( "Text" ) )
401 .Map( LIB_TEXTBOX_T, _HKI( "Text Box" ) )
402 .Map( LIB_PIN_T, _HKI( "Pin" ) )
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:853
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:88
EDA_ITEM & operator=(const EDA_ITEM &aItem)
Assign the members of aItem to another object.
Definition: eda_item.cpp:258
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType)
Definition: eda_item.cpp:37
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition: eda_item.cpp:74
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:280
wxString GetTypeDesc() const
Return a translated description of the type for this EDA_ITEM for display in user facing messages.
Definition: eda_item.cpp:320
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:126
void SetModified()
Definition: eda_item.cpp:64
bool m_forceVisible
Definition: eda_item.h:489
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:100
bool operator<(const EDA_ITEM &aItem) const
Test if another item is less than this object.
Definition: eda_item.cpp:249
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:490
virtual bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:375
virtual bool IsType(const std::vector< KICAD_T > &aScanTypes) const
Check whether the item is one of the listed types.
Definition: eda_item.h:175
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:91
virtual wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider) const
Return a user-visible description string of this item.
Definition: eda_item.cpp:108
virtual wxString GetFriendlyName() const
Definition: eda_item.cpp:329
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:82
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:273
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:488
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:186
KICAD_T m_structType
Run time identification, keep private so it can never be changed after a ctor sets it.
Definition: eda_item.h:497
virtual bool IsReplaceable() const
Override this method in any derived object that supports test find and replace.
Definition: eda_item.h:413
virtual BITMAPS GetMenuImage() const
Return a pointer to an image to be used in menus.
Definition: eda_item.cpp:288
static ENUM_MAP< T > & Instance()
Definition: property.h:663
void SetForcedTransparency(double aForcedTransparency)
Definition: view_item.h:152
double GetForcedTransparency() const
Definition: view_item.h:157
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:117
INSPECT_RESULT
Definition: eda_item.h:43
const INSPECTOR_FUNC & INSPECTOR
Definition: eda_item.h:81
#define IS_CHANGED
Item was edited, and modified.
Abstract pattern-matching tool and implementations.
@ CTX_SEARCH
Some functions to handle hotkeys in KiCad.
#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:366
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:153
@ 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:148
@ LIB_SYMBOL_T
Definition: typeinfo.h:202
@ SCH_NO_CONNECT_T
Definition: typeinfo.h:145
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
@ LIB_TEXT_T
Definition: typeinfo.h:204
@ 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:160
@ 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:154
@ 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
@ LIB_TEXTBOX_T
Definition: typeinfo.h:205
@ SCH_FIELD_T
Definition: typeinfo.h:159
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ SCH_DIRECTIVE_LABEL_T
Definition: typeinfo.h:158
@ SCH_LABEL_T
Definition: typeinfo.h:155
@ SCH_SHEET_T
Definition: typeinfo.h:162
@ LIB_SHAPE_T
Definition: typeinfo.h:203
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition: typeinfo.h:89
@ SCH_MARKER_T
Definition: typeinfo.h:143
@ PCB_ITEM_LIST_T
class BOARD_ITEM_LIST, a list of board items
Definition: typeinfo.h:108
@ SCH_SHAPE_T
Definition: typeinfo.h:149
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90
@ GERBER_DRAW_ITEM_T
Definition: typeinfo.h:212
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:157
@ 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:147
@ LIB_PIN_T
Definition: typeinfo.h:206
@ SCH_SCREEN_T
Definition: typeinfo.h:190
@ 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:161
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:101
@ SCH_TEXT_T
Definition: typeinfo.h:152
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ GERBER_IMAGE_T
Definition: typeinfo.h:213
@ SCH_BUS_WIRE_ENTRY_T
Definition: typeinfo.h:146
@ GERBER_LAYOUT_T
Definition: typeinfo.h:211
@ SCREEN_T
not really an item, used to identify a screen
Definition: typeinfo.h:83
@ SCH_BITMAP_T
Definition: typeinfo.h:150
@ 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:151
@ 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:156
@ SCH_JUNCTION_T
Definition: typeinfo.h:144
@ SCH_PIN_T
Definition: typeinfo.h:163
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588