KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
panel_template_fieldnames.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) 2009 Wayne Stambaugh <stambaughw@verizon.net>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
26
27#include <wx/msgdlg.h>
28
29#include <pgm_base.h>
31#include <eeschema_settings.h>
33#include <template_fieldnames.h>
34#include <grid_tricks.h>
35#include <bitmaps.h>
36#include <richio.h>
37#include <string_utils.h>
38
40 TEMPLATES* aProjectTemplateMgr ) :
42{
43 if( aProjectTemplateMgr )
44 {
45 m_title->SetLabel( _( "Project Field Name Templates" ) );
46 m_global = false;
47 m_templateMgr = aProjectTemplateMgr;
48 }
49 else
50 {
51 m_title->SetLabel( _( "Global Field Name Templates" ) );
52 m_global = true;
54
56 EESCHEMA_SETTINGS* cfg = mgr.GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" );
57
58 if( cfg && !cfg->m_Drawing.field_names.IsEmpty() )
60 }
61
62 m_addFieldButton->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
63 m_deleteFieldButton->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
64 m_bpMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
65 m_bpMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
66
67 m_checkboxColWidth = m_grid->GetColSize( 1 );
68
69 m_grid->SetUseNativeColLabels();
70
71 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
72 {
73 OnAddButtonClick( aEvent );
74 } ) );
75 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
76}
77
78
80{
81 // Delete the GRID_TRICKS.
82 m_grid->PopEventHandler( true );
83}
84
85
87{
89
90 return TransferDataToGrid();
91}
92
93
95{
97 return;
98
99 int row = m_grid->GetNumberRows();
101
102 TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
103 newFieldname.m_Visible = false;
104 m_fields.insert( m_fields.end(), newFieldname );
106
107 // wx documentation is wrong, SetGridCursor does not make visible.
108 m_grid->MakeCellVisible( row, 0 );
109 m_grid->SetGridCursor( row, 0 );
110
111 m_grid->EnableCellEditControl( true );
112 m_grid->ShowCellEditControl();
113}
114
115
117{
119 return;
120
121 wxArrayInt selectedRows = m_grid->GetSelectedRows();
122
123 if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
124 selectedRows.push_back( m_grid->GetGridCursorRow() );
125
126 if( selectedRows.empty() )
127 return;
128
129 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
130 selectedRows.Sort(
131 []( int* first, int* second )
132 {
133 return *second - *first;
134 } );
135
136 for( int row : selectedRows )
137 {
138 m_fields.erase( m_fields.begin() + row );
139 m_grid->DeleteRows( row );
140
141 m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
142 m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
143 }
144}
145
146
147void swapRows( WX_GRID* aGrid, int aRowA, int aRowB )
148{
149 for( int col = 0; col < aGrid->GetNumberCols(); ++col )
150 {
151 wxString temp = aGrid->GetCellValue( aRowA, col );
152 aGrid->SetCellValue( aRowA, col, aGrid->GetCellValue( aRowB, col ) );
153 aGrid->SetCellValue( aRowB, col, temp );
154 }
155}
156
157
158void PANEL_TEMPLATE_FIELDNAMES::OnMoveUp( wxCommandEvent& event )
159{
161 return;
162
163 int i = m_grid->GetGridCursorRow();
164
165 if( i > 0 )
166 {
167 swapRows( m_grid, i, i - 1 );
168
169 m_grid->SetGridCursor( i - 1, m_grid->GetGridCursorCol() );
170 m_grid->MakeCellVisible( m_grid->GetGridCursorRow(), m_grid->GetGridCursorCol() );
171 }
172 else
173 {
174 wxBell();
175 }
176}
177
178
179void PANEL_TEMPLATE_FIELDNAMES::OnMoveDown( wxCommandEvent& event )
180{
182 return;
183
184 int i = m_grid->GetGridCursorRow();
185
186 if( i >= 0 && i + 1 < m_grid->GetNumberRows() )
187 {
188 swapRows( m_grid, i, i + 1 );
189
190 m_grid->SetGridCursor( i + 1, m_grid->GetGridCursorCol() );
191 m_grid->MakeCellVisible( m_grid->GetGridCursorRow(), m_grid->GetGridCursorCol() );
192 }
193 else
194 {
195 wxBell();
196 }
197}
198
199
201{
202 m_grid->Freeze();
203
204 m_grid->ClearRows();
205 m_grid->AppendRows( m_fields.size() );
206
207 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
208 {
209 m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
210
211 // columns 1 and 2 show a boolean value (in a check box):
212 m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
213 m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
214
215 // Set cell properties
216 m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
217
218 // Render the Visible and URL columns as check boxes
219 m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
220 m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
221 m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
222
223 m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
224 m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
225 m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
226 }
227
228 m_grid->Thaw();
229
230 return true;
231}
232
233
235{
237 return false;
238
239 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
240 {
241 m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
242 m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
243 m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
244 }
245
246 return true;
247}
248
249
251{
252 if( !TransferDataFromGrid() )
253 return false;
254
256
257 for( TEMPLATE_FIELDNAME& field : m_fields )
258 {
259 if( !field.m_Name.IsEmpty() )
260 {
261 wxString trimmedName = field.m_Name;
262
263 trimmedName.Trim();
264 trimmedName.Trim( false );
265
266 // Check if the field name contains leading and/or trailing white space.
267 if( field.m_Name != trimmedName )
268 {
269 wxString msg;
270
271 msg.Printf( _( "The field name '%s' contains trailing and/or leading white space." ),
272 field.m_Name );
273
274 wxMessageDialog dlg( this, msg, _( "Warning" ), wxOK|wxCANCEL|wxCENTER|wxICON_WARNING );
275
276 dlg.SetExtendedMessage( _( "This may result in what appears to be duplicate field "
277 "names but are actually unique names differing only by "
278 "white space characters. Removing the white space "
279 "characters will have no effect on existing symbol "
280 "field names." ) );
281
282 dlg.SetOKCancelLabels( wxMessageDialog::ButtonLabel( _( "Remove White Space" ) ),
283 wxMessageDialog::ButtonLabel( _( "Keep White Space" ) ) );
284
285 if( dlg.ShowModal() == wxID_OK )
286 field.m_Name = trimmedName;
287 }
288
290 }
291 }
292
293 if( m_global )
294 {
296 EESCHEMA_SETTINGS* cfg = mgr.GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" );
297
298 if( cfg )
299 {
300 // Save global fieldname templates
302 m_templateMgr->Format( &sf, true );
303
304 wxString record = From_UTF8( sf.GetString().c_str() );
305 record.Replace( wxT( " " ), wxT( " " ), true ); // double space to single
306
307 cfg->m_Drawing.field_names = record.ToStdString();
308 }
309 }
310
311 return true;
312}
313
314
316{
317 if( aWidth <= 0 )
318 return;
319
320 // Account for scroll bars
321 aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
322
323 m_grid->SetColSize( 0, std::max( 72, aWidth - 2 * m_checkboxColWidth ) );
324 m_grid->SetColSize( 1, m_checkboxColWidth );
325 m_grid->SetColSize( 2, m_checkboxColWidth );
326}
327
328
330{
331 AdjustGridColumns( event.GetSize().GetX() );
332
333 event.Skip();
334}
335
336
338{
339 m_fields = templateMgr->GetTemplateFieldNames( m_global );
341}
342
343
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
Class PANEL_TEMPLATE_FIELDNAMES_BASE.
void OnAddButtonClick(wxCommandEvent &event) override
Adds a new template fieldname (with default values) to the template fieldnames data.
std::vector< TEMPLATE_FIELDNAME > m_fields
void OnMoveDown(wxCommandEvent &event) override
void OnMoveUp(wxCommandEvent &event) override
void ImportSettingsFrom(TEMPLATES *templateMgr)
PANEL_TEMPLATE_FIELDNAMES(wxWindow *aWindow, TEMPLATES *aProjectTemplateMgr)
void OnSizeGrid(wxSizeEvent &event) override
void OnDeleteButtonClick(wxCommandEvent &event) override
Deletes the selected template fieldname from the template fieldnames data.
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:125
T * GetAppSettings(const char *aFilename)
Return a handle to the a given settings by type.
void SetBitmap(const wxBitmapBundle &aBmp)
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:449
const std::string & GetString()
Definition: richio.h:472
void AddTemplateFieldName(const TEMPLATE_FIELDNAME &aFieldName, bool aGlobal)
Insert or append a wanted symbol field name into the field names template.
void AddTemplateFieldNames(const wxString &aSerializedFieldNames)
Add a serialized list of template field names.
const std::vector< TEMPLATE_FIELDNAME > & GetTemplateFieldNames()
Return a template field name list for read only access.
void DeleteAllFieldNameTemplates(bool aGlobal)
Delete the entire contents.
void Format(OUTPUTFORMATTER *out, bool aGlobal) const
Serialize this object out as text into the given OUTPUTFORMATTER.
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:193
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:644
#define _(s)
void swapRows(WX_GRID *aGrid, int aRowA, int aRowB)
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1071
see class PGM_BASE
wxString From_UTF8(const char *cstring)
Hold a name of a symbol's field, field value, and default visibility.