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.

113 lines
3.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <dialog_import_choose_project.h>
  20. #include <wx/msgdlg.h>
  21. #include <wx/listctrl.h>
  22. DIALOG_IMPORT_CHOOSE_PROJECT::DIALOG_IMPORT_CHOOSE_PROJECT( wxWindow* aParent,
  23. const std::vector<IMPORT_PROJECT_DESC>& aProjectDesc ) :
  24. DIALOG_IMPORT_CHOOSE_PROJECT_BASE( aParent )
  25. {
  26. m_project_desc = aProjectDesc;
  27. // Initialize columns in the wxListCtrl elements:
  28. int comboNameColId = m_listCtrl->AppendColumn( _( "Project Name" ) );
  29. int pcbNameColId = m_listCtrl->AppendColumn( _( "PCB" ) );
  30. int schNameColId = m_listCtrl->AppendColumn( _( "Schematic" ) );
  31. // Load the project/PCB/schematic names
  32. int row = 0;
  33. auto convertName = []( const wxString& aName, const wxString& aId ) -> wxString
  34. {
  35. if( aId.empty() )
  36. return wxEmptyString;
  37. return aName;
  38. };
  39. for( const IMPORT_PROJECT_DESC& desc : m_project_desc )
  40. {
  41. m_listCtrl->InsertItem( row, convertName( desc.ComboName, desc.ComboId ) );
  42. m_listCtrl->SetItem( row, pcbNameColId, convertName( desc.PCBName, desc.PCBId ) );
  43. m_listCtrl->SetItem( row, schNameColId, convertName( desc.SchematicName,
  44. desc.SchematicId ) );
  45. ++row;
  46. }
  47. // Auto select the first item to improve ease-of-use
  48. m_listCtrl->SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
  49. m_listCtrl->SetColumnWidth( comboNameColId, wxLIST_AUTOSIZE_USEHEADER );
  50. m_listCtrl->SetColumnWidth( pcbNameColId, wxLIST_AUTOSIZE_USEHEADER );
  51. m_listCtrl->SetColumnWidth( schNameColId, wxLIST_AUTOSIZE_USEHEADER );
  52. SetupStandardButtons();
  53. Fit();
  54. finishDialogSettings();
  55. }
  56. void DIALOG_IMPORT_CHOOSE_PROJECT::onItemActivated( wxListEvent& event )
  57. {
  58. EndModal( wxID_OK );
  59. }
  60. void DIALOG_IMPORT_CHOOSE_PROJECT::onClose( wxCloseEvent& event )
  61. {
  62. EndModal( wxID_CANCEL );
  63. }
  64. std::vector<IMPORT_PROJECT_DESC> DIALOG_IMPORT_CHOOSE_PROJECT::GetProjects()
  65. {
  66. std::vector<IMPORT_PROJECT_DESC> result;
  67. long selected = -1;
  68. do
  69. {
  70. selected = m_listCtrl->GetNextItem( selected, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
  71. if( selected != -1 && selected < long( m_project_desc.size() ) )
  72. result.emplace_back( m_project_desc[selected] );
  73. } while( selected != -1 );
  74. return result;
  75. }
  76. std::vector<IMPORT_PROJECT_DESC>
  77. DIALOG_IMPORT_CHOOSE_PROJECT::RunModal( wxWindow* aParent,
  78. const std::vector<IMPORT_PROJECT_DESC>& aProjectDesc )
  79. {
  80. DIALOG_IMPORT_CHOOSE_PROJECT dlg( aParent, aProjectDesc );
  81. if( dlg.ShowModal() != wxID_OK )
  82. return {};
  83. return dlg.GetProjects();
  84. }