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 (C) 1992-2023 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
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 <string_utils.h>
34
36 TEMPLATES* aProjectTemplateMgr ) :
38{
39 if( aProjectTemplateMgr )
40 {
41 m_title->SetLabel( _( "Project field name templates:" ) );
42 m_global = false;
43 m_templateMgr = aProjectTemplateMgr;
44 }
45 else
46 {
47 m_title->SetLabel( _( "Global field name templates:" ) );
48 m_global = true;
50
51 EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
52
53 if( cfg && !cfg->m_Drawing.field_names.IsEmpty() )
55 }
56
57 m_addFieldButton->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
58 m_deleteFieldButton->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
59
60 m_checkboxColWidth = m_grid->GetColSize( 1 );
61
62 m_grid->SetUseNativeColLabels();
63
64 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
65 {
66 OnAddButtonClick( aEvent );
67 } ) );
68 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
69}
70
71
73{
74 // Delete the GRID_TRICKS.
75 m_grid->PopEventHandler( true );
76}
77
78
80{
82
83 return TransferDataToGrid();
84}
85
86
88{
90 return;
91
92 int row = m_grid->GetNumberRows();
94
95 TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
96 newFieldname.m_Visible = false;
97 m_fields.insert( m_fields.end(), newFieldname );
99
100 // wx documentation is wrong, SetGridCursor does not make visible.
101 m_grid->MakeCellVisible( row, 0 );
102 m_grid->SetGridCursor( row, 0 );
103}
104
105
107{
109 return;
110
111 wxArrayInt selectedRows = m_grid->GetSelectedRows();
112
113 if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
114 selectedRows.push_back( m_grid->GetGridCursorRow() );
115
116 if( selectedRows.empty() )
117 return;
118
119 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
120 selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
121
122 for( int row : selectedRows )
123 {
124 m_fields.erase( m_fields.begin() + row );
125 m_grid->DeleteRows( row );
126
127 m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
128 m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
129 }
130}
131
132
134{
135 m_grid->Freeze();
136
137 m_grid->ClearRows();
138 m_grid->AppendRows( m_fields.size() );
139
140 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
141 {
142 m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
143 // columns 1 and 2 show a boolean value (in a check box):
144 m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
145 m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
146
147 // Set cell properties
148 m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
149
150 // Render the Visible and URL columns as check boxes
151 m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
152 m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
153 m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
154
155 m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
156 m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
157 m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
158 }
159
160 m_grid->Thaw();
161
162 return true;
163}
164
165
167{
169 return false;
170
171 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
172 {
173 m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
174 m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
175 m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
176 }
177
178 return true;
179}
180
181
183{
184 if( !TransferDataFromGrid() )
185 return false;
186
188
189 for( const TEMPLATE_FIELDNAME& field : m_fields )
190 {
191 if( !field.m_Name.IsEmpty() )
193 }
194
195 if( m_global )
196 {
197 EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
198
199 if( cfg )
200 {
201 // Save global fieldname templates
203 m_templateMgr->Format( &sf, 0, true );
204
205 wxString record = From_UTF8( sf.GetString().c_str() );
206 record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
207 record.Replace( wxT(" "), wxT(" "), true ); // double space to single
208
209 cfg->m_Drawing.field_names = record.ToStdString();
210 }
211 }
212
213 return true;
214}
215
216
218{
219 if( aWidth <= 0 )
220 return;
221
222 // Account for scroll bars
223 aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
224
225 m_grid->SetColSize( 0, std::max( 72, aWidth - 2 * m_checkboxColWidth ) );
226 m_grid->SetColSize( 1, m_checkboxColWidth );
227 m_grid->SetColSize( 2, m_checkboxColWidth );
228}
229
230
232{
233 AdjustGridColumns( event.GetSize().GetX() );
234
235 event.Skip();
236}
237
238
240{
241 m_fields = templateMgr->GetTemplateFieldNames( m_global );
243}
244
245
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.
void SetBitmap(const wxBitmapBundle &aBmp)
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:433
const std::string & GetString()
Definition: richio.h:456
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, int nestLevel, 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:147
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:462
#define _(s)
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
wxString From_UTF8(const char *cstring)
Hold a name of a symbol's field, field value, and default visibility.