1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2014-2021 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
22  */
23 
24 #include <mutex>
25 
26 #include <symbol_library.h>
27 #include <confirm.h>
28 #include <dialogs/panel_eeschema_color_settings.h>
29 #include <dialogs/panel_eeschema_display_options.h>
30 #include <dialogs/panel_eeschema_editing_options.h>
31 #include <dialogs/panel_eeschema_template_fieldnames.h>
32 #include <dialogs/panel_sym_color_settings.h>
33 #include <dialogs/panel_sym_editing_options.h>
34 #include <dialogs/dialog_schematic_setup.h>
35 #include <kiway.h>
36 #include <symbol_edit_frame.h>
37 #include <dialogs/panel_gal_display_options.h>
38 #include <panel_hotkeys_editor.h>
39 #include <pgm_base.h>
40 #include <project/project_file.h>
41 #include <project/net_settings.h>
42 #include <sch_edit_frame.h>
43 #include <sch_painter.h>
44 #include <schematic.h>
45 #include <settings/app_settings.h>
46 #include <settings/settings_manager.h>
47 #include <symbol_lib_table.h>
48 #include <widgets/paged_dialog.h>
49 #include <wildcards_and_files_ext.h>
50 #include <drawing_sheet/ds_data_model.h>
51 #include <zoom_defines.h>
52 #include <wx/treebook.h>
53 
54 
55 /// Helper for all the old plotting/printing code while it still exists
GetLayerColor(SCH_LAYER_ID aLayer)56 COLOR4D GetLayerColor( SCH_LAYER_ID aLayer )
57 {
58     return Pgm().GetSettingsManager().GetColorSettings()->GetColor( aLayer );
59 }
60 
61 
InstallPreferences(PAGED_DIALOG * aParent,PANEL_HOTKEYS_EDITOR * aHotkeysPanel)62 void SCH_EDIT_FRAME::InstallPreferences( PAGED_DIALOG* aParent,
63                                          PANEL_HOTKEYS_EDITOR* aHotkeysPanel  )
64 {
65     wxTreebook* book = aParent->GetTreebook();
66 
67     book->AddPage( new wxPanel( book ), _( "Schematic Editor" ) );
68     book->AddSubPage( new PANEL_EESCHEMA_DISPLAY_OPTIONS( this, book ), _( "Display Options" ) );
69     book->AddSubPage( new PANEL_EESCHEMA_EDITING_OPTIONS( this, book ), _( "Editing Options" ) );
70     book->AddSubPage( new PANEL_EESCHEMA_COLOR_SETTINGS( this, book ), _( "Colors" ) );
71     book->AddSubPage( new PANEL_EESCHEMA_TEMPLATE_FIELDNAMES( this, book, true ),
72                       _( "Field Name Templates" ) );
73 
74     aHotkeysPanel->AddHotKeys( GetToolManager() );
75 }
76 
77 
LoadProjectSettings()78 bool SCH_EDIT_FRAME::LoadProjectSettings()
79 {
80     SCHEMATIC_SETTINGS& settings = Schematic().Settings();
81     settings.m_JunctionSize = GetSchematicJunctionSize();
82 
83     GetRenderSettings()->SetDefaultPenWidth( settings.m_DefaultLineWidth );
84     GetRenderSettings()->m_LabelSizeRatio  = settings.m_LabelSizeRatio;
85     GetRenderSettings()->m_TextOffsetRatio = settings.m_TextOffsetRatio;
86     GetRenderSettings()->m_PinSymbolSize   = settings.m_PinSymbolSize;
87     GetRenderSettings()->m_JunctionSize    = settings.m_JunctionSize;
88 
89     // Verify some values, because the config file can be edited by hand, and have bad values:
90     LIB_SYMBOL::SetSubpartIdNotation( LIB_SYMBOL::GetSubpartIdSeparator(),
91                                       LIB_SYMBOL::GetSubpartFirstId() );
92 
93     // Load the drawing sheet from the filename stored in BASE_SCREEN::m_DrawingSheetFileName.
94     // If empty, or not existing, the default drawing sheet is loaded.
95     wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName,
96                                                     Prj().GetProjectPath() );
97 
98     if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) )
99         ShowInfoBarError( _( "Error loading drawing sheet." ), true );
100 
101     return true;
102 }
103 
104 
ShowSchematicSetupDialog(const wxString & aInitialPage)105 void SCH_EDIT_FRAME::ShowSchematicSetupDialog( const wxString& aInitialPage )
106 {
107     DIALOG_SCHEMATIC_SETUP dlg( this );
108 
109     if( !aInitialPage.IsEmpty() )
110         dlg.SetInitialPage( aInitialPage, wxEmptyString );
111 
112     if( dlg.ShowQuasiModal() == wxID_OK )
113     {
114         Prj().GetProjectFile().NetSettings().RebuildNetClassAssignments();
115 
116         SaveProjectSettings();
117 
118         Kiway().CommonSettingsChanged( false, true );
119 
120         GetRenderSettings()->SetDefaultPenWidth( Schematic().Settings().m_DefaultLineWidth );
121         GetRenderSettings()->m_LabelSizeRatio  = Schematic().Settings().m_LabelSizeRatio;
122         GetRenderSettings()->m_TextOffsetRatio = Schematic().Settings().m_TextOffsetRatio;
123         GetRenderSettings()->m_PinSymbolSize   = Schematic().Settings().m_PinSymbolSize;
124         GetRenderSettings()->m_JunctionSize    = Schematic().Settings().m_JunctionSize;
125 
126         GetCanvas()->GetView()->MarkDirty();
127         GetCanvas()->GetView()->UpdateAllItems( KIGFX::REPAINT );
128         GetCanvas()->Refresh();
129     }
130 }
131 
132 
GetSchematicJunctionSize()133 int SCH_EDIT_FRAME::GetSchematicJunctionSize()
134 {
135     std::vector<double>& sizeMultipliers = eeconfig()->m_Drawing.junction_size_mult_list;
136 
137     NETCLASSPTR defaultNetclass = Prj().GetProjectFile().NetSettings().m_NetClasses.GetDefault();
138     int         sizeChoice = Schematic().Settings().m_JunctionSizeChoice;
139     int         junctionSize = defaultNetclass->GetWireWidth() * sizeMultipliers[ sizeChoice ];
140 
141     return std::max( junctionSize, 1 );
142 }
143 
144 
SaveProjectSettings()145 void SCH_EDIT_FRAME::SaveProjectSettings()
146 {
147     wxFileName fn = Schematic().RootScreen()->GetFileName();  //ConfigFileName
148 
149     fn.SetExt( ProjectFileExtension );
150 
151     if( !fn.HasName() || !IsWritable( fn, false ) )
152         return;
153 
154     RecordERCExclusions();
155 
156     GetSettingsManager()->SaveProject( fn.GetFullPath() );
157 }
158 
159 
LoadSettings(APP_SETTINGS_BASE * aCfg)160 void SCH_EDIT_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
161 {
162     // For now, axes are forced off in Eeschema even if turned on in config
163     eeconfig()->m_Window.grid.axes_enabled = false;
164 
165     SCH_BASE_FRAME::LoadSettings( eeconfig() );
166 
167     GetRenderSettings()->m_ShowPinsElectricalType = false;
168     GetRenderSettings()->m_ShowHiddenText = false;
169     GetRenderSettings()->m_ShowHiddenPins = false;
170     GetRenderSettings()->m_ShowHiddenText = false;
171     GetRenderSettings()->SetShowPageLimits( true );
172     GetRenderSettings()->m_ShowUmbilicals = true;
173 
174     if( eeconfig() )
175     {
176         GetRenderSettings()->m_ShowHiddenPins = eeconfig()->m_Appearance.show_hidden_pins;
177         GetRenderSettings()->m_ShowHiddenText = eeconfig()->m_Appearance.show_hidden_fields;
178         GetRenderSettings()->SetShowPageLimits( eeconfig()->m_Appearance.show_page_limits );
179     }
180 }
181 
182 
SaveSettings(APP_SETTINGS_BASE * aCfg)183 void SCH_EDIT_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
184 {
185     SCH_BASE_FRAME::SaveSettings( eeconfig() );
186 
187     // TODO(JE) do we need to keep m_userUnits around?
188     if( eeconfig() )
189         eeconfig()->m_System.units = static_cast<int>( m_userUnits );
190 }
191 
192 
LoadSettings(APP_SETTINGS_BASE * aCfg)193 void SCH_BASE_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
194 {
195     wxCHECK_RET( aCfg, "Call to SCH_BASE_FRAME::SaveSettings with null settings" );
196 
197     EDA_DRAW_FRAME::LoadSettings( aCfg );
198 
199     // Currently values read from config file are not used because the user cannot
200     // change this config
201     // if( aCfg->m_Window.grid.sizes.empty() )  // Will be probably never enabled
202     {
203         /*
204          * Do NOT add others values (mainly grid values in mm), because they can break the
205          * schematic: Because wires and pins are considered as connected when the are to the
206          * same coordinate we cannot mix coordinates in mils (internal units) and mm (that
207          * cannot exactly converted in mils in many cases).  In fact schematic must only use
208          * 50 and 25 mils to place labels, wires and symbols others values are useful only
209          * for graphic items (mainly in library editor) so use integer values in mils only.
210          * The 100 mil grid is added to help conform to the KiCad Library Convention which
211          * states: "Using a 100mil grid, pin ends and origin must lie on grid nodes IEC-60617"
212          */
213         aCfg->m_Window.grid.sizes = { "100 mil",
214                                       "50 mil",
215                                       "25 mil",
216                                       "10 mil",
217                                       "5 mil",
218                                       "2 mil",
219                                       "1 mil" };
220     }
221 
222     // Currently values read from config file are not used because the user cannot
223     // change this config
224     // if( aCfg->m_Window.zoom_factors.empty() )
225     {
226         aCfg->m_Window.zoom_factors = { ZOOM_LIST_EESCHEMA };
227     }
228 }
229 
230 
SaveSettings(APP_SETTINGS_BASE * aCfg)231 void SCH_BASE_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
232 {
233     wxCHECK_RET( aCfg, "Call to SCH_BASE_FRAME::SaveSettings with null settings" );
234 
235     EDA_DRAW_FRAME::SaveSettings( aCfg );
236 }
237 
238 
InstallPreferences(PAGED_DIALOG * aParent,PANEL_HOTKEYS_EDITOR * aHotkeysPanel)239 void SYMBOL_EDIT_FRAME::InstallPreferences( PAGED_DIALOG* aParent,
240                                             PANEL_HOTKEYS_EDITOR* aHotkeysPanel )
241 {
242     wxTreebook* book = aParent->GetTreebook();
243 
244     book->AddPage( new wxPanel( book ), _( "Symbol Editor" ) );
245     book->AddSubPage( new PANEL_GAL_DISPLAY_OPTIONS( this, aParent ), _( "Display Options" ) );
246     book->AddSubPage( new PANEL_SYM_EDITING_OPTIONS( this, book ), _( "Editing Options" ) );
247     book->AddSubPage( new PANEL_SYM_COLOR_SETTINGS( this, book ), _( "Colors" ) );
248 
249     aHotkeysPanel->AddHotKeys( GetToolManager() );
250 }
251 
252 
253 static std::mutex s_symbolTableMutex;
254 
255 
SchSymbolLibTable()256 SYMBOL_LIB_TABLE* PROJECT::SchSymbolLibTable()
257 {
258     std::lock_guard<std::mutex> lock( s_symbolTableMutex );
259 
260     // This is a lazy loading function, it loads the project specific table when
261     // that table is asked for, not before.
262     SYMBOL_LIB_TABLE* tbl = (SYMBOL_LIB_TABLE*) GetElem( ELEM_SYMBOL_LIB_TABLE );
263 
264     // its gotta be NULL or a SYMBOL_LIB_TABLE, or a bug.
265     wxASSERT( !tbl || tbl->Type() == SYMBOL_LIB_TABLE_T );
266 
267     if( !tbl )
268     {
269         // Stack the project specific SYMBOL_LIB_TABLE overlay on top of the global table.
270         // ~SYMBOL_LIB_TABLE() will not touch the fallback table, so multiple projects may
271         // stack this way, all using the same global fallback table.
272         tbl = new SYMBOL_LIB_TABLE( &SYMBOL_LIB_TABLE::GetGlobalLibTable() );
273 
274         SetElem( ELEM_SYMBOL_LIB_TABLE, tbl );
275 
276         wxString prjPath;
277 
278         wxGetEnv( PROJECT_VAR_NAME, &prjPath );
279 
280         if( !prjPath.IsEmpty() )
281         {
282             wxFileName fn( prjPath, SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
283 
284             try
285             {
286                 tbl->Load( fn.GetFullPath() );
287             }
288             catch( const IO_ERROR& ioe )
289             {
290                 wxString msg;
291                 msg.Printf( _( "Error loading the symbol library table '%s'." ),
292                             fn.GetFullPath() );
293                 DisplayErrorMessage( nullptr, msg, ioe.What() );
294             }
295         }
296     }
297 
298     return tbl;
299 }
300