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 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, see <https://www.gnu.org/licenses/>.
19 */
20
22
23#include <wx/msgdlg.h>
24
25#include <pgm_base.h>
28#include <template_fieldnames.h>
29#include <grid_tricks.h>
30#include <bitmaps.h>
31#include <confirm.h>
32
33#include <algorithm>
34#include <set>
35
37 TEMPLATES* aProjectTemplateMgr ) :
39{
40 if( aProjectTemplateMgr )
41 {
42 m_title->SetLabel( _( "Project Field Name Templates" ) );
43 m_scope = TEMPLATES::SCOPE::PROJECT;
44 m_templateMgr = aProjectTemplateMgr;
45 }
46 else
47 {
48 m_title->SetLabel( _( "Global Field Name Templates" ) );
49 m_scope = TEMPLATES::SCOPE::GLOBAL;
51 }
52
57
58 m_grid->SetUseNativeColLabels();
59
60 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
61 {
62 OnAddButtonClick( aEvent );
63 } ) );
64 m_grid->SetupColumnAutosizer( 0 );
65 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
66}
67
68
70{
71 // Delete the GRID_TRICKS.
72 m_grid->PopEventHandler( true );
73}
74
75
77{
78 m_fields = m_templateMgr->GetTemplateFieldNames( m_scope );
79
80 return TransferDataToGrid();
81}
82
83
85{
86 m_grid->OnAddRow(
87 [&]() -> std::pair<int, int>
88 {
89 int row = m_grid->GetNumberRows();
91
92 const wxString untitledName = _( "Untitled Field" );
93 wxString name = untitledName;
94 int suffix = 2;
95
96 while( std::any_of( m_fields.begin(), m_fields.end(),
97 [&]( const TEMPLATE_FIELDNAME& aField )
98 {
99 return aField.m_Name == name;
100 } ) )
101 {
102 name = wxString::Format( wxS( "%s %d" ), untitledName, suffix++ );
103 }
104
105 TEMPLATE_FIELDNAME newFieldname( name );
106 newFieldname.m_Visible = false;
107 m_fields.insert( m_fields.end(), newFieldname );
108
110 return { row, 0 };
111 } );
112}
113
114
116{
117 m_grid->OnDeleteRows(
118 [&]( int row )
119 {
120 m_fields.erase( m_fields.begin() + row );
121 m_grid->DeleteRows( row );
122 } );
123}
124
125
126void PANEL_TEMPLATE_FIELDNAMES::OnMoveUp( wxCommandEvent& event )
127{
128 m_grid->OnMoveRowUp(
129 [&]( int row )
130 {
131 m_grid->SwapRows( row, row - 1 );
132 } );
133}
134
135
136void PANEL_TEMPLATE_FIELDNAMES::OnMoveDown( wxCommandEvent& event )
137{
138 m_grid->OnMoveRowDown(
139 [&]( int row )
140 {
141 m_grid->SwapRows( row, row + 1 );
142 } );
143}
144
145
147{
148 m_grid->Freeze();
149
150 m_grid->ClearRows();
151 m_grid->AppendRows( m_fields.size() );
152
153 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
154 {
155 m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
156
157 // columns 1 and 2 show a boolean value (in a check box):
158 m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
159 m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
160
161 // Set cell properties
162 m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
163
164 // Render the Visible and URL columns as check boxes
165 m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
166 m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
167 m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
168
169 m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
170 m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
171 m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
172 }
173
174 m_grid->Thaw();
175
176 return true;
177}
178
179
181{
182 if( !m_grid->CommitPendingChanges() )
183 return false;
184
185 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
186 {
187 m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
188 m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
189 m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
190 }
191
192 return true;
193}
194
195
197{
198 if( !TransferDataFromGrid() )
199 return false;
200
201 std::set<wxString> names;
202
203 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
204 {
205 TEMPLATE_FIELDNAME& field = m_fields[row];
206
207 if( !field.m_Name.IsEmpty() )
208 {
209 wxString trimmedName = field.m_Name;
210
211 trimmedName.Trim();
212 trimmedName.Trim( false );
213
214 // Check if the field name contains leading and/or trailing white space.
215 if( field.m_Name != trimmedName )
216 {
217 wxString msg;
218
219 msg.Printf( _( "The field name '%s' contains trailing and/or leading white space." ),
220 field.m_Name );
221
222 KICAD_MESSAGE_DIALOG dlg( this, msg, _( "Warning" ), wxOK | wxCANCEL | wxCENTER | wxICON_WARNING );
223
224 dlg.SetExtendedMessage( _( "This may result in what appears to be duplicate field "
225 "names but are actually unique names differing only by "
226 "white space characters. Removing the white space "
227 "characters will have no effect on existing symbol "
228 "field names." ) );
229
230 dlg.SetOKCancelLabels( KICAD_MESSAGE_DIALOG::ButtonLabel( _( "Remove White Space" ) ),
231 KICAD_MESSAGE_DIALOG::ButtonLabel( _( "Keep White Space" ) ) );
232
233 if( dlg.ShowModal() == wxID_OK )
234 {
235 field.m_Name = trimmedName;
236 m_grid->SetCellValue( row, 0, trimmedName );
237 }
238 }
239
240 for( FIELD_T fieldId : MANDATORY_FIELDS )
241 {
242 if( field.m_Name.CmpNoCase( GetDefaultFieldName( fieldId, UNTRANSLATED ) ) == 0 )
243 {
244 DisplayErrorMessage( this, wxString::Format(
245 _( "Field name template '%s' already exists as a mandatory field." ),
246 field.m_Name ) );
247 m_grid->MakeCellVisible( row, 0 );
248 m_grid->SetGridCursor( row, 0 );
249 m_grid->SetFocus();
250 return false;
251 }
252 }
253
254 if( !names.insert( field.m_Name ).second )
255 {
256 DisplayErrorMessage( this, wxString::Format(
257 _( "Field name template '%s' already exists." ), field.m_Name ) );
258 m_grid->MakeCellVisible( row, 0 );
259 m_grid->SetGridCursor( row, 0 );
260 m_grid->SetFocus();
261 return false;
262 }
263 }
264 }
265
266 m_templateMgr->DeleteFieldNameTemplates( m_scope );
267
268 for( const TEMPLATE_FIELDNAME& field : m_fields )
269 {
270 if( !field.m_Name.IsEmpty() )
271 m_templateMgr->AddTemplateFieldName( field, m_scope );
272 }
273
274 return true;
275}
276
277
const char * name
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
TEMPLATES m_FieldNameTemplates
Global field name templates shared by all project editors.
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:57
PANEL_TEMPLATE_FIELDNAMES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
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 OnDeleteButtonClick(wxCommandEvent &event) override
Deletes the selected template fieldname from the template fieldnames data.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:546
const std::vector< TEMPLATE_FIELDNAME > & GetTemplateFieldNames(SCOPE aScope)
Return a specific list (global or project) for read only access.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG
Definition confirm.h:48
#define _(s)
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
Hold a name of a symbol's field, field value, and default visibility.
wxString GetDefaultFieldName(FIELD_T aFieldId, TRANSLATION aTranslation)
Return a default symbol field name for a mandatory field type.
#define MANDATORY_FIELDS
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
@ UNTRANSLATED