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>
27#include <eeschema_settings.h>
29#include <template_fieldnames.h>
30#include <grid_tricks.h>
31#include <bitmaps.h>
32#include <richio.h>
33#include <string_utils.h>
34#include <confirm.h>
35
37 TEMPLATES* aProjectTemplateMgr ) :
39{
40 if( aProjectTemplateMgr )
41 {
42 m_title->SetLabel( _( "Project Field Name Templates" ) );
43 m_global = false;
44 m_templateMgr = aProjectTemplateMgr;
45 }
46 else
47 {
48 m_title->SetLabel( _( "Global Field Name Templates" ) );
49 m_global = true;
51
53 {
54 if( !cfg->m_Drawing.field_names.IsEmpty() )
55 m_templateMgr->AddTemplateFieldNames( cfg->m_Drawing.field_names );
56 }
57 }
58
63
64 m_grid->SetUseNativeColLabels();
65
66 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
67 {
68 OnAddButtonClick( aEvent );
69 } ) );
70 m_grid->SetupColumnAutosizer( 0 );
71 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
72}
73
74
76{
77 // Delete the GRID_TRICKS.
78 m_grid->PopEventHandler( true );
79}
80
81
83{
84 m_fields = m_templateMgr->GetTemplateFieldNames( m_global );
85
86 return TransferDataToGrid();
87}
88
89
91{
92 m_grid->OnAddRow(
93 [&]() -> std::pair<int, int>
94 {
95 int row = m_grid->GetNumberRows();
97
98 TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
99 newFieldname.m_Visible = false;
100 m_fields.insert( m_fields.end(), newFieldname );
101
103 return { row, 0 };
104 } );
105}
106
107
109{
110 m_grid->OnDeleteRows(
111 [&]( int row )
112 {
113 m_fields.erase( m_fields.begin() + row );
114 m_grid->DeleteRows( row );
115 } );
116}
117
118
119void PANEL_TEMPLATE_FIELDNAMES::OnMoveUp( wxCommandEvent& event )
120{
121 m_grid->OnMoveRowUp(
122 [&]( int row )
123 {
124 m_grid->SwapRows( row, row - 1 );
125 } );
126}
127
128
129void PANEL_TEMPLATE_FIELDNAMES::OnMoveDown( wxCommandEvent& event )
130{
131 m_grid->OnMoveRowDown(
132 [&]( int row )
133 {
134 m_grid->SwapRows( row, row + 1 );
135 } );
136}
137
138
140{
141 m_grid->Freeze();
142
143 m_grid->ClearRows();
144 m_grid->AppendRows( m_fields.size() );
145
146 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
147 {
148 m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
149
150 // columns 1 and 2 show a boolean value (in a check box):
151 m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
152 m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
153
154 // Set cell properties
155 m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
156
157 // Render the Visible and URL columns as check boxes
158 m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
159 m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
160 m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
161
162 m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
163 m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
164 m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
165 }
166
167 m_grid->Thaw();
168
169 return true;
170}
171
172
174{
175 if( !m_grid->CommitPendingChanges() )
176 return false;
177
178 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
179 {
180 m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
181 m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
182 m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
183 }
184
185 return true;
186}
187
188
190{
191 if( !TransferDataFromGrid() )
192 return false;
193
194 m_templateMgr->DeleteAllFieldNameTemplates( m_global );
195
196 for( TEMPLATE_FIELDNAME& field : m_fields )
197 {
198 if( !field.m_Name.IsEmpty() )
199 {
200 wxString trimmedName = field.m_Name;
201
202 trimmedName.Trim();
203 trimmedName.Trim( false );
204
205 // Check if the field name contains leading and/or trailing white space.
206 if( field.m_Name != trimmedName )
207 {
208 wxString msg;
209
210 msg.Printf( _( "The field name '%s' contains trailing and/or leading white space." ),
211 field.m_Name );
212
213 KICAD_MESSAGE_DIALOG dlg( this, msg, _( "Warning" ), wxOK | wxCANCEL | wxCENTER | wxICON_WARNING );
214
215 dlg.SetExtendedMessage( _( "This may result in what appears to be duplicate field "
216 "names but are actually unique names differing only by "
217 "white space characters. Removing the white space "
218 "characters will have no effect on existing symbol "
219 "field names." ) );
220
221 dlg.SetOKCancelLabels( KICAD_MESSAGE_DIALOG::ButtonLabel( _( "Remove White Space" ) ),
222 KICAD_MESSAGE_DIALOG::ButtonLabel( _( "Keep White Space" ) ) );
223
224 if( dlg.ShowModal() == wxID_OK )
225 field.m_Name = trimmedName;
226 }
227
228 m_templateMgr->AddTemplateFieldName( field, m_global );
229 }
230 }
231
232 if( m_global )
233 {
235 {
236 // Save global fieldname templates
238 m_templateMgr->Format( &sf, true );
239
240 wxString record = From_UTF8( sf.GetString().c_str() );
241 record.Replace( wxT( " " ), wxT( " " ), true ); // double space to single
242
243 cfg->m_Drawing.field_names = record.ToStdString();
244 }
245 }
246
247 return true;
248}
249
250
256
257
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
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.
Implement an OUTPUTFORMATTER to a memory buffer.
Definition richio.h:418
const std::string & GetString()
Definition richio.h:441
const std::vector< TEMPLATE_FIELDNAME > & GetTemplateFieldNames()
Return a template field name list for read only access.
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG
Definition confirm.h:48
#define _(s)
see class PGM_BASE
T * GetAppSettings(const char *aFilename)
wxString From_UTF8(const char *cstring)
Hold a name of a symbol's field, field value, and default visibility.