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 <dpi_scaling.h>
30 #include <kiface_i.h>
31 #include <pgm_base.h>
32 
33 #include <wx/graphics.h>
34 
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  m_textEditorBtn->SetBitmap( KiBitmap( folder_xpm ) );
70  m_pdfViewerBtn->SetBitmap( KiBitmap( folder_xpm ) );
71 }
72 
73 
75 {
76  wxConfigBase* commonSettings = Pgm().CommonSettings();
77 
78  int timevalue;
79  wxString msg;
80 
81  commonSettings->Read( AUTOSAVE_INTERVAL_KEY, &timevalue );
82  msg << timevalue / 60;
83  m_SaveTime->SetValue( msg );
84 
85  int fileHistorySize;
86  commonSettings->Read( FILE_HISTORY_SIZE_KEY, &fileHistorySize, DEFAULT_FILE_HISTORY_SIZE );
87  m_fileHistorySize->SetValue( fileHistorySize );
88 
89  int antialiasingMode;
90  commonSettings->Read( GAL_ANTIALIASING_MODE_KEY, &antialiasingMode, 0 );
91  m_antialiasing->SetSelection( antialiasingMode );
92 
93  commonSettings->Read( CAIRO_ANTIALIASING_MODE_KEY, &antialiasingMode, 0 );
94  m_antialiasingFallback->SetSelection( antialiasingMode );
95 
96  int icon_scale_fourths;
97  commonSettings->Read( ICON_SCALE_KEY, &icon_scale_fourths );
98 
99  if( icon_scale_fourths <= 0 )
100  {
101  m_iconScaleAuto->SetValue( true );
102  m_iconScaleSlider->SetValue( 25 * KiIconScale( GetParent() ) );
103  }
104  else
105  {
106  m_iconScaleAuto->SetValue( false );
107  m_iconScaleSlider->SetValue( icon_scale_fourths * 25 );
108  }
109 
110  {
111  const DPI_SCALING dpi( commonSettings, this );
112  m_canvasScaleCtrl->SetValue( dpi.GetScaleFactor() );
113  m_canvasScaleAuto->SetValue( dpi.GetCanvasIsAutoScaled() );
114  }
115 
116  bool option;
117  commonSettings->Read( USE_ICONS_IN_MENUS_KEY, &option );
118  m_checkBoxIconsInMenus->SetValue( option );
119 
120  commonSettings->Read( ENBL_ZOOM_NO_CENTER_KEY, &option );
121  m_ZoomCenterOpt->SetValue( !option );
122 
123  commonSettings->Read( ENBL_MOUSEWHEEL_PAN_KEY, &option );
124  m_MousewheelPANOpt->SetValue( option );
125 
126  commonSettings->Read( ENBL_AUTO_PAN_KEY, &option );
127  m_AutoPANOpt->SetValue( option );
128 
129  m_textEditorPath->SetValue( Pgm().GetEditorName( false ) );
130  m_defaultPDFViewer->SetValue( Pgm().UseSystemPdfBrowser() );
131  m_otherPDFViewer->SetValue( !Pgm().UseSystemPdfBrowser() );
132  m_PDFViewerPath->SetValue( Pgm().GetPdfBrowserName() );
133 
134  return true;
135 }
136 
137 
139 {
140  wxConfigBase* commonSettings = Pgm().CommonSettings();
141 
142  commonSettings->Write( AUTOSAVE_INTERVAL_KEY, m_SaveTime->GetValue() * 60 );
143 
144  commonSettings->Write( FILE_HISTORY_SIZE_KEY, m_fileHistorySize->GetValue() );
145 
146  commonSettings->Write( GAL_ANTIALIASING_MODE_KEY, m_antialiasing->GetSelection() );
147 
148  commonSettings->Write( CAIRO_ANTIALIASING_MODE_KEY, m_antialiasingFallback->GetSelection() );
149 
150  const int scale_fourths = m_iconScaleAuto->GetValue() ? -1 : m_iconScaleSlider->GetValue() / 25;
151  commonSettings->Write( ICON_SCALE_KEY, scale_fourths );
152 
153  {
154  DPI_SCALING dpi( commonSettings, this );
155  dpi.SetDpiConfig( m_canvasScaleAuto->GetValue(), m_canvasScaleCtrl->GetValue() );
156  }
157 
158  commonSettings->Write( USE_ICONS_IN_MENUS_KEY, m_checkBoxIconsInMenus->GetValue() );
159  commonSettings->Write( ENBL_ZOOM_NO_CENTER_KEY, !m_ZoomCenterOpt->GetValue() );
160  commonSettings->Write( ENBL_MOUSEWHEEL_PAN_KEY, m_MousewheelPANOpt->GetValue() );
161  commonSettings->Write( ENBL_AUTO_PAN_KEY, m_AutoPANOpt->GetValue() );
162 
163  Pgm().SetEditorName( m_textEditorPath->GetValue() );
164 
165  Pgm().SetPdfBrowserName( m_PDFViewerPath->GetValue() );
168 
169  return true;
170 }
171 
172 
173 void PANEL_COMMON_SETTINGS::OnScaleSlider( wxScrollEvent& aEvent )
174 {
175  m_iconScaleAuto->SetValue( false );
176 }
177 
178 
179 void PANEL_COMMON_SETTINGS::OnIconScaleAuto( wxCommandEvent& aEvent )
180 {
181  if( m_iconScaleAuto->GetValue() )
182  {
183  m_last_scale = m_iconScaleAuto->GetValue();
184  m_iconScaleSlider->SetValue( 25 * KiIconScale( GetParent() ) );
185  }
186  else
187  {
188  if( m_last_scale >= 0 )
189  m_iconScaleSlider->SetValue( m_last_scale );
190  }
191 }
192 
193 
194 void PANEL_COMMON_SETTINGS::OnCanvasScaleChange( wxCommandEvent& aEvent )
195 {
196  m_canvasScaleAuto->SetValue( false );
197 }
198 
199 
200 void PANEL_COMMON_SETTINGS::OnCanvasScaleAuto( wxCommandEvent& aEvent )
201 {
202  const bool automatic = m_canvasScaleAuto->GetValue();
203 
204  if( automatic )
205  {
206  // set the scale to the auto value, without consulting the config
207  DPI_SCALING dpi( nullptr, this );
208 
209  // update the field (no events sent)
210  m_canvasScaleCtrl->SetValue( dpi.GetScaleFactor() );
211  }
212 }
213 
214 
215 void PANEL_COMMON_SETTINGS::OnTextEditorClick( wxCommandEvent& event )
216 {
217  // Ask the user to select a new editor, but suggest the current one as the default.
218  wxString editorname = Pgm().AskUserForPreferredEditor( m_textEditorPath->GetValue() );
219 
220  // If we have a new editor name request it to be copied to m_editor_name and saved
221  // to the preferences file. If the user cancelled the dialog then the previous
222  // value will be retained.
223  if( !editorname.IsEmpty() )
224  m_textEditorPath->SetValue( editorname );
225 }
226 
227 
228 void PANEL_COMMON_SETTINGS::OnPDFViewerClick( wxCommandEvent& event )
229 {
230  wxString mask( wxT( "*" ) );
231 
232 #ifdef __WINDOWS__
233  mask += wxT( ".exe" );
234 #endif
235 
236  wxString wildcard = _( "Executable files (" ) + mask + wxT( ")|" ) + mask;
237 
239  wxFileName fn = Pgm().GetPdfBrowserName();
240 
241  wxFileDialog dlg( this, _( "Select Preferred PDF Browser" ), fn.GetPath(), fn.GetFullPath(),
242  wildcard, wxFD_OPEN | wxFD_FILE_MUST_EXIST );
243 
244  if( dlg.ShowModal() == wxID_CANCEL )
245  return;
246 
247  m_otherPDFViewer->SetValue( true );
248  m_PDFViewerPath->SetValue( dlg.GetPath() );
249 }
250 
251 
252 void PANEL_COMMON_SETTINGS::onUpdateUIPdfPath( wxUpdateUIEvent& event )
253 {
254  bool enabled = m_otherPDFViewer->GetValue();
255  m_PDFViewerPath->Enable( enabled );
256  m_pdfViewerBtn->Enable( enabled );
257 }
258 
VTBL_ENTRY void SetEditorName(const wxString &aFileName)
Definition: pgm_base.cpp:192
void OnCanvasScaleChange(wxCommandEvent &aEvent) override
Event fired when the canvas scale field is modified.
VTBL_ENTRY void ForceSystemPdfBrowser(bool aFlg)
Function ForceSystemPdfBrowser forces the use of system PDF browser, even if a preferred PDF browser ...
Definition: pgm_base.h:241
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:89
void OnPDFViewerClick(wxCommandEvent &event) override
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
Class DIALOG_SHIM may sit in the inheritance tree between wxDialog and any class written by wxFormBui...
Definition: dialog_shim.h:83
VTBL_ENTRY void WritePdfBrowserInfos()
Function WritePdfBrowserInfos saves the PDF browser choice to the common configuration.
Definition: eda_doc.cpp:55
VTBL_ENTRY const wxString AskUserForPreferredEditor(const wxString &aDefaultEditor=wxEmptyString)
Shows a dialog that instructs the user to select a new preferred editor.
Definition: pgm_base.cpp:237
static double GetDefaultScaleFactor()
Get the "default" scaling factor to use if not other config is available.
Class PANEL_COMMON_SETTINGS_BASE.
double GetScaleFactor() const
Get the DPI scale from all known sources in order:
#define ENBL_ZOOM_NO_CENTER_KEY
Definition: pgm_base.h:49
Class to handle configuration and automatic determination of the DPI scale to use for canvases.
Definition: dpi_scaling.h:35
bool TransferDataFromWindow() override
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
void OnTextEditorClick(wxCommandEvent &event) override
#define ENBL_AUTO_PAN_KEY
Definition: pgm_base.h:52
#define DEFAULT_FILE_HISTORY_SIZE
The default file history size is 9.
Definition: pgm_base.h:61
static constexpr int dpi_scaling_precision
void SetDpiConfig(bool aAuto, double aValue)
Set the common DPI config in a given config object.
static constexpr double dpi_scaling_increment
static double GetMaxScaleFactor()
VTBL_ENTRY void SetPdfBrowserName(const wxString &aFileName)
Definition: pgm_base.h:224
#define ENBL_MOUSEWHEEL_PAN_KEY
Definition: pgm_base.h:50
VTBL_ENTRY const wxString & GetPdfBrowserName() const
Definition: pgm_base.h:222
#define FILE_HISTORY_SIZE_KEY
Definition: pgm_base.h:53
static double GetMinScaleFactor()
void onUpdateUIPdfPath(wxUpdateUIEvent &event) override
#define ICON_SCALE_KEY
Definition: pgm_base.h:46
int m_last_scale
saved icon scale when Auto selected
VTBL_ENTRY wxConfigBase * CommonSettings() const
Definition: pgm_base.h:190
#define AUTOSAVE_INTERVAL_KEY
Definition: pgm_base.h:48
#define GAL_ANTIALIASING_MODE_KEY
Definition: pgm_base.h:55
VTBL_ENTRY void ReadPdfBrowserInfos()
Function ReadPdfBrowserInfos reads the PDF browser choice from the common configuration.
Definition: eda_doc.cpp:42
see class PGM_BASE
void OnIconScaleAuto(wxCommandEvent &aEvent) override
#define USE_ICONS_IN_MENUS_KEY
Definition: pgm_base.h:45
#define CAIRO_ANTIALIASING_MODE_KEY
Definition: pgm_base.h:56
void OnCanvasScaleAuto(wxCommandEvent &aEvent) override
Event fired when the canvas auto-scale option is changed.
bool TransferDataToWindow() override
PANEL_COMMON_SETTINGS(DIALOG_SHIM *aDialog, wxWindow *aParent)
void SetStep(int aSize)
Set the step size.
bool GetCanvasIsAutoScaled() const
Is the current value auto scaled, or is it user-set in the config.