KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_text_variables.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) 2020-2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <bitmaps.h>
27#include <confirm.h>
28#include <validators.h>
29#include <project.h>
30#include <grid_tricks.h>
33
34#include <algorithm>
35
37{
40};
41
42
43PANEL_TEXT_VARIABLES::PANEL_TEXT_VARIABLES( wxWindow* aParent, PROJECT* aProject ) :
45 m_project( aProject ),
46 m_lastCheckedTicker( 0 ),
47 m_errorRow( -1 ), m_errorCol( -1 ),
48 m_gridWidthsDirty( true )
49{
50 m_btnAddTextVar->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
51 m_btnDeleteTextVar->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
52
54 m_TextVars->SetUseNativeColLabels();
55
56 // prohibit these characters in the alias names: []{}()%~<>"='`;:.,&?/\|$
57 m_nameValidator.SetStyle( wxFILTER_EXCLUDE_CHAR_LIST );
58 m_nameValidator.SetCharExcludes( wxT( "{}[]()%~<>\"='`;:.,&?/\\|$" ) );
59
60 m_TextVars->PushEventHandler( new GRID_TRICKS( m_TextVars, [this]( wxCommandEvent& aEvent )
61 {
62 OnAddTextVar( aEvent );
63 } ) );
64 m_TextVars->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
65
66 // wxFormBuilder doesn't include this event...
67 m_TextVars->Connect( wxEVT_GRID_CELL_CHANGING,
68 wxGridEventHandler( PANEL_TEXT_VARIABLES::OnGridCellChanging ),
69 nullptr, this );
70
71 Bind( wxEVT_IDLE,
72 [this]( wxIdleEvent& aEvent )
73 {
74 // Careful of consuming CPU in an idle event handler. Check the ticker first to
75 // see if there's even a possibility of the text variables having changed.
77 {
78 wxWindow* dialog = wxGetTopLevelParent( this );
79 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
80
81 if( topLevelFocus == dialog && m_lastLoaded != m_project->GetTextVars() )
82 checkReload();
83 }
84 } );
85}
86
87
89{
90 // Delete the GRID_TRICKS.
91 m_TextVars->PopEventHandler( true );
92
93 m_TextVars->Disconnect( wxEVT_GRID_CELL_CHANGING,
94 wxGridEventHandler( PANEL_TEXT_VARIABLES::OnGridCellChanging ),
95 nullptr, this );
96}
97
98
100{
101 // MUST update the ticker before calling IsOK (or we'll end up re-entering through the idle
102 // event until we crash the stack).
104
105 if( IsOK( m_parent, _( "The text variables have been changed outside the Setup dialog.\n"
106 "Do you wish to reload them?" ) ) )
107 {
109
111
112 for( const auto& var : m_lastLoaded )
113 AppendTextVar( var.first, var.second );
114 }
115}
116
117
119{
122
123 for( const auto& var : m_lastLoaded )
124 AppendTextVar( var.first, var.second );
125
126 return true;
127}
128
129
130void PANEL_TEXT_VARIABLES::AppendTextVar( const wxString& aName, const wxString& aValue )
131{
132 int i = m_TextVars->GetNumberRows();
133
134 m_TextVars->AppendRows( 1 );
135
136 m_TextVars->SetCellValue( i, TV_NAME_COL, aName );
137
138 wxGridCellAttr* nameCellAttr = m_TextVars->GetOrCreateCellAttr( i, TV_NAME_COL );
139 wxGridCellTextEditor* nameTextEditor = new GRID_CELL_TEXT_EDITOR();
140 nameTextEditor->SetValidator( m_nameValidator );
141 nameCellAttr->SetEditor( nameTextEditor );
142 nameCellAttr->DecRef();
143
144 m_TextVars->SetCellValue( i, TV_VALUE_COL, aValue );
145}
146
147
149{
151 return false;
152
153 for( int row = 0; row < m_TextVars->GetNumberRows(); ++row )
154 {
155 if( m_TextVars->GetCellValue( row, TV_NAME_COL ).IsEmpty() )
156 {
157 m_errorRow = row;
159 m_errorMsg = _( "Variable name cannot be empty." );
160 return false;
161 }
162 }
163
164 std::map<wxString, wxString>& variables = m_project->GetTextVars();
165
166 variables.clear();
167
168 for( int row = 0; row < m_TextVars->GetNumberRows(); ++row )
169 {
170 wxString name = m_TextVars->GetCellValue( row, TV_NAME_COL );
171 wxString value = m_TextVars->GetCellValue( row, TV_VALUE_COL );
172 variables[ name ] = value;
173 }
174
175 return true;
176}
177
178
180{
181 int row = event.GetRow();
182 int col = event.GetCol();
183 wxString text = event.GetString();
184
185 if( text.IsEmpty() && col == TV_NAME_COL )
186 {
187 m_errorMsg = _( "Variable name cannot be empty." );
188 m_errorRow = row;
189 m_errorCol = col;
190
191 event.Veto();
192 }
193}
194
195
196void PANEL_TEXT_VARIABLES::OnAddTextVar( wxCommandEvent& event )
197{
199 return;
200
201 AppendTextVar( wxEmptyString, wxEmptyString );
202
203 m_TextVars->MakeCellVisible( m_TextVars->GetNumberRows() - 1, TV_NAME_COL );
204 m_TextVars->SetGridCursor( m_TextVars->GetNumberRows() - 1, TV_NAME_COL );
205
206 m_TextVars->EnableCellEditControl( true );
207 m_TextVars->ShowCellEditControl();
208}
209
210
211void PANEL_TEXT_VARIABLES::OnRemoveTextVar( wxCommandEvent& event )
212{
213 int curRow = m_TextVars->GetGridCursorRow();
214
215 if( curRow < 0 || m_TextVars->GetNumberRows() <= curRow )
216 return;
217
218 m_TextVars->CommitPendingChanges( true /* silent mode; we don't care if it's valid */ );
219 m_TextVars->DeleteRows( curRow, 1 );
220
221 m_TextVars->MakeCellVisible( std::max( 0, curRow-1 ), m_TextVars->GetGridCursorCol() );
222 m_TextVars->SetGridCursor( std::max( 0, curRow-1 ), m_TextVars->GetGridCursorCol() );
223}
224
225
226void PANEL_TEXT_VARIABLES::OnGridCellChange( wxGridEvent& aEvent )
227{
228 m_gridWidthsDirty = true;
229
230 aEvent.Skip();
231}
232
233
234void PANEL_TEXT_VARIABLES::OnUpdateUI( wxUpdateUIEvent& event )
235{
237 {
238 int width = m_TextVars->GetClientRect().GetWidth();
239
240 m_TextVars->AutoSizeColumn( TV_NAME_COL );
241 m_TextVars->SetColSize( TV_NAME_COL,
242 std::max( 72, m_TextVars->GetColSize( TV_NAME_COL ) ) );
243
244 m_TextVars->SetColSize( TV_VALUE_COL,
245 std::max( 120, width - m_TextVars->GetColSize( TV_NAME_COL ) ) );
246 m_gridWidthsDirty = false;
247 }
248
249 // Handle a grid error. This is delayed to OnUpdateUI so that we can change focus
250 // even when the original validation was triggered from a killFocus event (and for
251 // dialog with notebooks, so that the corresponding notebook page can be shown in
252 // the background when triggered from an OK).
253 if( !m_errorMsg.IsEmpty() )
254 {
255 // We will re-enter this routine when the error dialog is displayed, so make
256 // sure we don't keep putting up more dialogs.
257 wxString errorMsg = m_errorMsg;
258 m_errorMsg = wxEmptyString;
259
260 wxWindow* topLevelParent = wxGetTopLevelParent( this );
261
262 DisplayErrorMessage( topLevelParent, errorMsg );
263
264 m_TextVars->SetFocus();
265 m_TextVars->MakeCellVisible( m_errorRow, m_errorCol );
266 m_TextVars->SetGridCursor( m_errorRow, m_errorCol );
267
268 m_TextVars->EnableCellEditControl( true );
269 m_TextVars->ShowCellEditControl();
270 }
271}
272
273
274void PANEL_TEXT_VARIABLES::OnGridSize( wxSizeEvent& event )
275{
276 m_gridWidthsDirty = true;
277
278 event.Skip();
279}
280
const char * name
Definition: DXF_plotter.cpp:57
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
This class works around a bug in wxGrid where the first keystroke doesn't get sent through the valida...
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
Class PANEL_TEXT_VARIABLES_BASE.
STD_BITMAP_BUTTON * m_btnDeleteTextVar
bool TransferDataToWindow() override
std::map< wxString, wxString > m_lastLoaded
void OnGridCellChanging(wxGridEvent &event)
void OnUpdateUI(wxUpdateUIEvent &event) override
void OnRemoveTextVar(wxCommandEvent &event) override
bool TransferDataFromWindow() override
void OnAddTextVar(wxCommandEvent &event) override
void OnGridSize(wxSizeEvent &event) override
void AppendTextVar(const wxString &aName, const wxString &aValue)
void OnGridCellChange(wxGridEvent &event) override
wxTextValidator m_nameValidator
PANEL_TEXT_VARIABLES(wxWindow *aParent, PROJECT *aProject)
Container for project specific data.
Definition: project.h:62
int GetTextVarsTicker() const
Definition: project.h:94
virtual std::map< wxString, wxString > & GetTextVars() const
Definition: project.cpp:84
void SetBitmap(const wxBitmapBundle &aBmp)
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:165
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:594
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:250
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
This file is part of the common library.
#define _(s)
@ TV_VALUE_COL
Custom text control validator definitions.