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 <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
57 m_addFieldButton->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
58 m_deleteFieldButton->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
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 )
188 {
189 if( !field.m_Name.IsEmpty() )
191 }
192
193 if( m_global )
194 {
195 EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
196
197 if( cfg )
198 {
199 // Save global fieldname templates
201 m_templateMgr->Format( &sf, 0, true );
202
203 wxString record = FROM_UTF8( sf.GetString().c_str() );
204 record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
205 record.Replace( wxT(" "), wxT(" "), true ); // double space to single
206
207 cfg->m_Drawing.field_names = record.ToStdString();
208 }
209 }
210
211 return true;
212}
213
214
216{
217 if( aWidth <= 0 )
218 return;
219
220 // Account for scroll bars
221 aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
222
223 m_grid->SetColSize( 0, std::max( 72, aWidth - 2 * m_checkboxColWidth ) );
224 m_grid->SetColSize( 1, m_checkboxColWidth );
225 m_grid->SetColSize( 2, m_checkboxColWidth );
226}
227
228
230{
231 AdjustGridColumns( event.GetSize().GetX() );
232
233 event.Skip();
234}
235
236
238{
239 m_fields = templateMgr->GetTemplateFieldNames( m_global );
241}
242
243
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:106
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:427
const std::string & GetString()
Definition: richio.h:450
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:474
#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:115
Hold a name of a symbol's field, field value, and default visibility.