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-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 
21 #include <action_plugin.h>
22 #include <bitmaps.h>
24 #include <grid_tricks.h>
25 #include <kiface_base.h>
27 #include <pcb_edit_frame.h>
29 #include <pcbnew_settings.h>
30 #include <tool/tool_manager.h>
31 #include <tools/pcb_actions.h>
33 #include <widgets/paged_dialog.h>
34 #include <widgets/wx_grid.h>
35 
36 #include <wx/treebook.h>
37 
38 
39 #define GRID_CELL_MARGIN 4
40 
42  PAGED_DIALOG* aWindow ) :
43  PANEL_PCBNEW_ACTION_PLUGINS_BASE( aWindow->GetTreebook() ),
44  m_frame( aFrame )
45 {
47  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
48 
49  m_moveUpButton->SetBitmap( KiBitmap( BITMAPS::small_up ) );
54 }
55 
56 
58 {
59  m_grid->PopEventHandler( true );
60 }
61 
62 
64 {
65  SelectRow( event.GetRow() );
66 }
67 
68 
70 {
71  m_grid->ClearSelection();
72  m_grid->SelectRow( aRow );
73 }
74 
75 
77 {
78  auto selectedRows = m_grid->GetSelectedRows();
79 
80  // If nothing is selected or multiple rows are selected don't do anything.
81  if( selectedRows.size() != 1 ) return;
82 
83  int selectedRow = selectedRows[0];
84 
85  // If first row is selected, then it can't go any further up.
86  if( selectedRow == 0 )
87  {
88  wxBell();
89  return;
90  }
91 
92  SwapRows( selectedRow, selectedRow - 1 );
93 
94  SelectRow( selectedRow - 1 );
95 }
96 
97 
99 {
100  auto selectedRows = m_grid->GetSelectedRows();
101 
102  // If nothing is selected or multiple rows are selected don't do anything.
103  if( selectedRows.size() != 1 ) return;
104 
105  int selectedRow = selectedRows[0];
106 
107  // If last row is selected, then it can't go any further down.
108  if( selectedRow + 1 == m_grid->GetNumberRows() )
109  {
110  wxBell();
111  return;
112  }
113 
114  SwapRows( selectedRow, selectedRow + 1 );
115 
116  SelectRow( selectedRow + 1 );
117 }
118 
119 
120 void PANEL_PCBNEW_ACTION_PLUGINS::SwapRows( int aRowA, int aRowB )
121 {
122  m_grid->Freeze();
123 
124  // Swap all columns except icon
125  wxString tempStr;
126 
127  for( int column = 1; column < m_grid->GetNumberCols(); column++ )
128  {
129  tempStr = m_grid->GetCellValue( aRowA, column );
130  m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
131  m_grid->SetCellValue( aRowB, column, tempStr );
132  }
133 
134  // Swap icon column renderers
135  auto cellRenderer = m_grid->GetCellRenderer( aRowA, COLUMN_ICON );
136  m_grid->SetCellRenderer( aRowA, COLUMN_ICON, m_grid->GetCellRenderer( aRowB, COLUMN_ICON ) );
137  m_grid->SetCellRenderer( aRowB, COLUMN_ICON, cellRenderer );
138 
139  m_grid->Thaw();
140 }
141 
142 
144 {
147 }
148 
149 
151 {
152  PCBNEW_SETTINGS* settings = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
153  wxASSERT( settings );
154 
155  if( settings )
156  {
157  settings->m_VisibleActionPlugins.clear();
158 
159  for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
160  {
161  settings->m_VisibleActionPlugins.emplace_back( std::make_pair(
162  m_grid->GetCellValue( ii, COLUMN_PATH ),
163  m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT( "1" ) ) );
164  }
165  }
166 
167  return true;
168 }
169 
170 
172 {
173  m_grid->Freeze();
174 
175  m_grid->ClearRows();
176 
177  const auto& orderedPlugins = m_frame->GetOrderedActionPlugins();
178  m_grid->AppendRows( orderedPlugins.size() );
179 
180  for( size_t row = 0; row < orderedPlugins.size(); row++ )
181  {
182  ACTION_PLUGIN* ap = orderedPlugins[row];
183 
184  // Icon
185  m_grid->SetCellRenderer( row, COLUMN_ICON, new GRID_CELL_ICON_RENDERER(
186  ap->iconBitmap.IsOk() ? ap->iconBitmap : m_genericIcon ) );
187 
188  // Toolbar button checkbox
189  m_grid->SetCellRenderer( row, COLUMN_VISIBLE, new wxGridCellBoolRenderer() );
190  m_grid->SetCellAlignment( row, COLUMN_VISIBLE, wxALIGN_CENTER, wxALIGN_CENTER );
191 
192  bool showButton = m_frame->GetActionPluginButtonVisible( ap->GetPluginPath(),
193  ap->GetShowToolbarButton() );
194 
195  m_grid->SetCellValue( row, COLUMN_VISIBLE, showButton ? wxT( "1" ) : wxEmptyString );
196 
197  m_grid->SetCellValue( row, COLUMN_NAME, ap->GetName() );
198  m_grid->SetCellValue( row, COLUMN_CATEGORY, ap->GetCategoryName() );
199  m_grid->SetCellValue( row, COLUMN_DESCRIPTION, ap->GetDescription() );
200  m_grid->SetCellValue( row, COLUMN_PATH, ap->GetPluginPath() );
201  }
202 
203  for( int col = 0; col < m_grid->GetNumberCols(); col++ )
204  {
205  const wxString& heading = m_grid->GetColLabelValue( col );
206  int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
207 
208  // Set the minimal width to the column label size.
209  m_grid->SetColMinimalWidth( col, headingWidth );
210  // Set the width to see the full contents
211  m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true, false ) );
212  }
213 
214  m_grid->AutoSizeRows();
215 
216  m_grid->Thaw();
217 
218  // Show errors button should be disabled if there are no errors.
219  wxString trace;
220  pcbnewGetWizardsBackTrace( trace );
221  if( trace.empty() )
222  {
223  m_showErrorsButton->Disable();
224  m_showErrorsButton->Hide();
225  }
226  else
227  {
228  m_showErrorsButton->Enable();
229  m_showErrorsButton->Show();
230  }
231 
232  return true;
233 }
234 
236 {
238 }
239 
241 {
242  wxString trace;
243  pcbnewGetWizardsBackTrace( trace );
244 
245  // Now display the filtered trace in our dialog
246  // (a simple wxMessageBox is really not suitable for long messages)
247  DIALOG_FOOTPRINT_WIZARD_LOG logWindow( this );
248  logWindow.m_Message->SetValue( trace );
249  logWindow.ShowModal();
250 }
virtual wxString GetName()=0
This is the parent class from where any action plugin class must derive.
Definition: action_plugin.h:38
void OnShowErrorsButtonClick(wxCommandEvent &event) override
Shows plugin import errors.
virtual bool GetShowToolbarButton()=0
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
static TOOL_ACTION pluginsReload
Scripting Actions.
Definition: pcb_actions.h:312
virtual wxString GetDescription()=0
wxBitmap iconBitmap
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:55
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:143
Class DIALOG_FOOTPRINT_WIZARD_LOG.
bool GetActionPluginButtonVisible(const wxString &aPluginPath, bool aPluginDefault)
Return true if button visibility action plugin setting was set to true or it is unset and plugin defa...
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=false, bool aKeep=true)
Calculates the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:246
#define GRID_CELL_MARGIN
virtual wxString GetPluginPath()=0
void pcbnewGetWizardsBackTrace(wxString &aTrace)
Return the backtrace of errors (if any) when wizard python scripts are loaded.
void OnReloadButtonClick(wxCommandEvent &event) override
Reloads plugins and updates grid.
void OnOpenDirectoryButtonClick(wxCommandEvent &event) override
Opens user's action plugin directory.
void OnMoveUpButtonClick(wxCommandEvent &event) override
Moves plugin up in the grid.
std::vector< ACTION_PLUGIN * > GetOrderedActionPlugins()
Return ordered list of plugins in sequence in which they should appear on toolbar or in settings.
PANEL_PCBNEW_ACTION_PLUGINS(PCB_EDIT_FRAME *aFrame, PAGED_DIALOG *aWindow)
void OnMoveDownButtonClick(wxCommandEvent &event) override
Moves plugin down in the grid.
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
static TOOL_ACTION pluginsShowFolder
Definition: pcb_actions.h:313
virtual wxString GetCategoryName()=0
Class PCBNEW_ACTION_PLUGINS.
ACTION_PLUGIN_SETTINGS_LIST m_VisibleActionPlugins
The main frame for Pcbnew.
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:106
Class PANEL_PCBNEW_ACTION_PLUGINS_BASE.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
void OnGridCellClick(wxGridEvent &event) override
Selects a whole row.