KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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 <api/api_plugin.h>
23#include <bitmaps.h>
25#include <grid_tricks.h>
26#include <kiface_base.h>
27#include <kiplatform/ui.h>
29#include <pcb_edit_frame.h>
31#include <pcb_scripting_tool.h>
32#include <pcbnew_settings.h>
33#include <pgm_base.h>
37#include <widgets/wx_grid.h>
39
40
41#define GRID_CELL_MARGIN 4
42
43enum
44{
46};
47
49{
50public:
52 GRID_TRICKS( aGrid )
53 {}
54
55protected:
56 void showPopupMenu( wxMenu& menu, wxGridEvent& aEvent ) override;
57 void doPopupSelection( wxCommandEvent& event ) override;
58};
59
60
61void PLUGINS_GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
62{
63#ifdef KICAD_IPC_API
64 API_PLUGIN_MANAGER& mgr = Pgm().GetPluginManager();
65 wxString id = m_grid->GetCellValue( m_grid->GetGridCursorRow(),
67
68 if( std::optional<const PLUGIN_ACTION*> action = mgr.GetAction( id ) )
69 {
70 menu.Append( MYID_RECREATE_ENV, _( "Recreate Plugin Environment" ), _( "Recreate Plugin Environment" ) );
71 menu.AppendSeparator();
72 }
73#endif
74
75 GRID_TRICKS::showPopupMenu( menu, aEvent );
76}
77
78
79void PLUGINS_GRID_TRICKS::doPopupSelection( wxCommandEvent& event )
80{
81 if( event.GetId() == MYID_RECREATE_ENV )
82 {
83#ifdef KICAD_IPC_API
84 API_PLUGIN_MANAGER& mgr = Pgm().GetPluginManager();
85 wxString id = m_grid->GetCellValue( m_grid->GetGridCursorRow(),
87
88 if( std::optional<const PLUGIN_ACTION*> action = mgr.GetAction( id ) )
89 mgr.RecreatePluginEnvironment( ( *action )->plugin.Identifier() );
90#endif
91 }
92 else
93 {
95 }
96}
97
98
101{
102 m_genericIcon = KiBitmapBundle( BITMAPS::puzzle_piece );
103 m_grid->PushEventHandler( new PLUGINS_GRID_TRICKS( m_grid ) );
104 m_grid->SetUseNativeColLabels();
105
106 m_moveUpButton->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
107 m_moveDownButton->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
108 m_openDirectoryButton->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
109 m_reloadButton->SetBitmap( KiBitmapBundle( BITMAPS::small_refresh ) );
110 m_showErrorsButton->SetBitmap( KiBitmapBundle( BITMAPS::small_warning ) );
111}
112
113
115{
116 m_grid->PopEventHandler( true );
117}
118
119
121{
122 SelectRow( event.GetRow() );
123}
124
125
127{
128 m_grid->ClearSelection();
129 m_grid->SelectRow( aRow );
130}
131
132
134{
136 [&]( int row )
137 {
138 SwapRows( row, row - 1 );
139 } );
140}
141
142
144{
146 [&]( int row )
147 {
148 SwapRows( row, row + 1 );
149 } );
150}
151
152
153void PANEL_PCBNEW_ACTION_PLUGINS::SwapRows( int aRowA, int aRowB )
154{
155 m_grid->Freeze();
156
157 m_grid->SwapRows( aRowA, aRowB );
158
159 // Swap icon column renderers
160 auto cellRenderer = m_grid->GetCellRenderer( aRowA, COLUMN_ACTION_NAME );
161 m_grid->SetCellRenderer( aRowA, COLUMN_ACTION_NAME, m_grid->GetCellRenderer( aRowB, COLUMN_ACTION_NAME ) );
162 m_grid->SetCellRenderer( aRowB, COLUMN_ACTION_NAME, cellRenderer );
163
164 m_grid->Thaw();
165}
166
167
169{
172}
173
174
176{
177 PCBNEW_SETTINGS* settings = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
178 wxASSERT( settings );
179
180#ifdef KICAD_IPC_API
181 API_PLUGIN_MANAGER& mgr = Pgm().GetPluginManager();
182
183 if( settings )
184 {
185 settings->m_VisibleActionPlugins.clear();
186 settings->m_Plugins.actions.clear();
187
188 for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
189 {
190 wxString id = m_grid->GetCellValue( ii, COLUMN_SETTINGS_IDENTIFIER );
191
192 if( mgr.GetAction( id ) != std::nullopt )
193 {
194 settings->m_Plugins.actions.emplace_back( std::make_pair(
195 id, m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT( "1" ) ) );
196 }
197 else
198 {
199 settings->m_VisibleActionPlugins.emplace_back( std::make_pair(
200 id, m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT( "1" ) ) );
201 }
202 }
203 }
204#else
205 if( settings )
206 {
207 settings->m_VisibleActionPlugins.clear();
208
209 for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
210 {
211 wxString id = m_grid->GetCellValue( ii, COLUMN_SETTINGS_IDENTIFIER );
212
213 settings->m_VisibleActionPlugins.emplace_back( std::make_pair(
214 id, m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT( "1" ) ) );
215 }
216 }
217#endif
218
219 return true;
220}
221
222
224{
225 m_grid->Freeze();
226
227 m_grid->ClearRows();
228
229 const std::vector<LEGACY_OR_API_PLUGIN>& orderedPlugins = PCB_EDIT_FRAME::GetOrderedActionPlugins();
230 m_grid->AppendRows( orderedPlugins.size() );
231
233 wxSize iconSize( size, size );
234
235 for( size_t row = 0; row < orderedPlugins.size(); row++ )
236 {
237 if( std::holds_alternative<ACTION_PLUGIN*>( orderedPlugins[row] ) )
238 {
239 auto ap = std::get<ACTION_PLUGIN*>( orderedPlugins[row] );
240
241 // Icon
242 m_grid->SetCellRenderer( row, COLUMN_ACTION_NAME,
243 new GRID_CELL_ICON_TEXT_RENDERER( ap->iconBitmap.IsOk() ? wxBitmapBundle( ap->iconBitmap )
245 iconSize ) );
246 m_grid->SetCellValue( row, COLUMN_ACTION_NAME, ap->GetName() );
247 m_grid->SetCellValue( row, COLUMN_SETTINGS_IDENTIFIER, ap->GetPluginPath() );
248
249 // Toolbar button checkbox
250 m_grid->SetCellRenderer( row, COLUMN_VISIBLE, new wxGridCellBoolRenderer() );
251 m_grid->SetCellAlignment( row, COLUMN_VISIBLE, wxALIGN_CENTER, wxALIGN_CENTER );
252
253 bool show = PCB_EDIT_FRAME::GetActionPluginButtonVisible( ap->GetPluginPath(),
254 ap->GetShowToolbarButton() );
255
256 m_grid->SetCellValue( row, COLUMN_VISIBLE, show ? wxT( "1" ) : wxEmptyString );
257
258 m_grid->SetCellValue( row, COLUMN_PLUGIN_NAME, ap->GetClassName() );
259 m_grid->SetCellValue( row, COLUMN_DESCRIPTION, ap->GetDescription() );
260 }
261 else
262 {
263#ifdef KICAD_IPC_API
264 auto action = std::get<const PLUGIN_ACTION*>( orderedPlugins[row] );
265
266 const wxBitmapBundle& icon = KIPLATFORM::UI::IsDarkTheme() && action->icon_dark.IsOk() ? action->icon_dark
267 : action->icon_light;
268
269 // Icon
270 m_grid->SetCellRenderer( row, COLUMN_ACTION_NAME, new GRID_CELL_ICON_TEXT_RENDERER(
271 icon.IsOk() ? icon : m_genericIcon, iconSize ) );
272 m_grid->SetCellValue( row, COLUMN_ACTION_NAME, action->name );
273 m_grid->SetCellValue( row, COLUMN_SETTINGS_IDENTIFIER, action->identifier );
274
275 // Toolbar button checkbox
276 m_grid->SetCellRenderer( row, COLUMN_VISIBLE, new wxGridCellBoolRenderer() );
277 m_grid->SetCellAlignment( row, COLUMN_VISIBLE, wxALIGN_CENTER, wxALIGN_CENTER );
278
279 bool show = PCB_EDIT_FRAME::GetActionPluginButtonVisible( action->identifier, action->show_button );
280
281 m_grid->SetCellValue( row, COLUMN_VISIBLE, show ? wxT( "1" ) : wxEmptyString );
282
283 m_grid->SetCellValue( row, COLUMN_PLUGIN_NAME, action->plugin.Name() );
284 m_grid->SetCellValue( row, COLUMN_DESCRIPTION, action->description );
285#endif
286 }
287 }
288
289 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
290 {
291 const wxString& heading = m_grid->GetColLabelValue( col );
292 int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
293
294 // Set the minimal width to the column label size.
295 m_grid->SetColMinimalWidth( col, headingWidth );
296 // Set the width to see the full contents
297 m_grid->SetColSize( col, m_grid->GetVisibleWidth( col ) );
298 }
299
300 m_grid->AutoSizeRows();
301 m_grid->AutoSizeColumns();
303
304 m_grid->Thaw();
305
306 // Show errors button should be disabled if there are no errors.
307 wxString trace;
308
311
312 if( trace.empty() )
313 {
314 m_showErrorsButton->Disable();
315 m_showErrorsButton->Hide();
316 }
317 else
318 {
320 m_showErrorsButton->Show();
321 }
322
323 return true;
324}
325
326
328{
330}
331
332
334{
335 wxString trace;
337
338 // Now display the filtered trace in our dialog
339 // (a simple wxMessageBox is really not suitable for long messages)
340 DIALOG_FOOTPRINT_WIZARD_LOG logWindow( wxGetTopLevelParent( this ) );
341 logWindow.m_Message->SetValue( trace );
342 logWindow.ShowModal();
343}
Class PCBNEW_ACTION_PLUGINS.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
static int GetActionsCount()
Responsible for loading plugin definitions for API-based plugins (ones that do not run inside KiCad i...
void RecreatePluginEnvironment(const wxString &aIdentifier)
std::optional< const PLUGIN_ACTION * > GetAction(const wxString &aIdentifier)
APPEARANCE m_Appearance
Class DIALOG_FOOTPRINT_WIZARD_LOG.
int ShowModal() override
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
virtual void doPopupSelection(wxCommandEvent &event)
virtual void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent)
WX_GRID * m_grid
I don't own the grid, but he owns me.
Definition: grid_tricks.h:128
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:95
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< std::variant< ACTION_PLUGIN *, const PLUGIN_ACTION * > > 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...
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:565
void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent) override
void doPopupSelection(wxCommandEvent &event) override
static void ShowPluginFolder()
static void ReloadPlugins()
bool Enable(bool aEnable=true) override
void SetBitmap(const wxBitmapBundle &aBmp)
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=true, bool aKeep=false)
Calculate the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:916
void OnMoveRowUp(const std::function< void(int row)> &aMover)
Definition: wx_grid.cpp:766
void SwapRows(int aRowA, int aRowB)
These aren't that tricky, but might as well share code.
Definition: wx_grid.cpp:755
void OnMoveRowDown(const std::function< void(int row)> &aMover)
Definition: wx_grid.cpp:799
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:220
#define GRID_CELL_MARGIN
#define _(s)
@ GRIDTRICKS_FIRST_CLIENT_ID
Definition: grid_tricks.h:48
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: wxgtk/ui.cpp:48
void pcbnewGetWizardsBackTrace(wxString &aTrace)
Return the backtrace of errors (if any) when wizard python scripts are loaded.
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:902
see class PGM_BASE
std::vector< std::pair< wxString, bool > > actions
Ordered list of plugin actions mapped to whether or not they are shown in the toolbar.
Definition: app_settings.h:199