KiCad PCB EDA Suite
panel_pcbnew_action_plugins.cpp
Go to the documentation of this file.
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 
21 #include <pcb_edit_frame.h>
23 #include <widgets/paged_dialog.h>
25 #include <bitmaps.h>
26 #include <action_plugin.h>
27 #include <grid_tricks.h>
28 #include <widgets/wx_grid.h>
29 
30 
32  PANEL_PCBNEW_ACTION_PLUGINS_BASE( aWindow->GetTreebook() ),
33  m_frame( aFrame )
34 {
35  m_genericIcon = KiBitmap( hammer_xpm );
36  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
37 
38  m_moveUpButton->SetBitmap( KiBitmap( small_up_xpm ) );
39  m_moveDownButton->SetBitmap( KiBitmap( small_down_xpm ) );
40  m_reloadButton->SetBitmap( KiBitmap( refresh_xpm ) );
41 }
42 
43 
45 {
46  m_grid->PopEventHandler( true );
47 }
48 
49 
51 {
52  SelectRow( event.GetRow() );
53 }
54 
55 
57 {
58  m_grid->ClearSelection();
59  m_grid->SelectRow( aRow );
60 }
61 
62 
64 {
65  auto selectedRows = m_grid->GetSelectedRows();
66 
67  // If nothing is selected or multiple rows are selected don't do anything.
68  if( selectedRows.size() != 1 ) return;
69 
70  int selectedRow = selectedRows[0];
71 
72  // If first row is selected, then it can't go any further up.
73  if( selectedRow == 0 )
74  {
75  wxBell();
76  return;
77  }
78 
79  SwapRows( selectedRow, selectedRow - 1 );
80 
81  SelectRow( selectedRow - 1 );
82 }
83 
84 
86 {
87  auto selectedRows = m_grid->GetSelectedRows();
88 
89  // If nothing is selected or multiple rows are selected don't do anything.
90  if( selectedRows.size() != 1 ) return;
91 
92  int selectedRow = selectedRows[0];
93 
94  // If last row is selected, then it can't go any further down.
95  if( selectedRow + 1 == m_grid->GetNumberRows() )
96  {
97  wxBell();
98  return;
99  }
100 
101  SwapRows( selectedRow, selectedRow + 1 );
102 
103  SelectRow( selectedRow + 1 );
104 }
105 
106 
107 void PANEL_PCBNEW_ACTION_PLUGINS::SwapRows( int aRowA, int aRowB )
108 {
109  m_grid->Freeze();
110 
111  // Swap all columns except icon
112  wxString tempStr;
113 
114  for( int column = 1; column < m_grid->GetNumberCols(); column++ )
115  {
116  tempStr = m_grid->GetCellValue( aRowA, column );
117  m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
118  m_grid->SetCellValue( aRowB, column, tempStr );
119  }
120 
121  // Swap icon column renderers
122  auto cellRenderer = m_grid->GetCellRenderer( aRowA, COLUMN_ICON );
123  m_grid->SetCellRenderer( aRowA, COLUMN_ICON, m_grid->GetCellRenderer( aRowB, COLUMN_ICON ) );
124  m_grid->SetCellRenderer( aRowB, COLUMN_ICON, cellRenderer );
125 
126  m_grid->Thaw();
127 }
128 
129 
131 {
134 }
135 
136 
138 {
139  std::vector< std::pair<wxString, wxString> > pluginSettings;
140 
141  for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
142  {
143  pluginSettings.push_back( std::make_pair(
144  m_grid->GetCellValue( ii, COLUMN_PATH ),
145  m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT("1") ? wxT( "Visible" ) : wxT( "Hidden" )
146  ) );
147  }
148 
149  m_frame->SetActionPluginSettings( pluginSettings );
150 
151  return true;
152 }
153 
154 
156 {
157  m_grid->Freeze();
158  if( m_grid->GetNumberRows() != 0 )
159  m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
160 
161  const auto& orderedPlugins = m_frame->GetOrderedActionPlugins();
162  m_grid->AppendRows( orderedPlugins.size() );
163 
164  for( size_t row = 0; row < orderedPlugins.size(); row++ )
165  {
166  ACTION_PLUGIN* ap = orderedPlugins[row];
167 
168  // Icon
169  m_grid->SetCellRenderer( row, COLUMN_ICON, new GRID_CELL_ICON_RENDERER(
170  ap->iconBitmap.IsOk() ? ap->iconBitmap : m_genericIcon ) );
171 
172  // Toolbar button checkbox
173  m_grid->SetCellRenderer( row, COLUMN_VISIBLE, new wxGridCellBoolRenderer() );
174  m_grid->SetCellAlignment( row, COLUMN_VISIBLE, wxALIGN_CENTER, wxALIGN_CENTER );
175 
176  bool showButton = m_frame->GetActionPluginButtonVisible(
177  ap->GetPluginPath(), ap->GetShowToolbarButton() );
178 
179  m_grid->SetCellValue( row, COLUMN_VISIBLE, showButton ? wxT( "1" ) : wxEmptyString );
180 
181  // Name
182  m_grid->SetCellValue( row, COLUMN_NAME, ap->GetName() );
183 
184  // Category
185  m_grid->SetCellValue( row, COLUMN_CATEGORY, ap->GetCategoryName() );
186 
187  // Description
188  m_grid->SetCellValue( row, COLUMN_DESCRIPTION, ap->GetDescription() );
189 
190  // Path
191  m_grid->SetCellValue( row, COLUMN_PATH, ap->GetPluginPath() );
192  }
193 
194  m_grid->AutoSizeColumns();
195  m_grid->AutoSizeRows();
196 
197  m_grid->Thaw();
198 
199  return true;
200 }
virtual wxString GetName()=0
Function GetName.
Class ACTION_PLUGIN This is the parent class from where any action plugin class must derive...
Definition: action_plugin.h:40
virtual bool GetShowToolbarButton()=0
Function GetShowToolbarButton.
virtual wxString GetDescription()=0
Function GetDescription.
wxBitmap iconBitmap
Definition: action_plugin.h:50
Class GRID_TRICKS is used to add cut, copy, and paste to an otherwise unmodied wxGrid instance...
Definition: grid_tricks.h:51
virtual wxString GetPluginPath()=0
Function GetPluginPath.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
void OnReloadButtonClick(wxCommandEvent &event) override
Reloads plugins and updates grid.
void OnMoveUpButtonClick(wxCommandEvent &event) override
Moves plugin up in the grid.
PANEL_PCBNEW_ACTION_PLUGINS(PCB_EDIT_FRAME *aFrame, PAGED_DIALOG *aWindow)
void OnMoveDownButtonClick(wxCommandEvent &event) override
Moves plugin down in the grid.
virtual wxString GetCategoryName()=0
Function GetCategoryName.
Class PCBNEW_ACTION_PLUGINS.
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
Class PANEL_PCBNEW_ACTION_PLUGINS_BASE.
void PythonPluginsReload()
Reload the Python plugins if they are newer than the already loaded, and load new plugins if any Do n...
void OnGridCellClick(wxGridEvent &event) override
Selects a whole row.