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 EDA_ITEM::EDA_ITEM( EDA_ITEM* parent, KICAD_T idType ) :
36  m_status( 0 ),
37  m_parent( parent ),
38  m_forceVisible( false ),
39  m_flags( 0 ),
40  m_structType( idType )
41 { }
42 
43 
45  m_status( 0 ),
46  m_parent( nullptr ),
47  m_forceVisible( false ),
48  m_flags( 0 ),
49  m_structType( idType )
50 { }
51 
52 
53 EDA_ITEM::EDA_ITEM( const EDA_ITEM& base ) :
54  m_Uuid( base.m_Uuid ),
55  m_status( base.m_status ),
56  m_parent( base.m_parent ),
57  m_forceVisible( base.m_forceVisible ),
58  m_flags( base.m_flags ),
59  m_structType( base.m_structType )
60 { }
61 
62 
64 {
66 
67  // If this a child object, then the parent modification state also needs to be set.
68  if( m_parent )
70 }
71 
72 
74 {
75  // return a zero-sized box per default. derived classes should override
76  // this
77  return EDA_RECT( wxPoint( 0, 0 ), wxSize( 0, 0 ) );
78 }
79 
80 
82 {
83  wxCHECK_MSG( false, NULL, wxT( "Clone not implemented in derived class " ) + GetClass() +
84  wxT( ". Bad programmer!" ) );
85 }
86 
87 
88 // see base_struct.h
89 // many classes inherit this method, be careful:
90 //TODO (snh): Fix this to use std::set instead of C-style vector
91 SEARCH_RESULT EDA_ITEM::Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] )
92 {
93 #if 0 && defined(DEBUG)
94  std::cout << GetClass().mb_str() << ' ';
95 #endif
96 
97  if( IsType( scanTypes ) )
98  {
99  if( SEARCH_RESULT::QUIT == inspector( this, testData ) )
100  return SEARCH_RESULT::QUIT;
101  }
102 
104 }
105 
106 
107 wxString EDA_ITEM::GetSelectMenuText( EDA_UNITS aUnits ) const
108 {
109  wxFAIL_MSG( wxT( "GetSelectMenuText() was not overridden for schematic item type " ) +
110  GetClass() );
111 
112  return wxString( wxT( "Undefined menu text for " ) + GetClass() );
113 }
114 
115 
116 bool EDA_ITEM::Matches( const wxString& aText, const wxFindReplaceData& aSearchData ) const
117 {
118  wxString text = aText;
119  wxString searchText = aSearchData.GetFindString();
120 
121  // Don't match if searching for replaceable item and the item doesn't support text replace.
122  if( (aSearchData.GetFlags() & FR_SEARCH_REPLACE) && !IsReplaceable() )
123  return false;
124 
125  if( aSearchData.GetFlags() & wxFR_WHOLEWORD )
126  return aText.IsSameAs( searchText, aSearchData.GetFlags() & wxFR_MATCHCASE );
127 
128  if( aSearchData.GetFlags() & FR_MATCH_WILDCARD )
129  {
130  if( aSearchData.GetFlags() & wxFR_MATCHCASE )
131  return text.Matches( searchText );
132 
133  return text.MakeUpper().Matches( searchText.MakeUpper() );
134  }
135 
136  if( aSearchData.GetFlags() & wxFR_MATCHCASE )
137  return aText.Find( searchText ) != wxNOT_FOUND;
138 
139  return text.MakeUpper().Find( searchText.MakeUpper() ) != wxNOT_FOUND;
140 }
141 
142 
143 bool EDA_ITEM::Replace( const wxFindReplaceData& aSearchData, wxString& aText )
144 {
145  wxString searchString = (aSearchData.GetFlags() & wxFR_MATCHCASE) ? aText : aText.Upper();
146 
147  int result = searchString.Find( (aSearchData.GetFlags() & wxFR_MATCHCASE) ?
148  aSearchData.GetFindString() :
149  aSearchData.GetFindString().Upper() );
150 
151  if( result == wxNOT_FOUND )
152  return false;
153 
154  wxString prefix = aText.Left( result );
155  wxString suffix;
156 
157  if( aSearchData.GetFindString().length() + result < aText.length() )
158  suffix = aText.Right( aText.length() - ( aSearchData.GetFindString().length() + result ) );
159 
160  wxLogTrace( traceFindReplace, wxT( "Replacing '%s', prefix '%s', replace '%s', suffix '%s'." ),
161  aText, prefix, aSearchData.GetReplaceString(), suffix );
162 
163  aText = prefix + aSearchData.GetReplaceString() + suffix;
164 
165  return true;
166 }
167 
168 
169 bool EDA_ITEM::operator<( const EDA_ITEM& aItem ) const
170 {
171  wxFAIL_MSG( wxString::Format( wxT( "Less than operator not defined for item type %s." ),
172  GetClass() ) );
173 
174  return false;
175 }
176 
178 {
179  // do not call initVars()
180 
181  m_structType = aItem.m_structType;
182  m_flags = aItem.m_flags;
183  m_status = aItem.m_status;
184  m_parent = aItem.m_parent;
186 
187  return *this;
188 }
189 
191 {
192  // Basic fallback
193  EDA_RECT bbox = GetBoundingBox();
194 
195  return BOX2I( bbox.GetOrigin(), bbox.GetSize() );
196 }
197 
198 
199 void EDA_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
200 {
201  // Basic fallback
202  aCount = 1;
203  aLayers[0] = 0;
204 }
205 
207 {
208  return BITMAPS::dummy_item;
209 }
210 
211 #if defined(DEBUG)
212 
213 void EDA_ITEM::ShowDummy( std::ostream& os ) const
214 {
215  // XML output:
216  wxString s = GetClass();
217 
218  os << '<' << s.Lower().mb_str() << ">"
219  << " Need ::Show() override for this class "
220  << "</" << s.Lower().mb_str() << ">\n";
221 }
222 
223 
224 std::ostream& EDA_ITEM::NestedSpace( int nestLevel, std::ostream& os )
225 {
226  for( int i = 0; i<nestLevel; ++i )
227  os << " ";
228 
229  // number of spaces here controls indent per nest level
230 
231  return os;
232 }
233 
234 #endif
235 
236 
237 static struct EDA_ITEM_DESC
238 {
240  {
242  .Undefined( TYPE_NOT_INIT )
243  .Map( NOT_USED, wxT( "<not used>" ) )
244  .Map( SCREEN_T, _HKI( "Screen" ) )
245 
246  .Map( PCB_FOOTPRINT_T, _HKI( "Footprint" ) )
247  .Map( PCB_PAD_T, _HKI( "Pad" ) )
248  .Map( PCB_SHAPE_T, _HKI( "Graphic Shape" ) )
249  .Map( PCB_TEXT_T, _HKI( "Board Text" ) )
250  .Map( PCB_FP_TEXT_T, _HKI( "Footprint Text" ) )
251  .Map( PCB_FP_SHAPE_T, _HKI( "Graphic Shape" ) )
252  .Map( PCB_FP_ZONE_T, _HKI( "Zone" ) )
253  .Map( PCB_TRACE_T, _HKI( "Track" ) )
254  .Map( PCB_VIA_T, _HKI( "Via" ) )
255  .Map( PCB_MARKER_T, _HKI( "Board Marker" ) )
256  .Map( PCB_DIM_ALIGNED_T, _HKI( "Aligned Dimension" ) )
257  .Map( PCB_DIM_ORTHOGONAL_T, _HKI( "Orthogonal Dimension" ) )
258  .Map( PCB_DIM_CENTER_T, _HKI( "Center Dimension" ) )
259  .Map( PCB_DIM_LEADER_T, _HKI( "Leader" ) )
260  .Map( PCB_TARGET_T, _HKI( "Target" ) )
261  .Map( PCB_ZONE_T, _HKI( "Zone" ) )
262  .Map( PCB_ITEM_LIST_T, _HKI( "Item List" ) )
263  .Map( PCB_NETINFO_T, _HKI( "Net Info" ) )
264  .Map( PCB_GROUP_T, _HKI( "Group" ) )
265 
266  .Map( SCH_MARKER_T, _HKI( "Schematic Marker" ) )
267  .Map( SCH_JUNCTION_T, _HKI( "Junction" ) )
268  .Map( SCH_NO_CONNECT_T, _HKI( "No-Connect Flag" ) )
269  .Map( SCH_BUS_WIRE_ENTRY_T, _HKI( "Wire Entry" ) )
270  .Map( SCH_BUS_BUS_ENTRY_T, _HKI( "Bus Entry" ) )
271  .Map( SCH_LINE_T, _HKI( "Graphic Line" ) )
272  .Map( SCH_BITMAP_T, _HKI( "Bitmap" ) )
273  .Map( SCH_TEXT_T, _HKI( "Schematic Text" ) )
274  .Map( SCH_LABEL_T, _HKI( "Net Label" ) )
275  .Map( SCH_GLOBAL_LABEL_T, _HKI( "Global Label" ) )
276  .Map( SCH_HIER_LABEL_T, _HKI( "Hierarchical Label" ) )
277  .Map( SCH_FIELD_T, _HKI( "Schematic Field" ) )
278  .Map( SCH_COMPONENT_T, _HKI( "Component" ) )
279  .Map( SCH_SHEET_PIN_T, _HKI( "Sheet Pin" ) )
280  .Map( SCH_SHEET_T, _HKI( "Sheet" ) )
281 
282  // Synthetic search tokens don't need to be included...
283  //.Map( SCH_FIELD_LOCATE_REFERENCE_T, _HKI( "Field Locate Reference" ) )
284  //.Map( SCH_FIELD_LOCATE_VALUE_T, _HKI( "Field Locate Value" ) )
285  //.Map( SCH_FIELD_LOCATE_FOOTPRINT_T, _HKI( "Field Locate Footprint" ) )
286 
287  .Map( SCH_SCREEN_T, _HKI( "SCH Screen" ) )
288 
289  .Map( LIB_PART_T, _HKI( "Symbol" ) )
290  .Map( LIB_ALIAS_T, _HKI( "Alias" ) )
291  .Map( LIB_ARC_T, _HKI( "Arc" ) )
292  .Map( LIB_CIRCLE_T, _HKI( "Circle" ) )
293  .Map( LIB_TEXT_T, _HKI( "Symbol Text" ) )
294  .Map( LIB_RECTANGLE_T, _HKI( "Rectangle" ) )
295  .Map( LIB_POLYLINE_T, _HKI( "Polyline" ) )
296  .Map( LIB_BEZIER_T, _HKI( "Bezier" ) )
297  .Map( LIB_PIN_T, _HKI( "Pin" ) )
298  .Map( LIB_FIELD_T, _HKI( "Symbol Field" ) )
299 
300  .Map( GERBER_LAYOUT_T, _HKI( "Gerber Layout" ) )
301  .Map( GERBER_DRAW_ITEM_T, _HKI( "Draw Item" ) )
302  .Map( GERBER_IMAGE_T, _HKI( "Image" ) );
303 
306  propMgr.AddProperty( new PROPERTY_ENUM<EDA_ITEM, KICAD_T>( "Type",
308  }
310 
void SetModified()
Definition: eda_item.cpp:63
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:529
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:91
#define IS_CHANGED
Item was edited, and modified.
Definition: eda_item.h:103
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
virtual bool Matches(const wxFindReplaceData &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:415
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:169
class ALIGNED_DIMENSION, a linear dimension (graphic item)
Definition: typeinfo.h:100
class 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 CENTER_DIMENSION, 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
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
ENUM_TO_WXANY(KICAD_T)
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
class PAD, a pad in a footprint
Definition: typeinfo.h:89
#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:95
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:81
not really an item, used to identify a screen
Definition: typeinfo.h:85
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
STATUS_FLAGS m_status
Definition: eda_item.h:528
void SetFlags(STATUS_FLAGS aMask)
Definition: eda_item.h:203
#define NULL
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:199
const wxPoint GetOrigin() const
Definition: eda_rect.h:106
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:233
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:143
EDA_ITEM & operator=(const EDA_ITEM &aItem)
Assign the members of aItem to another object.
Definition: eda_item.cpp:177
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType)
Definition: eda_item.cpp:35
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 MARKER_PCB, 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:107
virtual BITMAPS GetMenuImage() const
Return a pointer to an image to be used in menus.
Definition: eda_item.cpp:206
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:453
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:150
KICAD_T m_structType
Run time identification, keep private so it can never be changed after a ctor sets it.
Definition: eda_item.h:538
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 ORTHOGONAL_DIMENSION, a linear dimension constrained to x/y
Definition: typeinfo.h:103
class 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:73
STATUS_FLAGS m_flags
Definition: eda_item.h:531
virtual const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Definition: eda_item.cpp:190
SEARCH_RESULT
Definition: eda_item.h:41
bool m_forceVisible
Definition: eda_item.h:530
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
#define _HKI(x)
const wxSize GetSize() const
Definition: eda_rect.h:96
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:163