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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <algorithm>
23
24#include <bitmaps.h>
25#include <confirm.h>
26#include <validators.h>
27#include <project.h>
28#include <grid_tricks.h>
31
32
38
39
40PANEL_TEXT_VARIABLES::PANEL_TEXT_VARIABLES( wxWindow* aParent, PROJECT* aProject ) :
42 m_project( aProject ),
44 m_errorRow( -1 ),
45 m_errorCol( -1 )
46{
49
50 m_TextVars->ClearRows();
51 m_TextVars->SetUseNativeColLabels();
52
53 // prohibit these characters in the alias names: []{}()%~<>"='`;:.,&?/\|$
54 m_nameValidator.SetStyle( wxFILTER_EXCLUDE_CHAR_LIST );
55 m_nameValidator.SetCharExcludes( wxT( "{}[]()%~<>\"='`;:.,&?/\\|$" ) );
56
57 m_TextVars->PushEventHandler( new GRID_TRICKS( m_TextVars, [this]( wxCommandEvent& aEvent )
58 {
59 OnAddTextVar( aEvent );
60 } ) );
61 m_TextVars->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
62
63 // wxFormBuilder doesn't include this event...
64 m_TextVars->Connect( wxEVT_GRID_CELL_CHANGING, wxGridEventHandler( PANEL_TEXT_VARIABLES::OnGridCellChanging ),
65 nullptr, this );
66
67 Bind( wxEVT_IDLE,
68 [this]( wxIdleEvent& aEvent )
69 {
70 // Careful of consuming CPU in an idle event handler. Check the ticker first to
71 // see if there's even a possibility of the text variables having changed.
72 if( m_project->GetTextVarsTicker() > m_lastCheckedTicker )
73 {
74 wxWindow* dialog = wxGetTopLevelParent( this );
75 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
76
77 if( topLevelFocus == dialog && m_lastLoaded != m_project->GetTextVars() )
79 }
80 } );
81
82 m_TextVars->SetupColumnAutosizer( TV_VALUE_COL );
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).
101 m_lastCheckedTicker = m_project->GetTextVarsTicker();
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 {
106 m_TextVars->ClearRows();
107
108 m_lastLoaded = m_project->GetTextVars();
109
110 for( const auto& var : m_lastLoaded )
111 AppendTextVar( var.first, var.second );
112 }
113}
114
115
117{
118 m_lastLoaded = m_project->GetTextVars();
119 m_lastCheckedTicker = m_project->GetTextVarsTicker();
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{
148 if( !m_TextVars->CommitPendingChanges() )
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{
196 m_TextVars->OnAddRow(
197 [&]() -> std::pair<int, int>
198 {
199 AppendTextVar( wxEmptyString, wxEmptyString );
200 return { m_TextVars->GetNumberRows() - 1, TV_NAME_COL };
201 } );
202}
203
204
205void PANEL_TEXT_VARIABLES::OnRemoveTextVar( wxCommandEvent& event )
206{
207 m_TextVars->OnDeleteRows(
208 [&]( int row )
209 {
210 m_TextVars->DeleteRows( row, 1 );
211 } );
212}
213
214
215void PANEL_TEXT_VARIABLES::OnUpdateUI( wxUpdateUIEvent& event )
216{
217 // Handle a grid error. This is delayed to OnUpdateUI so that we can change focus
218 // even when the original validation was triggered from a killFocus event (and for
219 // dialog with notebooks, so that the corresponding notebook page can be shown in
220 // the background when triggered from an OK).
221 if( !m_errorMsg.IsEmpty() )
222 {
223 // We will re-enter this routine when the error dialog is displayed, so make
224 // sure we don't keep putting up more dialogs.
225 wxString errorMsg = m_errorMsg;
226 m_errorMsg = wxEmptyString;
227
228 wxWindow* topLevelParent = wxGetTopLevelParent( this );
229
230 DisplayErrorMessage( topLevelParent, errorMsg );
231
232 m_TextVars->SetFocus();
233 m_TextVars->MakeCellVisible( m_errorRow, m_errorCol );
234 m_TextVars->SetGridCursor( m_errorRow, m_errorCol );
235
236 m_TextVars->EnableCellEditControl( true );
237 m_TextVars->ShowCellEditControl();
238 }
239
240 event.Skip();
241}
242
243
245{
246 // Fetch from other project...
247 m_lastLoaded = aOtherProject->GetTextVars();
248
249 // ... but use ticker from current project:
250 m_lastCheckedTicker = m_project->GetTextVarsTicker();
251
252 m_TextVars->ClearRows();
253
254 for( const auto& var : m_lastLoaded )
255 AppendTextVar( var.first, var.second );
256}
const char * name
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
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:57
PANEL_TEXT_VARIABLES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
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:62
virtual std::map< wxString, wxString > & GetTextVars() const
Definition project.cpp:126
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:274
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define _(s)
Custom text control validator definitions.