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-2020 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 
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( small_folder_xpm ) );
76  m_pdfViewerBtn->SetBitmap( KiBitmap( small_folder_xpm ) );
77 
78  m_canvasScaleCtrl->Connect( wxEVT_COMMAND_TEXT_UPDATED,
79  wxCommandEventHandler( PANEL_COMMON_SETTINGS::OnCanvasScaleChange ),
80  NULL, this );
81 }
82 
83 
85 {
86  m_canvasScaleCtrl->Disconnect( wxEVT_COMMAND_TEXT_UPDATED,
87  wxCommandEventHandler( PANEL_COMMON_SETTINGS::OnCanvasScaleChange ),
88  NULL, this );
89 }
90 
91 
93 {
94  COMMON_SETTINGS* commonSettings = Pgm().GetCommonSettings();
95 
96  applySettingsToPanel( *commonSettings );
97 
98  // TODO(JE) Move these into COMMON_SETTINGS probably
99  m_textEditorPath->SetValue( Pgm().GetEditorName( false ) );
100  m_defaultPDFViewer->SetValue( Pgm().UseSystemPdfBrowser() );
101  m_otherPDFViewer->SetValue( !Pgm().UseSystemPdfBrowser() );
102  m_PDFViewerPath->SetValue( Pgm().GetPdfBrowserName() );
103 
104  return true;
105 }
106 
107 
109 {
110  COMMON_SETTINGS* commonSettings = Pgm().GetCommonSettings();
111 
112  commonSettings->m_System.autosave_interval = m_SaveTime->GetValue() * 60;
113  commonSettings->m_System.file_history_size = m_fileHistorySize->GetValue();
114  commonSettings->m_System.clear_3d_cache_interval = m_Clear3DCacheFilesOlder->GetValue();
115 
116  commonSettings->m_Graphics.opengl_aa_mode = m_antialiasing->GetSelection();
117  commonSettings->m_Graphics.cairo_aa_mode = m_antialiasingFallback->GetSelection();
118 
119  const int scale_fourths = m_iconScaleAuto->GetValue() ? -1 : m_iconScaleSlider->GetValue() / 25;
120  commonSettings->m_Appearance.icon_scale = scale_fourths;
121 
122  {
123  DPI_SCALING dpi( commonSettings, this );
124  dpi.SetDpiConfig( m_canvasScaleAuto->GetValue(), m_canvasScaleCtrl->GetValue() );
125  }
126 
127  commonSettings->m_Appearance.use_icons_in_menus = m_checkBoxIconsInMenus->GetValue();
128 
129  commonSettings->m_Input.immediate_actions = !m_NonImmediateActions->GetValue();
130  commonSettings->m_Input.warp_mouse_on_move = m_warpMouseOnMove->GetValue();
131 
132  commonSettings->m_Backup.enabled = m_cbBackupEnabled->GetValue();
133  commonSettings->m_Backup.backup_on_autosave = m_cbBackupAutosave->GetValue();
134  commonSettings->m_Backup.limit_total_files = m_backupLimitTotalFiles->GetValue();
135  commonSettings->m_Backup.limit_daily_files = m_backupLimitDailyFiles->GetValue();
136  commonSettings->m_Backup.min_interval = m_backupMinInterval->GetValue() * 60;
137  commonSettings->m_Backup.limit_total_size = m_backupLimitTotalSize->GetValue() * 1024 * 1024;
138 
139  commonSettings->m_Session.remember_open_files = m_cbRememberOpenFiles->GetValue();
140 
141  Pgm().SetEditorName( m_textEditorPath->GetValue() );
142 
143  Pgm().SetPdfBrowserName( m_PDFViewerPath->GetValue() );
144  Pgm().ForceSystemPdfBrowser( m_defaultPDFViewer->GetValue() );
145  Pgm().WritePdfBrowserInfos();
146 
147  Pgm().GetSettingsManager().Save( commonSettings );
148 
149  return true;
150 }
151 
152 
154 {
155  COMMON_SETTINGS defaultSettings;
156 
157  defaultSettings.ResetToDefaults();
158 
159  applySettingsToPanel( defaultSettings );
160 
161  // TODO(JE) Move these into COMMON_SETTINGS probably
162  m_textEditorPath->SetValue( defaultSettings.m_System.editor_name );
163  m_defaultPDFViewer->SetValue( defaultSettings.m_System.use_system_pdf_viewer );
164  m_otherPDFViewer->SetValue( !defaultSettings.m_System.use_system_pdf_viewer );
165  m_PDFViewerPath->SetValue( defaultSettings.m_System.pdf_viewer_name );
166 }
167 
168 
170 {
171  int timevalue = aSettings.m_System.autosave_interval;
172  wxString msg;
173 
174  msg << timevalue / 60;
175  m_SaveTime->SetValue( msg );
176 
177  m_fileHistorySize->SetValue( aSettings.m_System.file_history_size );
178 
179  m_antialiasing->SetSelection( aSettings.m_Graphics.opengl_aa_mode );
180  m_antialiasingFallback->SetSelection( aSettings.m_Graphics.cairo_aa_mode );
181 
183 
184  int icon_scale_fourths = aSettings.m_Appearance.icon_scale;
185 
186  if( icon_scale_fourths <= 0 )
187  {
188  m_iconScaleAuto->SetValue( true );
189  m_iconScaleSlider->SetValue( 25 * KiIconScale( GetParent() ) );
190  }
191  else
192  {
193  m_iconScaleAuto->SetValue( false );
194  m_iconScaleSlider->SetValue( icon_scale_fourths * 25 );
195  }
196 
197  {
198  const DPI_SCALING dpi( &aSettings, this );
199  m_canvasScaleCtrl->SetValue( dpi.GetScaleFactor() );
200  m_canvasScaleAuto->SetValue( dpi.GetCanvasIsAutoScaled() );
201  }
202 
204 
205  m_warpMouseOnMove->SetValue( aSettings.m_Input.warp_mouse_on_move );
206  m_NonImmediateActions->SetValue( !aSettings.m_Input.immediate_actions );
207 
208  m_cbRememberOpenFiles->SetValue( aSettings.m_Session.remember_open_files );
209 
210  m_cbBackupEnabled->SetValue( aSettings.m_Backup.enabled );
211  m_cbBackupAutosave->SetValue( aSettings.m_Backup.backup_on_autosave );
212  m_backupLimitTotalFiles->SetValue( aSettings.m_Backup.limit_total_files );
213  m_backupLimitDailyFiles->SetValue( aSettings.m_Backup.limit_daily_files );
214  m_backupMinInterval->SetValue( aSettings.m_Backup.min_interval / 60 );
215  m_backupLimitTotalSize->SetValue( aSettings.m_Backup.limit_total_size / ( 1024 * 1024 ) );
216 }
217 
218 
219 void PANEL_COMMON_SETTINGS::OnScaleSlider( wxScrollEvent& aEvent )
220 {
221  m_iconScaleAuto->SetValue( false );
222  aEvent.Skip();
223 }
224 
225 
226 void PANEL_COMMON_SETTINGS::OnIconScaleAuto( wxCommandEvent& aEvent )
227 {
228  if( m_iconScaleAuto->GetValue() )
229  {
230  m_last_scale = m_iconScaleAuto->GetValue();
231  m_iconScaleSlider->SetValue( 25 * KiIconScale( GetParent() ) );
232  }
233  else
234  {
235  if( m_last_scale >= 0 )
236  m_iconScaleSlider->SetValue( m_last_scale );
237  }
238 }
239 
240 
241 void PANEL_COMMON_SETTINGS::OnCanvasScaleChange( wxCommandEvent& aEvent )
242 {
243  m_canvasScaleAuto->SetValue( false );
244 }
245 
246 
247 void PANEL_COMMON_SETTINGS::OnCanvasScaleAuto( wxCommandEvent& aEvent )
248 {
249  const bool automatic = m_canvasScaleAuto->GetValue();
250 
251  if( automatic )
252  {
253  // set the scale to the auto value, without consulting the config
254  DPI_SCALING dpi( nullptr, this );
255 
256  // update the field (no events sent)
257  m_canvasScaleCtrl->SetValue( dpi.GetScaleFactor() );
258  }
259 }
260 
261 
262 void PANEL_COMMON_SETTINGS::OnTextEditorClick( wxCommandEvent& event )
263 {
264  // Ask the user to select a new editor, but suggest the current one as the default.
265  wxString editorname = Pgm().AskUserForPreferredEditor( m_textEditorPath->GetValue() );
266 
267  // If we have a new editor name request it to be copied to m_editor_name and saved
268  // to the preferences file. If the user cancelled the dialog then the previous
269  // value will be retained.
270  if( !editorname.IsEmpty() )
271  m_textEditorPath->SetValue( editorname );
272 }
273 
274 
275 void PANEL_COMMON_SETTINGS::OnPDFViewerClick( wxCommandEvent& event )
276 {
277  wxString mask( wxT( "*" ) );
278 
279 #ifdef __WINDOWS__
280  mask += wxT( ".exe" );
281 #endif
282 
283  wxString wildcard = _( "Executable files (" ) + mask + wxT( ")|" ) + mask;
284 
285  Pgm().ReadPdfBrowserInfos();
286  wxFileName fn = Pgm().GetPdfBrowserName();
287 
288  wxFileDialog dlg( this, _( "Select Preferred PDF Browser" ), fn.GetPath(), fn.GetFullPath(),
289  wildcard, wxFD_OPEN | wxFD_FILE_MUST_EXIST );
290 
291  if( dlg.ShowModal() == wxID_CANCEL )
292  return;
293 
294  m_otherPDFViewer->SetValue( true );
295  m_PDFViewerPath->SetValue( dlg.GetPath() );
296 }
297 
298 
299 void PANEL_COMMON_SETTINGS::onUpdateUIPdfPath( wxUpdateUIEvent& event )
300 {
301  bool enabled = m_otherPDFViewer->GetValue();
302  m_PDFViewerPath->Enable( enabled );
303  m_pdfViewerBtn->Enable( enabled );
304 }
305 
void ResetToDefaults()
Resets all parameters to default values.
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.
const BITMAP_OPAQUE small_folder_xpm[1]
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:106
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.