KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
5 * Copyright (C) 1992-2023 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
57 if( cfg && !cfg->m_Drawing.field_names.IsEmpty() )
59 }
60
61 m_addFieldButton->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
62 m_deleteFieldButton->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
63
64 m_checkboxColWidth = m_grid->GetColSize( 1 );
65
66 m_grid->SetUseNativeColLabels();
67
68 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
69 {
70 OnAddButtonClick( aEvent );
71 } ) );
72 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
73}
74
75
77{
78 // Delete the GRID_TRICKS.
79 m_grid->PopEventHandler( true );
80}
81
82
84{
86
87 return TransferDataToGrid();
88}
89
90
92{
94 return;
95
96 int row = m_grid->GetNumberRows();
98
99 TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
100 newFieldname.m_Visible = false;
101 m_fields.insert( m_fields.end(), newFieldname );
103
104 // wx documentation is wrong, SetGridCursor does not make visible.
105 m_grid->MakeCellVisible( row, 0 );
106 m_grid->SetGridCursor( row, 0 );
107}
108
109
111{
113 return;
114
115 wxArrayInt selectedRows = m_grid->GetSelectedRows();
116
117 if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
118 selectedRows.push_back( m_grid->GetGridCursorRow() );
119
120 if( selectedRows.empty() )
121 return;
122
123 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
124 selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
125
126 for( int row : selectedRows )
127 {
128 m_fields.erase( m_fields.begin() + row );
129 m_grid->DeleteRows( row );
130
131 m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
132 m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
133 }
134}
135
136
138{
139 m_grid->Freeze();
140
141 m_grid->ClearRows();
142 m_grid->AppendRows( m_fields.size() );
143
144 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
145 {
146 m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
147 // columns 1 and 2 show a boolean value (in a check box):
148 m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
149 m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
150
151 // Set cell properties
152 m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
153
154 // Render the Visible and URL columns as check boxes
155 m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
156 m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
157 m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
158
159 m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
160 m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
161 m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
162 }
163
164 m_grid->Thaw();
165
166 return true;
167}
168
169
171{
173 return false;
174
175 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
176 {
177 m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
178 m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
179 m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
180 }
181
182 return true;
183}
184
185
187{
188 if( !TransferDataFromGrid() )
189 return false;
190
192
193 for( TEMPLATE_FIELDNAME& field : m_fields )
194 {
195 if( !field.m_Name.IsEmpty() )
196 {
197 wxString trimmedName = field.m_Name;
198
199 trimmedName.Trim();
200 trimmedName.Trim( false );
201
202 // Check if the field name contains leading and/or trailing white space.
203 if( field.m_Name != trimmedName )
204 {
205 wxString msg;
206
207 msg.Printf( _( "The field name \"%s\" contains trailing and/or leading white"
208 "space." ), field.m_Name );
209
210 wxMessageDialog dlg( this, msg, _( "Warning" ),
211 wxOK | wxCANCEL | wxCENTER | wxICON_WARNING );
212
213 dlg.SetExtendedMessage( _( "This may result in what appears to be duplicate field "
214 "names but are actually unique names differing only by "
215 "white space characters. Removing the white space "
216 "characters will have no effect on existing symbol "
217 "field names." ) );
218
219 dlg.SetOKCancelLabels( wxMessageDialog::ButtonLabel( _( "Remove White Space" ) ),
220 wxMessageDialog::ButtonLabel( _( "Keep White Space" ) ) );
221
222 if( dlg.ShowModal() == wxID_OK )
223 field.m_Name = trimmedName;
224 }
225
227 }
228 }
229
230 if( m_global )
231 {
233
234 if( cfg )
235 {
236 // Save global fieldname templates
238 m_templateMgr->Format( &sf, 0, true );
239
240 wxString record = From_UTF8( sf.GetString().c_str() );
241 record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
242 record.Replace( wxT(" "), wxT(" "), true ); // double space to single
243
244 cfg->m_Drawing.field_names = record.ToStdString();
245 }
246 }
247
248 return true;
249}
250
251
253{
254 if( aWidth <= 0 )
255 return;
256
257 // Account for scroll bars
258 aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
259
260 m_grid->SetColSize( 0, std::max( 72, aWidth - 2 * m_checkboxColWidth ) );
261 m_grid->SetColSize( 1, m_checkboxColWidth );
262 m_grid->SetColSize( 2, m_checkboxColWidth );
263}
264
265
267{
268 AdjustGridColumns( event.GetSize().GetX() );
269
270 event.Skip();
271}
272
273
275{
276 m_fields = templateMgr->GetTemplateFieldNames( m_global );
278}
279
280
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
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.
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:142
T * GetAppSettings()
Returns a handle to the a given settings by type If the settings have already been loaded,...
void SetBitmap(const wxBitmapBundle &aBmp)
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:436
const std::string & GetString()
Definition: richio.h:459
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.
void DeleteAllFieldNameTemplates(bool aGlobal)
Delete the entire contents.
const TEMPLATE_FIELDNAMES & GetTemplateFieldNames()
Return a template field name list for read only access.
void Format(OUTPUTFORMATTER *out, int nestLevel, 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:184
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:637
#define _(s)
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
wxString From_UTF8(const char *cstring)
Hold a name of a symbol's field, field value, and default visibility.