KiCad PCB EDA Suite
panel_common_settings.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) 2018 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 "panel_common_settings.h"
25 
26 #include <bitmap_types.h>
27 #include <bitmaps.h>
28 #include <dialog_shim.h>
29 #include <gal/dpi_scaling.h>
30 #include <kiface_i.h>
31 #include <pgm_base.h>
32 #include <id.h>
35 
36 static constexpr int dpi_scaling_precision = 1;
37 static constexpr double dpi_scaling_increment = 0.5;
38 
39 
41  : PANEL_COMMON_SETTINGS_BASE( aParent ),
42  m_dialog( aDialog ),
43  m_last_scale( -1 )
44 {
45  m_canvasScaleCtrl->SetRange(
50 
51  m_canvasScaleCtrl->SetToolTip(
52  _( "Set the scale for the canvas."
53  "\n\n"
54  "On high-DPI displays on some platforms, KiCad cannot determine the "
55  "scaling factor. In this case you may need to set this to a value to "
56  "match your system's DPI scaling. 2.0 is a common value. "
57  "\n\n"
58  "If this does not match the system DPI scaling, the canvas will "
59  "not match the window size and cursor position." ) );
60 
61  m_canvasScaleAuto->SetToolTip(
62  _( "Use an automatic value for the canvas scale."
63  "\n\n"
64  "On some platforms, the automatic value is incorrect and should be "
65  "set manually." ) );
66 
68 
69 #ifdef __WXMAC__
70  // Cairo canvas doesn't work on Mac, so no need for anti-aliasing options
71  m_antialiasingFallback->Show( false );
72  m_antialiasingFallbackLabel->Show( false );
73 #endif
74 
75  m_textEditorBtn->SetBitmap( KiBitmap( folder_xpm ) );
76  m_pdfViewerBtn->SetBitmap( KiBitmap( folder_xpm ) );
77 
78  m_canvasScaleCtrl->Connect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( PANEL_COMMON_SETTINGS::OnCanvasScaleChange ), NULL, this );
79 }
80 
81 
83 {
84  m_canvasScaleCtrl->Disconnect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( PANEL_COMMON_SETTINGS::OnCanvasScaleChange ), NULL, this );
85 }
86 
87 
89 {
90  COMMON_SETTINGS* commonSettings = Pgm().GetCommonSettings();
91 
92  applySettingsToPanel( *commonSettings );
93 
94  // TODO(JE) Move these into COMMON_SETTINGS probably
95  m_textEditorPath->SetValue( Pgm().GetEditorName( false ) );
96  m_defaultPDFViewer->SetValue( Pgm().UseSystemPdfBrowser() );
97  m_otherPDFViewer->SetValue( !Pgm().UseSystemPdfBrowser() );
98  m_PDFViewerPath->SetValue( Pgm().GetPdfBrowserName() );
99 
100  return true;
101 }
102 
103 
105 {
106  COMMON_SETTINGS* commonSettings = Pgm().GetCommonSettings();
107 
108  commonSettings->m_System.autosave_interval = m_SaveTime->GetValue() * 60;
109  commonSettings->m_System.file_history_size = m_fileHistorySize->GetValue();
110  commonSettings->m_System.clear_3d_cache_interval = m_Clear3DCacheFilesOlder->GetValue();
111 
112  commonSettings->m_Graphics.opengl_aa_mode = m_antialiasing->GetSelection();
113  commonSettings->m_Graphics.cairo_aa_mode = m_antialiasingFallback->GetSelection();
114 
115  const int scale_fourths = m_iconScaleAuto->GetValue() ? -1 : m_iconScaleSlider->GetValue() / 25;
116  commonSettings->m_Appearance.icon_scale = scale_fourths;
117 
118  {
119  DPI_SCALING dpi( commonSettings, this );
120  dpi.SetDpiConfig( m_canvasScaleAuto->GetValue(), m_canvasScaleCtrl->GetValue() );
121  }
122 
123  commonSettings->m_Appearance.use_icons_in_menus = m_checkBoxIconsInMenus->GetValue();
124 
125  commonSettings->m_Input.immediate_actions = !m_NonImmediateActions->GetValue();
126  commonSettings->m_Input.prefer_select_to_drag = m_PreferSelectToDrag->GetValue();
127  commonSettings->m_Input.warp_mouse_on_move = m_warpMouseOnMove->GetValue();
128 
129  commonSettings->m_Backup.enabled = m_cbBackupEnabled->GetValue();
130  commonSettings->m_Backup.backup_on_autosave = m_cbBackupAutosave->GetValue();
131  commonSettings->m_Backup.limit_total_files = m_backupLimitTotalFiles->GetValue();
132  commonSettings->m_Backup.limit_daily_files = m_backupLimitDailyFiles->GetValue();
133  commonSettings->m_Backup.min_interval = m_backupMinInterval->GetValue() * 60;
134  commonSettings->m_Backup.limit_total_size = m_backupLimitTotalSize->GetValue() * 1024 * 1024;
135 
136  commonSettings->m_Session.remember_open_files = m_cbRememberOpenFiles->GetValue();
137 
138  Pgm().SetEditorName( m_textEditorPath->GetValue() );
139 
140  Pgm().SetPdfBrowserName( m_PDFViewerPath->GetValue() );
141  Pgm().ForceSystemPdfBrowser( m_defaultPDFViewer->GetValue() );
142  Pgm().WritePdfBrowserInfos();
143 
144  Pgm().GetSettingsManager().Save( commonSettings );
145 
146  return true;
147 }
148 
149 
151 {
152  COMMON_SETTINGS defaultSettings;
153 
154  defaultSettings.ResetToDefaults();
155 
156  applySettingsToPanel( defaultSettings );
157 
158  // TODO(JE) Move these into COMMON_SETTINGS probably
159  m_textEditorPath->SetValue( defaultSettings.m_System.editor_name );
160  m_defaultPDFViewer->SetValue( defaultSettings.m_System.use_system_pdf_viewer );
161  m_otherPDFViewer->SetValue( !defaultSettings.m_System.use_system_pdf_viewer );
162  m_PDFViewerPath->SetValue( defaultSettings.m_System.pdf_viewer_name );
163 }
164 
165 
167 {
168  int timevalue = aSettings.m_System.autosave_interval;
169  wxString msg;
170 
171  msg << timevalue / 60;
172  m_SaveTime->SetValue( msg );
173 
174  m_fileHistorySize->SetValue( aSettings.m_System.file_history_size );
175 
176  m_antialiasing->SetSelection( aSettings.m_Graphics.opengl_aa_mode );
177  m_antialiasingFallback->SetSelection( aSettings.m_Graphics.cairo_aa_mode );
178 
180 
181  int icon_scale_fourths = aSettings.m_Appearance.icon_scale;
182 
183  if( icon_scale_fourths <= 0 )
184  {
185  m_iconScaleAuto->SetValue( true );
186  m_iconScaleSlider->SetValue( 25 * KiIconScale( GetParent() ) );
187  }
188  else
189  {
190  m_iconScaleAuto->SetValue( false );
191  m_iconScaleSlider->SetValue( icon_scale_fourths * 25 );
192  }
193 
194  {
195  const DPI_SCALING dpi( &aSettings, this );
196  m_canvasScaleCtrl->SetValue( dpi.GetScaleFactor() );
197  m_canvasScaleAuto->SetValue( dpi.GetCanvasIsAutoScaled() );
198  }
199 
201 
202  m_PreferSelectToDrag->SetValue( aSettings.m_Input.prefer_select_to_drag );
203  m_warpMouseOnMove->SetValue( aSettings.m_Input.warp_mouse_on_move );
204  m_NonImmediateActions->SetValue( !aSettings.m_Input.immediate_actions );
205 
206  m_cbRememberOpenFiles->SetValue( aSettings.m_Session.remember_open_files );
207 
208  m_cbBackupEnabled->SetValue( aSettings.m_Backup.enabled );
209  m_cbBackupAutosave->SetValue( aSettings.m_Backup.backup_on_autosave );
210  m_backupLimitTotalFiles->SetValue( aSettings.m_Backup.limit_total_files );
211  m_backupLimitDailyFiles->SetValue( aSettings.m_Backup.limit_daily_files );
212  m_backupMinInterval->SetValue( aSettings.m_Backup.min_interval / 60 );
213  m_backupLimitTotalSize->SetValue( aSettings.m_Backup.limit_total_size / ( 1024 * 1024 ) );
214 }
215 
216 
217 void PANEL_COMMON_SETTINGS::OnScaleSlider( wxScrollEvent& aEvent )
218 {
219  m_iconScaleAuto->SetValue( false );
220  aEvent.Skip();
221 }
222 
223 
224 void PANEL_COMMON_SETTINGS::OnIconScaleAuto( wxCommandEvent& aEvent )
225 {
226  if( m_iconScaleAuto->GetValue() )
227  {
228  m_last_scale = m_iconScaleAuto->GetValue();
229  m_iconScaleSlider->SetValue( 25 * KiIconScale( GetParent() ) );
230  }
231  else
232  {
233  if( m_last_scale >= 0 )
234  m_iconScaleSlider->SetValue( m_last_scale );
235  }
236 }
237 
238 
239 void PANEL_COMMON_SETTINGS::OnCanvasScaleChange( wxCommandEvent& aEvent )
240 {
241  m_canvasScaleAuto->SetValue( false );
242 }
243 
244 
245 void PANEL_COMMON_SETTINGS::OnCanvasScaleAuto( wxCommandEvent& aEvent )
246 {
247  const bool automatic = m_canvasScaleAuto->GetValue();
248 
249  if( automatic )
250  {
251  // set the scale to the auto value, without consulting the config
252  DPI_SCALING dpi( nullptr, this );
253 
254  // update the field (no events sent)
255  m_canvasScaleCtrl->SetValue( dpi.GetScaleFactor() );
256  }
257 }
258 
259 
260 void PANEL_COMMON_SETTINGS::OnTextEditorClick( wxCommandEvent& event )
261 {
262  // Ask the user to select a new editor, but suggest the current one as the default.
263  wxString editorname = Pgm().AskUserForPreferredEditor( m_textEditorPath->GetValue() );
264 
265  // If we have a new editor name request it to be copied to m_editor_name and saved
266  // to the preferences file. If the user cancelled the dialog then the previous
267  // value will be retained.
268  if( !editorname.IsEmpty() )
269  m_textEditorPath->SetValue( editorname );
270 }
271 
272 
273 void PANEL_COMMON_SETTINGS::OnPDFViewerClick( wxCommandEvent& event )
274 {
275  wxString mask( wxT( "*" ) );
276 
277 #ifdef __WINDOWS__
278  mask += wxT( ".exe" );
279 #endif
280 
281  wxString wildcard = _( "Executable files (" ) + mask + wxT( ")|" ) + mask;
282 
283  Pgm().ReadPdfBrowserInfos();
284  wxFileName fn = Pgm().GetPdfBrowserName();
285 
286  wxFileDialog dlg( this, _( "Select Preferred PDF Browser" ), fn.GetPath(), fn.GetFullPath(),
287  wildcard, wxFD_OPEN | wxFD_FILE_MUST_EXIST );
288 
289  if( dlg.ShowModal() == wxID_CANCEL )
290  return;
291 
292  m_otherPDFViewer->SetValue( true );
293  m_PDFViewerPath->SetValue( dlg.GetPath() );
294 }
295 
296 
297 void PANEL_COMMON_SETTINGS::onUpdateUIPdfPath( wxUpdateUIEvent& event )
298 {
299  bool enabled = m_otherPDFViewer->GetValue();
300  m_PDFViewerPath->Enable( enabled );
301  m_pdfViewerBtn->Enable( enabled );
302 }
303 
void ResetToDefaults()
Resets all parameters to default values.
const BITMAP_OPAQUE folder_xpm[1]
Definition: folder.cpp:20
void ResetPanel() override
Reset the contents of this panel.
unsigned long long limit_total_size
Maximum total size of backups (bytes), 0 for unlimited.
void OnCanvasScaleChange(wxCommandEvent &aEvent)
Event fired when the canvas scale field is modified.
void OnScaleSlider(wxScrollEvent &aEvent) override
int KiIconScale(wxWindow *aWindow)
Return the automatic scale factor that would be used for a given window by KiScaledBitmap and KiScale...
Definition: bitmap.cpp:92
bool enabled
Automatically back up the project when files are saved.
AUTO_BACKUP m_Backup
void OnPDFViewerClick(wxCommandEvent &event) override
APPEARANCE m_Appearance
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:83
static double GetDefaultScaleFactor()
Get the "default" scaling factor to use if not other config is available.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:102
Class PANEL_COMMON_SETTINGS_BASE.
double GetScaleFactor() const
Get the DPI scale from all known sources in order:
Class to handle configuration and automatic determination of the DPI scale to use for canvases.
Definition: dpi_scaling.h:36
bool TransferDataFromWindow() override
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:82
void OnTextEditorClick(wxCommandEvent &event) override
static constexpr int dpi_scaling_precision
void SetDpiConfig(bool aAuto, double aValue)
Set the common DPI config in a given config object.
void applySettingsToPanel(COMMON_SETTINGS &aSettings)
#define NULL
static constexpr double dpi_scaling_increment
static double GetMaxScaleFactor()
static double GetMinScaleFactor()
void onUpdateUIPdfPath(wxUpdateUIEvent &event) override
int m_last_scale
saved icon scale when Auto selected
int min_interval
Minimum time, in seconds, between subsequent backups.
see class PGM_BASE
void OnIconScaleAuto(wxCommandEvent &aEvent) override
#define _(s)
Definition: 3d_actions.cpp:33
void OnCanvasScaleAuto(wxCommandEvent &aEvent) override
Event fired when the canvas auto-scale option is changed.
bool TransferDataToWindow() override
bool backup_on_autosave
Trigger a backup on autosave.
PANEL_COMMON_SETTINGS(DIALOG_SHIM *aDialog, wxWindow *aParent)
int limit_daily_files
Maximum files to keep per day, 0 for unlimited.
void SetStep(int aSize)
Set the step size.
int limit_total_files
Maximum number of backup archives to retain.
bool GetCanvasIsAutoScaled() const
Is the current value auto scaled, or is it user-set in the config.