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.

145 lines
4.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 Wayne Stambaugh <stambaughw@gmail.com>
  5. * Copyright (C) 2019-2021 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 <dialogs/dialog_global_lib_table_config.h>
  21. #include <env_vars.h>
  22. #include <pgm_base.h>
  23. #include <search_stack.h>
  24. #include <systemdirsappend.h>
  25. #include <kiplatform/environment.h>
  26. DIALOG_GLOBAL_LIB_TABLE_CONFIG::DIALOG_GLOBAL_LIB_TABLE_CONFIG( wxWindow* aParent,
  27. const wxString& aTableName,
  28. const KIWAY::FACE_T aFaceType ) :
  29. DIALOG_GLOBAL_LIB_TABLE_CONFIG_BASE( aParent ),
  30. m_tableName( aTableName ),
  31. m_defaultFileFound( false ),
  32. m_faceType( aFaceType )
  33. {
  34. wxString tmp;
  35. tmp.Printf( _( "Configure Global %s Library Table" ), aTableName.Capitalize() );
  36. SetTitle( tmp );
  37. tmp.Printf( _( "KiCad has been run for the first time using the new %s library table for\n"
  38. "accessing libraries. In order for KiCad to access %s libraries,\n"
  39. "you must configure your global %s library table. Please select from one\n"
  40. "of the options below. If you are not sure which option to select, please\n"
  41. "use the default selection." ), aTableName, aTableName, aTableName );
  42. m_staticText1->SetLabel( tmp );
  43. tmp.Printf( _( "Copy default global %s library table (recommended)"), aTableName );
  44. m_defaultRb->SetLabel( tmp );
  45. tmp.Printf( _( "Select this option if you not sure about configuring the global %s library "
  46. "table" ), aTableName );
  47. m_defaultRb->SetToolTip( tmp );
  48. tmp.Printf( _( "Copy custom global %s library table" ), aTableName );
  49. m_customRb->SetLabel( tmp );
  50. tmp.Printf( _( "Select this option to copy a %s library table file other than the default" ),
  51. aTableName );
  52. m_customRb->SetToolTip( tmp );
  53. tmp.Printf( _( "Create an empty global %s library table" ), aTableName );
  54. m_emptyRb->SetLabel( tmp );
  55. tmp.Printf( _( "Select this option to define %s libraries in project specific library tables" ),
  56. aTableName );
  57. m_emptyRb->SetToolTip( tmp );
  58. tmp.Printf( _( "Select global %s library table file:" ), aTableName );
  59. m_staticText2->SetLabel( tmp );
  60. m_filePicker1->Connect( wxEVT_UPDATE_UI,
  61. wxUpdateUIEventHandler( DIALOG_GLOBAL_LIB_TABLE_CONFIG::onUpdateFilePicker ),
  62. nullptr, this );
  63. SetupStandardButtons();
  64. finishDialogSettings();
  65. }
  66. DIALOG_GLOBAL_LIB_TABLE_CONFIG::~DIALOG_GLOBAL_LIB_TABLE_CONFIG()
  67. {
  68. m_filePicker1->Disconnect( wxEVT_UPDATE_UI,
  69. wxUpdateUIEventHandler( DIALOG_GLOBAL_LIB_TABLE_CONFIG::onUpdateFilePicker ),
  70. nullptr, this );
  71. }
  72. void DIALOG_GLOBAL_LIB_TABLE_CONFIG::onUpdateFilePicker( wxUpdateUIEvent& aEvent )
  73. {
  74. aEvent.Enable( m_customRb->GetValue() );
  75. }
  76. void DIALOG_GLOBAL_LIB_TABLE_CONFIG::onUpdateDefaultSelection( wxUpdateUIEvent& aEvent )
  77. {
  78. aEvent.Enable( m_defaultFileFound );
  79. }
  80. bool DIALOG_GLOBAL_LIB_TABLE_CONFIG::TransferDataToWindow()
  81. {
  82. if( !wxDialog::TransferDataToWindow() )
  83. return false;
  84. wxFileName fn = GetGlobalTableFileName();
  85. SEARCH_STACK ss;
  86. GlobalPathsAppend( &ss, m_faceType );
  87. wxString templatePath;
  88. const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
  89. if( std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( envVars,
  90. wxT( "TEMPLATE_DIR" ) ) )
  91. {
  92. templatePath = *v;
  93. }
  94. if( !templatePath.IsEmpty() )
  95. ss.AddPaths( templatePath, 0 );
  96. else
  97. templatePath = KIPLATFORM::ENV::GetUserConfigPath();
  98. m_filePicker1->SetInitialDirectory( templatePath );
  99. // Attempt to find the default global file table from the KiCad template folder.
  100. wxString fileName = ss.FindValidPath( fn.GetName() );
  101. m_defaultFileFound = wxFileName::FileExists( fileName );
  102. if( m_defaultFileFound )
  103. {
  104. m_filePicker1->SetPath(fileName );
  105. m_filePicker1->Enable( false );
  106. }
  107. else
  108. {
  109. m_customRb->SetValue( true );
  110. }
  111. return true;
  112. }