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-2019 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 <kiface_i.h>
22 #include <pcb_edit_frame.h>
23 #include <pcbnew_settings.h>
26 #include <widgets/paged_dialog.h>
28 #include <bitmaps.h>
29 #include <action_plugin.h>
30 #include <grid_tricks.h>
31 #include <widgets/wx_grid.h>
32 
33 #if defined( KICAD_SCRIPTING ) || defined( KICAD_SCRIPTING_WXPYTHON )
34 #include <python_scripting.h>
35 #else
36 // Dummy functions, actually defined in python_scripting.h when KICAD_SCRIPTING is enabled
37 static void pcbnewGetWizardsBackTrace( wxString& aText ) {}
38 #endif
39 
40 #define GRID_CELL_MARGIN 4
41 
43  PAGED_DIALOG* aWindow ) :
44  PANEL_PCBNEW_ACTION_PLUGINS_BASE( aWindow->GetTreebook() ),
45  m_frame( aFrame )
46 {
48  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
49 
50  m_moveUpButton->SetBitmap( KiBitmap( small_up_xpm ) );
51  m_moveDownButton->SetBitmap( KiBitmap( small_down_xpm ) );
53  m_reloadButton->SetBitmap( KiBitmap( small_refresh_xpm ) );
55 }
56 
57 
59 {
60  m_grid->PopEventHandler( true );
61 }
62 
63 
65 {
66  SelectRow( event.GetRow() );
67 }
68 
69 
71 {
72  m_grid->ClearSelection();
73  m_grid->SelectRow( aRow );
74 }
75 
76 
78 {
79  auto selectedRows = m_grid->GetSelectedRows();
80 
81  // If nothing is selected or multiple rows are selected don't do anything.
82  if( selectedRows.size() != 1 ) return;
83 
84  int selectedRow = selectedRows[0];
85 
86  // If first row is selected, then it can't go any further up.
87  if( selectedRow == 0 )
88  {
89  wxBell();
90  return;
91  }
92 
93  SwapRows( selectedRow, selectedRow - 1 );
94 
95  SelectRow( selectedRow - 1 );
96 }
97 
98 
100 {
101  auto selectedRows = m_grid->GetSelectedRows();
102 
103  // If nothing is selected or multiple rows are selected don't do anything.
104  if( selectedRows.size() != 1 ) return;
105 
106  int selectedRow = selectedRows[0];
107 
108  // If last row is selected, then it can't go any further down.
109  if( selectedRow + 1 == m_grid->GetNumberRows() )
110  {
111  wxBell();
112  return;
113  }
114 
115  SwapRows( selectedRow, selectedRow + 1 );
116 
117  SelectRow( selectedRow + 1 );
118 }
119 
120 
121 void PANEL_PCBNEW_ACTION_PLUGINS::SwapRows( int aRowA, int aRowB )
122 {
123  m_grid->Freeze();
124 
125  // Swap all columns except icon
126  wxString tempStr;
127 
128  for( int column = 1; column < m_grid->GetNumberCols(); column++ )
129  {
130  tempStr = m_grid->GetCellValue( aRowA, column );
131  m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
132  m_grid->SetCellValue( aRowB, column, tempStr );
133  }
134 
135  // Swap icon column renderers
136  auto cellRenderer = m_grid->GetCellRenderer( aRowA, COLUMN_ICON );
137  m_grid->SetCellRenderer( aRowA, COLUMN_ICON, m_grid->GetCellRenderer( aRowB, COLUMN_ICON ) );
138  m_grid->SetCellRenderer( aRowB, COLUMN_ICON, cellRenderer );
139 
140  m_grid->Thaw();
141 }
142 
143 
145 {
148 }
149 
150 
152 {
153  PCBNEW_SETTINGS* settings = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
154  wxASSERT( settings );
155 
156  if( settings )
157  {
158  settings->m_VisibleActionPlugins.clear();
159 
160  for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
161  {
162  settings->m_VisibleActionPlugins.emplace_back( std::make_pair(
163  m_grid->GetCellValue( ii, COLUMN_PATH ),
164  m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT( "1" ) ) );
165  }
166  }
167 
168  return true;
169 }
170 
171 
173 {
174  m_grid->Freeze();
175 
176  if( m_grid->GetNumberRows() != 0 )
177  m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
178 
179  const auto& orderedPlugins = m_frame->GetOrderedActionPlugins();
180  m_grid->AppendRows( orderedPlugins.size() );
181 
182  for( size_t row = 0; row < orderedPlugins.size(); row++ )
183  {
184  ACTION_PLUGIN* ap = orderedPlugins[row];
185 
186  // Icon
187  m_grid->SetCellRenderer( row, COLUMN_ICON, new GRID_CELL_ICON_RENDERER(
188  ap->iconBitmap.IsOk() ? ap->iconBitmap : m_genericIcon ) );
189 
190  // Toolbar button checkbox
191  m_grid->SetCellRenderer( row, COLUMN_VISIBLE, new wxGridCellBoolRenderer() );
192  m_grid->SetCellAlignment( row, COLUMN_VISIBLE, wxALIGN_CENTER, wxALIGN_CENTER );
193 
194  bool showButton = m_frame->GetActionPluginButtonVisible( ap->GetPluginPath(),
195  ap->GetShowToolbarButton() );
196 
197  m_grid->SetCellValue( row, COLUMN_VISIBLE, showButton ? wxT( "1" ) : wxEmptyString );
198 
199  m_grid->SetCellValue( row, COLUMN_NAME, ap->GetName() );
200  m_grid->SetCellValue( row, COLUMN_CATEGORY, ap->GetCategoryName() );
201  m_grid->SetCellValue( row, COLUMN_DESCRIPTION, ap->GetDescription() );
202  m_grid->SetCellValue( row, COLUMN_PATH, ap->GetPluginPath() );
203  }
204 
205  for( int col = 0; col < m_grid->GetNumberCols(); col++ )
206  {
207  const wxString& heading = m_grid->GetColLabelValue( col );
208  int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
209 
210  // Set the minimal width to the column label size.
211  m_grid->SetColMinimalWidth( col, headingWidth );
212  // Set the width to see the full contents
213  m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true, false ) );
214  }
215 
216  m_grid->AutoSizeRows();
217 
218  m_grid->Thaw();
219 
220  // Show errors button should be disabled if there are no errors.
221  wxString trace;
222  pcbnewGetWizardsBackTrace( trace );
223  if( trace.empty() )
224  {
225  m_showErrorsButton->Disable();
226  m_showErrorsButton->Hide();
227  }
228  else
229  {
230  m_showErrorsButton->Enable();
231  m_showErrorsButton->Show();
232  }
233 
234  return true;
235 }
236 
238 {
240 }
241 
243 {
244  wxString trace;
245  pcbnewGetWizardsBackTrace( trace );
246 
247  // Now display the filtered trace in our dialog
248  // (a simple wxMessageBox is really not suitable for long messages)
249  DIALOG_FOOTPRINT_WIZARD_LOG logWindow( this );
250  logWindow.m_Message->SetValue( trace );
251  logWindow.ShowModal();
252 }
virtual wxString GetName()=0
Function GetName.
const BITMAP_OPAQUE small_refresh_xpm[1]
ACTION_PLUGIN This is the parent class from where any action plugin class must derive.
Definition: action_plugin.h:40
void OnShowErrorsButtonClick(wxCommandEvent &event) override
Shows plugin import errors.
static void pcbnewGetWizardsBackTrace(wxString &aText)
const BITMAP_OPAQUE small_folder_xpm[1]
virtual bool GetShowToolbarButton()=0
Function GetShowToolbarButton.
virtual wxString GetDescription()=0
Function GetDescription.
wxBitmap iconBitmap
Definition: action_plugin.h:50
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:52
const BITMAP_OPAQUE puzzle_piece_xpm[1]
Class DIALOG_FOOTPRINT_WIZARD_LOG.
const BITMAP_OPAQUE small_warning_xpm[1]
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:238
#define GRID_CELL_MARGIN
virtual wxString GetPluginPath()=0
Function GetPluginPath.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:82
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.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
const BITMAP_OPAQUE small_down_xpm[1]
Definition: small_down.cpp:23
PANEL_PCBNEW_ACTION_PLUGINS(PCB_EDIT_FRAME *aFrame, PAGED_DIALOG *aWindow)
void OnMoveDownButtonClick(wxCommandEvent &event) override
Moves plugin down in the grid.
void PythonPluginsShowFolder()
Open the plugins folder in the default system file browser.
const BITMAP_OPAQUE small_up_xpm[1]
Definition: small_up.cpp:23
virtual wxString GetCategoryName()=0
Function GetCategoryName.
Class PCBNEW_ACTION_PLUGINS.
The main frame for Pcbnew.
Class PANEL_PCBNEW_ACTION_PLUGINS_BASE.
void PythonPluginsReload()
Reload the Python plugins if they are newer than the already loaded, and load new plugins if any.
void OnGridCellClick(wxGridEvent &event) override
Selects a whole row.