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.

200 lines
5.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 Andrew Lutsenko, anlutsenko at gmail dot com
  5. * Copyright (C) 1992-2018 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 <pcb_edit_frame.h>
  21. #include <panel_pcbnew_action_plugins.h>
  22. #include <widgets/paged_dialog.h>
  23. #include <widgets/grid_icon_text_helpers.h>
  24. #include <bitmaps.h>
  25. #include <action_plugin.h>
  26. #include <grid_tricks.h>
  27. #include <widgets/wx_grid.h>
  28. PANEL_PCBNEW_ACTION_PLUGINS::PANEL_PCBNEW_ACTION_PLUGINS( PCB_EDIT_FRAME* aFrame, PAGED_DIALOG* aWindow ) :
  29. PANEL_PCBNEW_ACTION_PLUGINS_BASE( aWindow->GetTreebook() ),
  30. m_frame( aFrame )
  31. {
  32. m_genericIcon = KiBitmap( hammer_xpm );
  33. m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
  34. m_moveUpButton->SetBitmap( KiBitmap( small_up_xpm ) );
  35. m_moveDownButton->SetBitmap( KiBitmap( small_down_xpm ) );
  36. m_reloadButton->SetBitmap( KiBitmap( refresh_xpm ) );
  37. }
  38. PANEL_PCBNEW_ACTION_PLUGINS::~PANEL_PCBNEW_ACTION_PLUGINS()
  39. {
  40. m_grid->PopEventHandler( true );
  41. }
  42. void PANEL_PCBNEW_ACTION_PLUGINS::OnGridCellClick( wxGridEvent& event )
  43. {
  44. SelectRow( event.GetRow() );
  45. }
  46. void PANEL_PCBNEW_ACTION_PLUGINS::SelectRow( int aRow )
  47. {
  48. m_grid->ClearSelection();
  49. m_grid->SelectRow( aRow );
  50. }
  51. void PANEL_PCBNEW_ACTION_PLUGINS::OnMoveUpButtonClick( wxCommandEvent& event )
  52. {
  53. auto selectedRows = m_grid->GetSelectedRows();
  54. // If nothing is selected or multiple rows are selected don't do anything.
  55. if( selectedRows.size() != 1 ) return;
  56. int selectedRow = selectedRows[0];
  57. // If first row is selected, then it can't go any further up.
  58. if( selectedRow == 0 )
  59. {
  60. wxBell();
  61. return;
  62. }
  63. SwapRows( selectedRow, selectedRow - 1 );
  64. SelectRow( selectedRow - 1 );
  65. }
  66. void PANEL_PCBNEW_ACTION_PLUGINS::OnMoveDownButtonClick( wxCommandEvent& event )
  67. {
  68. auto selectedRows = m_grid->GetSelectedRows();
  69. // If nothing is selected or multiple rows are selected don't do anything.
  70. if( selectedRows.size() != 1 ) return;
  71. int selectedRow = selectedRows[0];
  72. // If last row is selected, then it can't go any further down.
  73. if( selectedRow + 1 == m_grid->GetNumberRows() )
  74. {
  75. wxBell();
  76. return;
  77. }
  78. SwapRows( selectedRow, selectedRow + 1 );
  79. SelectRow( selectedRow + 1 );
  80. }
  81. void PANEL_PCBNEW_ACTION_PLUGINS::SwapRows( int aRowA, int aRowB )
  82. {
  83. m_grid->Freeze();
  84. // Swap all columns except icon
  85. wxString tempStr;
  86. for( int column = 1; column < m_grid->GetNumberCols(); column++ )
  87. {
  88. tempStr = m_grid->GetCellValue( aRowA, column );
  89. m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
  90. m_grid->SetCellValue( aRowB, column, tempStr );
  91. }
  92. // Swap icon column renderers
  93. auto cellRenderer = m_grid->GetCellRenderer( aRowA, COLUMN_ICON );
  94. m_grid->SetCellRenderer( aRowA, COLUMN_ICON, m_grid->GetCellRenderer( aRowB, COLUMN_ICON ) );
  95. m_grid->SetCellRenderer( aRowB, COLUMN_ICON, cellRenderer );
  96. m_grid->Thaw();
  97. }
  98. void PANEL_PCBNEW_ACTION_PLUGINS::OnReloadButtonClick( wxCommandEvent& event )
  99. {
  100. m_frame->PythonPluginsReload();
  101. TransferDataToWindow();
  102. }
  103. bool PANEL_PCBNEW_ACTION_PLUGINS::TransferDataFromWindow()
  104. {
  105. std::vector< std::pair<wxString, wxString> > pluginSettings;
  106. for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
  107. {
  108. pluginSettings.push_back( std::make_pair(
  109. m_grid->GetCellValue( ii, COLUMN_PATH ),
  110. m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT("1") ? wxT( "Visible" ) : wxT( "Hidden" )
  111. ) );
  112. }
  113. m_frame->SetActionPluginSettings( pluginSettings );
  114. return true;
  115. }
  116. bool PANEL_PCBNEW_ACTION_PLUGINS::TransferDataToWindow()
  117. {
  118. m_grid->Freeze();
  119. if( m_grid->GetNumberRows() != 0 )
  120. m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
  121. const auto& orderedPlugins = m_frame->GetOrderedActionPlugins();
  122. m_grid->AppendRows( orderedPlugins.size() );
  123. for( size_t row = 0; row < orderedPlugins.size(); row++ )
  124. {
  125. ACTION_PLUGIN* ap = orderedPlugins[row];
  126. // Icon
  127. m_grid->SetCellRenderer( row, COLUMN_ICON, new GRID_CELL_ICON_RENDERER(
  128. ap->iconBitmap.IsOk() ? ap->iconBitmap : m_genericIcon ) );
  129. // Toolbar button checkbox
  130. m_grid->SetCellRenderer( row, COLUMN_VISIBLE, new wxGridCellBoolRenderer() );
  131. m_grid->SetCellAlignment( row, COLUMN_VISIBLE, wxALIGN_CENTER, wxALIGN_CENTER );
  132. bool showButton = m_frame->GetActionPluginButtonVisible(
  133. ap->GetPluginPath(), ap->GetShowToolbarButton() );
  134. m_grid->SetCellValue( row, COLUMN_VISIBLE, showButton ? wxT( "1" ) : wxEmptyString );
  135. // Name
  136. m_grid->SetCellValue( row, COLUMN_NAME, ap->GetName() );
  137. // Category
  138. m_grid->SetCellValue( row, COLUMN_CATEGORY, ap->GetCategoryName() );
  139. // Description
  140. m_grid->SetCellValue( row, COLUMN_DESCRIPTION, ap->GetDescription() );
  141. // Path
  142. m_grid->SetCellValue( row, COLUMN_PATH, ap->GetPluginPath() );
  143. }
  144. m_grid->AutoSizeColumns();
  145. m_grid->AutoSizeRows();
  146. m_grid->Thaw();
  147. return true;
  148. }