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
55 if( EESCHEMA_SETTINGS* cfg = GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" ) )
56 {
57 if( !cfg->m_Drawing.field_names.IsEmpty() )
58 m_templateMgr->AddTemplateFieldNames( cfg->m_Drawing.field_names );
59 }
60 }
61
62 m_addFieldButton->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
63 m_deleteFieldButton->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
64 m_bpMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
65 m_bpMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
66
67 m_checkboxColWidth = m_grid->GetColSize( 1 );
68
69 m_grid->SetUseNativeColLabels();
70
71 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
72 {
73 OnAddButtonClick( aEvent );
74 } ) );
75 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
76}
77
78
80{
81 // Delete the GRID_TRICKS.
82 m_grid->PopEventHandler( true );
83}
84
85
87{
89
90 return TransferDataToGrid();
91}
92
93
95{
97 [&]() -> std::pair<int, int>
98 {
99 int row = m_grid->GetNumberRows();
101
102 TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
103 newFieldname.m_Visible = false;
104 m_fields.insert( m_fields.end(), newFieldname );
105
107 return { row, 0 };
108 } );
109}
110
111
113{
115 [&]( int row )
116 {
117 m_fields.erase( m_fields.begin() + row );
118 m_grid->DeleteRows( row );
119 } );
120}
121
122
123void PANEL_TEMPLATE_FIELDNAMES::OnMoveUp( wxCommandEvent& event )
124{
126 [&]( int row )
127 {
128 m_grid->SwapRows( row, row - 1 );
129 } );
130}
131
132
133void PANEL_TEMPLATE_FIELDNAMES::OnMoveDown( wxCommandEvent& event )
134{
136 [&]( int row )
137 {
138 m_grid->SwapRows( row, row + 1 );
139 } );
140}
141
142
144{
145 m_grid->Freeze();
146
147 m_grid->ClearRows();
148 m_grid->AppendRows( m_fields.size() );
149
150 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
151 {
152 m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
153
154 // columns 1 and 2 show a boolean value (in a check box):
155 m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
156 m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
157
158 // Set cell properties
159 m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
160
161 // Render the Visible and URL columns as check boxes
162 m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
163 m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
164 m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
165
166 m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
167 m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
168 m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
169 }
170
171 m_grid->Thaw();
172
173 return true;
174}
175
176
178{
180 return false;
181
182 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
183 {
184 m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
185 m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
186 m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
187 }
188
189 return true;
190}
191
192
194{
195 if( !TransferDataFromGrid() )
196 return false;
197
199
200 for( TEMPLATE_FIELDNAME& field : m_fields )
201 {
202 if( !field.m_Name.IsEmpty() )
203 {
204 wxString trimmedName = field.m_Name;
205
206 trimmedName.Trim();
207 trimmedName.Trim( false );
208
209 // Check if the field name contains leading and/or trailing white space.
210 if( field.m_Name != trimmedName )
211 {
212 wxString msg;
213
214 msg.Printf( _( "The field name '%s' contains trailing and/or leading white space." ),
215 field.m_Name );
216
217 wxMessageDialog dlg( this, msg, _( "Warning" ), wxOK|wxCANCEL|wxCENTER|wxICON_WARNING );
218
219 dlg.SetExtendedMessage( _( "This may result in what appears to be duplicate field "
220 "names but are actually unique names differing only by "
221 "white space characters. Removing the white space "
222 "characters will have no effect on existing symbol "
223 "field names." ) );
224
225 dlg.SetOKCancelLabels( wxMessageDialog::ButtonLabel( _( "Remove White Space" ) ),
226 wxMessageDialog::ButtonLabel( _( "Keep White Space" ) ) );
227
228 if( dlg.ShowModal() == wxID_OK )
229 field.m_Name = trimmedName;
230 }
231
233 }
234 }
235
236 if( m_global )
237 {
238 if( EESCHEMA_SETTINGS* cfg = GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" ) )
239 {
240 // Save global fieldname templates
242 m_templateMgr->Format( &sf, true );
243
244 wxString record = From_UTF8( sf.GetString().c_str() );
245 record.Replace( wxT( " " ), wxT( " " ), true ); // double space to single
246
247 cfg->m_Drawing.field_names = record.ToStdString();
248 }
249 }
250
251 return true;
252}
253
254
256{
257 if( aWidth <= 0 )
258 return;
259
260 // Account for scroll bars
261 aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
262
263 m_grid->SetColSize( 0, std::max( 72, aWidth - 2 * m_checkboxColWidth ) );
264 m_grid->SetColSize( 1, m_checkboxColWidth );
265 m_grid->SetColSize( 2, m_checkboxColWidth );
266}
267
268
270{
271 AdjustGridColumns( event.GetSize().GetX() );
272
273 event.Skip();
274}
275
276
278{
279 m_fields = templateMgr->GetTemplateFieldNames( m_global );
281}
282
283
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
Class PANEL_TEMPLATE_FIELDNAMES_BASE.
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 OnSizeGrid(wxSizeEvent &event) override
void OnDeleteButtonClick(wxCommandEvent &event) override
Deletes the selected template fieldname from the template fieldnames data.
void SetBitmap(const wxBitmapBundle &aBmp)
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:449
const std::string & GetString()
Definition: richio.h:472
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.
const std::vector< TEMPLATE_FIELDNAME > & GetTemplateFieldNames()
Return a template field name list for read only access.
void DeleteAllFieldNameTemplates(bool aGlobal)
Delete the entire contents.
void Format(OUTPUTFORMATTER *out, bool aGlobal) const
Serialize this object out as text into the given OUTPUTFORMATTER.
void OnMoveRowUp(const std::function< void(int row)> &aMover)
Definition: wx_grid.cpp:766
void SwapRows(int aRowA, int aRowB)
These aren't that tricky, but might as well share code.
Definition: wx_grid.cpp:755
void OnMoveRowDown(const std::function< void(int row)> &aMover)
Definition: wx_grid.cpp:799
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition: wx_grid.cpp:704
void OnAddRow(const std::function< std::pair< int, int >()> &aAdder)
Definition: wx_grid.cpp:684
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:220
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:632
#define _(s)
see class PGM_BASE
wxString From_UTF8(const char *cstring)
Hold a name of a symbol's field, field value, and default visibility.