KiCad PCB EDA Suite
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 <macros.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
59
60 m_checkboxColWidth = m_grid->GetColSize( 1 );
61
62 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
63 {
64 OnAddButtonClick( aEvent );
65 } ) );
66 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
67}
68
69
71{
72 // Delete the GRID_TRICKS.
73 m_grid->PopEventHandler( true );
74}
75
76
78{
80
81 return TransferDataToGrid();
82}
83
84
86{
88 return;
89
90 int row = m_grid->GetNumberRows();
92
93 TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
94 newFieldname.m_Visible = false;
95 m_fields.insert( m_fields.end(), newFieldname );
97
98 // wx documentation is wrong, SetGridCursor does not make visible.
99 m_grid->MakeCellVisible( row, 0 );
100 m_grid->SetGridCursor( row, 0 );
101}
102
103
105{
107 return;
108
109 wxArrayInt selectedRows = m_grid->GetSelectedRows();
110
111 if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
112 selectedRows.push_back( m_grid->GetGridCursorRow() );
113
114 if( selectedRows.empty() )
115 return;
116
117 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
118 selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
119
120 for( int row : selectedRows )
121 {
122 m_fields.erase( m_fields.begin() + row );
123 m_grid->DeleteRows( row );
124
125 m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
126 m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
127 }
128}
129
130
132{
133 m_grid->Freeze();
134
135 m_grid->ClearRows();
136 m_grid->AppendRows( m_fields.size() );
137
138 for( int row = 0; row < m_grid->GetNumberRows(); ++row )
139 {
140 m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
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{
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
186
187 for( const TEMPLATE_FIELDNAME& field : m_fields )
189
190 if( m_global )
191 {
192 EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
193
194 if( cfg )
195 {
196 // Save global fieldname templates
198 m_templateMgr->Format( &sf, 0, true );
199
200 wxString record = FROM_UTF8( sf.GetString().c_str() );
201 record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
202 record.Replace( wxT(" "), wxT(" "), true ); // double space to single
203
204 cfg->m_Drawing.field_names = record.ToStdString();
205 }
206 }
207
208 return true;
209}
210
211
213{
214 if( aWidth <= 0 )
215 return;
216
217 // Account for scroll bars
218 aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
219
220 m_grid->SetColSize( 0, std::max( 72, aWidth - 2 * m_checkboxColWidth ) );
221 m_grid->SetColSize( 1, m_checkboxColWidth );
222 m_grid->SetColSize( 2, m_checkboxColWidth );
223}
224
225
227{
228 AdjustGridColumns( event.GetSize().GetX() );
229
230 event.Skip();
231}
232
233
235{
236 m_fields = templateMgr->GetTemplateFieldNames( m_global );
238}
239
240
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
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 wxBitmap &aBmp)
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:415
const std::string & GetString()
Definition: richio.h:438
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:139
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:423
#define _(s)
This file contains miscellaneous commonly used macros and functions.
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111
Hold a name of a symbol's field, field value, and default visibility.