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-2024 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( KiBitmapBundle( BITMAPS::small_plus ) );
41 m_deleteAlias->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
42 m_addMember->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
43 m_removeMember->SetBitmap( KiBitmapBundle( 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 m_aliasesGrid->SetUseNativeColLabels();
63 m_membersGrid->SetUseNativeColLabels();
64
65 // wxFormBuilder doesn't include this event...
66 m_aliasesGrid->Connect( wxEVT_GRID_CELL_CHANGING,
68 nullptr, this );
69 m_membersGrid->Connect( wxEVT_GRID_CELL_CHANGING,
71 nullptr, this );
72
73 Layout();
74}
75
76
78{
79 // Delete the GRID_TRICKS.
80 m_aliasesGrid->PopEventHandler( true );
81 m_membersGrid->PopEventHandler( true );
82
83 m_aliasesGrid->Disconnect( wxEVT_GRID_CELL_CHANGING,
85 nullptr, this );
86 m_membersGrid->Disconnect( wxEVT_GRID_CELL_CHANGING,
88 nullptr, this );
89}
90
92{
93 auto contains =
94 [&]( const std::shared_ptr<BUS_ALIAS>& alias ) -> bool
95 {
96 wxString aName = alias->GetName();
97 std::vector<wxString> aMembers = alias->Members();
98
99 std::sort( aMembers.begin(), aMembers.end() );
100
101 for( const std::shared_ptr<BUS_ALIAS>& candidate : m_aliases )
102 {
103 wxString bName = candidate->GetName();
104 std::vector<wxString> bMembers = candidate->Members();
105
106 std::sort( bMembers.begin(), bMembers.end() );
107
108 if( aName == bName && aMembers == bMembers )
109 return true;
110 }
111
112 return false;
113 };
114
115 SCH_SCREENS screens( m_frame->Schematic().Root() );
116
117 // collect aliases from each open sheet
118 for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
119 {
120 for( const std::shared_ptr<BUS_ALIAS>& alias : screen->GetBusAliases() )
121 {
122 if( !contains( alias ) )
123 m_aliases.push_back( alias->Clone() );
124 }
125 }
126
127 int ii = 0;
128
130 m_aliasesGrid->AppendRows( m_aliases.size() );
131
132 for( const std::shared_ptr<BUS_ALIAS>& alias : m_aliases )
133 m_aliasesGrid->SetCellValue( ii++, 0, alias->GetName() );
134
135 m_membersBook->SetSelection( 1 );
136
137 return true;
138}
139
140
142{
144 return false;
145
146 // Copy names back just in case they didn't get caught on the GridCellChanging event
147 for( int ii = 0; ii < m_aliasesGrid->GetNumberRows(); ++ii )
148 m_aliases[ii]->SetName( m_aliasesGrid->GetCellValue( ii, 0 ) );
149
150 SCH_SCREENS screens( m_frame->Schematic().Root() );
151
152 for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
153 screen->ClearBusAliases();
154
155 for( const std::shared_ptr<BUS_ALIAS>& alias : m_aliases )
156 alias->GetParent()->AddBusAlias( alias );
157
158 return true;
159}
160
161
162void PANEL_SETUP_BUSES::OnAddAlias( wxCommandEvent& aEvent )
163{
165 return;
166
167 // New aliases get stored on the currently visible sheet
168 m_aliases.push_back( std::make_shared<BUS_ALIAS>( m_frame->GetScreen() ) );
169
170 int row = m_aliasesGrid->GetNumberRows();
171 m_aliasesGrid->AppendRows();
172
173 m_aliasesGrid->MakeCellVisible( row, 0 );
174 m_aliasesGrid->SetGridCursor( row, 0 );
175
176 m_aliasesGrid->EnableCellEditControl( true );
177 m_aliasesGrid->ShowCellEditControl();
178}
179
180
181void PANEL_SETUP_BUSES::OnDeleteAlias( wxCommandEvent& aEvent )
182{
184 return;
185
186 int curRow = m_aliasesGrid->GetGridCursorRow();
187
188 if( curRow < 0 )
189 return;
190
191 // Clear the members grid first so we don't try to write it back to a deleted alias
193 m_lastAlias = -1;
194 m_lastAliasName = wxEmptyString;
195
196 m_aliases.erase( m_aliases.begin() + curRow );
197
198 m_aliasesGrid->DeleteRows( curRow, 1 );
199
200 if( m_aliasesGrid->GetNumberRows() > 0 )
201 {
202 m_aliasesGrid->MakeCellVisible( std::max( 0, curRow-1 ), 0 );
203 m_aliasesGrid->SelectRow( std::max( 0, curRow-1 ) );
204 }
205}
206
207
208void PANEL_SETUP_BUSES::OnAddMember( wxCommandEvent& aEvent )
209{
211 return;
212
213 int row = m_membersGrid->GetNumberRows();
214 m_membersGrid->AppendRows();
215
216 m_membersGrid->MakeCellVisible( row, 0 );
217 m_membersGrid->SetGridCursor( row, 0 );
218
219 m_membersGrid->EnableCellEditControl( true );
220 m_membersGrid->ShowCellEditControl();
221}
222
223
224void PANEL_SETUP_BUSES::OnRemoveMember( wxCommandEvent& aEvent )
225{
227 return;
228
229 int curRow = m_membersGrid->GetGridCursorRow();
230
231 if( curRow < 0 )
232 return;
233
234 m_membersGrid->DeleteRows( curRow, 1 );
235
236 // Update the member list of the current bus alias from the members grid
237 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
238 alias->Members().clear();
239
240 for( int ii = 0; ii < m_membersGrid->GetNumberRows(); ++ii )
241 alias->Members().push_back( m_membersGrid->GetCellValue( ii, 0 ) );
242
243 if( m_membersGrid->GetNumberRows() > 0 )
244 {
245 m_membersGrid->MakeCellVisible( std::max( 0, curRow-1 ), 0 );
246 m_membersGrid->SelectRow( std::max( 0, curRow-1 ) );
247 }
248}
249
250
252{
253 int row = event.GetRow();
254
255 if( row >= 0 )
256 {
257 wxString name = event.GetString();
258
259 for( int ii = 0; ii < m_aliasesGrid->GetNumberRows(); ++ii )
260 {
261 if( ii == event.GetRow() )
262 continue;
263
264 if( name == m_aliasesGrid->GetCellValue( ii, 0 )
265 && m_aliases[ row ]->GetParent() == m_aliases[ ii ]->GetParent() )
266 {
267 m_errorMsg = wxString::Format( _( "Alias name '%s' already in use." ), name );
269 m_errorRow = row;
270
271 event.Veto();
272 return;
273 }
274 }
275
276 m_aliases[ row ]->SetName( name );
277 }
278}
279
280
282{
283 int row = event.GetRow();
284
285 if( row >= 0 )
286 {
287 wxString name = event.GetString();
288
289 if( name.IsEmpty() )
290 {
291 m_errorMsg = _( "Member net/alias name cannot be empty." );
293 m_errorRow = event.GetRow();
294
295 event.Veto();
296 return;
297 }
298
299 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
300
301 alias->Members().clear();
302
303 for( int ii = 0; ii < m_membersGrid->GetNumberRows(); ++ii )
304 {
305 if( ii == row )
306 {
307 // Parse a space-separated list and add each one
308 wxStringTokenizer tok( name, " " );
309
310 if( tok.CountTokens() > 1 )
311 {
312 m_membersGridDirty = true;
313 Bind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
314 }
315
316 while( tok.HasMoreTokens() )
317 alias->Members().push_back( tok.GetNextToken() );
318 }
319 else
320 {
321 alias->Members().push_back( m_membersGrid->GetCellValue( ii, 0 ) );
322 }
323 }
324 }
325}
326
327
329{
330 if( m_lastAlias >= 0 && m_lastAlias < m_aliasesGrid->GetNumberRows() )
331 {
332 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
333 wxString source;
334 wxString membersLabel;
335
336 if( alias->GetParent() )
337 {
338 wxFileName sheet_name( alias->GetParent()->GetFileName() );
339 source.Printf( wxS( "(" ) + sheet_name.GetFullName() + wxS( ")" ) );
340 }
341
342 membersLabel.Printf( m_membersLabelTemplate, m_lastAliasName );
343
344 m_source->SetLabel( source );
345 m_membersLabel->SetLabel( membersLabel );
346
348 m_membersGrid->AppendRows( alias->Members().size() );
349
350 int ii = 0;
351
352 for( const wxString& member : alias->Members() )
353 m_membersGrid->SetCellValue( ii++, 0, member );
354 }
355
356 m_membersGridDirty = false;
357}
358
359
361{
364
365 Unbind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
366}
367
368
369
370void PANEL_SETUP_BUSES::OnSizeGrid( wxSizeEvent& event )
371{
372 auto setColSize =
373 []( WX_GRID* grid )
374 {
375 int colSize = std::max( grid->GetClientSize().x, grid->GetVisibleWidth( 0 ) );
376
377 if( grid->GetColSize( 0 ) != colSize )
378 grid->SetColSize( 0, colSize );
379 };
380
381 setColSize( m_aliasesGrid );
382 setColSize( m_membersGrid );
383
384 // Always propagate for a grid repaint (needed if the height changes, as well as width)
385 event.Skip();
386}
387
388
389void PANEL_SETUP_BUSES::OnUpdateUI( wxUpdateUIEvent& event )
390{
391 // Handle a grid error. This is delayed to OnUpdateUI so that we can change focus
392 // even when the original validation was triggered from a killFocus event.
393 if( !m_errorMsg.IsEmpty() )
394 {
395 // We will re-enter this routine when the error dialog is displayed, so make
396 // sure we don't keep putting up more dialogs.
397 wxString errorMsg = m_errorMsg;
398 m_errorMsg = wxEmptyString;
399
400 wxWindow* topLevelParent = wxGetTopLevelParent( this );
401
402 DisplayErrorMessage( topLevelParent, errorMsg );
403
404 m_errorGrid->SetFocus();
405 m_errorGrid->MakeCellVisible( m_errorRow, 0 );
406 m_errorGrid->SetGridCursor( m_errorRow, 0 );
407
408 m_errorGrid->EnableCellEditControl( true );
409 m_errorGrid->ShowCellEditControl();
410
411 return;
412 }
413
414 if( !m_membersGrid->IsCellEditControlShown() )
415 {
416 int row = -1;
417 wxString aliasName;
418
419 if( m_aliasesGrid->IsCellEditControlShown() )
420 {
421 row = m_aliasesGrid->GetGridCursorRow();
422 wxGridCellEditor* cellEditor = m_aliasesGrid->GetCellEditor( row, 0 );
423
424 if( wxTextEntry* txt = dynamic_cast<wxTextEntry*>( cellEditor->GetControl() ) )
425 aliasName = txt->GetValue();
426
427 cellEditor->DecRef();
428 }
429 else if( m_aliasesGrid->GetGridCursorRow() >= 0 )
430 {
431 row = m_aliasesGrid->GetGridCursorRow();
432 aliasName = m_aliasesGrid->GetCellValue( row, 0 );
433 }
434 else if( m_lastAlias >= 0 && m_lastAlias < m_aliasesGrid->GetNumberRows() )
435 {
436 row = m_lastAlias;
437 aliasName = m_lastAliasName;
438 }
439
440 if( row < 0 )
441 {
442 m_membersBook->SetSelection( 1 );
443 }
444 else if( row != m_lastAlias || aliasName != m_lastAliasName )
445 {
446 m_lastAlias = row;
447 m_lastAliasName = aliasName;
448
449 m_membersBook->SetSelection( 0 );
450 m_membersBook->GetPage( 0 )->Layout();
451
452 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ row ];
453 alias->SetName( aliasName );
454
455 m_membersGridDirty = true;
456 Bind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
457 }
458 }
459}
460
461
const char * name
Definition: DXF_plotter.cpp:57
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
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:703
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:165
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:590
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)
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:154