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