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::GetInfoFont( 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
179 // New aliases get stored on the currently visible sheet
180 m_aliases.push_back( std::make_shared<BUS_ALIAS>( m_frame->GetScreen() ) );
181
182 int row = m_aliasesGrid->GetNumberRows();
183
184 // Associate the respective members with the previous alias. This ensures that the association
185 // starts correctly when adding more than one row.
186 if( row > 0 )
187 updateAliasMembers( row - 1 );
188
189 m_aliasesGrid->AppendRows();
190
191 m_aliasesGrid->MakeCellVisible( row, 0 );
192 m_aliasesGrid->SetGridCursor( row, 0 );
193
194 m_aliasesGrid->EnableCellEditControl( true );
195 m_aliasesGrid->ShowCellEditControl();
196}
197
198
199void PANEL_SETUP_BUSES::OnDeleteAlias( wxCommandEvent& aEvent )
200{
202 return;
203
204 int curRow = m_aliasesGrid->GetGridCursorRow();
205
206 if( curRow < 0 )
207 return;
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() + curRow );
215
216 m_aliasesGrid->DeleteRows( curRow, 1 );
217
218 if( m_aliasesGrid->GetNumberRows() > 0 )
219 {
220 m_aliasesGrid->MakeCellVisible( std::max( 0, curRow-1 ), 0 );
221 m_aliasesGrid->SelectRow( std::max( 0, curRow-1 ) );
222 }
223}
224
225
226void PANEL_SETUP_BUSES::OnAddMember( wxCommandEvent& aEvent )
227{
229 return;
230
231 int row = m_membersGrid->GetNumberRows();
232 m_membersGrid->AppendRows();
233
234 m_membersGrid->MakeCellVisible( row, 0 );
235 m_membersGrid->SetGridCursor( row, 0 );
236
237 /*
238 * Check if the clipboard contains text data.
239 *
240 * - If `clipboardHasText` is true, select the specified row in the members grid to allow
241 * our custom context menu to paste the clipboard .
242 * - Otherwise, enable and display the cell edit control, allowing the user to manually edit
243 * the cell.
244 */
245 bool clipboardHasText = false;
246
247 if( wxTheClipboard->Open() )
248 {
249 if( wxTheClipboard->IsSupported( wxDF_TEXT )
250 || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
251 {
252 clipboardHasText = true;
253 }
254
255 wxTheClipboard->Close();
256 }
257
258 if( clipboardHasText )
259 {
260 m_membersGrid->SelectRow( row );
261 }
262 else
263 {
264 m_membersGrid->EnableCellEditControl( true );
265 m_membersGrid->ShowCellEditControl();
266 }
267}
268
269
270void PANEL_SETUP_BUSES::OnRemoveMember( wxCommandEvent& aEvent )
271{
273 return;
274
275 int curRow = m_membersGrid->GetGridCursorRow();
276
277 if( curRow < 0 )
278 return;
279
280 m_membersGrid->DeleteRows( curRow, 1 );
281
282 // Update the member list of the current bus alias from the members grid
283 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
284 alias->Members().clear();
285
286 for( int ii = 0; ii < m_membersGrid->GetNumberRows(); ++ii )
287 alias->Members().push_back( m_membersGrid->GetCellValue( ii, 0 ) );
288
289 if( m_membersGrid->GetNumberRows() > 0 )
290 {
291 m_membersGrid->MakeCellVisible( std::max( 0, curRow-1 ), 0 );
292 m_membersGrid->SelectRow( std::max( 0, curRow-1 ) );
293 }
294}
295
296
298{
299 int row = event.GetRow();
300
301 if( row >= 0 )
302 {
303 wxString name = event.GetString();
304
305 for( int ii = 0; ii < m_aliasesGrid->GetNumberRows(); ++ii )
306 {
307 if( ii == event.GetRow() )
308 continue;
309
310 if( name == m_aliasesGrid->GetCellValue( ii, 0 )
311 && m_aliases[ row ]->GetParent() == m_aliases[ ii ]->GetParent() )
312 {
313 m_errorMsg = wxString::Format( _( "Alias name '%s' already in use." ), name );
315 m_errorRow = row;
316
317 event.Veto();
318 return;
319 }
320 }
321
322 m_aliases[ row ]->SetName( name );
323 }
324}
325
326
328{
329 int row = event.GetRow();
330
331 if( row >= 0 )
332 {
333 wxString name = event.GetString();
334
335 if( name.IsEmpty() )
336 {
337 m_errorMsg = _( "Member net/alias name cannot be empty." );
339 m_errorRow = event.GetRow();
340
341 event.Veto();
342 return;
343 }
344
345 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
346
347 alias->Members().clear();
348
349 for( int ii = 0; ii < m_membersGrid->GetNumberRows(); ++ii )
350 {
351 if( ii == row )
352 {
353 // Parse a space-separated list and add each one
354 wxStringTokenizer tok( name, " " );
355
356 if( tok.CountTokens() > 1 )
357 {
358 m_membersGridDirty = true;
359 Bind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
360 }
361
362 while( tok.HasMoreTokens() )
363 alias->Members().push_back( tok.GetNextToken() );
364 }
365 else
366 {
367 alias->Members().push_back( m_membersGrid->GetCellValue( ii, 0 ) );
368 }
369 }
370 }
371}
372
373
375{
376 if( m_lastAlias >= 0 && m_lastAlias < m_aliasesGrid->GetNumberRows() )
377 {
378 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ m_lastAlias ];
379 wxString source;
380 wxString membersLabel;
381
382 if( alias->GetParent() )
383 {
384 wxFileName sheet_name( alias->GetParent()->GetFileName() );
385 source.Printf( wxS( "(" ) + sheet_name.GetFullName() + wxS( ")" ) );
386 }
387
388 membersLabel.Printf( m_membersLabelTemplate, m_lastAliasName );
389
390 m_source->SetLabel( source );
391 m_membersLabel->SetLabel( membersLabel );
392
394 m_membersGrid->AppendRows( alias->Members().size() );
395
396 int ii = 0;
397
398 for( const wxString& member : alias->Members() )
399 m_membersGrid->SetCellValue( ii++, 0, member );
400 }
401
402 m_membersGridDirty = false;
403}
404
405
407{
410
411 Unbind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
412}
413
414
415
416void PANEL_SETUP_BUSES::OnSizeGrid( wxSizeEvent& event )
417{
418 auto setColSize =
419 []( WX_GRID* grid )
420 {
421 int colSize = std::max( grid->GetClientSize().x, grid->GetVisibleWidth( 0 ) );
422
423 if( grid->GetColSize( 0 ) != colSize )
424 grid->SetColSize( 0, colSize );
425 };
426
427 setColSize( m_aliasesGrid );
428 setColSize( m_membersGrid );
429
430 // Always propagate for a grid repaint (needed if the height changes, as well as width)
431 event.Skip();
432}
433
434
435void PANEL_SETUP_BUSES::OnUpdateUI( wxUpdateUIEvent& event )
436{
437 // Handle a grid error. This is delayed to OnUpdateUI so that we can change focus
438 // even when the original validation was triggered from a killFocus event.
439 if( !m_errorMsg.IsEmpty() )
440 {
441 // We will re-enter this routine when the error dialog is displayed, so make
442 // sure we don't keep putting up more dialogs.
443 wxString errorMsg = m_errorMsg;
444 m_errorMsg = wxEmptyString;
445
446 wxWindow* topLevelParent = wxGetTopLevelParent( this );
447
448 DisplayErrorMessage( topLevelParent, errorMsg );
449
450 m_errorGrid->SetFocus();
451 m_errorGrid->MakeCellVisible( m_errorRow, 0 );
452 m_errorGrid->SetGridCursor( m_errorRow, 0 );
453
454 m_errorGrid->EnableCellEditControl( true );
455 m_errorGrid->ShowCellEditControl();
456
457 return;
458 }
459
460 if( !m_membersGrid->IsCellEditControlShown() )
461 {
462 int row = -1;
463 wxString aliasName;
464
465 if( m_aliasesGrid->IsCellEditControlShown() )
466 {
467 row = m_aliasesGrid->GetGridCursorRow();
468 wxGridCellEditor* cellEditor = m_aliasesGrid->GetCellEditor( row, 0 );
469
470 if( wxTextEntry* txt = dynamic_cast<wxTextEntry*>( cellEditor->GetControl() ) )
471 aliasName = txt->GetValue();
472
473 cellEditor->DecRef();
474 }
475 else if( m_aliasesGrid->GetGridCursorRow() >= 0 )
476 {
477 row = m_aliasesGrid->GetGridCursorRow();
478 aliasName = m_aliasesGrid->GetCellValue( row, 0 );
479 }
480 else if( m_lastAlias >= 0 && m_lastAlias < m_aliasesGrid->GetNumberRows() )
481 {
482 row = m_lastAlias;
483 aliasName = m_lastAliasName;
484 }
485
486 if( row < 0 )
487 {
488 m_membersBook->SetSelection( 1 );
489 }
490 else if( row != m_lastAlias || aliasName != m_lastAliasName )
491 {
492 m_lastAlias = row;
493 m_lastAliasName = aliasName;
494
495 m_membersBook->SetSelection( 0 );
496 m_membersBook->GetPage( 0 )->Layout();
497
498 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[ row ];
499 alias->SetName( aliasName );
500
501 m_membersGridDirty = true;
502 Bind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this );
503 }
504 }
505}
506
507
509{
510 loadAliases( aOtherSchematic );
511
512 // New aliases get stored on the currently visible sheet
513 for( const std::shared_ptr<BUS_ALIAS>& alias : m_aliases )
514 alias->SetParent( m_frame->GetScreen() );
515}
516
517
519{
520 if( !m_aliases.empty() && m_membersGrid->GetNumberRows() > 0 && aAliasIndex >= 0
521 && aAliasIndex < (int)m_aliases.size() )
522 {
523 const std::shared_ptr<BUS_ALIAS>& alias = m_aliases[aAliasIndex];
524
525 alias->Members().clear();
526
527 for( int ii = 0; ii < m_membersGrid->GetNumberRows(); ++ii )
528 {
529 wxString memberValue = m_membersGrid->GetCellValue( ii, 0 );
530
531 if( !memberValue.empty() )
532 {
533 alias->Members().push_back( memberValue );
534 }
535 }
536 }
537}
const char * name
Definition: DXF_plotter.cpp:59
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
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:82
SCH_SHEET & Root() const
Definition: schematic.h:130
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:712
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:215
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:193
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:646
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:155