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 The 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#include <wx/clipbrd.h>
30
32 PANEL_SETUP_BUSES_BASE( aWindow ),
33 m_frame( aFrame ),
34 m_lastAlias( 0 ),
35 m_membersGridDirty( false ),
36 m_errorGrid( nullptr ),
37 m_errorRow( -1 )
38{
40
41 m_addAlias->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
42 m_deleteAlias->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
43 m_addMember->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
44 m_removeMember->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
45
46 m_source->SetFont( KIUI::GetSmallInfoFont( aWindow ) );
47
48 m_aliasesGrid->OverrideMinSize( 0.6, 0.3 );
49 m_membersGrid->OverrideMinSize( 0.6, 0.3 );
50 m_aliasesGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
51 m_membersGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
52
53 m_aliasesGrid->PushEventHandler( new GRID_TRICKS( m_aliasesGrid,
54 [this]( wxCommandEvent& aEvent )
55 {
56 OnAddAlias( aEvent );
57 } ) );
58
59 m_membersGrid->PushEventHandler( new GRID_TRICKS( m_membersGrid,
60 [this]( wxCommandEvent& aEvent )
61 {
62 OnAddMember( aEvent );
63 } ) );
64
65 m_aliasesGrid->SetUseNativeColLabels();
66 m_membersGrid->SetUseNativeColLabels();
67
68 // wxFormBuilder doesn't include this event...
69 m_aliasesGrid->Connect( wxEVT_GRID_CELL_CHANGING,
71 nullptr, this );
72 m_membersGrid->Connect( wxEVT_GRID_CELL_CHANGING,
74 nullptr, this );
75
76 Layout();
77}
78
79
81{
82 // Delete the GRID_TRICKS.
83 m_aliasesGrid->PopEventHandler( true );
84 m_membersGrid->PopEventHandler( true );
85
86 m_aliasesGrid->Disconnect( wxEVT_GRID_CELL_CHANGING,
88 nullptr, this );
89 m_membersGrid->Disconnect( wxEVT_GRID_CELL_CHANGING,
91 nullptr, this );
92}
93
94
96{
97 auto contains =
98 [&]( const std::shared_ptr<BUS_ALIAS>& alias ) -> bool
99 {
100 wxString aName = alias->GetName();
101 std::vector<wxString> aMembers = alias->Members();
102
103 std::sort( aMembers.begin(), aMembers.end() );
104
105 for( const std::shared_ptr<BUS_ALIAS>& candidate : m_aliases )
106 {
107 wxString bName = candidate->GetName();
108 std::vector<wxString> bMembers = candidate->Members();
109
110 std::sort( bMembers.begin(), bMembers.end() );
111
112 if( aName == bName && aMembers == bMembers )
113 return true;
114 }
115
116 return false;
117 };
118
119 SCH_SCREENS screens( aSchematic.Root() );
120
121 // collect aliases from each open sheet
122 for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
123 {
124 for( const std::shared_ptr<BUS_ALIAS>& alias : screen->GetBusAliases() )
125 {
126 if( !contains( alias ) )
127 m_aliases.push_back( alias->Clone() );
128 }
129 }
130
131 int ii = 0;
132
134 m_aliasesGrid->AppendRows( m_aliases.size() );
135
136 for( const std::shared_ptr<BUS_ALIAS>& alias : m_aliases )
137 m_aliasesGrid->SetCellValue( ii++, 0, alias->GetName() );
138
139 m_membersBook->SetSelection( 1 );
140}
141
142
144{
146 return true;
147}
148
149
151{
153 return false;
154
155 // Copy names back just in case they didn't get caught on the GridCellChanging event
156 for( int ii = 0; ii < m_aliasesGrid->GetNumberRows(); ++ii )
157 m_aliases[ii]->SetName( m_aliasesGrid->GetCellValue( ii, 0 ) );
158
159 // Associate the respective members with the last alias that is active.
161
162 SCH_SCREENS screens( m_frame->Schematic().Root() );
163
164 for( SCH_SCREEN* screen = screens.GetFirst(); screen != nullptr; screen = screens.GetNext() )
165 screen->ClearBusAliases();
166
167 for( const std::shared_ptr<BUS_ALIAS>& alias : m_aliases )
168 alias->GetParent()->AddBusAlias( alias );
169
170 return true;
171}
172
173
174void PANEL_SETUP_BUSES::OnAddAlias( wxCommandEvent& aEvent )
175{
177 return;
178
180 [&]() -> std::pair<int, int>
181 {
182 // New aliases get stored on the currently visible sheet
183 m_aliases.push_back( std::make_shared<BUS_ALIAS>( m_frame->GetScreen() ) );
184
185 int row = m_aliasesGrid->GetNumberRows();
186
187 // Associate the respective members with the previous alias. This ensures that the association
188 // starts correctly when adding more than one row.
189 // But in order to avoid overwriting the members of the last (row - 1) alias with those of the
190 // selected alias (since the user may choose a different alias), the alias member update here
191 // should only happen if the current alias (m_lastAlias) is the last one (row - 1).
192 if( ( row > 0 ) && ( m_lastAlias == ( row - 1 ) ) )
193 updateAliasMembers( row - 1 );
194
195 m_aliasesGrid->AppendRows();
196 return { row, 0 };
197 } );
198}
199
200
201void PANEL_SETUP_BUSES::OnDeleteAlias( wxCommandEvent& aEvent )
202{
204 return;
205
207 [&]( int row )
208 {
209 // Clear the members grid first so we don't try to write it back to a deleted alias
211 m_lastAlias = -1;
212 m_lastAliasName = wxEmptyString;
213
214 m_aliases.erase( m_aliases.begin() + row );
215
216 m_aliasesGrid->DeleteRows( row, 1 );
217 } );
218}
219
220
221void PANEL_SETUP_BUSES::OnAddMember( wxCommandEvent& aEvent )
222{
224 [&]() -> std::pair<int, int>
225 {
226 int row = m_membersGrid->GetNumberRows();
227 m_membersGrid->AppendRows();
228
229 /*
230 * Check if the clipboard contains text data.
231 *
232 * - If `clipboardHasText` is true, select the specified row in the members grid to allow
233 * our custom context menu to paste the clipboard .
234 * - Otherwise, enable and display the cell edit control, allowing the user to manually edit
235 * the cell.
236 */
237 bool clipboardHasText = false;
238
239 if( wxTheClipboard->Open() )
240 {
241 if( wxTheClipboard->IsSupported( wxDF_TEXT )
242 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
243 {
244 clipboardHasText = true;
245 }
246
247 wxTheClipboard->Close();
248 }
249
250 if( clipboardHasText )
251 return { row, -1 };
252 else
253 return { row, 0 };
254 } );
255}
256
257
258void PANEL_SETUP_BUSES::OnRemoveMember( wxCommandEvent& aEvent )
259{
261 [&]( int row )
262 {
263 m_membersGrid->DeleteRows( row, 1 );
264
265 // Update the member list of the current bus alias from the members grid
266 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
267 alias->Members().clear();
268
269 for( int ii = 0; ii < m_membersGrid->GetNumberRows(); ++ii )
270 alias->Members().push_back( m_membersGrid->GetCellValue( ii, 0 ) );
271 } );
272}
273
274
276{
277 int row = event.GetRow();
278
279 if( row >= 0 )
280 {
281 wxString name = event.GetString();
282
283 for( int ii = 0; ii < m_aliasesGrid->GetNumberRows(); ++ii )
284 {
285 if( ii == event.GetRow() )
286 continue;
287
288 if( name == m_aliasesGrid->GetCellValue( ii, 0 )
289 && m_aliases[ row ]->GetParent() == m_aliases[ ii ]->GetParent() )
290 {
291 m_errorMsg = wxString::Format( _( "Alias name '%s' already in use." ), name );
293 m_errorRow = row;
294
295 event.Veto();
296 return;
297 }
298 }
299
300 m_aliases[ row ]->SetName( name );
301 }
302}
303
304
306{
307 int row = event.GetRow();
308
309 if( row >= 0 )
310 {
311 wxString name = event.GetString();
312
313 if( name.IsEmpty() )
314 {
315 m_errorMsg = _( "Member net/alias name cannot be empty." );
317 m_errorRow = event.GetRow();
318
319 event.Veto();
320 return;
321 }
322
323 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
324
325 alias->Members().clear();
326
327 for( int ii = 0; ii < m_membersGrid->GetNumberRows(); ++ii )
328 {
329 if( ii == row )
330 {
331 // Parse a space-separated list and add each one
332 wxStringTokenizer tok( name, " " );
333
334 if( tok.CountTokens() > 1 )
335 {
336 m_membersGridDirty = true;
337 Bind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
338 }
339
340 while( tok.HasMoreTokens() )
341 alias->Members().push_back( tok.GetNextToken() );
342 }
343 else
344 {
345 alias->Members().push_back( m_membersGrid->GetCellValue( ii, 0 ) );
346 }
347 }
348 }
349}
350
351
353{
354 if( m_lastAlias >= 0 && m_lastAlias < m_aliasesGrid->GetNumberRows() )
355 {
356 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
357 wxString source;
358 wxString membersLabel;
359
360 if( alias->GetParent() )
361 {
362 wxFileName sheet_name( alias->GetParent()->GetFileName() );
363 source.Printf( wxS( "(" ) + sheet_name.GetFullName() + wxS( ")" ) );
364 }
365
366 membersLabel.Printf( m_membersLabelTemplate, m_lastAliasName );
367
368 m_source->SetLabel( source );
369 m_membersLabel->SetLabel( membersLabel );
370
372 m_membersGrid->AppendRows( alias->Members().size() );
373
374 int ii = 0;
375
376 for( const wxString& member : alias->Members() )
377 m_membersGrid->SetCellValue( ii++, 0, member );
378 }
379
380 m_membersGridDirty = false;
381}
382
383
385{
388
389 Unbind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
390}
391
392
393
394void PANEL_SETUP_BUSES::OnSizeGrid( wxSizeEvent& event )
395{
396 auto setColSize =
397 []( WX_GRID* grid )
398 {
399 int colSize = std::max( grid->GetClientSize().x, grid->GetVisibleWidth( 0 ) );
400
401 if( grid->GetColSize( 0 ) != colSize )
402 grid->SetColSize( 0, colSize );
403 };
404
405 setColSize( m_aliasesGrid );
406 setColSize( m_membersGrid );
407
408 // Always propagate for a grid repaint (needed if the height changes, as well as width)
409 event.Skip();
410}
411
412
413void PANEL_SETUP_BUSES::OnUpdateUI( wxUpdateUIEvent& event )
414{
415 // Handle a grid error. This is delayed to OnUpdateUI so that we can change focus
416 // even when the original validation was triggered from a killFocus event.
417 if( !m_errorMsg.IsEmpty() )
418 {
419 // We will re-enter this routine when the error dialog is displayed, so make
420 // sure we don't keep putting up more dialogs.
421 wxString errorMsg = m_errorMsg;
422 m_errorMsg = wxEmptyString;
423
424 wxWindow* topLevelParent = wxGetTopLevelParent( this );
425
426 DisplayErrorMessage( topLevelParent, errorMsg );
427
428 m_errorGrid->SetFocus();
429 m_errorGrid->MakeCellVisible( m_errorRow, 0 );
430 m_errorGrid->SetGridCursor( m_errorRow, 0 );
431
432 m_errorGrid->EnableCellEditControl( true );
433 m_errorGrid->ShowCellEditControl();
434
435 return;
436 }
437
438 if( !m_membersGrid->IsCellEditControlShown() )
439 {
440 int row = -1;
441 wxString aliasName;
442
443 if( m_aliasesGrid->IsCellEditControlShown() )
444 {
445 row = m_aliasesGrid->GetGridCursorRow();
446 wxGridCellEditor* cellEditor = m_aliasesGrid->GetCellEditor( row, 0 );
447
448 if( wxTextEntry* txt = dynamic_cast<wxTextEntry*>( cellEditor->GetControl() ) )
449 aliasName = txt->GetValue();
450
451 cellEditor->DecRef();
452 }
453 else if( m_aliasesGrid->GetGridCursorRow() >= 0 )
454 {
455 row = m_aliasesGrid->GetGridCursorRow();
456 aliasName = m_aliasesGrid->GetCellValue( row, 0 );
457 }
458 else if( m_lastAlias >= 0 && m_lastAlias < m_aliasesGrid->GetNumberRows() )
459 {
460 row = m_lastAlias;
461 aliasName = m_lastAliasName;
462 }
463
464 if( row < 0 )
465 {
466 m_membersBook->SetSelection( 1 );
467 }
468 else if( row != m_lastAlias || aliasName != m_lastAliasName )
469 {
470 m_lastAlias = row;
471 m_lastAliasName = aliasName;
472
473 m_membersBook->SetSelection( 0 );
474 m_membersBook->GetPage( 0 )->Layout();
475
476 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ row ];
477 alias->SetName( aliasName );
478
479 m_membersGridDirty = true;
480 Bind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
481 }
482 }
483}
484
485
487{
488 loadAliases( aOtherSchematic );
489
490 // New aliases get stored on the currently visible sheet
491 for( const std::shared_ptr<BUS_ALIAS>& alias : m_aliases )
492 alias->SetParent( m_frame->GetScreen() );
493}
494
495
497{
498 if( !m_aliases.empty() && m_membersGrid->GetNumberRows() > 0 && aAliasIndex >= 0
499 && aAliasIndex < (int)m_aliases.size() )
500 {
501 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[aAliasIndex];
502
503 alias->Members().clear();
504
505 for( int ii = 0; ii < m_membersGrid->GetNumberRows(); ++ii )
506 {
507 wxString memberValue = m_membersGrid->GetCellValue( ii, 0 );
508
509 if( !memberValue.empty() )
510 {
511 alias->Members().push_back( memberValue );
512 }
513 }
514 }
515}
const char * name
Definition: DXF_plotter.cpp:62
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
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
void loadAliases(const SCHEMATIC &aSchematic)
SCH_EDIT_FRAME * m_frame
void OnSizeGrid(wxSizeEvent &event) override
PANEL_SETUP_BUSES(wxWindow *aWindow, SCH_EDIT_FRAME *aFrame)
bool TransferDataToWindow() override
void ImportSettingsFrom(const SCHEMATIC &aOtherSchematic)
void updateAliasMembers(int aAliasIndex)
When rows are created programmatically by pasting values from the clipboard, the cell change event ma...
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
Holds all the data relating to one schematic.
Definition: schematic.h:88
SCH_SHEET & Root() const
Definition: schematic.h:140
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:758
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
void SetBitmap(const wxBitmapBundle &aBmp)
void OverrideMinSize(double aXPct, double aYPct)
Grids that have column sizes automatically set to fill the available width don't want to shrink after...
Definition: wx_grid.h:242
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition: wx_grid.cpp:704
void OnAddRow(const std::function< std::pair< int, int >()> &aAdder)
Definition: wx_grid.cpp:684
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:220
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:632
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:194
This file is part of the common library.
#define _(s)
KICOMMON_API wxFont GetSmallInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:162