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