KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_setup_buses.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) 2018 CERN
5 * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Jon Evans <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <widgets/wx_grid.h>
24#include <confirm.h>
25#include <sch_edit_frame.h>
26#include <schematic.h>
28#include "grid_tricks.h"
29
31 PANEL_SETUP_BUSES_BASE( aWindow ),
32 m_frame( aFrame ),
33 m_lastAlias( 0 ),
34 m_membersGridDirty( false ),
35 m_errorGrid( nullptr ),
36 m_errorRow( -1 )
37{
39
40 m_addAlias->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
41 m_deleteAlias->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
42 m_addMember->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
43 m_removeMember->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
44
45 m_source->SetFont( KIUI::GetInfoFont( aWindow ) );
46
47 m_aliasesGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
48 m_membersGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
49
50 m_aliasesGrid->PushEventHandler( new GRID_TRICKS( m_aliasesGrid,
51 [this]( wxCommandEvent& aEvent )
52 {
53 OnAddAlias( aEvent );
54 } ) );
55
56 m_membersGrid->PushEventHandler( new GRID_TRICKS( m_membersGrid,
57 [this]( wxCommandEvent& aEvent )
58 {
59 OnAddMember( aEvent );
60 } ) );
61
62 // wxFormBuilder doesn't include this event...
63 m_aliasesGrid->Connect( wxEVT_GRID_CELL_CHANGING,
65 nullptr, this );
66 m_membersGrid->Connect( wxEVT_GRID_CELL_CHANGING,
68 nullptr, this );
69
70 Layout();
71}
72
73
75{
76 // Delete the GRID_TRICKS.
77 m_aliasesGrid->PopEventHandler( true );
78 m_membersGrid->PopEventHandler( true );
79
80 m_aliasesGrid->Disconnect( wxEVT_GRID_CELL_CHANGING,
82 nullptr, this );
83 m_membersGrid->Disconnect( wxEVT_GRID_CELL_CHANGING,
85 nullptr, this );
86}
87
89{
90 auto contains =
91 [&]( const std::shared_ptr<BUS_ALIAS>& alias ) -> bool
92 {
93 wxString aName = alias->GetName();
94 std::vector<wxString> aMembers = alias->Members();
95
96 std::sort( aMembers.begin(), aMembers.end() );
97
98 for( const std::shared_ptr<BUS_ALIAS>& candidate : m_aliases )
99 {
100 wxString bName = candidate->GetName();
101 std::vector<wxString> bMembers = candidate->Members();
102
103 std::sort( bMembers.begin(), bMembers.end() );
104
105 if( aName == bName && aMembers == bMembers )
106 return true;
107 }
108
109 return false;
110 };
111
112 SCH_SCREENS screens( m_frame->Schematic().Root() );
113
114 // collect aliases from each open sheet
115 for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
116 {
117 for( const std::shared_ptr<BUS_ALIAS>& alias : screen->GetBusAliases() )
118 {
119 if( !contains( alias ) )
120 m_aliases.push_back( alias->Clone() );
121 }
122 }
123
124 int ii = 0;
125
127 m_aliasesGrid->AppendRows( m_aliases.size() );
128
129 for( const std::shared_ptr<BUS_ALIAS>& alias : m_aliases )
130 m_aliasesGrid->SetCellValue( ii++, 0, alias->GetName() );
131
132 m_membersBook->SetSelection( 1 );
133
134 return true;
135}
136
137
139{
141 return false;
142
143 // Copy names back just in case they didn't get caught on the GridCellChanging event
144 for( int ii = 0; ii < m_aliasesGrid->GetNumberRows(); ++ii )
145 m_aliases[ii]->SetName( m_aliasesGrid->GetCellValue( ii, 0 ) );
146
147 SCH_SCREENS screens( m_frame->Schematic().Root() );
148
149 for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
150 screen->ClearBusAliases();
151
152 for( const std::shared_ptr<BUS_ALIAS>& alias : m_aliases )
153 alias->GetParent()->AddBusAlias( alias );
154
155 return true;
156}
157
158
159void PANEL_SETUP_BUSES::OnAddAlias( wxCommandEvent& aEvent )
160{
162 return;
163
164 // New aliases get stored on the currently visible sheet
165 m_aliases.push_back( std::make_shared<BUS_ALIAS>( m_frame->GetScreen() ) );
166
167 int row = m_aliasesGrid->GetNumberRows();
168 m_aliasesGrid->AppendRows();
169
170 m_aliasesGrid->MakeCellVisible( row, 0 );
171 m_aliasesGrid->SetGridCursor( row, 0 );
172
173 m_aliasesGrid->EnableCellEditControl( true );
174 m_aliasesGrid->ShowCellEditControl();
175}
176
177
178void PANEL_SETUP_BUSES::OnDeleteAlias( wxCommandEvent& aEvent )
179{
181 return;
182
183 int curRow = m_aliasesGrid->GetGridCursorRow();
184
185 if( curRow < 0 )
186 return;
187
188 // Clear the members grid first so we don't try to write it back to a deleted alias
190 m_lastAlias = -1;
191 m_lastAliasName = wxEmptyString;
192
193 m_aliases.erase( m_aliases.begin() + curRow );
194
195 m_aliasesGrid->DeleteRows( curRow, 1 );
196
197 if( m_aliasesGrid->GetNumberRows() > 0 )
198 {
199 m_aliasesGrid->MakeCellVisible( std::max( 0, curRow-1 ), 0 );
200 m_aliasesGrid->SelectRow( curRow-1 );
201 }
202}
203
204
205void PANEL_SETUP_BUSES::OnAddMember( wxCommandEvent& aEvent )
206{
208 return;
209
210 int row = m_membersGrid->GetNumberRows();
211 m_membersGrid->AppendRows();
212
213 m_membersGrid->MakeCellVisible( row, 0 );
214 m_membersGrid->SetGridCursor( row, 0 );
215
216 m_membersGrid->EnableCellEditControl( true );
217 m_membersGrid->ShowCellEditControl();
218}
219
220
221void PANEL_SETUP_BUSES::OnRemoveMember( wxCommandEvent& aEvent )
222{
224 return;
225
226 int curRow = m_membersGrid->GetGridCursorRow();
227
228 if( curRow < 0 )
229 return;
230
231 m_membersGrid->DeleteRows( curRow, 1 );
232
233 // Update the member list of the current bus alias from the members grid
234 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
235 alias->Members().clear();
236
237 for( int ii = 0; ii < m_membersGrid->GetNumberRows(); ++ii )
238 alias->Members().push_back( m_membersGrid->GetCellValue( ii, 0 ) );
239
240 if( m_membersGrid->GetNumberRows() > 0 )
241 {
242 m_membersGrid->MakeCellVisible( std::max( 0, curRow-1 ), 0 );
243 m_membersGrid->SelectRow( curRow-1 );
244 }
245}
246
247
249{
250 int row = event.GetRow();
251
252 if( row >= 0 )
253 {
254 wxString name = event.GetString();
255
256 for( int ii = 0; ii < m_aliasesGrid->GetNumberRows(); ++ii )
257 {
258 if( ii == event.GetRow() )
259 continue;
260
261 if( name == m_aliasesGrid->GetCellValue( ii, 0 )
262 && m_aliases[ row ]->GetParent() == m_aliases[ ii ]->GetParent() )
263 {
264 m_errorMsg = wxString::Format( _( "Alias name '%s' already in use." ), name );
266 m_errorRow = row;
267
268 event.Veto();
269 return;
270 }
271 }
272
273 m_aliases[ row ]->SetName( name );
274 }
275}
276
277
279{
280 int row = event.GetRow();
281
282 if( row >= 0 )
283 {
284 wxString name = event.GetString();
285
286 if( name.IsEmpty() )
287 {
288 m_errorMsg = _( "Member net/alias name cannot be empty." );
290 m_errorRow = event.GetRow();
291
292 event.Veto();
293 return;
294 }
295
296 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
297
298 alias->Members().clear();
299
300 for( int ii = 0; ii < m_membersGrid->GetNumberRows(); ++ii )
301 {
302 if( ii == row )
303 {
304 // Parse a space-separated list and add each one
305 wxStringTokenizer tok( name, " " );
306
307 if( tok.CountTokens() > 1 )
308 {
309 m_membersGridDirty = true;
310 Bind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
311 }
312
313 while( tok.HasMoreTokens() )
314 alias->Members().push_back( tok.GetNextToken() );
315 }
316 else
317 {
318 alias->Members().push_back( m_membersGrid->GetCellValue( ii, 0 ) );
319 }
320 }
321 }
322}
323
324
326{
327 if( m_lastAlias >= 0 && m_lastAlias < m_aliasesGrid->GetNumberRows() )
328 {
329 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
330 wxString source;
331 wxString membersLabel;
332
333 if( alias->GetParent() )
334 {
335 wxFileName sheet_name( alias->GetParent()->GetFileName() );
336 source.Printf( wxS( "(" ) + sheet_name.GetFullName() + wxS( ")" ) );
337 }
338
339 membersLabel.Printf( m_membersLabelTemplate, m_lastAliasName );
340
341 m_source->SetLabel( source );
342 m_membersLabel->SetLabel( membersLabel );
343
345 m_membersGrid->AppendRows( alias->Members().size() );
346
347 int ii = 0;
348
349 for( const wxString& member : alias->Members() )
350 m_membersGrid->SetCellValue( ii++, 0, member );
351 }
352
353 m_membersGridDirty = false;
354}
355
356
358{
361
362 Unbind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
363}
364
365
366
367void PANEL_SETUP_BUSES::OnSizeGrid( wxSizeEvent& event )
368{
369 auto setColSize =
370 []( WX_GRID* grid )
371 {
372 int colSize = std::max( grid->GetClientSize().x, grid->GetVisibleWidth( 0 ) );
373
374 if( grid->GetColSize( 0 ) != colSize )
375 grid->SetColSize( 0, colSize );
376 };
377
378 setColSize( m_aliasesGrid );
379 setColSize( m_membersGrid );
380
381 // Always propagate for a grid repaint (needed if the height changes, as well as width)
382 event.Skip();
383}
384
385
386void PANEL_SETUP_BUSES::OnUpdateUI( wxUpdateUIEvent& event )
387{
388 // Handle a grid error. This is delayed to OnUpdateUI so that we can change focus
389 // even when the original validation was triggered from a killFocus event.
390 if( !m_errorMsg.IsEmpty() )
391 {
392 // We will re-enter this routine when the error dialog is displayed, so make
393 // sure we don't keep putting up more dialogs.
394 wxString errorMsg = m_errorMsg;
395 m_errorMsg = wxEmptyString;
396
397 DisplayErrorMessage( this, errorMsg );
398
399 m_errorGrid->SetFocus();
400 m_errorGrid->MakeCellVisible( m_errorRow, 0 );
401 m_errorGrid->SetGridCursor( m_errorRow, 0 );
402
403 m_errorGrid->EnableCellEditControl( true );
404 m_errorGrid->ShowCellEditControl();
405
406 return;
407 }
408
409 if( !m_membersGrid->IsCellEditControlShown() )
410 {
411 int row = -1;
412 wxString aliasName;
413
414 if( m_aliasesGrid->IsCellEditControlShown() )
415 {
416 row = m_aliasesGrid->GetGridCursorRow();
417 wxGridCellEditor* cellEditor = m_aliasesGrid->GetCellEditor( row, 0 );
418
419 if( wxTextEntry* txt = dynamic_cast<wxTextEntry*>( cellEditor->GetControl() ) )
420 aliasName = txt->GetValue();
421
422 cellEditor->DecRef();
423 }
424 else if( m_aliasesGrid->GetGridCursorRow() >= 0 )
425 {
426 row = m_aliasesGrid->GetGridCursorRow();
427 aliasName = m_aliasesGrid->GetCellValue( row, 0 );
428 }
429 else if( m_lastAlias >= 0 && m_lastAlias < m_aliasesGrid->GetNumberRows() )
430 {
431 row = m_lastAlias;
432 aliasName = m_lastAliasName;
433 }
434
435 if( row < 0 )
436 {
437 m_membersBook->SetSelection( 1 );
438 }
439 else if( row != m_lastAlias || aliasName != m_lastAliasName )
440 {
441 m_lastAlias = row;
442 m_lastAliasName = aliasName;
443
444 m_membersBook->SetSelection( 0 );
445 m_membersBook->GetPage( 0 )->Layout();
446
447 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ row ];
448 alias->SetName( aliasName );
449
450 m_membersGridDirty = true;
451 Bind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
452 }
453 }
454}
455
456
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
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
Class PANEL_SETUP_BUSES_BASE.
STD_BITMAP_BUTTON * m_addAlias
STD_BITMAP_BUTTON * m_addMember
STD_BITMAP_BUTTON * m_deleteAlias
STD_BITMAP_BUTTON * m_removeMember
void OnAliasesGridCellChanging(wxGridEvent &event)
std::vector< std::shared_ptr< BUS_ALIAS > > m_aliases
void reloadMembersGridOnIdle(wxIdleEvent &aEvent)
void OnDeleteAlias(wxCommandEvent &aEvent) override
void OnUpdateUI(wxUpdateUIEvent &event) override
SCH_EDIT_FRAME * m_frame
void OnSizeGrid(wxSizeEvent &event) override
PANEL_SETUP_BUSES(wxWindow *aWindow, SCH_EDIT_FRAME *aFrame)
bool TransferDataToWindow() override
wxString m_membersLabelTemplate
void OnRemoveMember(wxCommandEvent &aEvent) override
void OnMemberGridCellChanging(wxGridEvent &event)
bool TransferDataFromWindow() override
void OnAddMember(wxCommandEvent &aEvent) override
void OnAddAlias(wxCommandEvent &aEvent) override
SCH_SHEET & Root() const
Definition: schematic.h:105
Schematic editor (Eeschema) main window.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
SCHEMATIC & Schematic() const
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:673
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
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
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)
wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:160