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.

515 lines
15 KiB

11 years ago
14 years ago
14 years ago
14 years ago
14 years ago
Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
12 years ago
Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
12 years ago
14 years ago
* 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
15 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
  5. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.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. #include <functional>
  26. using namespace std::placeholders;
  27. #include <class_board.h>
  28. #include <class_module.h>
  29. #include <confirm.h>
  30. #include <connectivity/connectivity_data.h>
  31. #include <dialog_choose_footprint.h>
  32. #include <dialog_get_footprint_by_name.h>
  33. #include <dialog_helpers.h>
  34. #include <footprint_edit_frame.h>
  35. #include <footprint_info_impl.h>
  36. #include <footprint_tree_pane.h>
  37. #include <footprint_viewer_frame.h>
  38. #include <fp_lib_table.h>
  39. #include <io_mgr.h>
  40. #include <kicad_string.h>
  41. #include <kiway.h>
  42. #include <lib_id.h>
  43. #include <macros.h>
  44. #include <pcb_draw_panel_gal.h>
  45. #include <pcb_edit_frame.h>
  46. #include <pcbnew.h>
  47. #include <pcbnew_settings.h>
  48. #include <pgm_base.h>
  49. #include <view/view_controls.h>
  50. #include <widgets/lib_tree.h>
  51. #include <widgets/progress_reporter.h>
  52. #include "fp_tree_model_adapter.h"
  53. static wxArrayString s_ModuleHistoryList;
  54. static unsigned s_ModuleHistoryMaxCount = 8;
  55. static void AddModuleToHistory( const wxString& aName )
  56. {
  57. // Remove duplicates
  58. for( int ii = s_ModuleHistoryList.GetCount() - 1; ii >= 0; --ii )
  59. {
  60. if( s_ModuleHistoryList[ ii ] == aName )
  61. s_ModuleHistoryList.RemoveAt( (size_t) ii );
  62. }
  63. // Add the new name at the beginning of the history list
  64. s_ModuleHistoryList.Insert( aName, 0 );
  65. // Remove extra names
  66. while( s_ModuleHistoryList.GetCount() >= s_ModuleHistoryMaxCount )
  67. s_ModuleHistoryList.RemoveAt( s_ModuleHistoryList.GetCount() - 1 );
  68. }
  69. static void clearModuleItemFlags( BOARD_ITEM* aItem )
  70. {
  71. aItem->ClearFlags();
  72. }
  73. #include <bitmaps.h>
  74. bool FOOTPRINT_EDIT_FRAME::Load_Module_From_BOARD( MODULE* aModule )
  75. {
  76. bool is_last_fp_from_brd = IsCurrentFPFromBoard();
  77. MODULE* newModule;
  78. PCB_EDIT_FRAME* frame = (PCB_EDIT_FRAME*) Kiway().Player( FRAME_PCB_EDITOR, false );
  79. if( frame == NULL ) // happens if no board editor opened
  80. return false;
  81. if( aModule == NULL )
  82. {
  83. if( !frame->GetBoard() || !frame->GetBoard()->GetFirstModule() )
  84. return false;
  85. aModule = SelectFootprintFromBoard( frame->GetBoard() );
  86. }
  87. if( aModule == NULL )
  88. return false;
  89. if( !Clear_Pcb( true ) )
  90. return false;
  91. newModule = (MODULE*) aModule->Duplicate();
  92. newModule->SetParent( GetBoard() );
  93. newModule->SetLink( aModule->m_Uuid );
  94. newModule->ClearFlags();
  95. newModule->RunOnChildren( std::bind( &clearModuleItemFlags, _1 ) );
  96. AddModuleToBoard( newModule );
  97. // Clear references to any net info, because the footprint editor
  98. // does know any thing about nets handled by the current edited board.
  99. // Morever we do not want to save any reference to an unknown net when
  100. // saving the footprint in lib cache
  101. // so we force the ORPHANED dummy net info for all pads
  102. newModule->ClearAllNets();
  103. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  104. PlaceModule( newModule );
  105. newModule->SetPosition( wxPoint( 0, 0 ) ); // cursor in GAL may not be initialized at the moment
  106. // Put it on FRONT layer,
  107. // because this is the default in ModEdit, and in libs
  108. if( newModule->GetLayer() != F_Cu )
  109. newModule->Flip( newModule->GetPosition(), frame->Settings().m_FlipLeftRight );
  110. // Put it in orientation 0,
  111. // because this is the default orientation in ModEdit, and in libs
  112. newModule->SetOrientation( 0 );
  113. Zoom_Automatique( false );
  114. m_adapter->SetPreselectNode( newModule->GetFPID(), 0 );
  115. ClearUndoRedoList();
  116. GetScreen()->ClrModify();
  117. // Update the save items if needed.
  118. if( !is_last_fp_from_brd )
  119. {
  120. ReCreateMenuBar();
  121. ReCreateHToolbar();
  122. if( IsSearchTreeShown() )
  123. ToggleSearchTree();
  124. }
  125. Update3DView( true );
  126. updateView();
  127. GetCanvas()->Refresh();
  128. m_treePane->GetLibTree()->RefreshLibTree(); // update any previously-highlighted items
  129. return true;
  130. }
  131. wxString PCB_BASE_FRAME::SelectFootprintFromLibBrowser()
  132. {
  133. // Close the current non-modal Lib browser if opened, and open a new one, in "modal" mode:
  134. FOOTPRINT_VIEWER_FRAME* viewer;
  135. viewer = (FOOTPRINT_VIEWER_FRAME*) Kiway().Player( FRAME_FOOTPRINT_VIEWER, false );
  136. if( viewer )
  137. {
  138. viewer->Destroy();
  139. // Destroy() does not immediately delete the viewer, if some events are pending.
  140. // (for this reason delete operator cannot be used blindly with "top level" windows)
  141. // so gives a slice of time to delete the viewer frame.
  142. // This is especially important in OpenGL mode to avoid recreating context before
  143. // the old one is deleted
  144. wxSafeYield();
  145. }
  146. SetFocus();
  147. // Creates the modal Lib browser:
  148. viewer = (FOOTPRINT_VIEWER_FRAME*) Kiway().Player( FRAME_FOOTPRINT_VIEWER_MODAL, true, this );
  149. wxString fpid;
  150. int ret = viewer->ShowModal( &fpid, this );
  151. (void) ret; // make static analyser quiet
  152. viewer->Destroy();
  153. return fpid;
  154. }
  155. MODULE* PCB_BASE_FRAME::SelectFootprintFromLibTree( LIB_ID aPreselect )
  156. {
  157. FP_LIB_TABLE* fpTable = Prj().PcbFootprintLibs();
  158. wxString moduleName;
  159. LIB_ID fpid;
  160. MODULE* module = NULL;
  161. static wxString lastComponentName;
  162. // Load footprint files:
  163. WX_PROGRESS_REPORTER* progressReporter = new WX_PROGRESS_REPORTER( this,
  164. _( "Loading Footprint Libraries" ), 3 );
  165. GFootprintList.ReadFootprintFiles( fpTable, nullptr, progressReporter );
  166. bool cancel = progressReporter->WasCancelled();
  167. progressReporter->Destroy();
  168. if( cancel )
  169. return nullptr;
  170. if( GFootprintList.GetErrorCount() )
  171. GFootprintList.DisplayErrors( this );
  172. auto adapterPtr( FP_TREE_MODEL_ADAPTER::Create( this, fpTable ) );
  173. auto adapter = static_cast<FP_TREE_MODEL_ADAPTER*>( adapterPtr.get() );
  174. std::vector<LIB_TREE_ITEM*> historyInfos;
  175. for( auto const& item : s_ModuleHistoryList )
  176. {
  177. LIB_TREE_ITEM* fp_info = GFootprintList.GetModuleInfo( item );
  178. // this can be null, for example, if the footprint has been deleted from a library.
  179. if( fp_info != nullptr )
  180. historyInfos.push_back( fp_info );
  181. }
  182. adapter->DoAddLibrary( "-- " + _( "Recently Used" ) + " --", wxEmptyString, historyInfos, true );
  183. if( aPreselect.IsValid() )
  184. adapter->SetPreselectNode( aPreselect, 0 );
  185. else if( historyInfos.size() )
  186. adapter->SetPreselectNode( historyInfos[0]->GetLibId(), 0 );
  187. adapter->AddLibraries();
  188. wxString title;
  189. title.Printf( _( "Choose Footprint (%d items loaded)" ), adapter->GetItemCount() );
  190. DIALOG_CHOOSE_FOOTPRINT dialog( this, title, adapterPtr );
  191. if( dialog.ShowQuasiModal() == wxID_CANCEL )
  192. return NULL;
  193. if( dialog.IsExternalBrowserSelected() )
  194. {
  195. // SelectFootprintFromLibBrowser() returns the "full" footprint name, i.e.
  196. // <lib_name>/<footprint name> or LIB_ID format "lib_name:fp_name:rev#"
  197. moduleName = SelectFootprintFromLibBrowser();
  198. if( moduleName.IsEmpty() ) // Cancel command
  199. return NULL;
  200. else
  201. fpid.Parse( moduleName, LIB_ID::ID_PCB );
  202. }
  203. else
  204. {
  205. fpid = dialog.GetSelectedLibId();
  206. if( !fpid.IsValid() )
  207. return NULL;
  208. else
  209. moduleName = fpid.Format();
  210. }
  211. try
  212. {
  213. module = loadFootprint( fpid );
  214. }
  215. catch( const IO_ERROR& ioe )
  216. {
  217. wxLogDebug( wxT( "Error loading footprint '%s'.\n\nError: %s" ),
  218. fpid.Format().c_str(),
  219. ioe.What() );
  220. }
  221. if( module )
  222. {
  223. lastComponentName = moduleName;
  224. AddModuleToHistory( moduleName );
  225. }
  226. return module;
  227. }
  228. MODULE* PCB_BASE_FRAME::LoadFootprint( const LIB_ID& aFootprintId )
  229. {
  230. MODULE* module = NULL;
  231. try
  232. {
  233. module = loadFootprint( aFootprintId );
  234. }
  235. catch( const IO_ERROR& ioe )
  236. {
  237. wxLogDebug( wxT( "An error occurred attemping to load footprint '%s'.\n\nError: %s" ),
  238. aFootprintId.Format().c_str(), GetChars( ioe.What() ) );
  239. }
  240. return module;
  241. }
  242. MODULE* PCB_BASE_FRAME::loadFootprint( const LIB_ID& aFootprintId )
  243. {
  244. FP_LIB_TABLE* fptbl = Prj().PcbFootprintLibs();
  245. wxCHECK_MSG( fptbl, NULL, wxT( "Cannot look up LIB_ID in NULL FP_LIB_TABLE." ) );
  246. MODULE *module = nullptr;
  247. try
  248. {
  249. module = fptbl->FootprintLoadWithOptionalNickname( aFootprintId );
  250. }
  251. catch( const IO_ERROR& ioe )
  252. {
  253. wxLogDebug( wxT( "An error occurred attemping to load footprint '%s'.\n\nError: %s" ),
  254. aFootprintId.Format().c_str(), GetChars( ioe.What() ) );
  255. }
  256. // If the module is found, clear all net info,
  257. // to be sure there is no broken links
  258. // to any netinfo list (should be not needed, but it can be edited from
  259. // the footprint editor )
  260. if( module )
  261. module->ClearAllNets();
  262. return module;
  263. }
  264. MODULE* FOOTPRINT_EDIT_FRAME::SelectFootprintFromBoard( BOARD* aPcb )
  265. {
  266. static wxString oldName; // Save name of last module selected.
  267. wxString fpname;
  268. wxString msg;
  269. wxArrayString listnames;
  270. for( auto module : aPcb->Modules() )
  271. listnames.Add( module->GetReference() );
  272. msg.Printf( _( "Footprints [%u items]" ), (unsigned) listnames.GetCount() );
  273. wxArrayString headers;
  274. headers.Add( _( "Footprint" ) );
  275. std::vector<wxArrayString> itemsToDisplay;
  276. // Conversion from wxArrayString to vector of ArrayString
  277. for( unsigned i = 0; i < listnames.GetCount(); i++ )
  278. {
  279. wxArrayString item;
  280. item.Add( listnames[i] );
  281. itemsToDisplay.push_back( item );
  282. }
  283. EDA_LIST_DIALOG dlg( this, msg, headers, itemsToDisplay, wxEmptyString );
  284. if( dlg.ShowModal() == wxID_OK )
  285. fpname = dlg.GetTextSelection();
  286. else
  287. return NULL;
  288. oldName = fpname;
  289. for( auto mod : aPcb->Modules() )
  290. {
  291. if( fpname == mod->GetReference() )
  292. return mod;
  293. }
  294. return nullptr;
  295. }
  296. bool FOOTPRINT_EDIT_FRAME::SaveLibraryAs( const wxString& aLibraryPath )
  297. {
  298. const wxString& curLibPath = aLibraryPath;
  299. wxString dstLibPath = CreateNewLibrary( wxEmptyString, aLibraryPath );
  300. if( !dstLibPath )
  301. return false; // user aborted in CreateNewLibrary()
  302. wxBusyCursor dummy;
  303. wxString msg;
  304. IO_MGR::PCB_FILE_T dstType = IO_MGR::GuessPluginTypeFromLibPath( dstLibPath );
  305. IO_MGR::PCB_FILE_T curType = IO_MGR::GuessPluginTypeFromLibPath( curLibPath );
  306. try
  307. {
  308. PLUGIN::RELEASER cur( IO_MGR::PluginFind( curType ) );
  309. PLUGIN::RELEASER dst( IO_MGR::PluginFind( dstType ) );
  310. wxArrayString footprints;
  311. cur->FootprintEnumerate( footprints, curLibPath, false );
  312. for( unsigned i = 0; i < footprints.size(); ++i )
  313. {
  314. const MODULE* footprint = cur->GetEnumeratedFootprint( curLibPath, footprints[i] );
  315. dst->FootprintSave( dstLibPath, footprint );
  316. msg = wxString::Format( _( "Footprint \"%s\" saved" ), footprints[i] );
  317. SetStatusText( msg );
  318. }
  319. }
  320. catch( const IO_ERROR& ioe )
  321. {
  322. DisplayError( this, ioe.What() );
  323. return false;
  324. }
  325. msg = wxString::Format( _( "Footprint library \"%s\" saved as \"%s\"." ),
  326. curLibPath,
  327. dstLibPath );
  328. DisplayInfoMessage( this, msg );
  329. SetStatusText( wxEmptyString );
  330. return true;
  331. }
  332. static MODULE* s_ModuleInitialCopy = NULL; // Copy of module for abort/undo command
  333. static PICKED_ITEMS_LIST s_PickedList; // a pick-list to save initial module
  334. // and dragged tracks
  335. MODULE* PCB_BASE_FRAME::GetFootprintFromBoardByReference()
  336. {
  337. wxString moduleName;
  338. wxArrayString fplist;
  339. // Build list of available fp references, to display them in dialog
  340. for( auto fp : GetBoard()->Modules() )
  341. fplist.Add( fp->GetReference() + wxT(" ( ") + fp->GetValue() + wxT(" )") );
  342. fplist.Sort();
  343. DIALOG_GET_FOOTPRINT_BY_NAME dlg( this, fplist );
  344. if( dlg.ShowModal() != wxID_OK ) //Aborted by user
  345. return NULL;
  346. moduleName = dlg.GetValue();
  347. moduleName.Trim( true );
  348. moduleName.Trim( false );
  349. if( !moduleName.IsEmpty() )
  350. {
  351. for( auto mod : GetBoard()->Modules() )
  352. {
  353. if( mod->GetReference().CmpNoCase( moduleName ) == 0 )
  354. return mod;
  355. }
  356. }
  357. return nullptr;
  358. }
  359. void PCB_BASE_FRAME::PlaceModule( MODULE* aModule, bool aRecreateRatsnest )
  360. {
  361. if( aModule == 0 )
  362. return;
  363. OnModify();
  364. if( aModule->IsNew() )
  365. {
  366. SaveCopyInUndoList( aModule, UR_NEW );
  367. }
  368. else if( aModule->IsMoving() )
  369. {
  370. ITEM_PICKER picker( nullptr, aModule, UR_CHANGED );
  371. picker.SetLink( s_ModuleInitialCopy );
  372. s_PickedList.PushItem( picker );
  373. s_ModuleInitialCopy = NULL; // the picker is now owner of s_ModuleInitialCopy.
  374. }
  375. if( s_PickedList.GetCount() )
  376. {
  377. SaveCopyInUndoList( s_PickedList, UR_UNSPECIFIED );
  378. // Clear list, but DO NOT delete items, because they are owned by the saved undo
  379. // list and they therefore in use
  380. s_PickedList.ClearItemsList();
  381. }
  382. aModule->SetPosition( (wxPoint) GetCanvas()->GetViewControls()->GetCursorPosition() );
  383. aModule->ClearFlags();
  384. delete s_ModuleInitialCopy;
  385. s_ModuleInitialCopy = NULL;
  386. if( aRecreateRatsnest )
  387. m_Pcb->GetConnectivity()->Update( aModule );
  388. if( aRecreateRatsnest )
  389. Compile_Ratsnest( true );
  390. SetMsgPanel( aModule );
  391. }