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>
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  m_canvasScaleCtrl->Connect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( PANEL_COMMON_SETTINGS::OnCanvasScaleChange ), NULL, this );
73 }
74 
75 
77 {
78  m_canvasScaleCtrl->Disconnect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( PANEL_COMMON_SETTINGS::OnCanvasScaleChange ), NULL, this );
79 }
80 
81 
83 {
84  COMMON_SETTINGS* commonSettings = Pgm().GetCommonSettings();
85 
86  int timevalue = commonSettings->m_System.autosave_interval;
87  wxString msg;
88 
89  msg << timevalue / 60;
90  m_SaveTime->SetValue( msg );
91 
92  m_fileHistorySize->SetValue( commonSettings->m_System.file_history_size );
93 
94  m_antialiasing->SetSelection( commonSettings->m_Graphics.opengl_aa_mode );
95  m_antialiasingFallback->SetSelection( commonSettings->m_Graphics.cairo_aa_mode );
96 
97  int icon_scale_fourths = commonSettings->m_Appearance.icon_scale;
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  m_checkBoxIconsInMenus->SetValue( commonSettings->m_Appearance.use_icons_in_menus );
117 
118  m_PreferSelectToDrag->SetValue( commonSettings->m_Input.prefer_select_to_drag );
119  m_warpMouseOnMove->SetValue( commonSettings->m_Input.warp_mouse_on_move );
120  m_NonImmediateActions->SetValue( !commonSettings->m_Input.immediate_actions );
121 
122  // TODO(JE) Move these into COMMON_SETTINGS probably
123  m_textEditorPath->SetValue( Pgm().GetEditorName( false ) );
124  m_defaultPDFViewer->SetValue( Pgm().UseSystemPdfBrowser() );
125  m_otherPDFViewer->SetValue( !Pgm().UseSystemPdfBrowser() );
126  m_PDFViewerPath->SetValue( Pgm().GetPdfBrowserName() );
127 
128  return true;
129 }
130 
131 
133 {
134  COMMON_SETTINGS* commonSettings = Pgm().GetCommonSettings();
135 
136  commonSettings->m_System.autosave_interval = m_SaveTime->GetValue() * 60;
137  commonSettings->m_System.file_history_size = m_fileHistorySize->GetValue();
138 
139  commonSettings->m_Graphics.opengl_aa_mode = m_antialiasing->GetSelection();
140  commonSettings->m_Graphics.cairo_aa_mode = m_antialiasingFallback->GetSelection();
141 
142  const int scale_fourths = m_iconScaleAuto->GetValue() ? -1 : m_iconScaleSlider->GetValue() / 25;
143  commonSettings->m_Appearance.icon_scale = scale_fourths;
144 
145  {
146  DPI_SCALING dpi( commonSettings, this );
147  dpi.SetDpiConfig( m_canvasScaleAuto->GetValue(), m_canvasScaleCtrl->GetValue() );
148  }
149 
150  commonSettings->m_Appearance.use_icons_in_menus = m_checkBoxIconsInMenus->GetValue();
151 
152  commonSettings->m_Input.immediate_actions = !m_NonImmediateActions->GetValue();
153  commonSettings->m_Input.prefer_select_to_drag = m_PreferSelectToDrag->GetValue();
154  commonSettings->m_Input.warp_mouse_on_move = m_warpMouseOnMove->GetValue();
155 
156  Pgm().SetEditorName( m_textEditorPath->GetValue() );
157 
158  Pgm().SetPdfBrowserName( m_PDFViewerPath->GetValue() );
159  Pgm().ForceSystemPdfBrowser( m_defaultPDFViewer->GetValue() );
160  Pgm().WritePdfBrowserInfos();
161 
162  Pgm().GetSettingsManager().Save( commonSettings );
163 
164  return true;
165 }
166 
167 
168 void PANEL_COMMON_SETTINGS::OnScaleSlider( wxScrollEvent& aEvent )
169 {
170  m_iconScaleAuto->SetValue( false );
171 }
172 
173 
174 void PANEL_COMMON_SETTINGS::OnIconScaleAuto( wxCommandEvent& aEvent )
175 {
176  if( m_iconScaleAuto->GetValue() )
177  {
178  m_last_scale = m_iconScaleAuto->GetValue();
179  m_iconScaleSlider->SetValue( 25 * KiIconScale( GetParent() ) );
180  }
181  else
182  {
183  if( m_last_scale >= 0 )
184  m_iconScaleSlider->SetValue( m_last_scale );
185  }
186 }
187 
188 
189 void PANEL_COMMON_SETTINGS::OnCanvasScaleChange( wxCommandEvent& aEvent )
190 {
191  m_canvasScaleAuto->SetValue( false );
192 }
193 
194 
195 void PANEL_COMMON_SETTINGS::OnCanvasScaleAuto( wxCommandEvent& aEvent )
196 {
197  const bool automatic = m_canvasScaleAuto->GetValue();
198 
199  if( automatic )
200  {
201  // set the scale to the auto value, without consulting the config
202  DPI_SCALING dpi( nullptr, this );
203 
204  // update the field (no events sent)
205  m_canvasScaleCtrl->SetValue( dpi.GetScaleFactor() );
206  }
207 }
208 
209 
210 void PANEL_COMMON_SETTINGS::OnTextEditorClick( wxCommandEvent& event )
211 {
212  // Ask the user to select a new editor, but suggest the current one as the default.
213  wxString editorname = Pgm().AskUserForPreferredEditor( m_textEditorPath->GetValue() );
214 
215  // If we have a new editor name request it to be copied to m_editor_name and saved
216  // to the preferences file. If the user cancelled the dialog then the previous
217  // value will be retained.
218  if( !editorname.IsEmpty() )
219  m_textEditorPath->SetValue( editorname );
220 }
221 
222 
223 void PANEL_COMMON_SETTINGS::OnPDFViewerClick( wxCommandEvent& event )
224 {
225  wxString mask( wxT( "*" ) );
226 
227 #ifdef __WINDOWS__
228  mask += wxT( ".exe" );
229 #endif
230 
231  wxString wildcard = _( "Executable files (" ) + mask + wxT( ")|" ) + mask;
232 
233  Pgm().ReadPdfBrowserInfos();
234  wxFileName fn = Pgm().GetPdfBrowserName();
235 
236  wxFileDialog dlg( this, _( "Select Preferred PDF Browser" ), fn.GetPath(), fn.GetFullPath(),
237  wildcard, wxFD_OPEN | wxFD_FILE_MUST_EXIST );
238 
239  if( dlg.ShowModal() == wxID_CANCEL )
240  return;
241 
242  m_otherPDFViewer->SetValue( true );
243  m_PDFViewerPath->SetValue( dlg.GetPath() );
244 }
245 
246 
247 void PANEL_COMMON_SETTINGS::onUpdateUIPdfPath( wxUpdateUIEvent& event )
248 {
249  bool enabled = m_otherPDFViewer->GetValue();
250  m_PDFViewerPath->Enable( enabled );
251  m_pdfViewerBtn->Enable( enabled );
252 }
253 
const BITMAP_OPAQUE folder_xpm[1]
Definition: folder.cpp:20
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
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:90
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: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:
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:80
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.
#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
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
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.