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
34 TEMPLATES* aProjectTemplateMgr ) :
36{
37 if( aProjectTemplateMgr )
38 {
39 m_title->SetLabel( _( "Project Field Name Templates" ) );
40 m_scope = TEMPLATES::SCOPE::PROJECT;
41 m_templateMgr = aProjectTemplateMgr;
42 }
43 else
44 {
45 m_title->SetLabel( _( "Global Field Name Templates" ) );
46 m_scope = TEMPLATES::SCOPE::GLOBAL;
48 }
49
54
55 m_grid->SetUseNativeColLabels();
56
57 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
58 {
59 OnAddButtonClick( aEvent );
60 } ) );
61 m_grid->SetupColumnAutosizer( 0 );
62 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
63}
64
65
67{
68 // Delete the GRID_TRICKS.
69 m_grid->PopEventHandler( true );
70}
71
72
74{
75 m_fields = m_templateMgr->GetTemplateFieldNames( m_scope );
76
77 return TransferDataToGrid();
78}
79
80
82{
83 m_grid->OnAddRow(
84 [&]() -> std::pair<int, int>
85 {
86 int row = m_grid->GetNumberRows();
88
89 TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
90 newFieldname.m_Visible = false;
91 m_fields.insert( m_fields.end(), newFieldname );
92
94 return { row, 0 };
95 } );
96}
97
98
100{
101 m_grid->OnDeleteRows(
102 [&]( int row )
103 {
104 m_fields.erase( m_fields.begin() + row );
105 m_grid->DeleteRows( row );
106 } );
107}
108
109
110void PANEL_TEMPLATE_FIELDNAMES::OnMoveUp( wxCommandEvent& event )
111{
112 m_grid->OnMoveRowUp(
113 [&]( int row )
114 {
115 m_grid->SwapRows( row, row - 1 );
116 } );
117}
118
119
120void PANEL_TEMPLATE_FIELDNAMES::OnMoveDown( wxCommandEvent& event )
121{
122 m_grid->OnMoveRowDown(
123 [&]( int row )
124 {
125 m_grid->SwapRows( row, row + 1 );
126 } );
127}
128
129
131{
132 m_grid->Freeze();
133
134 m_grid->ClearRows();
135 m_grid->AppendRows( m_fields.size() );
136
137 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
138 {
139 m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
140
141 // columns 1 and 2 show a boolean value (in a check box):
142 m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
143 m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
144
145 // Set cell properties
146 m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
147
148 // Render the Visible and URL columns as check boxes
149 m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
150 m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
151 m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
152
153 m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
154 m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
155 m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
156 }
157
158 m_grid->Thaw();
159
160 return true;
161}
162
163
165{
166 if( !m_grid->CommitPendingChanges() )
167 return false;
168
169 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
170 {
171 m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
172 m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
173 m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
174 }
175
176 return true;
177}
178
179
181{
182 if( !TransferDataFromGrid() )
183 return false;
184
185 m_templateMgr->DeleteFieldNameTemplates( m_scope );
186
187 for( TEMPLATE_FIELDNAME& field : m_fields )
188 {
189 if( !field.m_Name.IsEmpty() )
190 {
191 wxString trimmedName = field.m_Name;
192
193 trimmedName.Trim();
194 trimmedName.Trim( false );
195
196 // Check if the field name contains leading and/or trailing white space.
197 if( field.m_Name != trimmedName )
198 {
199 wxString msg;
200
201 msg.Printf( _( "The field name '%s' contains trailing and/or leading white space." ),
202 field.m_Name );
203
204 KICAD_MESSAGE_DIALOG dlg( this, msg, _( "Warning" ), wxOK | wxCANCEL | wxCENTER | wxICON_WARNING );
205
206 dlg.SetExtendedMessage( _( "This may result in what appears to be duplicate field "
207 "names but are actually unique names differing only by "
208 "white space characters. Removing the white space "
209 "characters will have no effect on existing symbol "
210 "field names." ) );
211
212 dlg.SetOKCancelLabels( KICAD_MESSAGE_DIALOG::ButtonLabel( _( "Remove White Space" ) ),
213 KICAD_MESSAGE_DIALOG::ButtonLabel( _( "Keep White Space" ) ) );
214
215 if( dlg.ShowModal() == wxID_OK )
216 field.m_Name = trimmedName;
217 }
218
219 m_templateMgr->AddTemplateFieldName( field, m_scope );
220 }
221 }
222
223 return true;
224}
225
226
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.
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.