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>
32
33#include <algorithm>
34
36{
39};
40
41
42PANEL_TEXT_VARIABLES::PANEL_TEXT_VARIABLES( wxWindow* aParent, PROJECT* aProject ) :
44 m_project( aProject ),
45 m_lastCheckedTicker( 0 ),
46 m_errorRow( -1 ), m_errorCol( -1 ),
47 m_gridWidthsDirty( true )
48{
49 m_btnAddTextVar->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
50 m_btnDeleteTextVar->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
51
53 m_TextVars->SetUseNativeColLabels();
54
55 // prohibit these characters in the alias names: []{}()%~<>"='`;:.,&?/\|$
56 m_nameValidator.SetStyle( wxFILTER_EXCLUDE_CHAR_LIST );
57 m_nameValidator.SetCharExcludes( wxT( "{}[]()%~<>\"='`;:.,&?/\\|$" ) );
58
59 m_TextVars->PushEventHandler( new GRID_TRICKS( m_TextVars, [this]( wxCommandEvent& aEvent )
60 {
61 OnAddTextVar( aEvent );
62 } ) );
63 m_TextVars->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
64
65 // wxFormBuilder doesn't include this event...
66 m_TextVars->Connect( wxEVT_GRID_CELL_CHANGING,
67 wxGridEventHandler( PANEL_TEXT_VARIABLES::OnGridCellChanging ),
68 nullptr, this );
69
70 Bind( wxEVT_IDLE,
71 [this]( wxIdleEvent& aEvent )
72 {
73 // Careful of consuming CPU in an idle event handler. Check the ticker first to
74 // see if there's even a possibility of the text variables having changed.
76 {
77 wxWindow* dialog = wxGetTopLevelParent( this );
78 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
79
80 if( topLevelFocus == dialog && m_lastLoaded != m_project->GetTextVars() )
81 checkReload();
82 }
83 } );
84}
85
86
88{
89 // Delete the GRID_TRICKS.
90 m_TextVars->PopEventHandler( true );
91
92 m_TextVars->Disconnect( wxEVT_GRID_CELL_CHANGING,
93 wxGridEventHandler( PANEL_TEXT_VARIABLES::OnGridCellChanging ),
94 nullptr, this );
95}
96
97
99{
100 // MUST update the ticker before calling IsOK (or we'll end up re-entering through the idle
101 // event until we crash the stack).
103
104 if( IsOK( m_parent, _( "The text variables have been changed outside the Setup dialog.\n"
105 "Do you wish to reload them?" ) ) )
106 {
108
110
111 for( const auto& var : m_lastLoaded )
112 AppendTextVar( var.first, var.second );
113 }
114}
115
116
118{
121
122 for( const auto& var : m_lastLoaded )
123 AppendTextVar( var.first, var.second );
124
125 return true;
126}
127
128
129void PANEL_TEXT_VARIABLES::AppendTextVar( const wxString& aName, const wxString& aValue )
130{
131 int i = m_TextVars->GetNumberRows();
132
133 m_TextVars->AppendRows( 1 );
134
135 m_TextVars->SetCellValue( i, TV_NAME_COL, aName );
136
137 wxGridCellAttr* nameCellAttr = m_TextVars->GetOrCreateCellAttr( i, TV_NAME_COL );
138 wxGridCellTextEditor* nameTextEditor = new GRID_CELL_TEXT_EDITOR();
139 nameTextEditor->SetValidator( m_nameValidator );
140 nameCellAttr->SetEditor( nameTextEditor );
141 nameCellAttr->DecRef();
142
143 m_TextVars->SetCellValue( i, TV_VALUE_COL, aValue );
144}
145
146
148{
150 return false;
151
152 for( int row = 0; row < m_TextVars->GetNumberRows(); ++row )
153 {
154 if( m_TextVars->GetCellValue( row, TV_NAME_COL ).IsEmpty() )
155 {
156 m_errorRow = row;
158 m_errorMsg = _( "Variable name cannot be empty." );
159 return false;
160 }
161 }
162
163 std::map<wxString, wxString>& variables = m_project->GetTextVars();
164
165 variables.clear();
166
167 for( int row = 0; row < m_TextVars->GetNumberRows(); ++row )
168 {
169 wxString name = m_TextVars->GetCellValue( row, TV_NAME_COL );
170 wxString value = m_TextVars->GetCellValue( row, TV_VALUE_COL );
171 variables[ name ] = value;
172 }
173
174 return true;
175}
176
177
179{
180 int row = event.GetRow();
181 int col = event.GetCol();
182 wxString text = event.GetString();
183
184 if( text.IsEmpty() && col == TV_NAME_COL )
185 {
186 m_errorMsg = _( "Variable name cannot be empty." );
187 m_errorRow = row;
188 m_errorCol = col;
189
190 event.Veto();
191 }
192}
193
194
195void PANEL_TEXT_VARIABLES::OnAddTextVar( wxCommandEvent& event )
196{
198 return;
199
200 AppendTextVar( wxEmptyString, wxEmptyString );
201
202 m_TextVars->MakeCellVisible( m_TextVars->GetNumberRows() - 1, TV_NAME_COL );
203 m_TextVars->SetGridCursor( m_TextVars->GetNumberRows() - 1, TV_NAME_COL );
204
205 m_TextVars->EnableCellEditControl( true );
206 m_TextVars->ShowCellEditControl();
207}
208
209
210void PANEL_TEXT_VARIABLES::OnRemoveTextVar( wxCommandEvent& event )
211{
212 int curRow = m_TextVars->GetGridCursorRow();
213
214 if( curRow < 0 || m_TextVars->GetNumberRows() <= curRow )
215 return;
216
217 m_TextVars->CommitPendingChanges( true /* silent mode; we don't care if it's valid */ );
218 m_TextVars->DeleteRows( curRow, 1 );
219
220 m_TextVars->MakeCellVisible( std::max( 0, curRow-1 ), m_TextVars->GetGridCursorCol() );
221 m_TextVars->SetGridCursor( std::max( 0, curRow-1 ), m_TextVars->GetGridCursorCol() );
222}
223
224
225void PANEL_TEXT_VARIABLES::OnGridCellChange( wxGridEvent& aEvent )
226{
227 m_gridWidthsDirty = true;
228
229 aEvent.Skip();
230}
231
232
233void PANEL_TEXT_VARIABLES::OnUpdateUI( wxUpdateUIEvent& event )
234{
236 {
237 int width = m_TextVars->GetClientRect().GetWidth();
238
239 m_TextVars->AutoSizeColumn( TV_NAME_COL );
240 m_TextVars->SetColSize( TV_NAME_COL,
241 std::max( 72, m_TextVars->GetColSize( TV_NAME_COL ) ) );
242
243 m_TextVars->SetColSize( TV_VALUE_COL,
244 std::max( 120, width - m_TextVars->GetColSize( TV_NAME_COL ) ) );
245 m_gridWidthsDirty = false;
246 }
247
248 // Handle a grid error. This is delayed to OnUpdateUI so that we can change focus
249 // even when the original validation was triggered from a killFocus event (and for
250 // dialog with notebooks, so that the corresponding notebook page can be shown in
251 // the background when triggered from an OK).
252 if( !m_errorMsg.IsEmpty() )
253 {
254 // We will re-enter this routine when the error dialog is displayed, so make
255 // sure we don't keep putting up more dialogs.
256 wxString errorMsg = m_errorMsg;
257 m_errorMsg = wxEmptyString;
258
259 wxWindow* topLevelParent = wxGetTopLevelParent( this );
260
261 DisplayErrorMessage( topLevelParent, errorMsg );
262
263 m_TextVars->SetFocus();
264 m_TextVars->MakeCellVisible( m_errorRow, m_errorCol );
265 m_TextVars->SetGridCursor( m_errorRow, m_errorCol );
266
267 m_TextVars->EnableCellEditControl( true );
268 m_TextVars->ShowCellEditControl();
269 }
270}
271
272
273void PANEL_TEXT_VARIABLES::OnGridSize( wxSizeEvent& event )
274{
275 m_gridWidthsDirty = true;
276
277 event.Skip();
278}
279
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...
Definition: validators.h:58
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:147
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:462
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:360
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:305
This file is part of the common library.
#define _(s)
@ TV_VALUE_COL
Custom text control validator definitions.