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.

189 lines
6.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <confirm.h>
  20. #include <sch_edit_frame.h>
  21. #include <schematic.h>
  22. #include <kiface_base.h>
  23. #include <dialog_sch_import_settings.h>
  24. #include <dialogs/panel_setup_netclasses.h>
  25. #include <dialogs/panel_setup_severities.h>
  26. #include <panel_setup_formatting.h>
  27. #include <panel_setup_pinmap.h>
  28. #include <erc_item.h>
  29. #include <panel_text_variables.h>
  30. #include <project/project_file.h>
  31. #include <project/net_settings.h>
  32. #include <settings/settings_manager.h>
  33. #include "dialog_schematic_setup.h"
  34. #include "panel_eeschema_template_fieldnames.h"
  35. #include <wx/treebook.h>
  36. DIALOG_SCHEMATIC_SETUP::DIALOG_SCHEMATIC_SETUP( SCH_EDIT_FRAME* aFrame ) :
  37. PAGED_DIALOG( aFrame, _( "Schematic Setup" ), true,
  38. _( "Import Settings from Another Project..." ) ),
  39. m_frame( aFrame ),
  40. m_severities( nullptr )
  41. {
  42. PROJECT_FILE& project = aFrame->Prj().GetProjectFile();
  43. SCHEMATIC& schematic = aFrame->Schematic();
  44. m_formatting = new PANEL_SETUP_FORMATTING( m_treebook, aFrame );
  45. m_fieldNameTemplates = new PANEL_EESCHEMA_TEMPLATE_FIELDNAMES( aFrame, m_treebook, false );
  46. m_pinMap = new PANEL_SETUP_PINMAP( m_treebook, aFrame );
  47. m_pinToPinError = ERC_ITEM::Create( ERCE_PIN_TO_PIN_WARNING );
  48. m_severities = new PANEL_SETUP_SEVERITIES( this, ERC_ITEM::GetItemsWithSeverities(),
  49. schematic.ErcSettings().m_Severities,
  50. m_pinToPinError.get() );
  51. m_textVars = new PANEL_TEXT_VARIABLES( m_treebook, &Prj() );
  52. m_netclasses = new PANEL_SETUP_NETCLASSES( this, &project.NetSettings().m_NetClasses,
  53. schematic.GetNetClassAssignmentCandidates(), true );
  54. /*
  55. * WARNING: If you change page names you MUST update calls to ShowSchematicSetupDialog().
  56. */
  57. m_treebook->AddPage( new wxPanel( this ), _( "General" ) );
  58. m_treebook->AddSubPage( m_formatting, _( "Formatting" ) );
  59. m_treebook->AddSubPage( m_fieldNameTemplates, _( "Field Name Templates" ) );
  60. m_treebook->AddPage( new wxPanel( this ), _( "Electrical Rules" ) );
  61. m_treebook->AddSubPage( m_severities, _( "Violation Severity" ) );
  62. m_treebook->AddSubPage( m_pinMap, _( "Pin Conflicts Map" ) );
  63. m_treebook->AddPage( new wxPanel( this ), _( "Project" ) );
  64. m_treebook->AddSubPage( m_netclasses, _( "Net Classes" ) );
  65. m_treebook->AddSubPage( m_textVars, _( "Text Variables" ) );
  66. for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
  67. m_macHack.push_back( true );
  68. // Connect Events
  69. m_treebook->Connect( wxEVT_TREEBOOK_PAGE_CHANGED,
  70. wxBookCtrlEventHandler( DIALOG_SCHEMATIC_SETUP::OnPageChange ), nullptr,
  71. this );
  72. finishDialogSettings();
  73. if( Prj().IsReadOnly() )
  74. {
  75. m_infoBar->ShowMessage( _( "Project is missing or read-only. Settings will not be "
  76. "editable." ), wxICON_WARNING );
  77. }
  78. }
  79. DIALOG_SCHEMATIC_SETUP::~DIALOG_SCHEMATIC_SETUP()
  80. {
  81. m_treebook->Disconnect( wxEVT_TREEBOOK_PAGE_CHANGED,
  82. wxBookCtrlEventHandler( DIALOG_SCHEMATIC_SETUP::OnPageChange ), nullptr,
  83. this );
  84. }
  85. void DIALOG_SCHEMATIC_SETUP::OnPageChange( wxBookCtrlEvent& event )
  86. {
  87. int page = event.GetSelection();
  88. if( Prj().IsReadOnly() )
  89. KIUI::Disable( m_treebook->GetPage( page ) );
  90. // Enable the reset button only if the page is resettable
  91. if( m_resetButton )
  92. {
  93. if( auto panel = dynamic_cast<RESETTABLE_PANEL*>( m_treebook->GetPage( page ) ) )
  94. {
  95. m_resetButton->SetToolTip( panel->GetResetTooltip() );
  96. m_resetButton->Enable( true );
  97. }
  98. else
  99. {
  100. m_resetButton->SetToolTip( wxString() );
  101. m_resetButton->Enable( false );
  102. }
  103. }
  104. // Work around an OSX bug where the wxGrid children don't get placed correctly until
  105. // the first resize event
  106. #ifdef __WXMAC__
  107. if( m_macHack[ page ] )
  108. {
  109. wxSize pageSize = m_treebook->GetPage( page )->GetSize();
  110. pageSize.x -= 1;
  111. pageSize.y += 2;
  112. m_treebook->GetPage( page )->SetSize( pageSize );
  113. m_macHack[ page ] = false;
  114. }
  115. #endif
  116. Layout();
  117. }
  118. // Run Import Settings... action
  119. void DIALOG_SCHEMATIC_SETUP::OnAuxiliaryAction( wxCommandEvent& event )
  120. {
  121. DIALOG_SCH_IMPORT_SETTINGS importDlg( this, m_frame );
  122. if( importDlg.ShowModal() == wxID_CANCEL )
  123. return;
  124. wxFileName projectFn( importDlg.GetFilePath() );
  125. if( !m_frame->GetSettingsManager()->LoadProject( projectFn.GetFullPath(), false ) )
  126. {
  127. wxString msg = wxString::Format( _( "Error importing settings from project:\n"
  128. "Project file %s could not be loaded." ),
  129. projectFn.GetFullPath() );
  130. DisplayErrorMessage( this, msg );
  131. return;
  132. }
  133. PROJECT* otherPrj = m_frame->GetSettingsManager()->GetProject( projectFn.GetFullPath() );
  134. SCHEMATIC otherSch( otherPrj );
  135. TEMPLATES templateMgr;
  136. PROJECT_FILE& file = otherPrj->GetProjectFile();
  137. wxASSERT( file.m_SchematicSettings );
  138. file.m_SchematicSettings->LoadFromFile();
  139. if( importDlg.m_FormattingOpt->GetValue() )
  140. m_formatting->ImportSettingsFrom( *file.m_SchematicSettings );
  141. if( importDlg.m_FieldNameTemplatesOpt->GetValue() )
  142. m_fieldNameTemplates->ImportSettingsFrom( &otherSch.Settings().m_TemplateFieldNames );
  143. if( importDlg.m_PinMapOpt->GetValue() )
  144. m_pinMap->ImportSettingsFrom( file.m_ErcSettings->m_PinMap );
  145. if( importDlg.m_SeveritiesOpt->GetValue() )
  146. m_severities->ImportSettingsFrom( file.m_ErcSettings->m_Severities );
  147. if( importDlg.m_NetClassesOpt->GetValue() )
  148. m_netclasses->ImportSettingsFrom( &file.m_NetSettings->m_NetClasses );
  149. m_frame->GetSettingsManager()->UnloadProject( otherPrj, false );
  150. }