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>
34#include <launch_ext.h>
37#include <widgets/wx_grid.h>
39#include <wx/app.h>
40
41
42#define GRID_CELL_MARGIN 4
43
44enum
45{
47};
48
50{
51public:
53 GRID_TRICKS( aGrid )
54 {}
55
56protected:
57 void showPopupMenu( wxMenu& menu, wxGridEvent& aEvent ) override;
58 void doPopupSelection( wxCommandEvent& event ) override;
59};
60
61
62void PLUGINS_GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
63{
64#ifdef KICAD_IPC_API
65 API_PLUGIN_MANAGER& mgr = Pgm().GetPluginManager();
66 wxString id = m_grid->GetCellValue( m_grid->GetGridCursorRow(),
68
69 if( std::optional<const PLUGIN_ACTION*> action = mgr.GetAction( id ) )
70 {
71 menu.Append( MYID_RECREATE_ENV, _( "Recreate Plugin Environment" ), _( "Recreate Plugin Environment" ) );
72 menu.AppendSeparator();
73 }
74#endif
75
76 GRID_TRICKS::showPopupMenu( menu, aEvent );
77}
78
79
80void PLUGINS_GRID_TRICKS::doPopupSelection( wxCommandEvent& event )
81{
82 if( event.GetId() == MYID_RECREATE_ENV )
83 {
84#ifdef KICAD_IPC_API
85 API_PLUGIN_MANAGER& mgr = Pgm().GetPluginManager();
86 wxString id = m_grid->GetCellValue( m_grid->GetGridCursorRow(),
88
89 if( std::optional<const PLUGIN_ACTION*> action = mgr.GetAction( id ) )
90 mgr.RecreatePluginEnvironment( ( *action )->plugin.Identifier() );
91#endif
92 }
93 else
94 {
96 }
97}
98
99
116
117
124
125
127{
128 m_grid->Enable();
130 aEvt.Skip();
131}
132
133
135{
136 SelectRow( event.GetRow() );
137}
138
139
141{
142 m_grid->ClearSelection();
143 m_grid->SelectRow( aRow );
144}
145
146
148{
149 m_grid->OnMoveRowUp(
150 [&]( int row )
151 {
152 SwapRows( row, row - 1 );
153 } );
154}
155
156
158{
159 m_grid->OnMoveRowDown(
160 [&]( int row )
161 {
162 SwapRows( row, row + 1 );
163 } );
164}
165
166
167void PANEL_PCBNEW_ACTION_PLUGINS::SwapRows( int aRowA, int aRowB )
168{
169 m_grid->Freeze();
170
171 m_grid->SwapRows( aRowA, aRowB );
172
173 // Swap icon column renderers
174 auto cellRenderer = m_grid->GetCellRenderer( aRowA, COLUMN_ACTION_NAME );
175 m_grid->SetCellRenderer( aRowA, COLUMN_ACTION_NAME, m_grid->GetCellRenderer( aRowB, COLUMN_ACTION_NAME ) );
176 m_grid->SetCellRenderer( aRowB, COLUMN_ACTION_NAME, cellRenderer );
177
178 m_grid->Thaw();
179}
180
181
183{
184 API_PLUGIN_MANAGER& mgr = Pgm().GetPluginManager();
185 mgr.ReloadPlugins();
186 m_grid->Disable();
187}
188
189
191{
192 PCBNEW_SETTINGS* settings = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
193 wxASSERT( settings );
194
195#ifdef KICAD_IPC_API
196 API_PLUGIN_MANAGER& mgr = Pgm().GetPluginManager();
197
198 if( settings )
199 {
200 settings->m_Plugins.actions.clear();
201
202 for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
203 {
204 wxString id = m_grid->GetCellValue( ii, COLUMN_SETTINGS_IDENTIFIER );
205
206 if( mgr.GetAction( id ) != std::nullopt )
207 {
208 settings->m_Plugins.actions.emplace_back( std::make_pair(
209 id, m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT( "1" ) ) );
210 }
211 }
212 }
213#endif
214
215 return true;
216}
217
218
220{
221 m_grid->Freeze();
222
223 m_grid->ClearRows();
224
225 const std::vector<const PLUGIN_ACTION*>& orderedPlugins = PCB_EDIT_FRAME::GetOrderedPluginActions();
226 m_grid->AppendRows( orderedPlugins.size() );
227
229 wxSize iconSize( size, size );
230
231 for( size_t row = 0; row < orderedPlugins.size(); row++ )
232 {
233#ifdef KICAD_IPC_API
234 const PLUGIN_ACTION* action = orderedPlugins[row];
235
236 const wxBitmapBundle& icon = KIPLATFORM::UI::IsDarkTheme() && action->icon_dark.IsOk() ? action->icon_dark
237 : action->icon_light;
238
239 // Icon
240 m_grid->SetCellRenderer( row, COLUMN_ACTION_NAME, new GRID_CELL_ICON_TEXT_RENDERER(
241 icon.IsOk() ? icon : m_genericIcon, iconSize ) );
242 m_grid->SetCellValue( row, COLUMN_ACTION_NAME, action->name );
243 m_grid->SetCellValue( row, COLUMN_SETTINGS_IDENTIFIER, action->identifier );
244
245 // Toolbar button checkbox
246 m_grid->SetCellRenderer( row, COLUMN_VISIBLE, new wxGridCellBoolRenderer() );
247 m_grid->SetCellAlignment( row, COLUMN_VISIBLE, wxALIGN_CENTER, wxALIGN_CENTER );
248
250
251 m_grid->SetCellValue( row, COLUMN_VISIBLE, show ? wxT( "1" ) : wxEmptyString );
252
253 m_grid->SetCellValue( row, COLUMN_PLUGIN_NAME, action->plugin.Name() );
254 m_grid->SetCellValue( row, COLUMN_DESCRIPTION, action->description );
255#endif
256 }
257
258 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
259 {
260 const wxString& heading = m_grid->GetColLabelValue( col );
261 int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
262
263 // Set the minimal width to the column label size.
264 m_grid->SetColMinimalWidth( col, headingWidth );
265 // Set the width to see the full contents
266 m_grid->SetColSize( col, m_grid->GetVisibleWidth( col ) );
267 }
268
269 m_grid->AutoSizeRows();
270 m_grid->AutoSizeColumns();
272
273 m_grid->Thaw();
274
275 // Show errors button should be disabled if there are no errors.
276 wxString trace;
277
278 if( trace.empty() )
279 {
280 m_showErrorsButton->Disable();
281 m_showErrorsButton->Hide();
282 }
283 else
284 {
285 m_showErrorsButton->Enable();
286 m_showErrorsButton->Show();
287 }
288
289 return true;
290}
291
292
294{
295 wxString dir( PATHS::GetUserPluginsPath() );
296 LaunchExternal( dir );
297}
298
299
301{
302 wxString trace;
303
304 // Now display the filtered trace in our dialog
305 // (a simple wxMessageBox is really not suitable for long messages)
306 DIALOG_FOOTPRINT_WIZARD_LOG logWindow( wxGetTopLevelParent( this ) );
307 logWindow.m_Message->SetValue( trace );
308 logWindow.ShowModal();
309}
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 RecreatePluginEnvironment(const wxString &aIdentifier)
std::optional< const PLUGIN_ACTION * > GetAction(const wxString &aIdentifier)
void ReloadPlugins(std::optional< wxString > aDirectoryToScan=std::nullopt)
Clears the loaded plugins and actions and re-scans the filesystem to register new ones.
const wxString & Name() const
APPEARANCE m_Appearance
Class DIALOG_FOOTPRINT_WIZARD_LOG.
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:49
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:71
wxBitmapBundle icon_light
Definition api_plugin.h:83
const API_PLUGIN & plugin
Definition api_plugin.h:86
wxString name
Definition api_plugin.h:77
wxString description
Definition api_plugin.h:78
wxString identifier
Definition api_plugin.h:76
wxBitmapBundle icon_dark
Definition api_plugin.h:84