You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
11 KiB

  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. #include <dialogs/panel_common_settings.h>
  24. #include <bitmap_types.h>
  25. #include <bitmaps.h>
  26. #include <dialog_shim.h>
  27. #include <gal/dpi_scaling.h>
  28. #include <kiface_i.h>
  29. #include <pgm_base.h>
  30. #include <id.h>
  31. #include <settings/common_settings.h>
  32. #include <settings/settings_manager.h>
  33. static constexpr int dpi_scaling_precision = 1;
  34. static constexpr double dpi_scaling_increment = 0.5;
  35. PANEL_COMMON_SETTINGS::PANEL_COMMON_SETTINGS( DIALOG_SHIM* aDialog, wxWindow* aParent )
  36. : PANEL_COMMON_SETTINGS_BASE( aParent ),
  37. m_dialog( aDialog ),
  38. m_last_scale( -1 )
  39. {
  40. m_canvasScaleCtrl->SetRange(
  41. DPI_SCALING::GetMinScaleFactor(), DPI_SCALING::GetMaxScaleFactor() );
  42. m_canvasScaleCtrl->SetDigits( dpi_scaling_precision );
  43. m_canvasScaleCtrl->SetIncrement( dpi_scaling_increment );
  44. m_canvasScaleCtrl->SetValue( DPI_SCALING::GetDefaultScaleFactor() );
  45. m_canvasScaleCtrl->SetToolTip(
  46. _( "Set the scale for the canvas."
  47. "\n\n"
  48. "On high-DPI displays on some platforms, KiCad cannot determine the "
  49. "scaling factor. In this case you may need to set this to a value to "
  50. "match your system's DPI scaling. 2.0 is a common value. "
  51. "\n\n"
  52. "If this does not match the system DPI scaling, the canvas will "
  53. "not match the window size and cursor position." ) );
  54. m_canvasScaleAuto->SetToolTip(
  55. _( "Use an automatic value for the canvas scale."
  56. "\n\n"
  57. "On some platforms, the automatic value is incorrect and should be "
  58. "set manually." ) );
  59. m_iconScaleSlider->SetStep( 25 );
  60. #ifdef __WXMAC__
  61. // Cairo canvas doesn't work on Mac, so no need for anti-aliasing options
  62. m_antialiasingFallback->Show( false );
  63. m_antialiasingFallbackLabel->Show( false );
  64. #endif
  65. m_textEditorBtn->SetBitmap( KiBitmap( small_folder_xpm ) );
  66. m_pdfViewerBtn->SetBitmap( KiBitmap( small_folder_xpm ) );
  67. m_canvasScaleCtrl->Connect( wxEVT_COMMAND_TEXT_UPDATED,
  68. wxCommandEventHandler( PANEL_COMMON_SETTINGS::OnCanvasScaleChange ),
  69. NULL, this );
  70. }
  71. PANEL_COMMON_SETTINGS::~PANEL_COMMON_SETTINGS()
  72. {
  73. m_canvasScaleCtrl->Disconnect( wxEVT_COMMAND_TEXT_UPDATED,
  74. wxCommandEventHandler( PANEL_COMMON_SETTINGS::OnCanvasScaleChange ),
  75. NULL, this );
  76. }
  77. bool PANEL_COMMON_SETTINGS::TransferDataToWindow()
  78. {
  79. COMMON_SETTINGS* commonSettings = Pgm().GetCommonSettings();
  80. applySettingsToPanel( *commonSettings );
  81. // TODO(JE) Move these into COMMON_SETTINGS probably
  82. m_textEditorPath->SetValue( Pgm().GetEditorName( false ) );
  83. m_defaultPDFViewer->SetValue( Pgm().UseSystemPdfBrowser() );
  84. m_otherPDFViewer->SetValue( !Pgm().UseSystemPdfBrowser() );
  85. m_PDFViewerPath->SetValue( Pgm().GetPdfBrowserName() );
  86. return true;
  87. }
  88. bool PANEL_COMMON_SETTINGS::TransferDataFromWindow()
  89. {
  90. COMMON_SETTINGS* commonSettings = Pgm().GetCommonSettings();
  91. commonSettings->m_System.autosave_interval = m_SaveTime->GetValue() * 60;
  92. commonSettings->m_System.file_history_size = m_fileHistorySize->GetValue();
  93. commonSettings->m_System.clear_3d_cache_interval = m_Clear3DCacheFilesOlder->GetValue();
  94. commonSettings->m_Graphics.opengl_aa_mode = m_antialiasing->GetSelection();
  95. commonSettings->m_Graphics.cairo_aa_mode = m_antialiasingFallback->GetSelection();
  96. const int scale_fourths = m_iconScaleAuto->GetValue() ? -1 : m_iconScaleSlider->GetValue() / 25;
  97. commonSettings->m_Appearance.icon_scale = scale_fourths;
  98. {
  99. DPI_SCALING dpi( commonSettings, this );
  100. dpi.SetDpiConfig( m_canvasScaleAuto->GetValue(), m_canvasScaleCtrl->GetValue() );
  101. }
  102. commonSettings->m_Appearance.use_icons_in_menus = m_checkBoxIconsInMenus->GetValue();
  103. commonSettings->m_Input.immediate_actions = !m_NonImmediateActions->GetValue();
  104. commonSettings->m_Input.warp_mouse_on_move = m_warpMouseOnMove->GetValue();
  105. commonSettings->m_Backup.enabled = m_cbBackupEnabled->GetValue();
  106. commonSettings->m_Backup.backup_on_autosave = m_cbBackupAutosave->GetValue();
  107. commonSettings->m_Backup.limit_total_files = m_backupLimitTotalFiles->GetValue();
  108. commonSettings->m_Backup.limit_daily_files = m_backupLimitDailyFiles->GetValue();
  109. commonSettings->m_Backup.min_interval = m_backupMinInterval->GetValue() * 60;
  110. commonSettings->m_Backup.limit_total_size = m_backupLimitTotalSize->GetValue() * 1024 * 1024;
  111. commonSettings->m_Session.remember_open_files = m_cbRememberOpenFiles->GetValue();
  112. Pgm().SetEditorName( m_textEditorPath->GetValue() );
  113. Pgm().SetPdfBrowserName( m_PDFViewerPath->GetValue() );
  114. Pgm().ForceSystemPdfBrowser( m_defaultPDFViewer->GetValue() );
  115. Pgm().WritePdfBrowserInfos();
  116. Pgm().GetSettingsManager().Save( commonSettings );
  117. return true;
  118. }
  119. void PANEL_COMMON_SETTINGS::ResetPanel()
  120. {
  121. COMMON_SETTINGS defaultSettings;
  122. defaultSettings.ResetToDefaults();
  123. applySettingsToPanel( defaultSettings );
  124. // TODO(JE) Move these into COMMON_SETTINGS probably
  125. m_textEditorPath->SetValue( defaultSettings.m_System.editor_name );
  126. m_defaultPDFViewer->SetValue( defaultSettings.m_System.use_system_pdf_viewer );
  127. m_otherPDFViewer->SetValue( !defaultSettings.m_System.use_system_pdf_viewer );
  128. m_PDFViewerPath->SetValue( defaultSettings.m_System.pdf_viewer_name );
  129. }
  130. void PANEL_COMMON_SETTINGS::applySettingsToPanel( COMMON_SETTINGS& aSettings )
  131. {
  132. int timevalue = aSettings.m_System.autosave_interval;
  133. wxString msg;
  134. msg << timevalue / 60;
  135. m_SaveTime->SetValue( msg );
  136. m_fileHistorySize->SetValue( aSettings.m_System.file_history_size );
  137. m_antialiasing->SetSelection( aSettings.m_Graphics.opengl_aa_mode );
  138. m_antialiasingFallback->SetSelection( aSettings.m_Graphics.cairo_aa_mode );
  139. m_Clear3DCacheFilesOlder->SetValue( aSettings.m_System.clear_3d_cache_interval );
  140. int icon_scale_fourths = aSettings.m_Appearance.icon_scale;
  141. if( icon_scale_fourths <= 0 )
  142. {
  143. m_iconScaleAuto->SetValue( true );
  144. m_iconScaleSlider->SetValue( 25 * KiIconScale( GetParent() ) );
  145. }
  146. else
  147. {
  148. m_iconScaleAuto->SetValue( false );
  149. m_iconScaleSlider->SetValue( icon_scale_fourths * 25 );
  150. }
  151. {
  152. const DPI_SCALING dpi( &aSettings, this );
  153. m_canvasScaleCtrl->SetValue( dpi.GetScaleFactor() );
  154. m_canvasScaleAuto->SetValue( dpi.GetCanvasIsAutoScaled() );
  155. }
  156. m_checkBoxIconsInMenus->SetValue( aSettings.m_Appearance.use_icons_in_menus );
  157. m_warpMouseOnMove->SetValue( aSettings.m_Input.warp_mouse_on_move );
  158. m_NonImmediateActions->SetValue( !aSettings.m_Input.immediate_actions );
  159. m_cbRememberOpenFiles->SetValue( aSettings.m_Session.remember_open_files );
  160. m_cbBackupEnabled->SetValue( aSettings.m_Backup.enabled );
  161. m_cbBackupAutosave->SetValue( aSettings.m_Backup.backup_on_autosave );
  162. m_backupLimitTotalFiles->SetValue( aSettings.m_Backup.limit_total_files );
  163. m_backupLimitDailyFiles->SetValue( aSettings.m_Backup.limit_daily_files );
  164. m_backupMinInterval->SetValue( aSettings.m_Backup.min_interval / 60 );
  165. m_backupLimitTotalSize->SetValue( aSettings.m_Backup.limit_total_size / ( 1024 * 1024 ) );
  166. }
  167. void PANEL_COMMON_SETTINGS::OnScaleSlider( wxScrollEvent& aEvent )
  168. {
  169. m_iconScaleAuto->SetValue( false );
  170. aEvent.Skip();
  171. }
  172. void PANEL_COMMON_SETTINGS::OnIconScaleAuto( wxCommandEvent& aEvent )
  173. {
  174. if( m_iconScaleAuto->GetValue() )
  175. {
  176. m_last_scale = m_iconScaleAuto->GetValue();
  177. m_iconScaleSlider->SetValue( 25 * KiIconScale( GetParent() ) );
  178. }
  179. else
  180. {
  181. if( m_last_scale >= 0 )
  182. m_iconScaleSlider->SetValue( m_last_scale );
  183. }
  184. }
  185. void PANEL_COMMON_SETTINGS::OnCanvasScaleChange( wxCommandEvent& aEvent )
  186. {
  187. m_canvasScaleAuto->SetValue( false );
  188. }
  189. void PANEL_COMMON_SETTINGS::OnCanvasScaleAuto( wxCommandEvent& aEvent )
  190. {
  191. const bool automatic = m_canvasScaleAuto->GetValue();
  192. if( automatic )
  193. {
  194. // set the scale to the auto value, without consulting the config
  195. DPI_SCALING dpi( nullptr, this );
  196. // update the field (no events sent)
  197. m_canvasScaleCtrl->SetValue( dpi.GetScaleFactor() );
  198. }
  199. }
  200. void PANEL_COMMON_SETTINGS::OnTextEditorClick( wxCommandEvent& event )
  201. {
  202. // Ask the user to select a new editor, but suggest the current one as the default.
  203. wxString editorname = Pgm().AskUserForPreferredEditor( m_textEditorPath->GetValue() );
  204. // If we have a new editor name request it to be copied to m_editor_name and saved
  205. // to the preferences file. If the user cancelled the dialog then the previous
  206. // value will be retained.
  207. if( !editorname.IsEmpty() )
  208. m_textEditorPath->SetValue( editorname );
  209. }
  210. void PANEL_COMMON_SETTINGS::OnPDFViewerClick( wxCommandEvent& event )
  211. {
  212. wxString mask( wxT( "*" ) );
  213. #ifdef __WINDOWS__
  214. mask += wxT( ".exe" );
  215. #endif
  216. wxString wildcard = _( "Executable files (" ) + mask + wxT( ")|" ) + mask;
  217. Pgm().ReadPdfBrowserInfos();
  218. wxFileName fn = Pgm().GetPdfBrowserName();
  219. wxFileDialog dlg( this, _( "Select Preferred PDF Browser" ), fn.GetPath(), fn.GetFullPath(),
  220. wildcard, wxFD_OPEN | wxFD_FILE_MUST_EXIST );
  221. if( dlg.ShowModal() == wxID_CANCEL )
  222. return;
  223. m_otherPDFViewer->SetValue( true );
  224. m_PDFViewerPath->SetValue( dlg.GetPath() );
  225. }
  226. void PANEL_COMMON_SETTINGS::onUpdateUIPdfPath( wxUpdateUIEvent& event )
  227. {
  228. bool enabled = m_otherPDFViewer->GetValue();
  229. m_PDFViewerPath->Enable( enabled );
  230. m_pdfViewerBtn->Enable( enabled );
  231. }