KiCad PCB EDA Suite
dialog_schematic_setup.cpp
Go to the documentation of this file.
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 
20 #include <confirm.h>
21 #include <sch_edit_frame.h>
22 #include <schematic.h>
23 #include <kiface_base.h>
27 #include <panel_setup_formatting.h>
28 #include <panel_setup_pinmap.h>
29 #include <erc_item.h>
30 #include <panel_text_variables.h>
31 #include <project/project_file.h>
32 #include <project/net_settings.h>
34 #include "dialog_schematic_setup.h"
36 #include <wx/treebook.h>
37 
38 
40  PAGED_DIALOG( aFrame, _( "Schematic Setup" ), true,
41  _( "Import Settings from Another Project..." ) ),
42  m_frame( aFrame ),
43  m_severities( nullptr )
44 {
45  PROJECT_FILE& project = aFrame->Prj().GetProjectFile();
46  SCHEMATIC& schematic = aFrame->Schematic();
47 
50  m_pinMap = new PANEL_SETUP_PINMAP( m_treebook, aFrame );
51 
54  schematic.ErcSettings().m_Severities,
55  m_pinToPinError.get() );
56 
58 
59  m_netclasses = new PANEL_SETUP_NETCLASSES( this, &project.NetSettings().m_NetClasses,
60  schematic.GetNetClassAssignmentCandidates(), true );
61 
62  /*
63  * WARNING: If you change page names you MUST update calls to ShowSchematicSetupDialog().
64  */
65 
66  m_treebook->AddPage( new wxPanel( this ), _( "General" ) );
67  m_treebook->AddSubPage( m_formatting, _( "Formatting" ) );
68  m_treebook->AddSubPage( m_fieldNameTemplates, _( "Field Name Templates" ) );
69 
70  m_treebook->AddPage( new wxPanel( this ), _( "Electrical Rules" ) );
71  m_treebook->AddSubPage( m_severities, _( "Violation Severity" ) );
72  m_treebook->AddSubPage( m_pinMap, _( "Pin Conflicts Map" ) );
73 
74  m_treebook->AddPage( new wxPanel( this ), _( "Project" ) );
75  m_treebook->AddSubPage( m_netclasses, _( "Net Classes" ) );
76  m_treebook->AddSubPage( m_textVars, _( "Text Variables" ) );
77 
78  for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
79  m_macHack.push_back( true );
80 
81  // Connect Events
82  m_treebook->Connect( wxEVT_TREEBOOK_PAGE_CHANGED,
83  wxBookCtrlEventHandler( DIALOG_SCHEMATIC_SETUP::OnPageChange ), nullptr,
84  this );
85 
87 
88  if( Prj().IsReadOnly() )
89  {
90  m_infoBar->ShowMessage( _( "Project is missing or read-only. Settings will not be "
91  "editable." ), wxICON_WARNING );
92  }
93 }
94 
95 
97 {
98  m_treebook->Disconnect( wxEVT_TREEBOOK_PAGE_CHANGED,
99  wxBookCtrlEventHandler( DIALOG_SCHEMATIC_SETUP::OnPageChange ), nullptr,
100  this );
101 }
102 
103 
104 void DIALOG_SCHEMATIC_SETUP::OnPageChange( wxBookCtrlEvent& event )
105 {
106  int page = event.GetSelection();
107 
108  if( Prj().IsReadOnly() )
109  KIUI::Disable( m_treebook->GetPage( page ) );
110 
111  // Enable the reset button only if the page is resettable
112  if( m_resetButton )
113  {
114  if( auto panel = dynamic_cast<RESETTABLE_PANEL*>( m_treebook->GetPage( page ) ) )
115  {
116  m_resetButton->SetToolTip( panel->GetResetTooltip() );
117  m_resetButton->Enable( true );
118  }
119  else
120  {
121  m_resetButton->SetToolTip( wxString() );
122  m_resetButton->Enable( false );
123  }
124  }
125 
126  // Work around an OSX bug where the wxGrid children don't get placed correctly until
127  // the first resize event
128 #ifdef __WXMAC__
129  if( m_macHack[ page ] )
130  {
131  wxSize pageSize = m_treebook->GetPage( page )->GetSize();
132  pageSize.x -= 1;
133  pageSize.y += 2;
134 
135  m_treebook->GetPage( page )->SetSize( pageSize );
136  m_macHack[ page ] = false;
137  }
138 #endif
139 
140  Layout();
141 }
142 
143 
144 // Run Import Settings... action
145 void DIALOG_SCHEMATIC_SETUP::OnAuxiliaryAction( wxCommandEvent& event )
146 {
147  DIALOG_SCH_IMPORT_SETTINGS importDlg( this, m_frame );
148 
149  if( importDlg.ShowModal() == wxID_CANCEL )
150  return;
151 
152  wxFileName projectFn( importDlg.GetFilePath() );
153 
154  if( !m_frame->GetSettingsManager()->LoadProject( projectFn.GetFullPath(), false ) )
155  {
156  wxString msg = wxString::Format( _( "Error importing settings from project:\n"
157  "Project file %s could not be loaded." ),
158  projectFn.GetFullPath() );
159  DisplayErrorMessage( this, msg );
160 
161  return;
162  }
163 
164  PROJECT* otherPrj = m_frame->GetSettingsManager()->GetProject( projectFn.GetFullPath() );
165  SCHEMATIC otherSch( otherPrj );
166  TEMPLATES templateMgr;
167  PROJECT_FILE& file = otherPrj->GetProjectFile();
168 
169  wxASSERT( file.m_SchematicSettings );
170 
172 
173  if( importDlg.m_FormattingOpt->GetValue() )
175 
176  if( importDlg.m_FieldNameTemplatesOpt->GetValue() )
178 
179  if( importDlg.m_PinMapOpt->GetValue() )
181 
182  if( importDlg.m_SeveritiesOpt->GetValue() )
184 
185  if( importDlg.m_NetClassesOpt->GetValue() )
186  m_netclasses->ImportSettingsFrom( &file.m_NetSettings->m_NetClasses );
187 
188  m_frame->GetSettingsManager()->UnloadProject( otherPrj, false );
189 }
void ShowMessage(const wxString &aMessage, int aFlags=wxICON_INFORMATION) override
Show the info bar with the provided message and icon.
Definition: infobar.cpp:142
Container for project specific data.
Definition: project.h:62
PANEL_SETUP_PINMAP * m_pinMap
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:292
SETTINGS_MANAGER * GetSettingsManager() const
DIALOG_SCHEMATIC_SETUP(SCH_EDIT_FRAME *aFrame)
Holds all the data relating to one schematic.
Definition: schematic.h:59
This file is part of the common library.
PANEL_TEXT_VARIABLES * m_textVars
static std::vector< std::reference_wrapper< RC_ITEM > > GetItemsWithSeverities()
Definition: erc_item.h:50
static std::shared_ptr< ERC_ITEM > Create(int aErrorCode)
Constructs an ERC_ITEM for the given error code.
Definition: erc_item.cpp:194
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:145
void Disable(wxWindow *aWindow)
Makes a window read-only.
Definition: ui_common.cpp:276
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:167
TEMPLATES m_TemplateFieldNames
void OnAuxiliaryAction(wxCommandEvent &event) override
Schematic editor (Eeschema) main window.
std::shared_ptr< ERC_ITEM > m_pinToPinError
void ImportSettingsFrom(std::map< int, SEVERITY > &aSettings)
bool LoadFromFile(const wxString &aDirectory="") override
Loads the JSON document from the parent and then calls Load()
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:64
void ImportSettingsFrom(PIN_ERROR aPinMap[][ELECTRICAL_PINTYPES_TOTAL])
std::vector< bool > m_macHack
PANEL_SETUP_FORMATTING * m_formatting
PANEL_SETUP_SEVERITIES * m_severities
std::shared_ptr< NET_SETTINGS > m_NetSettings
Net settings for this project (owned here)
Definition: project_file.h:166
WX_INFOBAR * m_infoBar
Definition: paged_dialog.h:66
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
void OnPageChange(wxBookCtrlEvent &event)
wxTreebook * m_treebook
Definition: paged_dialog.h:62
SCHEMATIC & Schematic() const
PIN_ERROR m_PinMap[ELECTRICAL_PINTYPES_TOTAL][ELECTRICAL_PINTYPES_TOTAL]
Definition: erc_settings.h:169
#define _(s)
ERC_SETTINGS * m_ErcSettings
Eeschema params.
Definition: project_file.h:125
SCHEMATIC_SETTINGS * m_SchematicSettings
Definition: project_file.h:128
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
std::map< int, SEVERITY > m_Severities
Definition: erc_settings.h:166
PANEL_EESCHEMA_TEMPLATE_FIELDNAMES * m_fieldNameTemplates
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Saves, unloads and unregisters the given PROJECT.
wxButton * m_resetButton
Definition: paged_dialog.h:64
ERC_SETTINGS & ErcSettings() const
Definition: schematic.cpp:174
void ImportSettingsFrom(SCHEMATIC_SETTINGS &aSettings)
void ImportSettingsFrom(NETCLASSES *aBoard)
std::vector< wxString > GetNetClassAssignmentCandidates()
Return a list of name candidates for netclass assignment.
Definition: schematic.cpp:235
PROJECT * GetProject(const wxString &aFullPath) const
Retrieves a loaded project by name.
PANEL_SETUP_NETCLASSES * m_netclasses