KiCad PCB EDA Suite
dialog_fp_plugin_options.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) 2013 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright (C) 2013 CERN
6 * Copyright (C) 2013-2022 KiCad Developers, see change_log.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26
27#include <invoke_pcb_dialog.h>
29#include <fp_lib_table.h>
30#include <grid_tricks.h>
31#include <widgets/wx_grid.h>
33#include <bitmaps.h>
34#include <macros.h>
35
36
37#define INITIAL_HELP \
38 _( "Select an <b>Option Choice</b> in the listbox above, and then click the <b>Append Selected Option</b> button." )
39
40
41using std::string;
42
43
51{
52
53public:
54 DIALOG_FP_PLUGIN_OPTIONS( wxWindow* aParent, const wxString& aNickname,
55 const wxString& aPluginType, const wxString& aOptions,
56 wxString* aResult ) :
58 m_callers_options( aOptions ),
59 m_result( aResult ),
62 {
63 SetTitle( wxString::Format( _( "Options for Library '%s'" ), aNickname ) );
64
65 // Give a bit more room for combobox editors
66 m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 );
67
68 m_grid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
69
70 // add Cut, Copy, and Paste to wxGrid
71 m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
72
73 // Option Choices Panel:
74
75 IO_MGR::PCB_FILE_T pi_type = IO_MGR::EnumFromStr( aPluginType );
76 PLUGIN::RELEASER pi( IO_MGR::PluginFind( pi_type ) );
77
79
80 if( m_choices.size() )
81 {
82 unsigned int row = 0;
83
84 for( STRING_UTF8_MAP::const_iterator it = m_choices.begin(); it != m_choices.end();
85 ++it, ++row )
86 {
87 wxString item = FROM_UTF8( it->first.c_str() );
88
89 m_listbox->InsertItems( 1, &item, row );
90 }
91 }
92
94
95 // Configure button logos
98
99 // initial focus on the grid please.
101
103 }
104
106 {
107 // destroy GRID_TRICKS before m_grid.
108 m_grid->PopEventHandler( true );
109 }
110
111 bool TransferDataToWindow() override
112 {
113 if( !DIALOG_SHIM::TransferDataToWindow() )
114 return false;
115
116 // Fill the grid with existing aOptions
117 string options = TO_UTF8( m_callers_options );
118
119 STRING_UTF8_MAP* props = LIB_TABLE::ParseOptions( options );
120
121 if( props )
122 {
123 if( (int) props->size() > m_grid->GetNumberRows() )
124 m_grid->AppendRows( props->size() - m_grid->GetNumberRows() );
125
126 int row = 0;
127
128 for( STRING_UTF8_MAP::const_iterator it = props->begin(); it != props->end();
129 ++it, ++row )
130 {
131 m_grid->SetCellValue( row, 0, FROM_UTF8( it->first.c_str() ) );
132 m_grid->SetCellValue( row, 1, it->second );
133 }
134
135 delete props;
136 }
137
138 return true;
139 }
140
142 {
144 return false;
145
146 if( !DIALOG_SHIM::TransferDataFromWindow() )
147 return false;
148
149 STRING_UTF8_MAP props;
150 const int rowCount = m_grid->GetNumberRows();
151
152 for( int row = 0; row<rowCount; ++row )
153 {
154 string name = TO_UTF8( m_grid->GetCellValue( row, 0 ).Trim( false ).Trim() );
155 UTF8 value = m_grid->GetCellValue( row, 1 ).Trim( false ).Trim();
156
157 if( name.size() )
158 {
159 props[name] = value;
160 }
161 }
162
164 return true;
165 }
166
167private:
168 const wxString& m_callers_options;
169 wxString* m_result;
173
175 {
176 int row = m_grid->GetNumberRows();
177
178 m_grid->AppendRows( 1 );
179
180 // wx documentation is wrong, SetGridCursor does not make visible.
181 m_grid->MakeCellVisible( row, 0 );
182 m_grid->SetGridCursor( row, 0 );
183
184 return row;
185 }
186
188 {
189 int selected_row = m_listbox->GetSelection();
190 if( selected_row != wxNOT_FOUND )
191 {
192 wxString option = m_listbox->GetString( selected_row );
193
194 int row_count = m_grid->GetNumberRows();
195 int row;
196
197 for( row=0; row<row_count; ++row )
198 {
199 wxString col0 = m_grid->GetCellValue( row, 0 );
200
201 if( !col0 ) // empty col0
202 break;
203 }
204
205 if( row == row_count )
206 row = appendRow();
207
208 m_grid->SetCellValue( row, 0, option );
209 m_grid_widths_dirty = true;
210 }
211 }
212
213 //-----<event handlers>------------------------------------------------------
214
215 void onListBoxItemSelected( wxCommandEvent& event ) override
216 {
217 // change the help text based on the m_listbox selection:
218 if( event.IsSelection() )
219 {
220 string option = TO_UTF8( event.GetString() );
221 UTF8 help_text;
222
223 if( m_choices.Value( option.c_str(), &help_text ) )
224 m_html->SetPage( help_text );
225 else
227 }
228 }
229
230 void onListBoxItemDoubleClicked( wxCommandEvent& event ) override
231 {
232 appendOption();
233 }
234
235 void onAppendOption( wxCommandEvent& ) override
236 {
238 return;
239
240 appendOption();
241 }
242
243 void onAppendRow( wxCommandEvent& ) override
244 {
246 return;
247
248 appendRow();
249 }
250
251 void onDeleteRow( wxCommandEvent& ) override
252 {
254 return;
255
256 int curRow = m_grid->GetGridCursorRow();
257
258 m_grid->DeleteRows( curRow );
259 m_grid_widths_dirty = true;
260
261 curRow = std::max( 0, curRow - 1 );
262 m_grid->MakeCellVisible( curRow, m_grid->GetGridCursorCol() );
263 m_grid->SetGridCursor( curRow, m_grid->GetGridCursorCol() );
264 }
265
266 void onGridCellChange( wxGridEvent& aEvent ) override
267 {
268 m_grid_widths_dirty = true;
269
270 aEvent.Skip();
271 }
272
273 void onUpdateUI( wxUpdateUIEvent& ) override
274 {
275 if( m_grid_widths_dirty && !m_grid->IsCellEditControlShown() )
276 {
277 int width = m_grid->GetClientRect().GetWidth();
278
279 m_grid->AutoSizeColumn( 0 );
280 m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) );
281
282 m_grid->SetColSize( 1, std::max( 120, width - m_grid->GetColSize( 0 ) ) );
283
284 m_grid_widths_dirty = false;
285 }
286 }
287
288 void onSize( wxSizeEvent& aEvent ) override
289 {
290 m_grid_widths_dirty = true;
291
292 aEvent.Skip();
293 }
294};
295
296
297void InvokePluginOptionsEditor( wxWindow* aCaller, const wxString& aNickname,
298 const wxString& aPluginType, const wxString& aOptions,
299 wxString* aResult )
300{
301 DIALOG_FP_PLUGIN_OPTIONS dlg( aCaller, aNickname, aPluginType, aOptions, aResult );
302
303 dlg.ShowModal();
304}
const char * name
Definition: DXF_plotter.cpp:56
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
Class DIALOG_FP_PLUGIN_OPTIONS_BASE.
DIALOG_FP_PLUGIN_OPTIONS is an options editor in the form of a two column name/value spreadsheet like...
void onListBoxItemSelected(wxCommandEvent &event) override
void onAppendRow(wxCommandEvent &) override
void onListBoxItemDoubleClicked(wxCommandEvent &event) override
void onSize(wxSizeEvent &aEvent) override
void onUpdateUI(wxUpdateUIEvent &) override
void onGridCellChange(wxGridEvent &aEvent) override
DIALOG_FP_PLUGIN_OPTIONS(wxWindow *aParent, const wxString &aNickname, const wxString &aPluginType, const wxString &aOptions, wxString *aResult)
void onDeleteRow(wxCommandEvent &) override
void onAppendOption(wxCommandEvent &) override
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:97
void SetupStandardButtons(std::map< int, wxString > aLabels={})
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
bool SetPage(const wxString &aSource) override
Definition: html_window.cpp:38
PCB_FILE_T
The set of file types that the IO_MGR knows about, and for which there has been a plugin written.
Definition: io_mgr.h:54
static PCB_FILE_T EnumFromStr(const wxString &aFileType)
Return the PCB_FILE_T from the corresponding plugin type name: "kicad", "legacy", etc.
Definition: io_mgr.cpp:93
static PLUGIN * PluginFind(PCB_FILE_T aFileType)
Return a PLUGIN which the caller can use to import, export, save, or load design documents.
Definition: io_mgr.cpp:58
static STRING_UTF8_MAP * ParseOptions(const std::string &aOptionsList)
Parses aOptionsList and places the result into a #PROPERTIES object which is returned.
static UTF8 FormatOptions(const STRING_UTF8_MAP *aProperties)
Returns a list of options from the aProperties parameter.
Releases a PLUGIN in the context of a potential thrown exception through its destructor.
Definition: io_mgr.h:564
virtual void FootprintLibOptions(STRING_UTF8_MAP *aListToAppendTo) const
Append supported PLUGIN options to aListToAppenTo along with internationalized descriptions.
Definition: plugin.cpp:145
void SetBitmap(const wxBitmap &aBmp)
A name/value tuple with unique names and optional values.
bool Value(const char *aName, UTF8 *aFetchedValue=nullptr) const
Fetch a property by aName and returns true if that property was found, else false.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:71
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:423
#define INITIAL_HELP
void InvokePluginOptionsEditor(wxWindow *aCaller, const wxString &aNickname, const wxString &aPluginType, const wxString &aOptions, wxString *aResult)
Function InvokePluginOptionsEditor calls DIALOG_FP_PLUGIN_OPTIONS dialog so that plugin options set c...
#define _(s)
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200