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, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <pgm_base.h>
30#include <confirm.h>
31#include <sch_tool_utils.h>
32#include <eeschema_id.h>
33#include <general.h>
34#include <kidialog.h>
35#include <kiway.h>
36#include <symbol_viewer_frame.h>
39#include <algorithm>
40#include <sch_symbol.h>
41#include <sch_commit.h>
42#include <sch_edit_frame.h>
43#include <tool/tool_manager.h>
44#include <tools/sch_actions.h>
45#include <project_sch.h>
46
48
50 std::vector<PICKED_SYMBOL>& aHistoryList,
51 std::vector<PICKED_SYMBOL>& aAlreadyPlaced,
52 bool aShowFootprints, const LIB_ID* aHighlight,
53 bool aAllowFields )
54{
55 std::unique_lock<std::mutex> dialogLock( DIALOG_SYMBOL_CHOOSER::g_Mutex, std::defer_lock );
56
57 // One DIALOG_SYMBOL_CHOOSER dialog at a time. User probably can't handle more anyway.
58 if( !dialogLock.try_lock() )
59 return PICKED_SYMBOL();
60
61 bool aCancelled = false;
62
63 DIALOG_SYMBOL_CHOOSER dlg( this, aHighlight, aFilter, aHistoryList, aAlreadyPlaced,
64 aAllowFields, aShowFootprints, aCancelled );
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 );
71
72 if( !id.IsValid() )
73 return PICKED_SYMBOL();
74
75 if( sel.Unit == 0 )
76 sel.Unit = 1;
77
78 sel.Fields = dlg.GetFields();
79 sel.LibId = id;
80
81 if( sel.LibId.IsValid() )
82 {
83 std::erase_if( aHistoryList, [&sel]( PICKED_SYMBOL const& i )
84 {
85 return i.LibId == sel.LibId;
86 } );
87
88 aHistoryList.insert( aHistoryList.begin(), sel );
89 }
90
91 sel.KeepSymbol = dlg.GetKeepSymbol();
93 return sel;
94}
95
96
97void SCH_EDIT_FRAME::SelectUnit( SCH_SYMBOL* aSymbol, int aUnit )
98{
99 SCH_COMMIT commit( m_toolManager );
100 LIB_SYMBOL* symbol = GetLibSymbol( aSymbol->GetLibId() );
101
102 if( !symbol )
103 return;
104
105 const int unitCount = symbol->GetUnitCount();
106 const int currentUnit = aSymbol->GetUnit();
107
108 if( unitCount <= 1 || currentUnit == aUnit )
109 return;
110
111 if( aUnit > unitCount )
112 aUnit = unitCount;
113
114 const SCH_SHEET_PATH& sheetPath = GetCurrentSheet();
115 bool swapWithOther = false;
116 std::optional<SCH_REFERENCE> otherSymbolRef = FindSymbolByRefAndUnit( *aSymbol->Schematic(),
117 aSymbol->GetRef( &sheetPath, false ),
118 aUnit );
119
120 if( otherSymbolRef )
121 {
122 const wxString targetUnitName = symbol->GetUnitDisplayName( aUnit, false );
123 const wxString currUnitName = symbol->GetUnitDisplayName( currentUnit, false );
124 wxString otherSheetName = otherSymbolRef->GetSheetPath().PathHumanReadable( true, true );
125
126 if( otherSheetName.IsEmpty() )
127 otherSheetName = _( "Root" );
128
129 const wxString msg = wxString::Format( _( "Symbol unit '%s' is already placed (on sheet '%s')" ),
130 targetUnitName, otherSheetName );
131
132 KIDIALOG dlg( this, msg, _( "Unit Already Placed" ), wxYES_NO | wxCANCEL | wxICON_WARNING );
133 dlg.SetYesNoLabels( wxString::Format( _( "&Swap '%s' and '%s'" ), targetUnitName, currUnitName ),
134 wxString::Format( _( "&Duplicate '%s'" ), targetUnitName ) );
135 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
136
137 int ret = dlg.ShowModal();
138
139 if( ret == wxID_CANCEL )
140 return;
141
142 if( ret == wxID_YES )
143 swapWithOther = true;
144 }
145
146 if( swapWithOther )
147 {
148 // We were reliably informed this would exist.
149 wxASSERT( otherSymbolRef );
150
151 SCH_SYMBOL* otherSymbol = otherSymbolRef->GetSymbol();
152
153 if( !otherSymbol->GetEditFlags() )
154 commit.Modify( otherSymbol, otherSymbolRef->GetSheetPath().LastScreen() );
155
156 // Give that symbol the unit we used to have
157 otherSymbol->SetUnitSelection( &otherSymbolRef->GetSheetPath(), currentUnit );
158 otherSymbol->SetUnit( currentUnit );
159 }
160
161 if( !aSymbol->GetEditFlags() ) // No command in progress: save in undo list
162 commit.Modify( aSymbol, GetScreen() );
163
164 // Update the unit number.
165 aSymbol->SetUnit( aUnit );
166 aSymbol->SetUnitSelection( &sheetPath, aUnit );
167
168 if( !commit.Empty() )
169 {
170 if( eeconfig()->m_AutoplaceFields.enable )
171 {
172 AUTOPLACE_ALGO fieldsAutoplaced = aSymbol->GetFieldsAutoplaced();
173
174 if( fieldsAutoplaced == AUTOPLACE_AUTO || fieldsAutoplaced == AUTOPLACE_MANUAL )
175 aSymbol->AutoplaceFields( GetScreen(), fieldsAutoplaced );
176 }
177
178 if( swapWithOther )
179 commit.Push( _( "Swap Units" ) );
180 else
181 commit.Push( _( "Change Unit" ) );
182 }
183}
184
185
186void SCH_EDIT_FRAME::SelectBodyStyle( SCH_SYMBOL* aSymbol, int aBodyStyle )
187{
188 if( !aSymbol || !aSymbol->GetLibSymbolRef() )
189 return;
190
191 const int bodyStyleCount = aSymbol->GetLibSymbolRef()->GetBodyStyleCount();
192 const int currentBodyStyle = aSymbol->GetBodyStyle();
193
194 if( bodyStyleCount <= 1 || currentBodyStyle == aBodyStyle )
195 return;
196
197 if( aBodyStyle > bodyStyleCount )
198 aBodyStyle = bodyStyleCount;
199
200 SCH_COMMIT commit( m_toolManager );
201
202 commit.Modify( aSymbol, GetScreen() );
203
204 aSymbol->SetBodyStyle( aBodyStyle );
205
206 // If selected make sure all the now-included pins are selected
207 if( aSymbol->IsSelected() )
208 m_toolManager->RunAction<EDA_ITEM*>( ACTIONS::selectItem, aSymbol );
209
210 commit.Push( _( "Change Body Style" ) );
211}
212
213
214void SCH_EDIT_FRAME::SetAltPinFunction( SCH_PIN* aPin, const wxString& aFunction )
215{
216 if( !aPin )
217 return;
218
219 SCH_COMMIT commit( m_toolManager );
220 commit.Modify( aPin, GetScreen() );
221
222 if( aFunction == aPin->GetName() )
223 aPin->SetAlt( wxEmptyString );
224 else
225 aPin->SetAlt( aFunction );
226
227 commit.Push( _( "Set Pin Function" ) );
228}
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition actions.h:226
bool Empty() const
Definition commit.h:137
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:106
int ShowModal() override
std::vector< std::pair< FIELD_T, wxString > > GetFields() const
Get a list of fields edited by the user.
LIB_ID GetSelectedLibId(int *aUnit=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:148
bool IsSelected() const
Definition eda_item.h:127
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition kidialog.h:42
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition kidialog.cpp:55
int ShowModal() override
Definition kidialog.cpp:93
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:172
Define a library symbol object.
Definition lib_symbol.h:85
int GetBodyStyleCount() const override
Definition lib_symbol.h:752
int GetUnitCount() const override
wxString GetUnitDisplayName(int aUnit, bool aLabel) const override
Return the user-defined display name for aUnit for symbols with units.
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)
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.
virtual void Push(const wxString &aMessage=wxT("A commit"), 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 SetAltPinFunction(SCH_PIN *aPin, const wxString &aFunction)
void SelectBodyStyle(SCH_SYMBOL *aSymbol, int aBodyStyle)
SCH_SHEET_PATH & GetCurrentSheet() const
void SelectUnit(SCH_SYMBOL *aSymbol, int aUnit)
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:244
int GetBodyStyle() const
Definition sch_item.h:247
int GetUnit() const
Definition sch_item.h:238
virtual void SetUnit(int aUnit)
Definition sch_item.h:237
AUTOPLACE_ALGO GetFieldsAutoplaced() const
Return whether the fields have been automatically placed.
Definition sch_item.h:606
void SetAlt(const wxString &aAlt)
Set the name of the alternate pin.
Definition sch_pin.cpp:433
const wxString & GetName() const
Definition sch_pin.cpp:401
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:76
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:165
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:184
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:68
@ AUTOPLACE_MANUAL
Definition sch_item.h:71
@ AUTOPLACE_AUTO
Definition sch_item.h:70
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:85
std::vector< std::pair< FIELD_T, wxString > > Fields
Definition sch_screen.h:87