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 
34  PAGED_DIALOG* aWindow ) :
35  PANEL_PCBNEW_ACTION_PLUGINS_BASE( aWindow->GetTreebook() ),
36  m_frame( aFrame )
37 {
39  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
40 
41  m_moveUpButton->SetBitmap( KiBitmap( small_up_xpm ) );
42  m_moveDownButton->SetBitmap( KiBitmap( small_down_xpm ) );
43  m_reloadButton->SetBitmap( KiBitmap( refresh_xpm ) );
44 }
45 
46 
48 {
49  m_grid->PopEventHandler( true );
50 }
51 
52 
54 {
55  SelectRow( event.GetRow() );
56 }
57 
58 
60 {
61  m_grid->ClearSelection();
62  m_grid->SelectRow( aRow );
63 }
64 
65 
67 {
68  auto selectedRows = m_grid->GetSelectedRows();
69 
70  // If nothing is selected or multiple rows are selected don't do anything.
71  if( selectedRows.size() != 1 ) return;
72 
73  int selectedRow = selectedRows[0];
74 
75  // If first row is selected, then it can't go any further up.
76  if( selectedRow == 0 )
77  {
78  wxBell();
79  return;
80  }
81 
82  SwapRows( selectedRow, selectedRow - 1 );
83 
84  SelectRow( selectedRow - 1 );
85 }
86 
87 
89 {
90  auto selectedRows = m_grid->GetSelectedRows();
91 
92  // If nothing is selected or multiple rows are selected don't do anything.
93  if( selectedRows.size() != 1 ) return;
94 
95  int selectedRow = selectedRows[0];
96 
97  // If last row is selected, then it can't go any further down.
98  if( selectedRow + 1 == m_grid->GetNumberRows() )
99  {
100  wxBell();
101  return;
102  }
103 
104  SwapRows( selectedRow, selectedRow + 1 );
105 
106  SelectRow( selectedRow + 1 );
107 }
108 
109 
110 void PANEL_PCBNEW_ACTION_PLUGINS::SwapRows( int aRowA, int aRowB )
111 {
112  m_grid->Freeze();
113 
114  // Swap all columns except icon
115  wxString tempStr;
116 
117  for( int column = 1; column < m_grid->GetNumberCols(); column++ )
118  {
119  tempStr = m_grid->GetCellValue( aRowA, column );
120  m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
121  m_grid->SetCellValue( aRowB, column, tempStr );
122  }
123 
124  // Swap icon column renderers
125  auto cellRenderer = m_grid->GetCellRenderer( aRowA, COLUMN_ICON );
126  m_grid->SetCellRenderer( aRowA, COLUMN_ICON, m_grid->GetCellRenderer( aRowB, COLUMN_ICON ) );
127  m_grid->SetCellRenderer( aRowB, COLUMN_ICON, cellRenderer );
128 
129  m_grid->Thaw();
130 }
131 
132 
134 {
137 }
138 
139 
141 {
142  PCBNEW_SETTINGS* settings = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
143  wxASSERT( settings );
144 
145  if( settings )
146  {
147  settings->m_VisibleActionPlugins.clear();
148 
149  for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
150  {
151  settings->m_VisibleActionPlugins.emplace_back( std::make_pair(
152  m_grid->GetCellValue( ii, COLUMN_PATH ),
153  m_grid->GetCellValue( ii, COLUMN_VISIBLE ) == wxT( "1" ) ) );
154  }
155  }
156 
157  return true;
158 }
159 
160 
162 {
163  m_grid->Freeze();
164 
165  if( m_grid->GetNumberRows() != 0 )
166  m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
167 
168  const auto& orderedPlugins = m_frame->GetOrderedActionPlugins();
169  m_grid->AppendRows( orderedPlugins.size() );
170 
171  for( size_t row = 0; row < orderedPlugins.size(); row++ )
172  {
173  ACTION_PLUGIN* ap = orderedPlugins[row];
174 
175  // Icon
176  m_grid->SetCellRenderer( row, COLUMN_ICON, new GRID_CELL_ICON_RENDERER(
177  ap->iconBitmap.IsOk() ? ap->iconBitmap : m_genericIcon ) );
178 
179  // Toolbar button checkbox
180  m_grid->SetCellRenderer( row, COLUMN_VISIBLE, new wxGridCellBoolRenderer() );
181  m_grid->SetCellAlignment( row, COLUMN_VISIBLE, wxALIGN_CENTER, wxALIGN_CENTER );
182 
183  bool showButton = m_frame->GetActionPluginButtonVisible( ap->GetPluginPath(),
184  ap->GetShowToolbarButton() );
185 
186  m_grid->SetCellValue( row, COLUMN_VISIBLE, showButton ? wxT( "1" ) : wxEmptyString );
187 
188  m_grid->SetCellValue( row, COLUMN_NAME, ap->GetName() );
189  m_grid->SetCellValue( row, COLUMN_CATEGORY, ap->GetCategoryName() );
190  m_grid->SetCellValue( row, COLUMN_DESCRIPTION, ap->GetDescription() );
191  m_grid->SetCellValue( row, COLUMN_PATH, ap->GetPluginPath() );
192  }
193 
194  for( int col = 0; col < m_grid->GetNumberCols(); col++ )
195  {
196  // Set the minimal width to the column label size.
197  m_grid->SetColMinimalWidth( col, m_grid->GetVisibleWidth( col, true, false, false ) );
198  // Set the width to see the full contents
199  m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true, false ) );
200  }
201 
202  m_grid->AutoSizeRows();
203 
204  m_grid->Thaw();
205 
206  return true;
207 }
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:51
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:230
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:80
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:25
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:26
const BITMAP_OPAQUE refresh_xpm[1]
Definition: refresh.cpp:32
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 Do n...
void OnGridCellClick(wxGridEvent &event) override
Selects a whole row.