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 #include <id.h>
33 
34 static constexpr int dpi_scaling_precision = 1;
35 static constexpr double dpi_scaling_increment = 0.5;
36 
37 
39  : PANEL_COMMON_SETTINGS_BASE( aParent ),
40  m_dialog( aDialog ),
41  m_last_scale( -1 )
42 {
43  m_canvasScaleCtrl->SetRange(
48 
49  m_canvasScaleCtrl->SetToolTip(
50  _( "Set the scale for the canvas."
51  "\n\n"
52  "On high-DPI displays on some platforms, KiCad cannot determine the "
53  "scaling factor. In this case you may need to set this to a value to "
54  "match your system's DPI scaling. 2.0 is a common value. "
55  "\n\n"
56  "If this does not match the system DPI scaling, the canvas will "
57  "not match the window size and cursor position." ) );
58 
59  m_canvasScaleAuto->SetToolTip(
60  _( "Use an automatic value for the canvas scale."
61  "\n\n"
62  "On some platforms, the automatic value is incorrect and should be "
63  "set manually." ) );
64 
66 
67  m_textEditorBtn->SetBitmap( KiBitmap( folder_xpm ) );
68  m_pdfViewerBtn->SetBitmap( KiBitmap( folder_xpm ) );
69 
70  m_canvasScaleCtrl->Connect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( PANEL_COMMON_SETTINGS::OnCanvasScaleChange ), NULL, this );
71 }
72 
73 
75 {
76  m_canvasScaleCtrl->Disconnect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( PANEL_COMMON_SETTINGS::OnCanvasScaleChange ), NULL, this );
77 }
78 
79 
81 {
82  wxConfigBase* commonSettings = Pgm().CommonSettings();
83 
84  int timevalue;
85  wxString msg;
86 
87  commonSettings->Read( AUTOSAVE_INTERVAL_KEY, &timevalue );
88  msg << timevalue / 60;
89  m_SaveTime->SetValue( msg );
90 
91  int fileHistorySize;
92  commonSettings->Read( FILE_HISTORY_SIZE_KEY, &fileHistorySize, DEFAULT_FILE_HISTORY_SIZE );
93  m_fileHistorySize->SetValue( fileHistorySize );
94 
95  int antialiasingMode;
96  commonSettings->Read( GAL_ANTIALIASING_MODE_KEY, &antialiasingMode, 0 );
97  m_antialiasing->SetSelection( antialiasingMode );
98 
99  commonSettings->Read( CAIRO_ANTIALIASING_MODE_KEY, &antialiasingMode, 0 );
100  m_antialiasingFallback->SetSelection( antialiasingMode );
101 
102  int icon_scale_fourths;
103  commonSettings->Read( ICON_SCALE_KEY, &icon_scale_fourths );
104 
105  if( icon_scale_fourths <= 0 )
106  {
107  m_iconScaleAuto->SetValue( true );
108  m_iconScaleSlider->SetValue( 25 * KiIconScale( GetParent() ) );
109  }
110  else
111  {
112  m_iconScaleAuto->SetValue( false );
113  m_iconScaleSlider->SetValue( icon_scale_fourths * 25 );
114  }
115 
116  {
117  const DPI_SCALING dpi( commonSettings, this );
118  m_canvasScaleCtrl->SetValue( dpi.GetScaleFactor() );
119  m_canvasScaleAuto->SetValue( dpi.GetCanvasIsAutoScaled() );
120  }
121 
122  bool option;
123  commonSettings->Read( USE_ICONS_IN_MENUS_KEY, &option );
124  m_checkBoxIconsInMenus->SetValue( option );
125 
126  commonSettings->Read( ENBL_ZOOM_NO_CENTER_KEY, &option );
127  m_ZoomCenterOpt->SetValue( !option );
128 
129  commonSettings->Read( ENBL_MOUSEWHEEL_PAN_KEY, &option );
130  m_MousewheelPANOpt->SetValue( option );
131 
132  commonSettings->Read( ENBL_AUTO_PAN_KEY, &option );
133  m_AutoPANOpt->SetValue( option );
134 
135  if( !commonSettings->Read( PREFER_SELECT_TO_DRAG_KEY, &option ) )
136  {
137  // Legacy versions stored the property only for PCBNew, so see if we have it there
138  std::unique_ptr<wxConfigBase> pcbSettings = GetNewConfig( wxT( "pcbnew" ) );
139  pcbSettings->Read( "DragSelects", &option, true );
140  }
141  m_PreferSelectToDrag->SetValue( option );
142 
143  commonSettings->Read( WARP_MOUSE_ON_MOVE_KEY, &option );
144  m_warpMouseOnMove->SetValue( option );
145 
146  commonSettings->Read( IMMEDIATE_ACTIONS_KEY, &option );
147  m_NonImmediateActions->SetValue( !option );
148 
149  m_textEditorPath->SetValue( Pgm().GetEditorName( false ) );
150  m_defaultPDFViewer->SetValue( Pgm().UseSystemPdfBrowser() );
151  m_otherPDFViewer->SetValue( !Pgm().UseSystemPdfBrowser() );
152  m_PDFViewerPath->SetValue( Pgm().GetPdfBrowserName() );
153 
154  return true;
155 }
156 
157 
159 {
160  wxConfigBase* commonSettings = Pgm().CommonSettings();
161 
162  commonSettings->Write( AUTOSAVE_INTERVAL_KEY, m_SaveTime->GetValue() * 60 );
163 
164  commonSettings->Write( FILE_HISTORY_SIZE_KEY, m_fileHistorySize->GetValue() );
165 
166  commonSettings->Write( GAL_ANTIALIASING_MODE_KEY, m_antialiasing->GetSelection() );
167 
168  commonSettings->Write( CAIRO_ANTIALIASING_MODE_KEY, m_antialiasingFallback->GetSelection() );
169 
170  const int scale_fourths = m_iconScaleAuto->GetValue() ? -1 : m_iconScaleSlider->GetValue() / 25;
171  commonSettings->Write( ICON_SCALE_KEY, scale_fourths );
172 
173  {
174  DPI_SCALING dpi( commonSettings, this );
175  dpi.SetDpiConfig( m_canvasScaleAuto->GetValue(), m_canvasScaleCtrl->GetValue() );
176  }
177 
178  commonSettings->Write( USE_ICONS_IN_MENUS_KEY, m_checkBoxIconsInMenus->GetValue() );
179  commonSettings->Write( ENBL_ZOOM_NO_CENTER_KEY, !m_ZoomCenterOpt->GetValue() );
180  commonSettings->Write( ENBL_MOUSEWHEEL_PAN_KEY, m_MousewheelPANOpt->GetValue() );
181  commonSettings->Write( ENBL_AUTO_PAN_KEY, m_AutoPANOpt->GetValue() );
182  commonSettings->Write( PREFER_SELECT_TO_DRAG_KEY, m_PreferSelectToDrag->GetValue() );
183  commonSettings->Write( WARP_MOUSE_ON_MOVE_KEY, m_warpMouseOnMove->GetValue() );
184  commonSettings->Write( IMMEDIATE_ACTIONS_KEY, !m_NonImmediateActions->GetValue() );
185 
186  Pgm().SetEditorName( m_textEditorPath->GetValue() );
187 
188  Pgm().SetPdfBrowserName( m_PDFViewerPath->GetValue() );
189  Pgm().ForceSystemPdfBrowser( m_defaultPDFViewer->GetValue() );
190  Pgm().WritePdfBrowserInfos();
191 
192  return true;
193 }
194 
195 
196 void PANEL_COMMON_SETTINGS::OnScaleSlider( wxScrollEvent& aEvent )
197 {
198  m_iconScaleAuto->SetValue( false );
199 }
200 
201 
202 void PANEL_COMMON_SETTINGS::OnIconScaleAuto( wxCommandEvent& aEvent )
203 {
204  if( m_iconScaleAuto->GetValue() )
205  {
206  m_last_scale = m_iconScaleAuto->GetValue();
207  m_iconScaleSlider->SetValue( 25 * KiIconScale( GetParent() ) );
208  }
209  else
210  {
211  if( m_last_scale >= 0 )
212  m_iconScaleSlider->SetValue( m_last_scale );
213  }
214 }
215 
216 
217 void PANEL_COMMON_SETTINGS::OnCanvasScaleChange( wxCommandEvent& aEvent )
218 {
219  m_canvasScaleAuto->SetValue( false );
220 }
221 
222 
223 void PANEL_COMMON_SETTINGS::OnCanvasScaleAuto( wxCommandEvent& aEvent )
224 {
225  const bool automatic = m_canvasScaleAuto->GetValue();
226 
227  if( automatic )
228  {
229  // set the scale to the auto value, without consulting the config
230  DPI_SCALING dpi( nullptr, this );
231 
232  // update the field (no events sent)
233  m_canvasScaleCtrl->SetValue( dpi.GetScaleFactor() );
234  }
235 }
236 
237 
238 void PANEL_COMMON_SETTINGS::OnTextEditorClick( wxCommandEvent& event )
239 {
240  // Ask the user to select a new editor, but suggest the current one as the default.
241  wxString editorname = Pgm().AskUserForPreferredEditor( m_textEditorPath->GetValue() );
242 
243  // If we have a new editor name request it to be copied to m_editor_name and saved
244  // to the preferences file. If the user cancelled the dialog then the previous
245  // value will be retained.
246  if( !editorname.IsEmpty() )
247  m_textEditorPath->SetValue( editorname );
248 }
249 
250 
251 void PANEL_COMMON_SETTINGS::OnPDFViewerClick( wxCommandEvent& event )
252 {
253  wxString mask( wxT( "*" ) );
254 
255 #ifdef __WINDOWS__
256  mask += wxT( ".exe" );
257 #endif
258 
259  wxString wildcard = _( "Executable files (" ) + mask + wxT( ")|" ) + mask;
260 
261  Pgm().ReadPdfBrowserInfos();
262  wxFileName fn = Pgm().GetPdfBrowserName();
263 
264  wxFileDialog dlg( this, _( "Select Preferred PDF Browser" ), fn.GetPath(), fn.GetFullPath(),
265  wildcard, wxFD_OPEN | wxFD_FILE_MUST_EXIST );
266 
267  if( dlg.ShowModal() == wxID_CANCEL )
268  return;
269 
270  m_otherPDFViewer->SetValue( true );
271  m_PDFViewerPath->SetValue( dlg.GetPath() );
272 }
273 
274 
275 void PANEL_COMMON_SETTINGS::onUpdateUIPdfPath( wxUpdateUIEvent& event )
276 {
277  bool enabled = m_otherPDFViewer->GetValue();
278  m_PDFViewerPath->Enable( enabled );
279  m_pdfViewerBtn->Enable( enabled );
280 }
281 
#define IMMEDIATE_ACTIONS_KEY
Definition: pgm_base.h:58
const BITMAP_OPAQUE folder_xpm[1]
Definition: folder.cpp:20
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:98
#define PREFER_SELECT_TO_DRAG_KEY
Definition: pgm_base.h:59
std::unique_ptr< wxConfigBase > GetNewConfig(const wxString &aProgName)
Create a new wxConfig so we can put configuration files in a more proper place for each platform.
Definition: common.cpp:231
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:89
void OnPDFViewerClick(wxCommandEvent &event) override
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:84
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
static constexpr int dpi_scaling_precision
void SetDpiConfig(bool aAuto, double aValue)
Set the common DPI config in a given config object.
#define NULL
static constexpr double dpi_scaling_increment
static double GetMaxScaleFactor()
#define ENBL_MOUSEWHEEL_PAN_KEY
Definition: pgm_base.h:50
#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
#define AUTOSAVE_INTERVAL_KEY
Definition: pgm_base.h:48
#define GAL_ANTIALIASING_MODE_KEY
Definition: pgm_base.h:55
see class PGM_BASE
void OnIconScaleAuto(wxCommandEvent &aEvent) override
#define _(s)
Definition: 3d_actions.cpp:31
#define DEFAULT_FILE_HISTORY_SIZE
IDs range for menuitems file history: The default range file history size is 9 (compatible with defau...
Definition: id.h:69
#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.
#define WARP_MOUSE_ON_MOVE_KEY
Definition: pgm_base.h:57