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.

177 lines
4.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 Jon Evans <jon@craftyjon.com>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <bitmaps.h>
  21. #include <dialogs/dialog_migrate_settings.h>
  22. #include <settings/settings_manager.h>
  23. #include <widgets/std_bitmap_button.h>
  24. #include <wx/dirdlg.h>
  25. DIALOG_MIGRATE_SETTINGS::DIALOG_MIGRATE_SETTINGS( SETTINGS_MANAGER* aManager ) :
  26. DIALOG_MIGRATE_SETTINGS_BASE( nullptr ), m_manager( aManager )
  27. {
  28. SetMinSize( FromDIP( GetMinSize() ) );
  29. m_standardButtonsCancel->SetLabel( _( "Quit KiCad" ) );
  30. m_btnCustomPath->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
  31. // Disabled for now. See https://gitlab.com/kicad/code/kicad/-/issues/9826
  32. m_cbCopyLibraryTables->Hide();
  33. SetupStandardButtons();
  34. Layout();
  35. m_sizer->Fit( this );
  36. Centre();
  37. }
  38. DIALOG_MIGRATE_SETTINGS::~DIALOG_MIGRATE_SETTINGS()
  39. {
  40. }
  41. bool DIALOG_MIGRATE_SETTINGS::TransferDataToWindow()
  42. {
  43. if( !wxDialog::TransferDataToWindow() )
  44. return false;
  45. wxString str;
  46. str.Printf( _( "Welcome to KiCad %s!" ), SETTINGS_MANAGER::GetSettingsVersion() );
  47. m_lblWelcome->SetLabelText( str );
  48. std::vector<wxString> paths;
  49. m_btnUseDefaults->SetValue( true );
  50. if( !m_manager->GetPreviousVersionPaths( &paths ) )
  51. {
  52. m_btnPrevVer->SetLabelText( _( "Import settings from a previous version (none found)" ) );
  53. }
  54. else
  55. {
  56. m_cbPath->Clear();
  57. for( const auto& path : paths )
  58. m_cbPath->Append( path );
  59. m_cbPath->SetSelection( 0 );
  60. }
  61. // SetValue does not fire the "OnRadioButton" event, so have to fabricate this
  62. wxCommandEvent dummy;
  63. OnDefaultSelected( dummy );
  64. Fit();
  65. return true;
  66. }
  67. bool DIALOG_MIGRATE_SETTINGS::TransferDataFromWindow()
  68. {
  69. if( !wxDialog::TransferDataFromWindow() )
  70. return false;
  71. if( m_btnPrevVer->GetValue() )
  72. {
  73. m_manager->SetMigrateLibraryTables( false );
  74. // Round-trip through a wxFileName object to remove any trailing separators
  75. wxFileName path( m_cbPath->GetValue(), wxEmptyString );
  76. m_manager->SetMigrationSource( path.GetPath() );
  77. }
  78. else
  79. {
  80. m_manager->SetMigrateLibraryTables( false );
  81. m_manager->SetMigrationSource( wxEmptyString );
  82. }
  83. return true;
  84. }
  85. void DIALOG_MIGRATE_SETTINGS::OnPrevVerSelected( wxCommandEvent& event )
  86. {
  87. m_standardButtons->GetAffirmativeButton()->Enable();
  88. m_cbPath->Enable();
  89. m_btnCustomPath->Enable();
  90. m_cbCopyLibraryTables->Enable();
  91. validatePath();
  92. }
  93. void DIALOG_MIGRATE_SETTINGS::OnPathChanged( wxCommandEvent& event )
  94. {
  95. validatePath();
  96. }
  97. void DIALOG_MIGRATE_SETTINGS::OnPathDefocused( wxFocusEvent& event )
  98. {
  99. validatePath();
  100. }
  101. void DIALOG_MIGRATE_SETTINGS::OnChoosePath( wxCommandEvent& event )
  102. {
  103. wxDirDialog dlg( nullptr, _( "Select Settings Path" ), m_cbPath->GetValue(),
  104. wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
  105. if( dlg.ShowModal() == wxID_OK )
  106. {
  107. m_cbPath->SetValue( dlg.GetPath() );
  108. validatePath();
  109. }
  110. }
  111. void DIALOG_MIGRATE_SETTINGS::OnDefaultSelected( wxCommandEvent& event )
  112. {
  113. m_standardButtons->GetAffirmativeButton()->Enable();
  114. m_cbPath->Disable();
  115. m_btnCustomPath->Disable();
  116. m_cbCopyLibraryTables->Disable();
  117. showPathError( false );
  118. }
  119. bool DIALOG_MIGRATE_SETTINGS::validatePath()
  120. {
  121. wxString path = m_cbPath->GetValue();
  122. bool valid = m_manager->IsSettingsPathValid( path );
  123. showPathError( !valid );
  124. m_standardButtons->GetAffirmativeButton()->Enable( valid && !path.IsEmpty() );
  125. return valid;
  126. }
  127. void DIALOG_MIGRATE_SETTINGS::showPathError( bool aShow )
  128. {
  129. m_lblPathError->Show( aShow );
  130. Layout();
  131. Fit();
  132. }