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.

528 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
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2021 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. * @file eeschema/dialogs/dialog_bom.cpp
  26. * @brief Dialog box for creating bom and other documents from generic netlist.
  27. */
  28. #include <bitmaps.h>
  29. #include <bom_plugins.h>
  30. #include <confirm.h>
  31. #include <dialog_bom_base.h>
  32. #include <dialog_helpers.h>
  33. #include <eeschema_settings.h>
  34. #include <gestfich.h>
  35. #include <dialogs/html_messagebox.h>
  36. #include <i18n_utility.h> // for _HKI definition used in dialog_bom_help_md.h
  37. #include <invoke_sch_dialog.h>
  38. #include <kiface_i.h>
  39. #include <netlist_exporter_xml.h>
  40. #include <pgm_base.h>
  41. #include <reporter.h>
  42. #include <sch_edit_frame.h>
  43. #include <paths.h>
  44. #include <wx/filedlg.h>
  45. #include <wx/log.h>
  46. #include <wx/textdlg.h>
  47. wxString s_bomHelpInfo =
  48. #include <dialog_bom_help_md.h>
  49. ;
  50. // BOM "plugins" are not actually plugins. They are external tools
  51. // (scripts or executables) called by this dialog.
  52. typedef std::vector<BOM_GENERATOR_HANDLER::PTR> BOM_GENERATOR_ARRAY;
  53. // The main dialog frame to run scripts to build bom
  54. class DIALOG_BOM : public DIALOG_BOM_BASE
  55. {
  56. private:
  57. SCH_EDIT_FRAME* m_parent;
  58. BOM_GENERATOR_ARRAY m_generators;
  59. bool m_initialized;
  60. HTML_MESSAGE_BOX* m_helpWindow;
  61. public:
  62. DIALOG_BOM( SCH_EDIT_FRAME* parent );
  63. ~DIALOG_BOM();
  64. private:
  65. void OnGeneratorSelected( wxCommandEvent& event ) override;
  66. void OnRunGenerator( wxCommandEvent& event ) override;
  67. void OnHelp( wxCommandEvent& event ) override;
  68. void OnAddGenerator( wxCommandEvent& event ) override;
  69. void OnRemoveGenerator( wxCommandEvent& event ) override;
  70. void OnEditGenerator( wxCommandEvent& event ) override;
  71. void OnCommandLineEdited( wxCommandEvent& event ) override;
  72. void OnNameEdited( wxCommandEvent& event ) override;
  73. void OnShowConsoleChanged( wxCommandEvent& event ) override;
  74. void OnIdle( wxIdleEvent& event ) override;
  75. void pluginInit();
  76. void installGeneratorsList();
  77. BOM_GENERATOR_HANDLER* addGenerator( const wxString& aPath,
  78. const wxString& aName = wxEmptyString );
  79. bool pluginExists( const wxString& aName );
  80. BOM_GENERATOR_HANDLER* selectedGenerator()
  81. {
  82. int idx = m_lbGenerators->GetSelection();
  83. if( idx < 0 || idx >= (int)m_generators.size() )
  84. return nullptr;
  85. return m_generators[idx].get();
  86. }
  87. wxString chooseGenerator();
  88. };
  89. // Create and show DIALOG_BOM.
  90. int InvokeDialogCreateBOM( SCH_EDIT_FRAME* aCaller )
  91. {
  92. DIALOG_BOM dlg( aCaller );
  93. // QuasiModal so syntax help works
  94. return dlg.ShowQuasiModal();
  95. }
  96. DIALOG_BOM::DIALOG_BOM( SCH_EDIT_FRAME* parent ) :
  97. DIALOG_BOM_BASE( parent ),
  98. m_parent( parent ),
  99. m_initialized( false ),
  100. m_helpWindow( nullptr )
  101. {
  102. m_buttonAddGenerator->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
  103. m_buttonDelGenerator->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
  104. m_buttonEdit->SetBitmap( KiBitmap( BITMAPS::small_edit ) );
  105. installGeneratorsList();
  106. #ifndef __WINDOWS__
  107. m_checkBoxShowConsole->Show( false );
  108. #endif
  109. m_sdbSizerOK->SetLabel( _( "Generate" ) );
  110. m_sdbSizerCancel->SetLabel( _( "Close" ) );
  111. m_sdbSizer->Layout();
  112. SetInitialFocus( m_lbGenerators );
  113. m_sdbSizerOK->SetDefault();
  114. // Now all widgets have the size fixed, call FinishDialogSettings
  115. finishDialogSettings();
  116. m_buttonReset->Bind( wxEVT_BUTTON,
  117. [&]( wxCommandEvent& )
  118. {
  119. EESCHEMA_SETTINGS* cfg = m_parent->eeconfig();
  120. cfg->m_BomPanel.selected_plugin = wxEmptyString;
  121. cfg->m_BomPanel.plugins = cfg->DefaultBomPlugins();
  122. installGeneratorsList();
  123. } );
  124. }
  125. DIALOG_BOM::~DIALOG_BOM()
  126. {
  127. if( m_helpWindow )
  128. m_helpWindow->Destroy();
  129. EESCHEMA_SETTINGS* cfg = m_parent->eeconfig();
  130. cfg->m_BomPanel.plugins.clear();
  131. for( const std::unique_ptr<BOM_GENERATOR_HANDLER>& plugin : m_generators )
  132. {
  133. wxString name = plugin->GetName();
  134. wxFileName path( plugin->GetStoredPath() );
  135. // handle empty nickname by stripping path
  136. if( name.IsEmpty() )
  137. name = path.GetName();
  138. EESCHEMA_SETTINGS::BOM_PLUGIN_SETTINGS setting( name, path.GetFullPath() );
  139. setting.command = plugin->GetCommand();
  140. cfg->m_BomPanel.plugins.emplace_back( setting );
  141. }
  142. cfg->m_BomPanel.selected_plugin = m_lbGenerators->GetStringSelection().ToStdString();
  143. }
  144. // Read the initialized plugins in config and fill the list of names
  145. void DIALOG_BOM::installGeneratorsList()
  146. {
  147. EESCHEMA_SETTINGS* cfg = m_parent->eeconfig();
  148. wxString active_plugin_name = cfg->m_BomPanel.selected_plugin;
  149. m_generators.clear();
  150. for( EESCHEMA_SETTINGS::BOM_PLUGIN_SETTINGS& setting : cfg->m_BomPanel.plugins )
  151. {
  152. auto plugin = std::make_unique<BOM_GENERATOR_HANDLER>( setting.path );
  153. plugin->SetName( setting.name );
  154. if( !setting.command.IsEmpty() )
  155. plugin->SetCommand( setting.command );
  156. m_generators.emplace_back( std::move( plugin ) );
  157. }
  158. m_lbGenerators->Clear();
  159. if( !m_generators.empty() )
  160. {
  161. for( unsigned ii = 0; ii < m_generators.size(); ii++ )
  162. {
  163. wxString name = m_generators[ii]->GetName();
  164. if( !m_generators[ii]->FindFilePath().Exists( wxFILE_EXISTS_REGULAR ) )
  165. {
  166. wxLogTrace( BOM_TRACE, "BOM plugin %s not found",
  167. m_generators[ii]->FindFilePath().GetFullName() );
  168. name.Append( wxT( " " ) + _( "(file missing)" ) );
  169. if( active_plugin_name == name )
  170. active_plugin_name.Clear();
  171. }
  172. m_lbGenerators->Append( name );
  173. if( active_plugin_name == name )
  174. m_lbGenerators->SetSelection( ii );
  175. }
  176. }
  177. pluginInit();
  178. }
  179. BOM_GENERATOR_HANDLER* DIALOG_BOM::addGenerator( const wxString& aPath, const wxString& aName )
  180. {
  181. BOM_GENERATOR_HANDLER* ret = nullptr;
  182. auto plugin = std::make_unique<BOM_GENERATOR_HANDLER>( aPath );
  183. if( !plugin )
  184. return nullptr;
  185. if( !aName.IsEmpty() )
  186. {
  187. plugin->SetName( aName );
  188. m_lbGenerators->Append( aName );
  189. }
  190. else
  191. {
  192. m_lbGenerators->Append( plugin->GetName() );
  193. }
  194. ret = plugin.get();
  195. m_generators.push_back( std::move( plugin ) );
  196. return ret;
  197. }
  198. bool DIALOG_BOM::pluginExists( const wxString& aName )
  199. {
  200. for( unsigned ii = 0; ii < m_generators.size(); ii++ )
  201. {
  202. if( aName == m_generators[ii]->GetName() )
  203. return true;
  204. }
  205. return false;
  206. }
  207. void DIALOG_BOM::OnGeneratorSelected( wxCommandEvent& event )
  208. {
  209. pluginInit();
  210. }
  211. void DIALOG_BOM::pluginInit()
  212. {
  213. BOM_GENERATOR_HANDLER* plugin = selectedGenerator();
  214. if( !plugin )
  215. {
  216. m_textCtrlName->SetValue( wxEmptyString );
  217. m_textCtrlCommand->SetValue( wxEmptyString );
  218. m_Messages->SetValue( wxEmptyString );
  219. return;
  220. }
  221. if( !plugin->FindFilePath().Exists( wxFILE_EXISTS_REGULAR ) )
  222. {
  223. m_textCtrlName->SetValue( wxEmptyString );
  224. m_textCtrlCommand->SetValue( wxEmptyString );
  225. wxString msg =
  226. wxString::Format( _( "The selected BOM generator script %s could not be found." ),
  227. plugin->GetFile().GetFullPath() );
  228. if( !plugin->GetFile().IsAbsolute() )
  229. {
  230. msg.Append( wxString::Format( _( "\n\nSearched:\n\t%s\n\t%s" ),
  231. PATHS::GetUserPluginsPath(),
  232. PATHS::GetStockPluginsPath() ) );
  233. }
  234. m_Messages->SetValue( msg );
  235. return;
  236. }
  237. m_textCtrlName->SetValue( plugin->GetName() );
  238. m_textCtrlCommand->SetValue( plugin->GetCommand() );
  239. m_Messages->SetValue( plugin->GetInfo() );
  240. m_Messages->SetSelection( 0, 0 );
  241. #ifdef __WINDOWS__
  242. if( plugin->Options().Index( wxT( "show_console" ) ) == wxNOT_FOUND )
  243. m_checkBoxShowConsole->SetValue( false );
  244. else
  245. m_checkBoxShowConsole->SetValue( true );
  246. #endif
  247. // A plugin can be not working, so do not left the OK button enabled if
  248. // the plugin is not ready to use
  249. m_sdbSizerOK->Enable( plugin->IsOk() );
  250. }
  251. void DIALOG_BOM::OnRunGenerator( wxCommandEvent& event )
  252. {
  253. // Calculate the xml netlist filename
  254. wxFileName fn = m_parent->Schematic().GetFileName();
  255. fn.ClearExt();
  256. wxString fullfilename = fn.GetFullPath();
  257. m_parent->ClearMsgPanel();
  258. wxString reportmsg;
  259. WX_STRING_REPORTER reporter( &reportmsg );
  260. m_parent->SetNetListerCommand( m_textCtrlCommand->GetValue() );
  261. #ifdef __WINDOWS__
  262. if( m_checkBoxShowConsole->IsChecked() )
  263. m_parent->SetExecFlags( wxEXEC_SHOW_CONSOLE );
  264. #endif
  265. if( m_parent->ReadyToNetlist( _( "Generating BOM requires a fully annotated schematic." ) ) )
  266. m_parent->WriteNetListFile( -1, fullfilename, GNL_OPT_BOM, &reporter );
  267. m_Messages->SetValue( reportmsg );
  268. // Force focus back on the dialog
  269. SetFocus();
  270. }
  271. void DIALOG_BOM::OnRemoveGenerator( wxCommandEvent& event )
  272. {
  273. int ii = m_lbGenerators->GetSelection();
  274. if( ii < 0 )
  275. return;
  276. m_lbGenerators->Delete( ii );
  277. m_generators.erase( m_generators.begin() + ii );
  278. // Select the next item, if exists
  279. if( m_lbGenerators->GetCount() )
  280. m_lbGenerators->SetSelection( std::min( ii, (int) m_lbGenerators->GetCount() - 1 ) );
  281. pluginInit();
  282. }
  283. void DIALOG_BOM::OnAddGenerator( wxCommandEvent& event )
  284. {
  285. wxString filename = chooseGenerator();
  286. if( filename.IsEmpty() )
  287. return;
  288. // Creates a new plugin entry
  289. wxFileName fn( filename );
  290. wxString name = wxGetTextFromUser( _( "Generator nickname:" ), _( "Add Generator" ),
  291. fn.GetName(), this );
  292. if( name.IsEmpty() )
  293. return;
  294. // Verify if it does not exists
  295. if( pluginExists( name ) )
  296. {
  297. wxMessageBox( wxString::Format( _( "Nickname \"%s\" already in use." ), name ) );
  298. return;
  299. }
  300. try
  301. {
  302. auto plugin = addGenerator( fn.GetFullPath(), name );
  303. if( plugin )
  304. {
  305. m_lbGenerators->SetSelection( m_lbGenerators->GetCount() - 1 );
  306. m_textCtrlCommand->SetValue( plugin->GetCommand() );
  307. pluginInit();
  308. }
  309. }
  310. catch( const std::runtime_error& e )
  311. {
  312. DisplayError( this, e.what() );
  313. }
  314. }
  315. wxString DIALOG_BOM::chooseGenerator()
  316. {
  317. static wxString lastPath;
  318. if( lastPath.IsEmpty() )
  319. lastPath = PATHS::GetUserPluginsPath();
  320. wxString fullFileName = EDA_FILE_SELECTOR( _( "Generator files:" ), lastPath, wxEmptyString,
  321. wxEmptyString, wxFileSelectorDefaultWildcardStr,
  322. this, wxFD_OPEN, true );
  323. return fullFileName;
  324. }
  325. void DIALOG_BOM::OnEditGenerator( wxCommandEvent& event )
  326. {
  327. auto plugin = selectedGenerator();
  328. if( !plugin )
  329. return;
  330. wxString pluginFile = plugin->GetFile().GetFullPath();
  331. if( pluginFile.Length() <= 2 ) // if name != ""
  332. {
  333. wxMessageBox( _( "Generator file name not found." ) );
  334. return;
  335. }
  336. AddDelimiterString( pluginFile );
  337. wxString editorname = Pgm().GetEditorName();
  338. if( !editorname.IsEmpty() )
  339. ExecuteFile( this, editorname, pluginFile );
  340. else
  341. wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
  342. }
  343. void DIALOG_BOM::OnHelp( wxCommandEvent& event )
  344. {
  345. if( m_helpWindow )
  346. {
  347. m_helpWindow->ShowModeless();
  348. return;
  349. }
  350. m_helpWindow = new HTML_MESSAGE_BOX( nullptr, _( "Bill of Material Generation Help" ) );
  351. m_helpWindow->SetDialogSizeInDU( 500, 350 );
  352. wxString html_txt;
  353. ConvertMarkdown2Html( wxGetTranslation( s_bomHelpInfo ), html_txt );
  354. m_helpWindow->m_htmlWindow->AppendToPage( html_txt );
  355. m_helpWindow->ShowModeless();
  356. }
  357. void DIALOG_BOM::OnCommandLineEdited( wxCommandEvent& event )
  358. {
  359. auto generator = selectedGenerator();
  360. if( generator )
  361. generator->SetCommand( m_textCtrlCommand->GetValue() );
  362. }
  363. void DIALOG_BOM::OnNameEdited( wxCommandEvent& event )
  364. {
  365. if( m_textCtrlName->GetValue().IsEmpty() )
  366. return;
  367. int ii = m_lbGenerators->GetSelection();
  368. if( ii < 0 )
  369. return;
  370. m_generators[ii]->SetName( m_textCtrlName->GetValue() );
  371. m_lbGenerators->SetString( ii, m_generators[ii]->GetName() );
  372. }
  373. void DIALOG_BOM::OnShowConsoleChanged( wxCommandEvent& event )
  374. {
  375. #ifdef __WINDOWS__
  376. static constexpr wxChar OPT_SHOW_CONSOLE[] = wxT( "show_console" );
  377. auto plugin = selectedGenerator();
  378. if( !plugin )
  379. return;
  380. if( m_checkBoxShowConsole->IsChecked() )
  381. {
  382. if( plugin->Options().Index( OPT_SHOW_CONSOLE ) == wxNOT_FOUND )
  383. plugin->Options().Add( OPT_SHOW_CONSOLE );
  384. }
  385. else
  386. {
  387. plugin->Options().Remove( OPT_SHOW_CONSOLE );
  388. }
  389. #endif
  390. }
  391. void DIALOG_BOM::OnIdle( wxIdleEvent& event )
  392. {
  393. // On some platforms we initialize wxTextCtrls to all-selected, but we don't want that
  394. // for the messages text box.
  395. if( !m_initialized )
  396. {
  397. m_Messages->SetSelection( 0, 0 );
  398. m_initialized = true;
  399. }
  400. }