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 <api/api_plugin.h>
22#include <bitmaps.h>
24#include <grid_tricks.h>
25#include <kiface_base.h>
26#include <kiplatform/ui.h>
28#include <paths.h>
29#include <pcb_edit_frame.h>
30#include <pcbnew_settings.h>
31#include <pgm_base.h>
32#include <reporter.h>
36#include <launch_ext.h>
37#include <widgets/kistatusbar.h>
40#include <widgets/wx_grid.h>
43#include <wx/app.h>
44
45#include <algorithm>
46
47
48#define GRID_CELL_MARGIN 4
49
50enum
51{
53};
54
56{
57public:
59 GRID_TRICKS( aGrid )
60 {}
61
62protected:
63 void showPopupMenu( wxMenu& menu, wxGridEvent& aEvent ) override;
64 void doPopupSelection( wxCommandEvent& event ) override;
65};
66
67
68void PLUGINS_GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
69{
70 const int clickedRow = aEvent.GetRow();
71
72 if( clickedRow >= 0 )
73 {
74 m_grid->SetGridCursor( clickedRow, m_grid->GetGridCursorCol() );
75 m_grid->ClearSelection();
76 m_grid->SelectRow( clickedRow );
77
78#ifdef KICAD_IPC_API
79 API_PLUGIN_MANAGER& mgr = Pgm().GetPluginManager();
80 wxString id = m_grid->GetCellValue( clickedRow,
82
83 if( std::optional<const PLUGIN_ACTION*> action = mgr.GetAction( id );
84 action && ( *action )->plugin.Runtime().type == PLUGIN_RUNTIME_TYPE::PYTHON )
85 {
86 menu.Append( MYID_RECREATE_ENV, _( "Recreate Plugin Environment" ), _( "Recreate Plugin Environment" ) );
87 menu.AppendSeparator();
88 }
89#endif
90 }
91
92 GRID_TRICKS::showPopupMenu( menu, aEvent );
93}
94
95
96void PLUGINS_GRID_TRICKS::doPopupSelection( wxCommandEvent& event )
97{
98 if( event.GetId() == MYID_RECREATE_ENV )
99 {
100#ifdef KICAD_IPC_API
101 API_PLUGIN_MANAGER& mgr = Pgm().GetPluginManager();
102 wxString id = m_grid->GetCellValue( m_grid->GetGridCursorRow(),
104
105 if( std::optional<const PLUGIN_ACTION*> action = mgr.GetAction( id );
106 action && ( *action )->plugin.Runtime().type == PLUGIN_RUNTIME_TYPE::PYTHON )
107 {
108 mgr.RecreatePluginEnvironment( ( *action )->plugin.Identifier() );
109 }
110#endif
111 }
112 else
113 {
115 }
116}
117
118
121{
123 m_grid->PushEventHandler( new PLUGINS_GRID_TRICKS( m_grid ) );
124 m_grid->SetUseNativeColLabels();
125
126 // Pin best size before TransferDataToWindow grows columns past the screen (#24408).
127 m_grid->OverrideMinSize( 1.0, 1.0 );
128
134
135 m_errorDialog = new DIALOG_HTML_REPORTER( aParent );
136 m_allowErrorDialog = false;
137
140}
141
142
150
151
153{
154 m_grid->Enable();
156
157 if( m_allowErrorDialog && m_errorDialog->m_Reporter->HasMessage() )
158 {
159 m_errorDialog->m_Reporter->Flush();
160 m_allowErrorDialog = false;
161 m_errorDialog->ShowModal();
162 }
163
164 aEvt.Skip();
165}
166
167
169{
170 SelectRow( event.GetRow() );
171}
172
173
175{
176 m_grid->ClearSelection();
177 m_grid->SelectRow( aRow );
178}
179
180
182{
183 m_grid->OnMoveRowUp(
184 [&]( int row )
185 {
186 SwapRows( row, row - 1 );
187 } );
188}
189
190
192{
193 m_grid->OnMoveRowDown(
194 [&]( int row )
195 {
196 SwapRows( row, row + 1 );
197 } );
198}
199
200
201void PANEL_PCBNEW_ACTION_PLUGINS::SwapRows( int aRowA, int aRowB )
202{
203 m_grid->Freeze();
204
205 m_grid->SwapRows( aRowA, aRowB );
206
207 // Swap icon column renderers
208 auto cellRenderer = m_grid->GetCellRenderer( aRowA, COLUMN_ACTION_NAME );
209 m_grid->SetCellRenderer( aRowA, COLUMN_ACTION_NAME, m_grid->GetCellRenderer( aRowB, COLUMN_ACTION_NAME ) );
210 m_grid->SetCellRenderer( aRowB, COLUMN_ACTION_NAME, cellRenderer );
211
212 m_grid->Thaw();
213}
214
215
217{
218 API_PLUGIN_MANAGER& mgr = Pgm().GetPluginManager();
219 m_errorDialog->m_Reporter->Clear();
220 auto reporter = std::make_shared<REDIRECT_REPORTER>( m_errorDialog->m_Reporter );
221 m_allowErrorDialog = true;
222 mgr.ReloadPlugins( std::nullopt, reporter );
223 m_grid->Disable();
224}
225
226
228{
229 PCBNEW_SETTINGS* settings = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
230 wxASSERT( settings );
231
232#ifdef KICAD_IPC_API
233 API_PLUGIN_MANAGER& mgr = Pgm().GetPluginManager();
234
235 if( settings )
236 {
237 settings->m_Plugins.actions.clear();
238
239 for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
240 {
241 wxString id = m_grid->GetCellValue( ii, COLUMN_SETTINGS_IDENTIFIER );
242
243 if( mgr.GetAction( id ) != std::nullopt )
244 {
245 settings->m_Plugins.actions.emplace_back( std::make_pair(
246 id, m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT( "1" ) ) );
247 }
248 }
249 }
250#endif
251
252 return true;
253}
254
255
257{
258 m_grid->Freeze();
259
260 m_grid->ClearRows();
261
262 const std::vector<const PLUGIN_ACTION*>& orderedPlugins = PCB_EDIT_FRAME::GetOrderedPluginActions();
263 m_grid->AppendRows( orderedPlugins.size() );
264
266 wxSize iconSize( size, size );
267
268 for( size_t row = 0; row < orderedPlugins.size(); row++ )
269 {
270#ifdef KICAD_IPC_API
271 const PLUGIN_ACTION* action = orderedPlugins[row];
272
273 const wxBitmapBundle& icon = KIPLATFORM::UI::IsDarkTheme() && action->icon_dark.IsOk() ? action->icon_dark
274 : action->icon_light;
275
276 // Icon
277 m_grid->SetCellRenderer( row, COLUMN_ACTION_NAME, new GRID_CELL_ICON_TEXT_RENDERER(
278 icon.IsOk() ? icon : m_genericIcon, iconSize ) );
279 m_grid->SetCellValue( row, COLUMN_ACTION_NAME, action->name );
280 m_grid->SetCellValue( row, COLUMN_SETTINGS_IDENTIFIER, action->identifier );
281
282 // Toolbar button checkbox
283 m_grid->SetCellRenderer( row, COLUMN_VISIBLE, new wxGridCellBoolRenderer() );
284 m_grid->SetCellAlignment( row, COLUMN_VISIBLE, wxALIGN_CENTER, wxALIGN_CENTER );
285
287
288 m_grid->SetCellValue( row, COLUMN_VISIBLE, show ? wxT( "1" ) : wxEmptyString );
289
290 m_grid->SetCellValue( row, COLUMN_PLUGIN_NAME, action->plugin.Name() );
291 m_grid->SetCellValue( row, COLUMN_DESCRIPTION, action->description );
292#endif
293 }
294
295 const int colMaxWidth = FromDIP( 400 );
296
297 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
298 {
299 const wxString& heading = m_grid->GetColLabelValue( col );
300 int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
301
302 m_grid->SetColMinimalWidth( col, headingWidth );
303 int width = std::min( m_grid->GetVisibleWidth( col ), colMaxWidth );
304 m_grid->SetColSize( col, std::max( headingWidth, width ) );
305 }
306
307 m_grid->AutoSizeRows();
308 // AutoSizeColumns() would re-expand columns to full content width (setAsMin=true) and undo
309 // the cap above (#24408).
311
312 m_grid->Thaw();
313
314 // Show errors button should be disabled if there are no errors.
315 wxString trace;
316
317 if( trace.empty() )
318 {
319 m_showErrorsButton->Disable();
320 m_showErrorsButton->Hide();
321 }
322 else
323 {
324 m_showErrorsButton->Enable();
325 m_showErrorsButton->Show();
326 }
327
328 return true;
329}
330
331
333{
334 wxString dir( PATHS::GetUserPluginsPath() );
335 LaunchExternal( dir );
336}
337
338
340{
341 wxString trace;
342
343 // Now display the filtered trace in our dialog
344 // (a simple wxMessageBox is really not suitable for long messages)
345 DIALOG_FOOTPRINT_WIZARD_LOG logWindow( wxGetTopLevelParent( this ) );
346 logWindow.m_Message->SetValue( trace );
347 logWindow.ShowModal();
348}
const KICOMMON_API wxEventTypeTag< wxCommandEvent > EDA_EVT_PLUGIN_AVAILABILITY_CHANGED
Notifies other parts of KiCad when plugin availability changes.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
Responsible for loading plugin definitions for API-based plugins (ones that do not run inside KiCad i...
void ReloadPlugins(std::optional< wxString > aDirectoryToScan=std::nullopt, std::shared_ptr< REPORTER > aReporter=nullptr)
Clears the loaded plugins and actions and re-scans the filesystem to register new ones.
void RecreatePluginEnvironment(const wxString &aIdentifier)
std::optional< const PLUGIN_ACTION * > GetAction(const wxString &aIdentifier)
const wxString & Name() const
APPEARANCE m_Appearance
Class DIALOG_FOOTPRINT_WIZARD_LOG.
Class DIALOG_HTML_REPORTER.
int ShowModal() override
GRID_TRICKS(WX_GRID *aGrid)
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.
APP_SETTINGS_BASE * KifaceSettings() const
Definition kiface_base.h:95
PANEL_PCBNEW_ACTION_PLUGINS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
void onPluginAvailabilityChanged(wxCommandEvent &aEvt)
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.
static wxString GetUserPluginsPath()
Gets the user path for plugins.
Definition paths.cpp:49
static bool GetPluginActionButtonVisible(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 std::vector< const PLUGIN_ACTION * > GetOrderedPluginActions()
Return ordered list of plugins in sequence in which they should appear on toolbar or in settings.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:541
void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent) override
void doPopupSelection(wxCommandEvent &event) override
#define GRID_CELL_MARGIN
#define _(s)
@ GRIDTRICKS_FIRST_CLIENT_ID
Definition grid_tricks.h:48
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition wxgtk/ui.cpp:50
PGM_BASE & Pgm()
The global program "get" accessor.
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.
An action performed by a plugin via the IPC API.
Definition api_plugin.h:72
wxBitmapBundle icon_light
Definition api_plugin.h:84
const API_PLUGIN & plugin
Definition api_plugin.h:87
wxString name
Definition api_plugin.h:78
wxString description
Definition api_plugin.h:79
wxString identifier
Definition api_plugin.h:77
wxBitmapBundle icon_dark
Definition api_plugin.h:85
IbisParser parser & reporter