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.

443 lines
14 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
16 years ago
18 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
  6. * Copyright (C) 2004-2018 KiCad Developers, see change_log.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file eeschema/find.cpp
  27. * @brief Functions for searching for a schematic item.
  28. */
  29. /*
  30. * Search a text (text, value, reference) within a component or
  31. * search a component in libraries, a marker ...,
  32. * in current sheet or whole the project
  33. */
  34. #include <fctsys.h>
  35. #include <pgm_base.h>
  36. #include <sch_draw_panel.h>
  37. #include <confirm.h>
  38. #include <kicad_string.h>
  39. #include <gestfich.h>
  40. #include <sch_edit_frame.h>
  41. #include <base_units.h>
  42. #include <trace_helpers.h>
  43. #include <sch_view.h>
  44. #include <general.h>
  45. #include <class_library.h>
  46. #include <lib_pin.h>
  47. #include <sch_marker.h>
  48. #include <sch_component.h>
  49. #include <sch_sheet.h>
  50. #include <sch_sheet_path.h>
  51. #include <symbol_lib_table.h>
  52. #include <kicad_device_context.h>
  53. #include <dialogs/dialog_schematic_find.h>
  54. void SCH_EDIT_FRAME::OnFindDrcMarker( wxFindDialogEvent& event )
  55. {
  56. static SCH_MARKER* lastMarker = NULL;
  57. wxString msg;
  58. SCH_SHEET_LIST schematic( g_RootSheet );
  59. SCH_SHEET_PATH* sheetFoundIn = NULL;
  60. bool wrap = ( event.GetFlags() & FR_SEARCH_WRAP ) != 0;
  61. bool warpCursor = ( ( event.GetId() == wxEVT_COMMAND_FIND_CLOSE ) ||
  62. !( event.GetFlags() & FR_NO_WARP_CURSOR ) );
  63. if( event.GetFlags() & FR_CURRENT_SHEET_ONLY )
  64. {
  65. sheetFoundIn = m_CurrentSheet;
  66. lastMarker = (SCH_MARKER*) m_CurrentSheet->FindNextItem( SCH_MARKER_T, lastMarker, wrap );
  67. }
  68. else
  69. {
  70. lastMarker = (SCH_MARKER*) schematic.FindNextItem( SCH_MARKER_T, &sheetFoundIn,
  71. lastMarker, wrap );
  72. }
  73. if( lastMarker != NULL )
  74. {
  75. if( *sheetFoundIn != *m_CurrentSheet )
  76. {
  77. sheetFoundIn->LastScreen()->SetZoom( GetScreen()->GetZoom() );
  78. *m_CurrentSheet = *sheetFoundIn;
  79. m_CurrentSheet->UpdateAllScreenReferences();
  80. }
  81. SetCrossHairPosition( lastMarker->GetPosition() );
  82. CenterScreen( lastMarker->GetPosition(), warpCursor );
  83. msg.Printf( _( "Design rule check marker found in sheet %s at %s, %s" ),
  84. sheetFoundIn->Path(),
  85. MessageTextFromValue( m_UserUnits, lastMarker->GetPosition().x ),
  86. MessageTextFromValue( m_UserUnits, lastMarker->GetPosition().y ) );
  87. SetStatusText( msg );
  88. }
  89. else
  90. {
  91. SetStatusText( _( "No more markers were found." ) );
  92. }
  93. }
  94. SCH_ITEM* SCH_EDIT_FRAME::FindComponentAndItem( const wxString& aReference,
  95. bool aSearchHierarchy,
  96. SCH_SEARCH_T aSearchType,
  97. const wxString& aSearchText )
  98. {
  99. SCH_SHEET_PATH* sheet = NULL;
  100. SCH_SHEET_PATH* sheetWithComponentFound = NULL;
  101. SCH_ITEM* item = NULL;
  102. SCH_COMPONENT* Component = NULL;
  103. wxPoint pos;
  104. bool notFound = true;
  105. LIB_PIN* pin;
  106. SCH_SHEET_LIST sheetList( g_RootSheet );
  107. if( !aSearchHierarchy )
  108. sheetList.push_back( *m_CurrentSheet );
  109. else
  110. sheetList.BuildSheetList( g_RootSheet );
  111. for( SCH_SHEET_PATHS_ITER it = sheetList.begin(); it != sheetList.end(); ++it )
  112. {
  113. sheet = &(*it);
  114. item = (*it).LastDrawList();
  115. for( ; ( item != NULL ) && ( notFound == true ); item = item->Next() )
  116. {
  117. if( item->Type() != SCH_COMPONENT_T )
  118. continue;
  119. SCH_COMPONENT* pSch = (SCH_COMPONENT*) item;
  120. if( aReference.CmpNoCase( pSch->GetRef( sheet ) ) == 0 )
  121. {
  122. Component = pSch;
  123. sheetWithComponentFound = sheet;
  124. switch( aSearchType )
  125. {
  126. default:
  127. case FIND_COMPONENT_ONLY: // Find component only
  128. notFound = false;
  129. pos = pSch->GetPosition();
  130. break;
  131. case FIND_PIN: // find a pin
  132. pos = pSch->GetPosition(); // temporary: will be changed if the pin is found.
  133. pin = pSch->GetPin( aSearchText );
  134. if( pin == NULL )
  135. break;
  136. notFound = false;
  137. pos += pin->GetPosition();
  138. break;
  139. case FIND_REFERENCE: // find reference
  140. notFound = false;
  141. pos = pSch->GetField( REFERENCE )->GetPosition();
  142. break;
  143. case FIND_VALUE: // find value
  144. pos = pSch->GetPosition();
  145. if( aSearchText.CmpNoCase( pSch->GetField( VALUE )->GetShownText() ) != 0 )
  146. break;
  147. notFound = false;
  148. pos = pSch->GetField( VALUE )->GetPosition();
  149. break;
  150. }
  151. }
  152. }
  153. if( notFound == false )
  154. break;
  155. }
  156. if( Component )
  157. {
  158. sheet = sheetWithComponentFound;
  159. if( *sheet != *m_CurrentSheet )
  160. {
  161. sheet->LastScreen()->SetZoom( GetScreen()->GetZoom() );
  162. *m_CurrentSheet = *sheet;
  163. DisplayCurrentSheet();
  164. }
  165. wxPoint delta;
  166. pos -= Component->GetPosition();
  167. delta = Component->GetTransform().TransformCoordinate( pos );
  168. pos = delta + Component->GetPosition();
  169. SetCrossHairPosition( pos );
  170. CenterScreen( pos, false );
  171. }
  172. /* Print diag */
  173. wxString msg_item;
  174. wxString msg;
  175. switch( aSearchType )
  176. {
  177. default:
  178. case FIND_COMPONENT_ONLY: msg_item = _( "component" ); break;
  179. case FIND_PIN: msg_item.Printf( _( "pin %s" ), aSearchText ); break;
  180. case FIND_REFERENCE: msg_item.Printf( _( "reference %s" ), aSearchText ); break;
  181. case FIND_VALUE: msg_item.Printf( _( "value %s" ), aSearchText ); break;
  182. case FIND_FIELD: msg_item.Printf( _( "field %s" ), aSearchText ); break;
  183. }
  184. if( Component )
  185. {
  186. if( !notFound )
  187. msg.Printf( _( "%s %s found" ), aReference, msg_item );
  188. else
  189. msg.Printf( _( "%s found but %s not found" ), aReference, msg_item );
  190. }
  191. else
  192. msg.Printf( _( "Component %s not found" ), aReference );
  193. SetStatusText( msg );
  194. return item;
  195. }
  196. bool SCH_EDIT_FRAME::IsSearchCacheObsolete( const SCH_FIND_REPLACE_DATA& aSearchCriteria )
  197. {
  198. int mod_hash = Prj().SchSymbolLibTable()->GetModifyHash();
  199. // the cache is obsolete whenever any library changes.
  200. if( mod_hash != m_foundItems.GetLibHash() )
  201. {
  202. m_foundItems.SetForceSearch();
  203. m_foundItems.SetLibHash( mod_hash );
  204. return true;
  205. }
  206. else if( m_foundItems.IsSearchRequired( aSearchCriteria ) )
  207. return true;
  208. else
  209. return false;
  210. }
  211. void SCH_EDIT_FRAME::OnFindSchematicItem( wxFindDialogEvent& aEvent )
  212. {
  213. SCH_FIND_REPLACE_DATA searchCriteria;
  214. SCH_FIND_COLLECTOR_DATA data;
  215. searchCriteria.SetFlags( aEvent.GetFlags() );
  216. searchCriteria.SetFindString( aEvent.GetFindString() );
  217. searchCriteria.SetReplaceString( aEvent.GetReplaceString() );
  218. if( aEvent.GetEventType() == wxEVT_COMMAND_FIND_CLOSE )
  219. {
  220. if( m_foundItems.GetCount() == 0 )
  221. return;
  222. }
  223. else if( IsSearchCacheObsolete( searchCriteria ) )
  224. {
  225. if( aEvent.GetFlags() & FR_CURRENT_SHEET_ONLY && g_RootSheet->CountSheets() > 1 )
  226. {
  227. m_foundItems.Collect( searchCriteria, m_CurrentSheet );
  228. }
  229. else
  230. {
  231. m_foundItems.Collect( searchCriteria );
  232. }
  233. }
  234. else
  235. {
  236. EDA_ITEM* currentItem = m_foundItems.GetItem( data );
  237. if( currentItem != NULL )
  238. currentItem->SetForceVisible( false );
  239. m_foundItems.UpdateIndex();
  240. }
  241. updateFindReplaceView( aEvent );
  242. }
  243. void SCH_EDIT_FRAME::OnFindReplace( wxFindDialogEvent& aEvent )
  244. {
  245. SCH_ITEM* item;
  246. SCH_SHEET_PATH* sheet;
  247. SCH_SHEET_LIST schematic( g_RootSheet );
  248. SCH_FIND_COLLECTOR_DATA data;
  249. SCH_FIND_REPLACE_DATA searchCriteria;
  250. searchCriteria.SetFlags( aEvent.GetFlags() );
  251. searchCriteria.SetFindString( aEvent.GetFindString() );
  252. searchCriteria.SetReplaceString( aEvent.GetReplaceString() );
  253. m_foundItems.SetReplaceString( aEvent.GetReplaceString() );
  254. if( IsSearchCacheObsolete( searchCriteria ) )
  255. {
  256. if( aEvent.GetFlags() & FR_CURRENT_SHEET_ONLY && g_RootSheet->CountSheets() > 1 )
  257. {
  258. m_foundItems.Collect( searchCriteria, m_CurrentSheet );
  259. }
  260. else
  261. {
  262. m_foundItems.Collect( searchCriteria );
  263. }
  264. }
  265. if( aEvent.GetEventType() == wxEVT_COMMAND_FIND_REPLACE_ALL )
  266. {
  267. while( ( item = (SCH_ITEM*) m_foundItems.GetItem( data ) ) != NULL )
  268. {
  269. SCH_ITEM* undoItem = data.GetParent();
  270. // Don't save child items in undo list.
  271. if( undoItem == NULL )
  272. undoItem = item;
  273. SetUndoItem( undoItem );
  274. sheet = schematic.GetSheetByPath( data.GetSheetPath() );
  275. wxCHECK_RET( sheet != NULL, wxT( "Could not find sheet path " ) + data.GetSheetPath() );
  276. if( m_foundItems.ReplaceItem( sheet ) )
  277. {
  278. GetCanvas()->GetView()->Update( undoItem, KIGFX::ALL );
  279. OnModify();
  280. SaveUndoItemInUndoList( undoItem );
  281. updateFindReplaceView( aEvent );
  282. }
  283. m_foundItems.IncrementIndex();
  284. if( m_foundItems.PassedEnd() )
  285. break;
  286. }
  287. }
  288. else
  289. {
  290. item = (SCH_ITEM*) m_foundItems.GetItem( data );
  291. wxCHECK_RET( item != NULL, wxT( "Invalid replace item in find collector list." ) );
  292. SCH_ITEM* undoItem = data.GetParent();
  293. if( undoItem == NULL )
  294. undoItem = item;
  295. SetUndoItem( undoItem );
  296. sheet = schematic.GetSheetByPath( data.GetSheetPath() );
  297. wxCHECK_RET( sheet != NULL, wxT( "Could not find sheet path " ) + data.GetSheetPath() );
  298. if( m_foundItems.ReplaceItem( sheet ) )
  299. {
  300. GetCanvas()->GetView()->Update( undoItem, KIGFX::ALL );
  301. OnModify();
  302. SaveUndoItemInUndoList( undoItem );
  303. updateFindReplaceView( aEvent );
  304. // A single replace is part of the search; it does not dirty it.
  305. m_foundItems.SetForceSearch( false );
  306. }
  307. m_foundItems.IncrementIndex();
  308. }
  309. // End the replace if we are at the end if the list. This prevents an infinite loop if
  310. // wrap search is selected and all of the items have been replaced with a value that
  311. // still satisfies the search criteria.
  312. if( m_foundItems.PassedEnd() )
  313. aEvent.SetFlags( aEvent.GetFlags() & ~FR_REPLACE_ITEM_FOUND );
  314. }
  315. void SCH_EDIT_FRAME::updateFindReplaceView( wxFindDialogEvent& aEvent )
  316. {
  317. wxString msg;
  318. SCH_SHEET_LIST schematic( g_RootSheet );
  319. SCH_FIND_COLLECTOR_DATA data;
  320. bool warpCursor = !( aEvent.GetFlags() & FR_NO_WARP_CURSOR );
  321. if( m_foundItems.GetItem( data ) != NULL )
  322. {
  323. wxLogTrace( traceFindReplace, wxT( "Found " ) + m_foundItems.GetText( MILLIMETRES ) );
  324. SCH_SHEET_PATH* sheet = schematic.GetSheetByPath( data.GetSheetPath() );
  325. wxCHECK_RET( sheet != NULL, wxT( "Could not find sheet path " ) +
  326. data.GetSheetPath() );
  327. SCH_ITEM* item = (SCH_ITEM*)m_foundItems.GetItem( data );
  328. // Make the item temporarily visible just in case it's hide flag is set. This
  329. // has no effect on objects that don't support hiding. If this is a close find
  330. // dialog event, clear the temporary visibility flag.
  331. if( item )
  332. {
  333. if( aEvent.GetEventType() == wxEVT_COMMAND_FIND_CLOSE )
  334. item->SetForceVisible( false );
  335. else if( item->Type() == SCH_FIELD_T && !( (SCH_FIELD*) item )->IsVisible() )
  336. item->SetForceVisible( true );
  337. }
  338. if( sheet->PathHumanReadable() != m_CurrentSheet->PathHumanReadable() )
  339. {
  340. sheet->LastScreen()->SetZoom( GetScreen()->GetZoom() );
  341. *m_CurrentSheet = *sheet;
  342. m_CurrentSheet->UpdateAllScreenReferences();
  343. SetScreen( sheet->LastScreen() );
  344. sheet->LastScreen()->TestDanglingEnds();
  345. }
  346. SetCrossHairPosition( data.GetPosition() );
  347. CenterScreen( data.GetPosition(), warpCursor );
  348. msg = m_foundItems.GetText( m_UserUnits );
  349. if( aEvent.GetFlags() & FR_SEARCH_REPLACE )
  350. aEvent.SetFlags( aEvent.GetFlags() | FR_REPLACE_ITEM_FOUND );
  351. }
  352. else
  353. {
  354. if( aEvent.GetFlags() & FR_SEARCH_REPLACE )
  355. aEvent.SetFlags( aEvent.GetFlags() & ~FR_REPLACE_ITEM_FOUND );
  356. msg.Printf( _( "No item found matching %s." ), GetChars( aEvent.GetFindString() ) );
  357. }
  358. *m_findReplaceStatus = msg;
  359. SetStatusText( msg );
  360. }