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 <algorithm>
27
28#include <bitmaps.h>
29#include <confirm.h>
30#include <validators.h>
31#include <project.h>
32#include <grid_tricks.h>
36
37
39{
42};
43
44
45PANEL_TEXT_VARIABLES::PANEL_TEXT_VARIABLES( wxWindow* aParent, PROJECT* aProject ) :
46 PANEL_TEXT_VARIABLES_BASE( aParent ), m_project( aProject ), m_lastCheckedTicker( 0 ),
47 m_errorRow( -1 ), m_errorCol( -1 )
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 m_autoSizer = std::make_unique<WX_GRID_AUTOSIZER>( *m_TextVars,
87 { TV_NAME_COL, 72 },
88 { TV_VALUE_COL, 120 },
89 },
91}
92
93
95{
96 // Delete the GRID_TRICKS.
97 m_TextVars->PopEventHandler( true );
98
99 m_TextVars->Disconnect( wxEVT_GRID_CELL_CHANGING,
100 wxGridEventHandler( PANEL_TEXT_VARIABLES::OnGridCellChanging ),
101 nullptr, this );
102}
103
104
106{
107 // MUST update the ticker before calling IsOK (or we'll end up re-entering through the idle
108 // event until we crash the stack).
110
111 if( IsOK( m_parent, _( "The text variables have been changed outside the Setup dialog.\n"
112 "Do you wish to reload them?" ) ) )
113 {
115
117
118 for( const auto& var : m_lastLoaded )
119 AppendTextVar( var.first, var.second );
120 }
121}
122
123
125{
128
129 for( const auto& var : m_lastLoaded )
130 AppendTextVar( var.first, var.second );
131
132 return true;
133}
134
135
136void PANEL_TEXT_VARIABLES::AppendTextVar( const wxString& aName, const wxString& aValue )
137{
138 int i = m_TextVars->GetNumberRows();
139
140 m_TextVars->AppendRows( 1 );
141
142 m_TextVars->SetCellValue( i, TV_NAME_COL, aName );
143
144 wxGridCellAttr* nameCellAttr = m_TextVars->GetOrCreateCellAttr( i, TV_NAME_COL );
145 wxGridCellTextEditor* nameTextEditor = new GRID_CELL_TEXT_EDITOR();
146 nameTextEditor->SetValidator( m_nameValidator );
147 nameCellAttr->SetEditor( nameTextEditor );
148 nameCellAttr->DecRef();
149
150 m_TextVars->SetCellValue( i, TV_VALUE_COL, aValue );
151}
152
153
155{
157 return false;
158
159 for( int row = 0; row < m_TextVars->GetNumberRows(); ++row )
160 {
161 if( m_TextVars->GetCellValue( row, TV_NAME_COL ).IsEmpty() )
162 {
163 m_errorRow = row;
165 m_errorMsg = _( "Variable name cannot be empty." );
166 return false;
167 }
168 }
169
170 std::map<wxString, wxString>& variables = m_project->GetTextVars();
171
172 variables.clear();
173
174 for( int row = 0; row < m_TextVars->GetNumberRows(); ++row )
175 {
176 wxString name = m_TextVars->GetCellValue( row, TV_NAME_COL );
177 wxString value = m_TextVars->GetCellValue( row, TV_VALUE_COL );
178 variables[ name ] = value;
179 }
180
181 return true;
182}
183
184
186{
187 int row = event.GetRow();
188 int col = event.GetCol();
189 wxString text = event.GetString();
190
191 if( text.IsEmpty() && col == TV_NAME_COL )
192 {
193 m_errorMsg = _( "Variable name cannot be empty." );
194 m_errorRow = row;
195 m_errorCol = col;
196
197 event.Veto();
198 }
199}
200
201
202void PANEL_TEXT_VARIABLES::OnAddTextVar( wxCommandEvent& event )
203{
205 return;
206
207 AppendTextVar( wxEmptyString, wxEmptyString );
208
209 m_TextVars->MakeCellVisible( m_TextVars->GetNumberRows() - 1, TV_NAME_COL );
210 m_TextVars->SetGridCursor( m_TextVars->GetNumberRows() - 1, TV_NAME_COL );
211
212 m_TextVars->EnableCellEditControl( true );
213 m_TextVars->ShowCellEditControl();
214}
215
216
217void PANEL_TEXT_VARIABLES::OnRemoveTextVar( wxCommandEvent& event )
218{
219 int curRow = m_TextVars->GetGridCursorRow();
220
221 if( curRow < 0 || m_TextVars->GetNumberRows() <= curRow )
222 return;
223
224 m_TextVars->CommitPendingChanges( true /* silent mode; we don't care if it's valid */ );
225 m_TextVars->DeleteRows( curRow, 1 );
226
227 m_TextVars->MakeCellVisible( std::max( 0, curRow-1 ), m_TextVars->GetGridCursorCol() );
228 m_TextVars->SetGridCursor( std::max( 0, curRow-1 ), m_TextVars->GetGridCursorCol() );
229}
230
231
232void PANEL_TEXT_VARIABLES::OnUpdateUI( wxUpdateUIEvent& event )
233{
234 // Handle a grid error. This is delayed to OnUpdateUI so that we can change focus
235 // even when the original validation was triggered from a killFocus event (and for
236 // dialog with notebooks, so that the corresponding notebook page can be shown in
237 // the background when triggered from an OK).
238 if( !m_errorMsg.IsEmpty() )
239 {
240 // We will re-enter this routine when the error dialog is displayed, so make
241 // sure we don't keep putting up more dialogs.
242 wxString errorMsg = m_errorMsg;
243 m_errorMsg = wxEmptyString;
244
245 wxWindow* topLevelParent = wxGetTopLevelParent( this );
246
247 DisplayErrorMessage( topLevelParent, errorMsg );
248
249 m_TextVars->SetFocus();
250 m_TextVars->MakeCellVisible( m_errorRow, m_errorCol );
251 m_TextVars->SetGridCursor( m_errorRow, m_errorCol );
252
253 m_TextVars->EnableCellEditControl( true );
254 m_TextVars->ShowCellEditControl();
255 }
256}
257
258
260{
261 // Fetch from other project...
262 m_lastLoaded = aOtherProject->GetTextVars();
263 // ... but use ticker from current project:
265
267
268 for( const auto& var : m_lastLoaded )
269 AppendTextVar( var.first, var.second );
270}
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
std::unique_ptr< WX_GRID_AUTOSIZER > m_autoSizer
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 AppendTextVar(const wxString &aName, const wxString &aValue)
wxTextValidator m_nameValidator
void ImportSettingsFrom(const PROJECT *aOtherProject)
PANEL_TEXT_VARIABLES(wxWindow *aParent, PROJECT *aProject)
Container for project specific data.
Definition: project.h:64
int GetTextVarsTicker() const
Definition: project.h:114
virtual std::map< wxString, wxString > & GetTextVars() const
Definition: project.cpp:84
void SetBitmap(const wxBitmapBundle &aBmp)
std::map< int, int > COL_MIN_WIDTHS
Map of column indices to minimum widths.
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:184
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:637
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.