You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

401 lines
12 KiB

  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-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. #include <algorithm>
  25. #include <bitmaps.h>
  26. #include <eda_item.h>
  27. #include <trace_helpers.h>
  28. #include <trigo.h>
  29. #include <i18n_utility.h>
  30. #include <wx/log.h>
  31. #include <wx/fdrepdlg.h>
  32. #include <eda_pattern_match.h>
  33. EDA_ITEM::EDA_ITEM( EDA_ITEM* parent, KICAD_T idType ) :
  34. m_parent( parent ),
  35. m_forceVisible( false ),
  36. m_flags( 0 ),
  37. m_structType( idType )
  38. { }
  39. EDA_ITEM::EDA_ITEM( KICAD_T idType ) :
  40. m_parent( nullptr ),
  41. m_forceVisible( false ),
  42. m_flags( 0 ),
  43. m_structType( idType )
  44. { }
  45. EDA_ITEM::EDA_ITEM( const EDA_ITEM& base ) :
  46. m_Uuid( base.m_Uuid ),
  47. m_parent( base.m_parent ),
  48. m_forceVisible( base.m_forceVisible ),
  49. m_flags( base.m_flags ),
  50. m_structType( base.m_structType )
  51. {
  52. SetForcedTransparency( base.GetForcedTransparency() );
  53. }
  54. void EDA_ITEM::SetModified()
  55. {
  56. SetFlags( IS_CHANGED );
  57. // If this a child object, then the parent modification state also needs to be set.
  58. if( m_parent )
  59. m_parent->SetModified();
  60. }
  61. const BOX2I EDA_ITEM::GetBoundingBox() const
  62. {
  63. // return a zero-sized box per default. derived classes should override
  64. // this
  65. return BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( 0, 0 ) );
  66. }
  67. EDA_ITEM* EDA_ITEM::Clone() const
  68. {
  69. wxCHECK_MSG( false, nullptr, wxT( "Clone not implemented in derived class " ) + GetClass() +
  70. wxT( ". Bad programmer!" ) );
  71. }
  72. // see base_struct.h
  73. // many classes inherit this method, be careful:
  74. INSPECT_RESULT EDA_ITEM::Visit( INSPECTOR inspector, void* testData,
  75. const std::vector<KICAD_T>& aScanTypes )
  76. {
  77. #if 0 && defined(DEBUG)
  78. std::cout << GetClass().mb_str() << ' ';
  79. #endif
  80. if( IsType( aScanTypes ) )
  81. {
  82. if( INSPECT_RESULT::QUIT == inspector( this, testData ) )
  83. return INSPECT_RESULT::QUIT;
  84. }
  85. return INSPECT_RESULT::CONTINUE;
  86. }
  87. wxString EDA_ITEM::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
  88. {
  89. wxFAIL_MSG( wxT( "GetItemDescription() was not overridden for schematic item type " ) +
  90. GetClass() );
  91. return wxString( wxT( "Undefined item description for " ) + GetClass() );
  92. }
  93. bool EDA_ITEM::Matches( const wxString& aText, const EDA_SEARCH_DATA& aSearchData ) const
  94. {
  95. wxString text = aText;
  96. wxString searchText = aSearchData.findString;
  97. // Don't match if searching for replaceable item and the item doesn't support text replace.
  98. if( aSearchData.searchAndReplace && !IsReplaceable() )
  99. return false;
  100. if( !aSearchData.matchCase )
  101. {
  102. text.MakeUpper();
  103. searchText.MakeUpper();
  104. }
  105. if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::PERMISSIVE )
  106. {
  107. EDA_COMBINED_MATCHER matcher( searchText, CTX_SEARCH );
  108. return matcher.Find( text );
  109. }
  110. else if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::WHOLEWORD )
  111. {
  112. int ii = 0;
  113. while( ii < (int) text.length() )
  114. {
  115. int next = text.find( searchText, ii );
  116. if( next == wxNOT_FOUND )
  117. return false;
  118. ii = next;
  119. next += searchText.length();
  120. bool startOK = ( ii == 0 || !wxIsalnum( text.GetChar( ii - 1 ) ) );
  121. bool endOK = ( next == (int) text.length() || !wxIsalnum( text.GetChar( next ) ) );
  122. if( startOK && endOK )
  123. return true;
  124. else
  125. ii++;
  126. }
  127. return false;
  128. }
  129. else if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::WILDCARD )
  130. {
  131. return text.Matches( searchText );
  132. }
  133. else
  134. {
  135. return text.Find( searchText ) != wxNOT_FOUND;
  136. }
  137. }
  138. bool EDA_ITEM::Replace( const EDA_SEARCH_DATA& aSearchData, wxString& aText )
  139. {
  140. wxString text = aText;
  141. wxString searchText = aSearchData.findString;
  142. wxString result;
  143. bool replaced = false;
  144. if( !aSearchData.matchCase )
  145. {
  146. text = text.Upper();
  147. searchText = searchText.Upper();
  148. }
  149. int ii = 0;
  150. while( ii < (int) text.length() )
  151. {
  152. int next = text.find( searchText, ii );
  153. if( next == wxNOT_FOUND )
  154. {
  155. result += aText.Mid( ii, wxString::npos );
  156. break;
  157. }
  158. if( next > ii )
  159. result += aText.Mid( ii, next - ii );
  160. ii = next;
  161. next += searchText.length();
  162. bool startOK;
  163. bool endOK;
  164. if( aSearchData.matchMode == EDA_SEARCH_MATCH_MODE::WHOLEWORD )
  165. {
  166. startOK = ( ii == 0 || !wxIsalnum( text.GetChar( ii - 1 ) ) );
  167. endOK = ( next == (int) text.length() || !wxIsalnum( text.GetChar( next ) ) );
  168. }
  169. else
  170. {
  171. startOK = true;
  172. endOK = true;
  173. }
  174. if( startOK && endOK )
  175. {
  176. result += aSearchData.replaceString;
  177. replaced = true;
  178. ii = next;
  179. }
  180. else
  181. {
  182. result += aText.GetChar( ii );
  183. ii++;
  184. }
  185. }
  186. aText = result;
  187. return replaced;
  188. }
  189. bool EDA_ITEM::operator<( const EDA_ITEM& aItem ) const
  190. {
  191. wxFAIL_MSG( wxString::Format( wxT( "Less than operator not defined for item type %s." ),
  192. GetClass() ) );
  193. return false;
  194. }
  195. EDA_ITEM& EDA_ITEM::operator=( const EDA_ITEM& aItem )
  196. {
  197. // do not call initVars()
  198. m_structType = aItem.m_structType;
  199. m_flags = aItem.m_flags;
  200. m_parent = aItem.m_parent;
  201. m_forceVisible = aItem.m_forceVisible;
  202. SetForcedTransparency( aItem.GetForcedTransparency() );
  203. return *this;
  204. }
  205. const BOX2I EDA_ITEM::ViewBBox() const
  206. {
  207. // Basic fallback
  208. return GetBoundingBox();
  209. }
  210. void EDA_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
  211. {
  212. // Basic fallback
  213. aCount = 1;
  214. aLayers[0] = 0;
  215. }
  216. BITMAPS EDA_ITEM::GetMenuImage() const
  217. {
  218. return BITMAPS::dummy_item;
  219. }
  220. #if defined( DEBUG )
  221. void EDA_ITEM::ShowDummy( std::ostream& os ) const
  222. {
  223. // XML output:
  224. wxString s = GetClass();
  225. os << '<' << s.Lower().mb_str() << ">"
  226. << " Need ::Show() override for this class "
  227. << "</" << s.Lower().mb_str() << ">\n";
  228. }
  229. std::ostream& EDA_ITEM::NestedSpace( int nestLevel, std::ostream& os )
  230. {
  231. for( int i = 0; i<nestLevel; ++i )
  232. os << " ";
  233. // number of spaces here controls indent per nest level
  234. return os;
  235. }
  236. #endif
  237. wxString EDA_ITEM::GetTypeDesc() const
  238. {
  239. //@see EDA_ITEM_DESC for definition of ENUM_MAP<KICAD_T>
  240. wxString typeDescr = ENUM_MAP<KICAD_T>::Instance().ToString( Type() );
  241. return wxGetTranslation( typeDescr );
  242. }
  243. wxString EDA_ITEM::GetFriendlyName() const
  244. {
  245. return GetTypeDesc();
  246. }
  247. static struct EDA_ITEM_DESC
  248. {
  249. EDA_ITEM_DESC()
  250. {
  251. ENUM_MAP<KICAD_T>::Instance()
  252. .Undefined( TYPE_NOT_INIT )
  253. .Map( NOT_USED, wxT( "<not used>" ) )
  254. .Map( SCREEN_T, _HKI( "Screen" ) )
  255. .Map( PCB_FOOTPRINT_T, _HKI( "Footprint" ) )
  256. .Map( PCB_PAD_T, _HKI( "Pad" ) )
  257. .Map( PCB_SHAPE_T, _HKI( "Graphic" ) )
  258. .Map( PCB_BITMAP_T, _HKI( "Bitmap" ) )
  259. .Map( PCB_FIELD_T, _HKI( "Field" ) )
  260. .Map( PCB_TEXT_T, _HKI( "Text" ) )
  261. .Map( PCB_TEXTBOX_T, _HKI( "Text Box" ) )
  262. .Map( PCB_TRACE_T, _HKI( "Track" ) )
  263. .Map( PCB_ARC_T, _HKI( "Track" ) )
  264. .Map( PCB_VIA_T, _HKI( "Via" ) )
  265. .Map( PCB_MARKER_T, _HKI( "Marker" ) )
  266. .Map( PCB_DIM_ALIGNED_T, _HKI( "Dimension" ) )
  267. .Map( PCB_DIM_ORTHOGONAL_T, _HKI( "Dimension" ) )
  268. .Map( PCB_DIM_CENTER_T, _HKI( "Dimension" ) )
  269. .Map( PCB_DIM_RADIAL_T, _HKI( "Dimension" ) )
  270. .Map( PCB_DIM_LEADER_T, _HKI( "Leader" ) )
  271. .Map( PCB_TARGET_T, _HKI( "Target" ) )
  272. .Map( PCB_ZONE_T, _HKI( "Zone" ) )
  273. .Map( PCB_ITEM_LIST_T, _HKI( "ItemList" ) )
  274. .Map( PCB_NETINFO_T, _HKI( "NetInfo" ) )
  275. .Map( PCB_GROUP_T, _HKI( "Group" ) )
  276. .Map( SCH_MARKER_T, _HKI( "Marker" ) )
  277. .Map( SCH_JUNCTION_T, _HKI( "Junction" ) )
  278. .Map( SCH_NO_CONNECT_T, _HKI( "No-Connect Flag" ) )
  279. .Map( SCH_BUS_WIRE_ENTRY_T, _HKI( "Wire Entry" ) )
  280. .Map( SCH_BUS_BUS_ENTRY_T, _HKI( "Bus Entry" ) )
  281. .Map( SCH_LINE_T, _HKI( "Line" ) )
  282. .Map( SCH_BITMAP_T, _HKI( "Bitmap" ) )
  283. .Map( SCH_SHAPE_T, _HKI( "Graphic" ) )
  284. .Map( SCH_TEXT_T, _HKI( "Text" ) )
  285. .Map( SCH_TEXTBOX_T, _HKI( "Text Box" ) )
  286. .Map( SCH_LABEL_T, _HKI( "Net Label" ) )
  287. .Map( SCH_DIRECTIVE_LABEL_T, _HKI( "Directive Label" ) )
  288. .Map( SCH_GLOBAL_LABEL_T, _HKI( "Global Label" ) )
  289. .Map( SCH_HIER_LABEL_T, _HKI( "Hierarchical Label" ) )
  290. .Map( SCH_FIELD_T, _HKI( "Field" ) )
  291. .Map( SCH_SYMBOL_T, _HKI( "Symbol" ) )
  292. .Map( SCH_PIN_T, _HKI( "Pin" ) )
  293. .Map( SCH_SHEET_PIN_T, _HKI( "Sheet Pin" ) )
  294. .Map( SCH_SHEET_T, _HKI( "Sheet" ) )
  295. // Synthetic search tokens don't need to be included...
  296. //.Map( SCH_FIELD_LOCATE_REFERENCE_T, _HKI( "Field Locate Reference" ) )
  297. //.Map( SCH_FIELD_LOCATE_VALUE_T, _HKI( "Field Locate Value" ) )
  298. //.Map( SCH_FIELD_LOCATE_FOOTPRINT_T, _HKI( "Field Locate Footprint" ) )
  299. .Map( SCH_SCREEN_T, _HKI( "SCH Screen" ) )
  300. .Map( LIB_SYMBOL_T, _HKI( "Symbol" ) )
  301. .Map( LIB_SHAPE_T, _HKI( "Graphic" ) )
  302. .Map( LIB_TEXT_T, _HKI( "Text" ) )
  303. .Map( LIB_TEXTBOX_T, _HKI( "Text Box" ) )
  304. .Map( LIB_PIN_T, _HKI( "Pin" ) )
  305. .Map( LIB_FIELD_T, _HKI( "Symbol Field" ) )
  306. .Map( GERBER_LAYOUT_T, _HKI( "Gerber Layout" ) )
  307. .Map( GERBER_DRAW_ITEM_T, _HKI( "Draw Item" ) )
  308. .Map( GERBER_IMAGE_T, _HKI( "Image" ) );
  309. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  310. REGISTER_TYPE( EDA_ITEM );
  311. propMgr.AddProperty( new PROPERTY_ENUM<EDA_ITEM, KICAD_T>( wxS( "Type" ),
  312. NO_SETTER( EDA_ITEM, KICAD_T ), &EDA_ITEM::Type ) )
  313. .SetIsHiddenFromPropertiesManager();
  314. }
  315. } _EDA_ITEM_DESC;
  316. ENUM_TO_WXANY( KICAD_T );