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.

417 lines
12 KiB

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