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>
25 #include <widgets/paged_dialog.h>
27 #include <bitmaps.h>
28 #include <action_plugin.h>
29 #include <grid_tricks.h>
30 #include <widgets/wx_grid.h>
31 
32 #define GRID_CELL_MARGIN 4
33 
35  PAGED_DIALOG* aWindow ) :
36  PANEL_PCBNEW_ACTION_PLUGINS_BASE( aWindow->GetTreebook() ),
37  m_frame( aFrame )
38 {
40  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
41 
42  m_moveUpButton->SetBitmap( KiBitmap( small_up_xpm ) );
43  m_moveDownButton->SetBitmap( KiBitmap( small_down_xpm ) );
44  m_reloadButton->SetBitmap( KiBitmap( refresh_xpm ) );
45 }
46 
47 
49 {
50  m_grid->PopEventHandler( true );
51 }
52 
53 
55 {
56  SelectRow( event.GetRow() );
57 }
58 
59 
61 {
62  m_grid->ClearSelection();
63  m_grid->SelectRow( aRow );
64 }
65 
66 
68 {
69  auto selectedRows = m_grid->GetSelectedRows();
70 
71  // If nothing is selected or multiple rows are selected don't do anything.
72  if( selectedRows.size() != 1 ) return;
73 
74  int selectedRow = selectedRows[0];
75 
76  // If first row is selected, then it can't go any further up.
77  if( selectedRow == 0 )
78  {
79  wxBell();
80  return;
81  }
82 
83  SwapRows( selectedRow, selectedRow - 1 );
84 
85  SelectRow( selectedRow - 1 );
86 }
87 
88 
90 {
91  auto selectedRows = m_grid->GetSelectedRows();
92 
93  // If nothing is selected or multiple rows are selected don't do anything.
94  if( selectedRows.size() != 1 ) return;
95 
96  int selectedRow = selectedRows[0];
97 
98  // If last row is selected, then it can't go any further down.
99  if( selectedRow + 1 == m_grid->GetNumberRows() )
100  {
101  wxBell();
102  return;
103  }
104 
105  SwapRows( selectedRow, selectedRow + 1 );
106 
107  SelectRow( selectedRow + 1 );
108 }
109 
110 
111 void PANEL_PCBNEW_ACTION_PLUGINS::SwapRows( int aRowA, int aRowB )
112 {
113  m_grid->Freeze();
114 
115  // Swap all columns except icon
116  wxString tempStr;
117 
118  for( int column = 1; column < m_grid->GetNumberCols(); column++ )
119  {
120  tempStr = m_grid->GetCellValue( aRowA, column );
121  m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
122  m_grid->SetCellValue( aRowB, column, tempStr );
123  }
124 
125  // Swap icon column renderers
126  auto cellRenderer = m_grid->GetCellRenderer( aRowA, COLUMN_ICON );
127  m_grid->SetCellRenderer( aRowA, COLUMN_ICON, m_grid->GetCellRenderer( aRowB, COLUMN_ICON ) );
128  m_grid->SetCellRenderer( aRowB, COLUMN_ICON, cellRenderer );
129 
130  m_grid->Thaw();
131 }
132 
133 
135 {
138 }
139 
140 
142 {
143  PCBNEW_SETTINGS* settings = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
144  wxASSERT( settings );
145 
146  if( settings )
147  {
148  settings->m_VisibleActionPlugins.clear();
149 
150  for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
151  {
152  settings->m_VisibleActionPlugins.emplace_back( std::make_pair(
153  m_grid->GetCellValue( ii, COLUMN_PATH ),
154  m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT( "1" ) ) );
155  }
156  }
157 
158  return true;
159 }
160 
161 
163 {
164  m_grid->Freeze();
165 
166  if( m_grid->GetNumberRows() != 0 )
167  m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
168 
169  const auto& orderedPlugins = m_frame->GetOrderedActionPlugins();
170  m_grid->AppendRows( orderedPlugins.size() );
171 
172  for( size_t row = 0; row < orderedPlugins.size(); row++ )
173  {
174  ACTION_PLUGIN* ap = orderedPlugins[row];
175 
176  // Icon
177  m_grid->SetCellRenderer( row, COLUMN_ICON, new GRID_CELL_ICON_RENDERER(
178  ap->iconBitmap.IsOk() ? ap->iconBitmap : m_genericIcon ) );
179 
180  // Toolbar button checkbox
181  m_grid->SetCellRenderer( row, COLUMN_VISIBLE, new wxGridCellBoolRenderer() );
182  m_grid->SetCellAlignment( row, COLUMN_VISIBLE, wxALIGN_CENTER, wxALIGN_CENTER );
183 
184  bool showButton = m_frame->GetActionPluginButtonVisible( ap->GetPluginPath(),
185  ap->GetShowToolbarButton() );
186 
187  m_grid->SetCellValue( row, COLUMN_VISIBLE, showButton ? wxT( "1" ) : wxEmptyString );
188 
189  m_grid->SetCellValue( row, COLUMN_NAME, ap->GetName() );
190  m_grid->SetCellValue( row, COLUMN_CATEGORY, ap->GetCategoryName() );
191  m_grid->SetCellValue( row, COLUMN_DESCRIPTION, ap->GetDescription() );
192  m_grid->SetCellValue( row, COLUMN_PATH, ap->GetPluginPath() );
193  }
194 
195  for( int col = 0; col < m_grid->GetNumberCols(); col++ )
196  {
197  const wxString& heading = m_grid->GetColLabelValue( col );
198  int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
199 
200  // Set the minimal width to the column label size.
201  m_grid->SetColMinimalWidth( col, headingWidth );
202  // Set the width to see the full contents
203  m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true, false ) );
204  }
205 
206  m_grid->AutoSizeRows();
207 
208  m_grid->Thaw();
209 
210  return true;
211 }
virtual wxString GetName()=0
Function GetName.
ACTION_PLUGIN This is the parent class from where any action plugin class must derive.
Definition: action_plugin.h:40
virtual bool GetShowToolbarButton()=0
Function GetShowToolbarButton.
virtual wxString GetDescription()=0
Function GetDescription.
wxBitmap iconBitmap
Definition: action_plugin.h:50
GRID_TRICKS is used to add mouse and command handling (such as cut, copy, and paste) to a WX_GRID ins...
Definition: grid_tricks.h:52
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:236
#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 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:24
PANEL_PCBNEW_ACTION_PLUGINS(PCB_EDIT_FRAME *aFrame, PAGED_DIALOG *aWindow)
const BITMAP_OPAQUE hammer_xpm[1]
Definition: hammer.cpp:74
void OnMoveDownButtonClick(wxCommandEvent &event) override
Moves plugin down in the grid.
const BITMAP_OPAQUE small_up_xpm[1]
Definition: small_up.cpp:25
const BITMAP_OPAQUE refresh_xpm[1]
Definition: refresh.cpp:28
virtual wxString GetCategoryName()=0
Function GetCategoryName.
Class PCBNEW_ACTION_PLUGINS.
PCB_EDIT_FRAME is 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.