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 EESCHEMA_SETTINGS* cfg = mgr.GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" );
57
58 if( cfg && !cfg->m_Drawing.field_names.IsEmpty() )
60 }
61
62 m_addFieldButton->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
63 m_deleteFieldButton->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
64
65 m_checkboxColWidth = m_grid->GetColSize( 1 );
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->SetSelectionMode( wxGrid::wxGridSelectRows );
74}
75
76
78{
79 // Delete the GRID_TRICKS.
80 m_grid->PopEventHandler( true );
81}
82
83
85{
87
88 return TransferDataToGrid();
89}
90
91
93{
95 return;
96
97 int row = m_grid->GetNumberRows();
99
100 TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
101 newFieldname.m_Visible = false;
102 m_fields.insert( m_fields.end(), newFieldname );
104
105 // wx documentation is wrong, SetGridCursor does not make visible.
106 m_grid->MakeCellVisible( row, 0 );
107 m_grid->SetGridCursor( row, 0 );
108}
109
110
112{
114 return;
115
116 wxArrayInt selectedRows = m_grid->GetSelectedRows();
117
118 if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
119 selectedRows.push_back( m_grid->GetGridCursorRow() );
120
121 if( selectedRows.empty() )
122 return;
123
124 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
125 selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
126
127 for( int row : selectedRows )
128 {
129 m_fields.erase( m_fields.begin() + row );
130 m_grid->DeleteRows( row );
131
132 m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
133 m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
134 }
135}
136
137
139{
140 m_grid->Freeze();
141
142 m_grid->ClearRows();
143 m_grid->AppendRows( m_fields.size() );
144
145 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
146 {
147 m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
148
149 // columns 1 and 2 show a boolean value (in a check box):
150 m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
151 m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
152
153 // Set cell properties
154 m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
155
156 // Render the Visible and URL columns as check boxes
157 m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
158 m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
159 m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
160
161 m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
162 m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
163 m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
164 }
165
166 m_grid->Thaw();
167
168 return true;
169}
170
171
173{
175 return false;
176
177 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
178 {
179 m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
180 m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
181 m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
182 }
183
184 return true;
185}
186
187
189{
190 if( !TransferDataFromGrid() )
191 return false;
192
194
195 for( TEMPLATE_FIELDNAME& field : m_fields )
196 {
197 if( !field.m_Name.IsEmpty() )
198 {
199 wxString trimmedName = field.m_Name;
200
201 trimmedName.Trim();
202 trimmedName.Trim( false );
203
204 // Check if the field name contains leading and/or trailing white space.
205 if( field.m_Name != trimmedName )
206 {
207 wxString msg;
208
209 msg.Printf( _( "The field name \"%s\" contains trailing and/or leading white"
210 "space." ), field.m_Name );
211
212 wxMessageDialog dlg( this, msg, _( "Warning" ),
213 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( wxMessageDialog::ButtonLabel( _( "Remove White Space" ) ),
222 wxMessageDialog::ButtonLabel( _( "Keep White Space" ) ) );
223
224 if( dlg.ShowModal() == wxID_OK )
225 field.m_Name = trimmedName;
226 }
227
229 }
230 }
231
232 if( m_global )
233 {
235 EESCHEMA_SETTINGS* cfg = mgr.GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" );
236
237 if( cfg )
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
255{
256 if( aWidth <= 0 )
257 return;
258
259 // Account for scroll bars
260 aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
261
262 m_grid->SetColSize( 0, std::max( 72, aWidth - 2 * m_checkboxColWidth ) );
263 m_grid->SetColSize( 1, m_checkboxColWidth );
264 m_grid->SetColSize( 2, m_checkboxColWidth );
265}
266
267
269{
270 AdjustGridColumns( event.GetSize().GetX() );
271
272 event.Skip();
273}
274
275
277{
278 m_fields = templateMgr->GetTemplateFieldNames( m_global );
280}
281
282
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
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.
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.
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:125
T * GetAppSettings(const wxString &aFilename)
Return a handle to the a given settings by type.
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.
void DeleteAllFieldNameTemplates(bool aGlobal)
Delete the entire contents.
const TEMPLATE_FIELDNAMES & GetTemplateFieldNames()
Return a template field name list for read only access.
void Format(OUTPUTFORMATTER *out, bool aGlobal) const
Serialize this object out as text into the given OUTPUTFORMATTER.
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:193
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:646
#define _(s)
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1073
see class PGM_BASE
wxString From_UTF8(const char *cstring)
Hold a name of a symbol's field, field value, and default visibility.