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, 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->m_Drawing.field_names.IsEmpty() )
58 m_templateMgr->AddTemplateFieldNames( cfg->m_Drawing.field_names );
59 }
60 }
61
66
67 m_grid->SetUseNativeColLabels();
68
69 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
70 {
71 OnAddButtonClick( aEvent );
72 } ) );
73 m_grid->SetupColumnAutosizer( 0 );
74 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
75}
76
77
79{
80 // Delete the GRID_TRICKS.
81 m_grid->PopEventHandler( true );
82}
83
84
86{
87 m_fields = m_templateMgr->GetTemplateFieldNames( m_global );
88
89 return TransferDataToGrid();
90}
91
92
94{
95 m_grid->OnAddRow(
96 [&]() -> std::pair<int, int>
97 {
98 int row = m_grid->GetNumberRows();
100
101 TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
102 newFieldname.m_Visible = false;
103 m_fields.insert( m_fields.end(), newFieldname );
104
106 return { row, 0 };
107 } );
108}
109
110
112{
113 m_grid->OnDeleteRows(
114 [&]( int row )
115 {
116 m_fields.erase( m_fields.begin() + row );
117 m_grid->DeleteRows( row );
118 } );
119}
120
121
122void PANEL_TEMPLATE_FIELDNAMES::OnMoveUp( wxCommandEvent& event )
123{
124 m_grid->OnMoveRowUp(
125 [&]( int row )
126 {
127 m_grid->SwapRows( row, row - 1 );
128 } );
129}
130
131
132void PANEL_TEMPLATE_FIELDNAMES::OnMoveDown( wxCommandEvent& event )
133{
134 m_grid->OnMoveRowDown(
135 [&]( int row )
136 {
137 m_grid->SwapRows( row, row + 1 );
138 } );
139}
140
141
143{
144 m_grid->Freeze();
145
146 m_grid->ClearRows();
147 m_grid->AppendRows( m_fields.size() );
148
149 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
150 {
151 m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
152
153 // columns 1 and 2 show a boolean value (in a check box):
154 m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
155 m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
156
157 // Set cell properties
158 m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
159
160 // Render the Visible and URL columns as check boxes
161 m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
162 m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
163 m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
164
165 m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
166 m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
167 m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
168 }
169
170 m_grid->Thaw();
171
172 return true;
173}
174
175
177{
178 if( !m_grid->CommitPendingChanges() )
179 return false;
180
181 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
182 {
183 m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
184 m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
185 m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
186 }
187
188 return true;
189}
190
191
193{
194 if( !TransferDataFromGrid() )
195 return false;
196
197 m_templateMgr->DeleteAllFieldNameTemplates( m_global );
198
199 for( TEMPLATE_FIELDNAME& field : m_fields )
200 {
201 if( !field.m_Name.IsEmpty() )
202 {
203 wxString trimmedName = field.m_Name;
204
205 trimmedName.Trim();
206 trimmedName.Trim( false );
207
208 // Check if the field name contains leading and/or trailing white space.
209 if( field.m_Name != trimmedName )
210 {
211 wxString msg;
212
213 msg.Printf( _( "The field name '%s' contains trailing and/or leading white space." ),
214 field.m_Name );
215
216 wxMessageDialog dlg( this, msg, _( "Warning" ), wxOK|wxCANCEL|wxCENTER|wxICON_WARNING );
217
218 dlg.SetExtendedMessage( _( "This may result in what appears to be duplicate field "
219 "names but are actually unique names differing only by "
220 "white space characters. Removing the white space "
221 "characters will have no effect on existing symbol "
222 "field names." ) );
223
224 dlg.SetOKCancelLabels( wxMessageDialog::ButtonLabel( _( "Remove White Space" ) ),
225 wxMessageDialog::ButtonLabel( _( "Keep White Space" ) ) );
226
227 if( dlg.ShowModal() == wxID_OK )
228 field.m_Name = trimmedName;
229 }
230
231 m_templateMgr->AddTemplateFieldName( field, m_global );
232 }
233 }
234
235 if( m_global )
236 {
238 {
239 // Save global fieldname templates
241 m_templateMgr->Format( &sf, true );
242
243 wxString record = From_UTF8( sf.GetString().c_str() );
244 record.Replace( wxT( " " ), wxT( " " ), true ); // double space to single
245
246 cfg->m_Drawing.field_names = record.ToStdString();
247 }
248 }
249
250 return true;
251}
252
253
259
260
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
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:449
const std::string & GetString()
Definition richio.h:472
const std::vector< TEMPLATE_FIELDNAME > & GetTemplateFieldNames()
Return a template field name list for read only access.
#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.