KiCad PCB EDA Suite
Loading...
Searching...
No Matches
picksymbol.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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <pgm_base.h>
26#include <confirm.h>
27#include <sch_tool_utils.h>
28#include <eeschema_id.h>
29#include <general.h>
30#include <kidialog.h>
31#include <kiway.h>
32#include <symbol_viewer_frame.h>
35#include <algorithm>
36#include <sch_symbol.h>
37#include <sch_commit.h>
38#include <sch_edit_frame.h>
39#include <tool/tool_manager.h>
40#include <tools/sch_actions.h>
41#include <project_sch.h>
42
45
47 std::vector<PICKED_SYMBOL>& aHistoryList,
48 std::vector<PICKED_SYMBOL>& aAlreadyPlaced,
49 bool aShowFootprints, const LIB_ID* aHighlight,
50 bool aAllowFields, SYMBOL_COMPAT_FUNC aCompatFunc )
51{
52 std::unique_lock<std::mutex> dialogLock( DIALOG_SYMBOL_CHOOSER::g_Mutex, std::defer_lock );
53
54 // One DIALOG_SYMBOL_CHOOSER dialog at a time. User probably can't handle more anyway.
55 if( !dialogLock.try_lock() )
56 return PICKED_SYMBOL();
57
58 bool aCancelled = false;
59
60 DIALOG_SYMBOL_CHOOSER dlg( this, aHighlight, aFilter, aHistoryList, aAlreadyPlaced,
61 aAllowFields, aShowFootprints, aCancelled );
62
63 if( aCompatFunc )
64 dlg.GetChooserPanel()->SetCompatibilityCallback( std::move( aCompatFunc ) );
65
66 if( aCancelled || dlg.ShowModal() == wxID_CANCEL )
67 return PICKED_SYMBOL();
68
69 PICKED_SYMBOL sel;
70 LIB_ID id = dlg.GetSelectedLibId( &sel.Unit, &sel.BodyStyle );
71
72 if( !id.IsValid() )
73 return PICKED_SYMBOL();
74
75 if( sel.Unit == 0 )
76 sel.Unit = 1;
77
78 if( sel.BodyStyle == 0 )
79 sel.BodyStyle = 1;
80
81 sel.Fields = dlg.GetFields();
82 sel.LibId = id;
83
84 if( sel.LibId.IsValid() )
85 {
86 std::erase_if( aHistoryList, [&sel]( PICKED_SYMBOL const& i )
87 {
88 return i.LibId == sel.LibId;
89 } );
90
91 aHistoryList.insert( aHistoryList.begin(), sel );
92 }
93
94 sel.KeepSymbol = dlg.GetKeepSymbol();
96 return sel;
97}
98
99
100void SCH_EDIT_FRAME::SelectUnit( SCH_SYMBOL* aSymbol, int aUnit, SCH_COMMIT* aCommit )
101{
102 SCH_COMMIT localCommit( m_toolManager );
103 SCH_COMMIT* commit = aCommit ? aCommit : &localCommit;
104 bool staged = false;
105 LIB_SYMBOL* symbol = GetLibSymbol( aSymbol->GetLibId() );
106
107 if( !symbol )
108 return;
109
110 const int unitCount = symbol->GetUnitCount();
111 const int currentUnit = aSymbol->GetUnit();
112
113 if( unitCount <= 1 || currentUnit == aUnit )
114 return;
115
116 if( aUnit > unitCount )
117 aUnit = unitCount;
118
119 const SCH_SHEET_PATH& sheetPath = GetCurrentSheet();
120 bool swapWithOther = false;
121 std::optional<SCH_REFERENCE> otherSymbolRef = FindSymbolByRefAndUnit( *aSymbol->Schematic(),
122 aSymbol->GetRef( &sheetPath, false ),
123 aUnit );
124
125 if( otherSymbolRef )
126 {
127 const wxString targetUnitName = symbol->GetUnitDisplayName( aUnit, false );
128 const wxString currUnitName = symbol->GetUnitDisplayName( currentUnit, false );
129 wxString otherSheetName = otherSymbolRef->GetSheetPath().PathHumanReadable( true, true );
130
131 if( otherSheetName.IsEmpty() )
132 otherSheetName = _( "Root" );
133
134 const wxString msg = wxString::Format( _( "Symbol unit '%s' is already placed (on sheet '%s')" ),
135 targetUnitName, otherSheetName );
136
137 KIDIALOG dlg( this, msg, _( "Unit Already Placed" ), wxYES_NO | wxCANCEL | wxICON_WARNING );
138 dlg.SetYesNoLabels( wxString::Format( _( "&Swap '%s' and '%s'" ), targetUnitName, currUnitName ),
139 wxString::Format( _( "&Duplicate '%s'" ), targetUnitName ) );
140 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
141
142 int ret = dlg.ShowModal();
143
144 if( ret == wxID_CANCEL )
145 return;
146
147 if( ret == wxID_YES )
148 swapWithOther = true;
149 }
150
151 if( swapWithOther )
152 {
153 // We were reliably informed this would exist.
154 wxASSERT( otherSymbolRef );
155
156 SCH_SYMBOL* otherSymbol = otherSymbolRef->GetSymbol();
157
158 if( !otherSymbol->GetEditFlags() )
159 {
160 commit->Modify( otherSymbol, otherSymbolRef->GetSheetPath().LastScreen() );
161 staged = true;
162 }
163
164 // Give that symbol the unit we used to have
165 otherSymbol->SetUnitSelection( &otherSymbolRef->GetSheetPath(), currentUnit );
166 otherSymbol->SetUnit( currentUnit );
167 }
168
169 if( !aSymbol->GetEditFlags() ) // No command in progress: save in undo list
170 {
171 commit->Modify( aSymbol, GetScreen() );
172 staged = true;
173 }
174
175 // Update the unit number.
176 aSymbol->SetUnit( aUnit );
177 aSymbol->SetUnitSelection( &sheetPath, aUnit );
178
179 if( !staged )
180 return;
181
182 if( eeconfig()->m_AutoplaceFields.enable )
183 {
184 AUTOPLACE_ALGO fieldsAutoplaced = aSymbol->GetFieldsAutoplaced();
185
186 if( fieldsAutoplaced == AUTOPLACE_AUTO || fieldsAutoplaced == AUTOPLACE_MANUAL )
187 aSymbol->AutoplaceFields( GetScreen(), fieldsAutoplaced );
188 }
189
190 if( aCommit )
191 return;
192
193 if( swapWithOther )
194 localCommit.Push( _( "Swap Units" ) );
195 else
196 localCommit.Push( _( "Change Unit" ) );
197}
198
199
200void SCH_EDIT_FRAME::SelectBodyStyle( SCH_SYMBOL* aSymbol, int aBodyStyle, SCH_COMMIT* aCommit )
201{
202 if( !aSymbol || !aSymbol->GetLibSymbolRef() )
203 return;
204
205 const int bodyStyleCount = aSymbol->GetLibSymbolRef()->GetBodyStyleCount();
206 const int currentBodyStyle = aSymbol->GetBodyStyle();
207
208 if( bodyStyleCount <= 1 || currentBodyStyle == aBodyStyle )
209 return;
210
211 if( aBodyStyle > bodyStyleCount )
212 aBodyStyle = bodyStyleCount;
213
214 SCH_COMMIT localCommit( m_toolManager );
215 SCH_COMMIT* commit = aCommit ? aCommit : &localCommit;
216
217 // A symbol with edit flags was already staged by the command in progress
218 if( !aSymbol->GetEditFlags() )
219 commit->Modify( aSymbol, GetScreen() );
220
221 aSymbol->SetBodyStyle( aBodyStyle );
222
223 // If selected make sure all the now-included pins are selected
224 if( aSymbol->IsSelected() )
225 m_toolManager->RunAction<EDA_ITEM*>( ACTIONS::selectItem, aSymbol );
226
227 if( !localCommit.Empty() )
228 localCommit.Push( _( "Change Body Style" ) );
229}
230
231
232void SCH_EDIT_FRAME::SetAltPinFunction( SCH_PIN* aPin, const wxString& aFunction )
233{
234 if( !aPin )
235 return;
236
237 SCH_COMMIT commit( m_toolManager );
238 commit.Modify( aPin, GetScreen() );
239
240 if( aFunction == aPin->GetName() )
241 aPin->SetAlt( wxEmptyString );
242 else
243 aPin->SetAlt( aFunction );
244
245 commit.Push( _( "Set Pin Function" ) );
246}
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition actions.h:223
bool Empty() const
Definition commit.h:145
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
int ShowModal() override
PANEL_SYMBOL_CHOOSER * GetChooserPanel() const
std::vector< std::pair< FIELD_T, wxString > > GetFields() const
Get a list of fields edited by the user.
LIB_ID GetSelectedLibId(int *aUnit=nullptr, int *aBodyStyle=nullptr) const
To be called after this dialog returns from ShowModal().
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
EDA_ITEM_FLAGS GetEditFlags() const
Definition eda_item.h:170
bool IsSelected() const
Definition eda_item.h:134
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition kidialog.h:38
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition kidialog.cpp:51
int ShowModal() override
Definition kidialog.cpp:89
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:168
Define a library symbol object.
Definition lib_symbol.h:114
int GetBodyStyleCount() const override
The body styles are a property of the drawings, which a derived symbol inherits from its root symbol ...
int GetUnitCount() const override
wxString GetUnitDisplayName(int aUnit, bool aLabel) const override
Return the user-defined display name for aUnit for symbols with units.
void SetCompatibilityCallback(SYMBOL_COMPAT_FUNC aFunc)
PICKED_SYMBOL PickSymbolFromLibrary(const SYMBOL_LIBRARY_FILTER *aFilter, std::vector< PICKED_SYMBOL > &aHistoryList, std::vector< PICKED_SYMBOL > &aAlreadyPlaced, bool aShowFootprints, const LIB_ID *aHighlight=nullptr, bool aAllowFields=true, SYMBOL_COMPAT_FUNC aCompatFunc=nullptr)
Call the library viewer to select symbol to import into schematic.
EESCHEMA_SETTINGS * eeconfig() const
LIB_SYMBOL * GetLibSymbol(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load symbol from symbol library table.
Commit for the schematic and symbol editors.
Definition sch_commit.h:54
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void SelectBodyStyle(SCH_SYMBOL *aSymbol, int aBodyStyle, SCH_COMMIT *aCommit=nullptr)
Change the body style of aSymbol.
void SetAltPinFunction(SCH_PIN *aPin, const wxString &aFunction)
SCH_SHEET_PATH & GetCurrentSheet() const
void SelectUnit(SCH_SYMBOL *aSymbol, int aUnit, SCH_COMMIT *aCommit=nullptr)
Change the unit of aSymbol, swapping with another placed unit if the user asks.
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:302
int GetBodyStyle() const
Definition sch_item.h:253
int GetUnit() const
Definition sch_item.h:243
virtual void SetUnit(int aUnit)
Definition sch_item.h:242
AUTOPLACE_ALGO GetFieldsAutoplaced() const
Return whether the fields have been automatically placed.
Definition sch_item.h:673
void SetAlt(const wxString &aAlt)
Set the name of the alternate pin.
Definition sch_pin.cpp:560
const wxString & GetName() const
Definition sch_pin.cpp:527
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition sch_symbol.h:73
void AutoplaceFields(SCH_SCREEN *aScreen, AUTOPLACE_ALGO aAlgo) override
Automatically orient all the fields in the symbol.
void SetBodyStyle(int aBodyStyle) override
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:163
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:182
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Helper object to filter a list of libraries.
TOOL_MANAGER * m_toolManager
This file is part of the common library.
#define _(s)
see class PGM_BASE
AUTOPLACE_ALGO
Definition sch_item.h:73
@ AUTOPLACE_MANUAL
Definition sch_item.h:76
@ AUTOPLACE_AUTO
Definition sch_item.h:75
std::optional< SCH_REFERENCE > FindSymbolByRefAndUnit(const SCHEMATIC &aSchematic, const wxString &aRef, int aUnit)
Find a symbol by reference and unit.
bool PlaceAllUnits
Definition sch_screen.h:89
std::vector< std::pair< FIELD_T, wxString > > Fields
Definition sch_screen.h:91
std::function< std::vector< VARIANT_COMPAT_RESULT >(const LIB_ID &)> SYMBOL_COMPAT_FUNC
Callback that evaluates variant symbol compatibility for a given candidate LIB_ID.