KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_pin_properties.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) 2010 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <bitmaps.h>
22#include <widgets/wx_infobar.h>
23#include <sch_painter.h>
24#include <symbol_edit_frame.h>
25#include <sch_pin.h>
27#include <confirm.h>
28#include <kiplatform/ui.h>
30#include <widgets/wx_grid.h>
31#include <grid_tricks.h>
34#include <wx/hyperlink.h>
36
37class ALT_PIN_DATA_MODEL : public WX_GRID_TABLE_BASE, public std::vector<SCH_PIN::ALT>
38{
39public:
41 {
42 }
43
44 int GetNumberRows() override { return (int) size(); }
45 int GetNumberCols() override { return COL_COUNT; }
46
47 wxString GetColLabelValue( int aCol ) override
48 {
49 switch( aCol )
50 {
51 case COL_NAME: return _( "Alternate Pin Name" );
52 case COL_TYPE: return _( "Electrical Type" );
53 case COL_SHAPE: return _( "Graphic Style" );
54 default: wxFAIL; return wxEmptyString;
55 }
56 }
57
58 bool IsEmptyCell( int row, int col ) override
59 {
60 return false; // don't allow adjacent cell overflow, even if we are actually empty
61 }
62
63 wxString GetValue( int aRow, int aCol ) override
64 {
65 switch( aCol )
66 {
67 case COL_NAME: return at( aRow ).m_Name;
68 case COL_TYPE: return PinTypeNames()[static_cast<int>( at( aRow ).m_Type )];
69 case COL_SHAPE: return PinShapeNames()[static_cast<int>( at( aRow ).m_Shape )];
70 default: wxFAIL; return wxEmptyString;
71 }
72 }
73
74 void SetValue( int aRow, int aCol, const wxString &aValue ) override
75 {
76 switch( aCol )
77 {
78 case COL_NAME:
79 at( aRow ).m_Name = aValue;
80 break;
81
82 case COL_TYPE:
83 if( PinTypeNames().Index( aValue ) != wxNOT_FOUND )
84 at( aRow ).m_Type = (ELECTRICAL_PINTYPE) PinTypeNames().Index( aValue );
85
86 break;
87
88 case COL_SHAPE:
89 if( PinShapeNames().Index( aValue ) != wxNOT_FOUND )
90 at( aRow ).m_Shape = (GRAPHIC_PINSHAPE) PinShapeNames().Index( aValue );
91
92 break;
93
94 default:
95 wxFAIL;
96 break;
97 }
98 }
99
100 void AppendRow( const SCH_PIN::ALT& aAlt )
101 {
102 push_back( aAlt );
103
104 if ( GetView() )
105 {
106 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
107 GetView()->ProcessTableMessage( msg );
108 }
109 }
110
111 void RemoveRow( int aRow )
112 {
113 erase( begin() + aRow );
114
115 if ( GetView() )
116 {
117 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aRow, 1 );
118 GetView()->ProcessTableMessage( msg );
119 }
120 }
121};
122
123
125 bool aFocusPinNumber ) :
127 m_frame( parent ),
128 m_pin( aPin ),
134 m_delayedFocusRow( -1 ),
136 m_initialized( false )
137{
138 // Create a dummy symbol with a single pin for the preview widget:
139 m_dummyParent = new LIB_SYMBOL( *static_cast<LIB_SYMBOL*>( m_pin->GetParentSymbol() ) );
140
141 // Move everything in the copied symbol to unit 1; we'll use unit 2 for the dummy pin:
142 m_dummyParent->SetUnitCount( 2, false );
143 m_dummyParent->RunOnChildren( [&]( SCH_ITEM* child )
144 {
145 child->SetUnit( 1 );
146 },
148
149 m_dummyPin = new SCH_PIN( *m_pin );
150 m_dummyPin->ClearFlags( IS_NEW ); // Needed to display the dummy pin
151 m_dummyPin->SetUnit( 2 );
152 m_dummyParent->AddDrawItem( m_dummyPin, false );
153
154 m_dummyParent->SetShowPinNames( true );
155 m_dummyParent->SetShowPinNumbers( true );
156
158 m_frame->GetCanvas()->GetBackend() );
159
160 m_previewWidget->SetLayoutDirection( wxLayout_LeftToRight );
161 m_previewWidget->DisplayPart( m_dummyParent, m_dummyPin->GetUnit(), m_dummyPin->GetBodyStyle() );
162
163 wxBoxSizer* previewSizer = new wxBoxSizer( wxHORIZONTAL );
164 previewSizer->Add( m_previewWidget, 1, wxEXPAND, 5 );
165 m_panelShowPin->SetSizer( previewSizer );
166
167 m_previewWidget->GetRenderSettings()->m_ShowHiddenPins = true;
168
169 const wxArrayString& orientationNames = PinOrientationNames();
170 const std::vector<BITMAPS>& orientationIcons = PinOrientationIcons();
171
172 for ( unsigned ii = 0; ii < orientationNames.GetCount(); ii++ )
173 m_choiceOrientation->Insert( orientationNames[ii], KiBitmapBundle( orientationIcons[ii] ), ii );
174
175 // We can't set the tab order through wxWidgets due to shortcomings in their mnemonics
176 // implementation on MSW
177 m_tabOrder = {
193 };
194
195 // Default alternates turndown to whether or not alternates exist, or if we've had it open
196 // before
197 m_alternatesTurndown->Collapse( m_pin->GetAlternates().size() == 0 && !s_alternatesTurndownOpen );
198
199 // wxwidgets doesn't call the OnCollapseChange even at init, so we update this value if
200 // the alternates pane defaults to open
201 if ( m_pin->GetAlternates().size() > 0 )
203
205
206 // Save original columns widths so we can do proportional sizing.
207 for( int i = 0; i < COL_COUNT; ++i )
208 m_originalColWidths[ i ] = m_alternatesGrid->GetColSize( i );
209
211 m_alternatesGrid->PushEventHandler( new GRID_TRICKS( m_alternatesGrid,
212 [this]( wxCommandEvent& aEvent )
213 {
214 OnAddAlternate( aEvent );
215 } ) );
216 m_alternatesGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
217
218 if( aPin->GetParentSymbol()->IsMultiBodyStyle() )
219 {
220 m_alternatesTurndown->Collapse();
221 m_alternatesTurndown->Disable();
222 m_alternatesTurndown->SetToolTip( _( "Alternate pin assignments are not available for symbols with "
223 "multiple body styles." ) );
224 }
225
226 // Set special attributes
227 wxGridCellAttr* attr;
228
229 attr = new wxGridCellAttr;
230 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinTypeIcons(), PinTypeNames() ) );
231 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinTypeIcons(), PinTypeNames() ) );
232 m_alternatesGrid->SetColAttr( COL_TYPE, attr );
233
234 attr = new wxGridCellAttr;
235 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinShapeIcons(), PinShapeNames() ) );
236 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinShapeIcons(), PinShapeNames() ) );
237 m_alternatesGrid->SetColAttr( COL_SHAPE, attr );
238
241 m_addAlternate->GetParent()->Layout();
242
244
245 SetInitialFocus( aFocusPinNumber ? m_textPinNumber : m_textPinName );
246
247 // We should call FinishDialogSettings() when all widgets have the size fixed.
248 // However m_infoBar is not yet initialized, so it will called later
249 // See TransferDataToWindow()
250
251 // On some window managers (Unity, XFCE) the dialog is not always raised, depending on
252 // how it is run.
253 Raise();
254
255 m_initialized = true;
256}
257
258
260{
261 delete m_dummyParent;
262
263 // Prevents crash bug in wxGrid's d'tor
265
266 // Delete the GRID_TRICKS.
267 m_alternatesGrid->PopEventHandler( true );
268}
269
270
272{
273 if( !DIALOG_SHIM::TransferDataToWindow() )
274 return false;
275
276 m_origPos = m_pin->GetPosition();
277
278 m_choiceOrientation->SetSelection( PinOrientationIndex( m_pin->GetOrientation() ) );
279 m_choiceStyle->SetSelection( m_pin->GetShape() );
280 m_choiceElectricalType->SetSelection( m_pin->GetType() );
281 m_textPinName->SetValue( m_pin->GetName() );
282 m_nameSize.SetValue( m_pin->GetNameTextSize() );
283 m_posX.SetValue( m_origPos.x );
284 m_posY.SetValue( m_origPos.y );
285 m_textPinNumber->SetValue( m_pin->GetNumber() );
286 m_numberSize.SetValue( m_pin->GetNumberTextSize() );
287 m_pinLength.SetValue( m_pin->GetLength() );
288 m_checkApplyToAllParts->Enable( m_pin->GetParentSymbol()->IsMultiUnit() );
289 m_checkApplyToAllParts->SetValue( m_pin->GetParentSymbol()->IsMultiUnit() && m_pin->GetUnit() == 0 );
290 m_checkApplyToAllBodyStyles->Enable( m_pin->GetParentSymbol()->IsMultiBodyStyle() );
291 m_checkApplyToAllBodyStyles->SetValue( m_pin->GetBodyStyle() == 0 );
292 m_checkShow->SetValue( m_pin->IsVisible() );
293
294 m_dummyPin->SetVisible( m_pin->IsVisible() );
295
296 wxString commonUnitsToolTip;
297
298 if( m_frame->m_SyncPinEdit )
299 {
300 wxHyperlinkCtrl* button = new wxHyperlinkCtrl( m_infoBar, wxID_ANY, _( "Exit sync pins mode" ),
301 wxEmptyString );
302
303 button->Bind( wxEVT_COMMAND_HYPERLINK,
304 std::function<void( wxHyperlinkEvent& aEvent )>(
305 [&]( wxHyperlinkEvent& aEvent )
306 {
307 m_frame->m_SyncPinEdit = false;
308 m_infoBar->Dismiss();
309 } ) );
310
311 m_infoBar->RemoveAllButtons();
312 m_infoBar->AddButton( button );
313 m_infoBar->ShowMessage( getSyncPinsMessage() );
314
315 commonUnitsToolTip = _( "Synchronized pins mode is enabled.\n"
316 "Similar pins will be edited regardless of this option." );
317 }
318 else
319 {
320 commonUnitsToolTip = _( "If checked, this pin will exist in all units." );
321 }
322
323 if( !m_pin->GetParentSymbol()->IsMultiUnit() )
324 commonUnitsToolTip = _( "This symbol only has one unit. This control has no effect." );
325
326 m_checkApplyToAllParts->SetToolTip( commonUnitsToolTip );
327
328 for( const std::pair<const wxString, SCH_PIN::ALT>& alt : m_pin->GetAlternates() )
329 m_alternatesDataModel->AppendRow( alt.second );
330
331 // We can call FinishDialogSettings() now all widgets have the size fixed.
333
334 return true;
335}
336
337
339{
340 if( !m_alternatesGrid->CommitPendingChanges() )
341 return false;
342
343 // Check for missing alternate names.
344 for( size_t i = 0; i < m_alternatesDataModel->size(); ++i )
345 {
346 if( m_alternatesDataModel->at( i ).m_Name.IsEmpty() )
347 {
348 DisplayErrorMessage( this, _( "Alternate pin definitions must have a name." ) );
349
352
353 return false;
354 }
355 }
356
357 if( !DIALOG_SHIM::TransferDataFromWindow() )
358 return false;
359
360 VECTOR2I newPos( m_posX.GetIntValue(), m_posY.GetIntValue() );
361
362 const int standard_grid = 50;
363
364 // Only show the warning if the position has been changed
365 if( ( m_origPos != newPos )
366 && ( ( m_posX.GetValue() % standard_grid ) || ( m_posY.GetValue() % standard_grid ) ) )
367 {
368 wxString msg = wxString::Format( _( "This pin is not on a %d mils grid which will make it "
369 "difficult to connect to in the schematic.\n"
370 "Do you wish to continue?" ),
371 standard_grid );
372 if( !IsOK( this, msg ) )
373 return false;
374 }
375
376 m_pin->SetName( m_textPinName->GetValue() );
377 m_pin->SetNumber( m_textPinNumber->GetValue() );
378 m_pin->SetNameTextSize( m_nameSize.GetIntValue() );
379 m_pin->SetNumberTextSize( m_numberSize.GetIntValue() );
380 m_pin->SetOrientation( PinOrientationCode( m_choiceOrientation->GetSelection() ) );
381 m_pin->SetPosition( newPos );
382 m_pin->ChangeLength( m_pinLength.GetIntValue() );
383 m_pin->SetType( m_choiceElectricalType->GetPinTypeSelection() );
384 m_pin->SetShape( m_choiceStyle->GetPinShapeSelection() );
385 m_pin->SetBodyStyle( m_checkApplyToAllBodyStyles->GetValue() ? 0 : m_frame->GetBodyStyle() );
386 m_pin->SetUnit( m_checkApplyToAllParts->GetValue() ? 0 : m_frame->GetUnit() );
387 m_pin->SetVisible( m_checkShow->GetValue() );
388
389 std::map<wxString, SCH_PIN::ALT>& alternates = m_pin->GetAlternates();
390 alternates.clear();
391
392 for( const SCH_PIN::ALT& alt : *m_alternatesDataModel )
393 alternates[ alt.m_Name ] = alt;
394
395 return true;
396}
397
398
399void DIALOG_PIN_PROPERTIES::OnPropertiesChange( wxCommandEvent& event )
400{
401 if( !IsShownOnScreen() ) // do nothing at init time
402 return;
403
404 m_dummyPin->SetName( m_textPinName->GetValue() );
405 m_dummyPin->SetNumber( m_textPinNumber->GetValue() );
406 m_dummyPin->SetNameTextSize( m_nameSize.GetIntValue() );
407 m_dummyPin->SetNumberTextSize( m_numberSize.GetIntValue() );
408 m_dummyPin->SetOrientation( PinOrientationCode( m_choiceOrientation->GetSelection() ) );
409 m_dummyPin->SetLength( m_pinLength.GetIntValue() );
410 m_dummyPin->SetType( m_choiceElectricalType->GetPinTypeSelection() );
411 m_dummyPin->SetShape( m_choiceStyle->GetPinShapeSelection() );
412 m_dummyPin->SetVisible( m_checkShow->GetValue() );
413
414 if( event.GetEventObject() == m_checkApplyToAllParts && m_frame->m_SyncPinEdit )
415 {
416 m_infoBar->ShowMessage( getSyncPinsMessage() );
417 m_infoBar->GetSizer()->Layout();
418 }
419
420 m_previewWidget->DisplayPart( m_dummyParent, m_dummyPin->GetUnit(), m_dummyPin->GetBodyStyle() );
421}
422
423
425{
426 if( m_checkApplyToAllParts->GetValue() )
427 return _( "Synchronized Pins Mode." );
428 else if( m_pin->IsNew() )
429 return _( "Synchronized Pins Mode. New pin will be added to all units." );
430 else
431 return _( "Synchronized Pins Mode. Matching pins in other units will be updated." );
432}
433
434
435void DIALOG_PIN_PROPERTIES::OnAddAlternate( wxCommandEvent& event )
436{
437 if( !m_alternatesGrid->CommitPendingChanges() )
438 return;
439
440 m_alternatesGrid->OnAddRow(
441 [&]() -> std::pair<int, int>
442 {
443 SCH_PIN::ALT newAlt;
444 newAlt.m_Name = wxEmptyString;
445 newAlt.m_Type = m_pin->GetType();
446 newAlt.m_Shape = m_pin->GetShape();
447
448 m_alternatesDataModel->AppendRow( newAlt );
449 return { m_alternatesGrid->GetNumberRows() - 1, COL_NAME };
450 } );
451}
452
453
454void DIALOG_PIN_PROPERTIES::OnDeleteAlternate( wxCommandEvent& event )
455{
456 m_alternatesGrid->OnDeleteRows(
457 [&]( int row )
458 {
459 m_alternatesDataModel->RemoveRow( row );
460 } );
461}
462
463
465{
466 // Account for scroll bars
468
469 wxGridUpdateLocker deferRepaintsTillLeavingScope;
470
473
475}
476
477
478void DIALOG_PIN_PROPERTIES::OnSize( wxSizeEvent& event )
479{
480 auto new_size = event.GetSize();
481
482 if( m_initialized && m_size != new_size )
483 {
484 m_size = new_size;
485
487 }
488
489 // Always propagate for a grid repaint (needed if the height changes, as well as width)
490 event.Skip();
491}
492
493
494void DIALOG_PIN_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
495{
496 // Handle a delayed focus
497 if( m_delayedFocusRow >= 0 )
498 {
499 m_alternatesTurndown->Collapse( false );
500
501 m_alternatesGrid->SetFocus();
504
505 m_alternatesGrid->EnableCellEditControl( true );
506 m_alternatesGrid->ShowCellEditControl();
507
510 }
511}
512
513
514void DIALOG_PIN_PROPERTIES::OnCollapsiblePaneChange( wxCollapsiblePaneEvent& event )
515{
516 if( !event.GetCollapsed() )
517 {
518 wxTopLevelWindow* tlw = dynamic_cast<wxTopLevelWindow*>( wxGetTopLevelParent( this ) );
519
520 if( tlw )
521 {
522 tlw->InvalidateBestSize();
523 wxSize bestSize = tlw->GetBestSize();
524 wxSize currentSize = tlw->GetSize();
525 tlw->SetSize( wxMax( bestSize.GetWidth(), currentSize.GetWidth() ),
526 wxMax( bestSize.GetHeight(), currentSize.GetHeight() ) );
527 }
528 }
529}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
wxString GetColLabelValue(int aCol) override
void SetValue(int aRow, int aCol, const wxString &aValue) override
ALT_PIN_DATA_MODEL(EDA_UNITS aUserUnits)
wxString GetValue(int aRow, int aCol) override
void AppendRow(const SCH_PIN::ALT &aAlt)
bool IsEmptyCell(int row, int col) override
DIALOG_PIN_PROPERTIES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Pin Properties"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
SYMBOL_EDIT_FRAME * m_frame
void OnUpdateUI(wxUpdateUIEvent &event) override
std::map< int, int > m_originalColWidths
void OnCollapsiblePaneChange(wxCollapsiblePaneEvent &event) override
void OnPropertiesChange(wxCommandEvent &event) override
void OnAddAlternate(wxCommandEvent &event) override
SYMBOL_PREVIEW_WIDGET * m_previewWidget
ALT_PIN_DATA_MODEL * m_alternatesDataModel
void OnSize(wxSizeEvent &event) override
DIALOG_PIN_PROPERTIES(SYMBOL_EDIT_FRAME *parent, SCH_PIN *aPin, bool aFocusPinNumber)
void OnDeleteAlternate(wxCommandEvent &event) override
std::vector< wxWindow * > m_tabOrder
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition dialog_shim.h:79
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
EDA_UNITS GetUserUnits() const
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:57
Define a library symbol object.
Definition lib_symbol.h:79
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:274
virtual void SetUnit(int aUnit)
Definition sch_item.h:232
The symbol library editor main window.
virtual bool IsMultiBodyStyle() const =0
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)
@ NO_RECURSE
Definition eda_item.h:50
#define IS_NEW
New item, just created.
EDA_UNITS
Definition eda_units.h:44
wxSize GetUnobscuredSize(const wxWindow *aWindow)
Tries to determine the size of the viewport of a scrollable widget (wxDataViewCtrl,...
Definition wxgtk/ui.cpp:294
const std::vector< BITMAPS > & PinTypeIcons()
Definition pin_type.cpp:158
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition pin_type.h:32
const wxArrayString & PinTypeNames()
Definition pin_type.cpp:149
int PinOrientationIndex(PIN_ORIENTATION code)
Definition pin_type.cpp:66
const wxArrayString & PinShapeNames()
Definition pin_type.cpp:167
const std::vector< BITMAPS > & PinShapeIcons()
Definition pin_type.cpp:176
const wxArrayString & PinOrientationNames()
Definition pin_type.cpp:185
PIN_ORIENTATION PinOrientationCode(size_t index)
Definition pin_type.cpp:59
const std::vector< BITMAPS > & PinOrientationIcons()
Definition pin_type.cpp:194
GRAPHIC_PINSHAPE
Definition pin_type.h:80
wxString m_Name
Definition sch_pin.h:45
GRAPHIC_PINSHAPE m_Shape
Definition sch_pin.h:46
ELECTRICAL_PINTYPE m_Type
Definition sch_pin.h:47
Functions for manipulating tab traversal in forms and dialogs.
table push_back({ "Source", "Layer", "Vertices", "Strategy", "Build(us)", "ns/query", "Mquery/s", "Inside" })
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683