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-2022 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 <pcb_scripting_tool.h>
30#include <pcbnew_settings.h>
33#include <widgets/wx_grid.h>
35
36
37#define GRID_CELL_MARGIN 4
38
41{
43 m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
44
50}
51
52
54{
55 m_grid->PopEventHandler( true );
56}
57
58
60{
61 SelectRow( event.GetRow() );
62}
63
64
66{
67 m_grid->ClearSelection();
68 m_grid->SelectRow( aRow );
69}
70
71
73{
74 auto selectedRows = m_grid->GetSelectedRows();
75
76 // If nothing is selected or multiple rows are selected don't do anything.
77 if( selectedRows.size() != 1 ) return;
78
79 int selectedRow = selectedRows[0];
80
81 // If first row is selected, then it can't go any further up.
82 if( selectedRow == 0 )
83 {
84 wxBell();
85 return;
86 }
87
88 SwapRows( selectedRow, selectedRow - 1 );
89
90 SelectRow( selectedRow - 1 );
91}
92
93
95{
96 auto selectedRows = m_grid->GetSelectedRows();
97
98 // If nothing is selected or multiple rows are selected don't do anything.
99 if( selectedRows.size() != 1 ) return;
100
101 int selectedRow = selectedRows[0];
102
103 // If last row is selected, then it can't go any further down.
104 if( selectedRow + 1 == m_grid->GetNumberRows() )
105 {
106 wxBell();
107 return;
108 }
109
110 SwapRows( selectedRow, selectedRow + 1 );
111
112 SelectRow( selectedRow + 1 );
113}
114
115
116void PANEL_PCBNEW_ACTION_PLUGINS::SwapRows( int aRowA, int aRowB )
117{
118 m_grid->Freeze();
119
120 // Swap all columns except icon
121 wxString tempStr;
122
123 for( int column = 1; column < m_grid->GetNumberCols(); column++ )
124 {
125 tempStr = m_grid->GetCellValue( aRowA, column );
126 m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
127 m_grid->SetCellValue( aRowB, column, tempStr );
128 }
129
130 // Swap icon column renderers
131 auto cellRenderer = m_grid->GetCellRenderer( aRowA, COLUMN_ICON );
132 m_grid->SetCellRenderer( aRowA, COLUMN_ICON, m_grid->GetCellRenderer( aRowB, COLUMN_ICON ) );
133 m_grid->SetCellRenderer( aRowB, COLUMN_ICON, cellRenderer );
134
135 m_grid->Thaw();
136}
137
138
140{
143}
144
145
147{
148 PCBNEW_SETTINGS* settings = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
149 wxASSERT( settings );
150
151 if( settings )
152 {
153 settings->m_VisibleActionPlugins.clear();
154
155 for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
156 {
157 settings->m_VisibleActionPlugins.emplace_back( std::make_pair(
158 m_grid->GetCellValue( ii, COLUMN_PATH ),
159 m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT( "1" ) ) );
160 }
161 }
162
163 return true;
164}
165
166
168{
169 m_grid->Freeze();
170
171 m_grid->ClearRows();
172
173 const auto& orderedPlugins = PCB_EDIT_FRAME::GetOrderedActionPlugins();
174 m_grid->AppendRows( orderedPlugins.size() );
175
176 for( size_t row = 0; row < orderedPlugins.size(); row++ )
177 {
178 ACTION_PLUGIN* ap = orderedPlugins[row];
179
180 // Icon
181 m_grid->SetCellRenderer( row, COLUMN_ICON, new GRID_CELL_ICON_RENDERER(
182 ap->iconBitmap.IsOk() ? ap->iconBitmap : m_genericIcon ) );
183
184 // Toolbar button checkbox
185 m_grid->SetCellRenderer( row, COLUMN_VISIBLE, new wxGridCellBoolRenderer() );
186 m_grid->SetCellAlignment( row, COLUMN_VISIBLE, wxALIGN_CENTER, wxALIGN_CENTER );
187
189 ap->GetShowToolbarButton() );
190
191 m_grid->SetCellValue( row, COLUMN_VISIBLE, show ? wxT( "1" ) : wxEmptyString );
192
193 m_grid->SetCellValue( row, COLUMN_NAME, ap->GetName() );
194 m_grid->SetCellValue( row, COLUMN_CATEGORY, ap->GetCategoryName() );
195 m_grid->SetCellValue( row, COLUMN_DESCRIPTION, ap->GetDescription() );
196 m_grid->SetCellValue( row, COLUMN_PATH, ap->GetPluginPath() );
197 }
198
199 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
200 {
201 const wxString& heading = m_grid->GetColLabelValue( col );
202 int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
203
204 // Set the minimal width to the column label size.
205 m_grid->SetColMinimalWidth( col, headingWidth );
206 // Set the width to see the full contents
207 m_grid->SetColSize( col, m_grid->GetVisibleWidth( col ) );
208 }
209
210 m_grid->AutoSizeRows();
211
212 m_grid->Thaw();
213
214 // Show errors button should be disabled if there are no errors.
215 wxString trace;
216
219
220 if( trace.empty() )
221 {
222 m_showErrorsButton->Disable();
223 m_showErrorsButton->Hide();
224 m_reloadButton->Disable();
225 }
226 else
227 {
229 m_showErrorsButton->Show();
231 }
232
233 return true;
234}
235
236
238{
240}
241
242
244{
245 wxString trace;
247
248 // Now display the filtered trace in our dialog
249 // (a simple wxMessageBox is really not suitable for long messages)
250 DIALOG_FOOTPRINT_WIZARD_LOG logWindow( this );
251 logWindow.m_Message->SetValue( trace );
252 logWindow.ShowModal();
253}
Class PCBNEW_ACTION_PLUGINS.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
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
@ small_warning
@ small_folder
@ puzzle_piece
@ small_refresh
static int GetActionsCount()
This is the parent class from where any action plugin class must derive.
Definition: action_plugin.h:39
wxBitmap iconBitmap
virtual wxString GetDescription()=0
virtual wxString GetPluginPath()=0
virtual wxString GetCategoryName()=0
virtual wxString GetName()=0
virtual bool GetShowToolbarButton()=0
Class DIALOG_FOOTPRINT_WIZARD_LOG.
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:93
Class PANEL_PCBNEW_ACTION_PLUGINS_BASE.
void OnShowErrorsButtonClick(wxCommandEvent &event) override
Shows plugin import errors.
void OnMoveUpButtonClick(wxCommandEvent &event) override
Moves plugin up in the grid.
void OnGridCellClick(wxGridEvent &event) override
Selects a whole row.
void OnMoveDownButtonClick(wxCommandEvent &event) override
Moves plugin down in the grid.
void OnOpenDirectoryButtonClick(wxCommandEvent &event) override
Opens user's action plugin directory.
void OnReloadButtonClick(wxCommandEvent &event) override
Reloads plugins and updates grid.
ACTION_PLUGIN_SETTINGS_LIST m_VisibleActionPlugins
static std::vector< ACTION_PLUGIN * > GetOrderedActionPlugins()
Return ordered list of plugins in sequence in which they should appear on toolbar or in settings.
static 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...
static void ShowPluginFolder()
static void ReloadPlugins()
void SetBitmap(const wxBitmap &aBmp)
bool Enable(bool aEnable=true) override
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=true, bool aKeep=false)
Calculates the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:520
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:139
#define GRID_CELL_MARGIN
void pcbnewGetWizardsBackTrace(wxString &aTrace)
Return the backtrace of errors (if any) when wizard python scripts are loaded.