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 (C) 2004-2023 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>
29#include <core/kicad_algo.h>
31#include <confirm.h>
32#include <eeschema_id.h>
33#include <general.h>
34#include <kiway.h>
35#include <symbol_viewer_frame.h>
38#include <sch_symbol.h>
39#include <sch_commit.h>
40#include <sch_edit_frame.h>
41#include <symbol_lib_table.h>
42#include <tool/tool_manager.h>
43#include <tools/ee_actions.h>
44
46
48 const SYMBOL_LIBRARY_FILTER* aFilter,
49 const LIB_ID& aPreselectedLibId,
50 int aUnit, int aConvert )
51{
52 // Close any open non-modal Lib browser, and open a new one, in "modal" mode:
54
55 if( viewer )
56 viewer->Destroy();
57
58 viewer = (SYMBOL_VIEWER_FRAME*) Kiway().Player( FRAME_SCH_VIEWER_MODAL, true, aParent );
59
60 if( aFilter )
61 viewer->SetFilter( aFilter );
62
63 if( aPreselectedLibId.IsValid() )
64 {
65 viewer->SetSelectedLibrary( aPreselectedLibId.GetLibNickname() );
66 viewer->SetSelectedSymbol( aPreselectedLibId.GetLibItemName());
67 }
68
69 viewer->SetUnitAndConvert( aUnit, aConvert );
70
71 viewer->Refresh();
72
73 PICKED_SYMBOL sel;
74 wxString symbol;
75
76 if( viewer->ShowModal( &symbol, aParent ) )
77 {
78 LIB_ID id;
79
80 if( id.Parse( symbol ) == -1 )
81 sel.LibId = id;
82
83 sel.Unit = viewer->GetUnit();
84 sel.Convert = viewer->GetConvert();
85 }
86
87 viewer->Destroy();
88
89 return sel;
90}
91
92
94 std::vector<PICKED_SYMBOL>& aHistoryList,
95 bool aUseLibBrowser, int aUnit, int aConvert,
96 bool aShowFootprints, const LIB_ID* aHighlight,
97 bool aAllowFields )
98{
99 std::unique_lock<std::mutex> dialogLock( DIALOG_CHOOSE_SYMBOL::g_Mutex, std::defer_lock );
100 SYMBOL_LIB_TABLE* libs = Prj().SchSymbolLibTable();
101 COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
103
104 // One DIALOG_CHOOSE_SYMBOL dialog at a time. User probably can't handle more anyway.
105 if( !dialogLock.try_lock() )
106 return PICKED_SYMBOL();
107
108 // Make sure settings are loaded before we start running multi-threaded symbol loaders
109 Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
110 Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
111
112
113 wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER> dataPtr
114 = SYMBOL_TREE_MODEL_ADAPTER::Create( this, libs );
115 SYMBOL_TREE_MODEL_ADAPTER* modelAdapter
116 = static_cast<SYMBOL_TREE_MODEL_ADAPTER*>( dataPtr.get() );
117 bool loaded = false;
118
119 if( aFilter )
120 {
121 const wxArrayString& liblist = aFilter->GetAllowedLibList();
122
123 for( const wxString& nickname : liblist )
124 {
125 if( libs->HasLibrary( nickname, true ) )
126 {
127 loaded = true;
128
129 bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, nickname )
130 || alg::contains( project.m_PinnedSymbolLibs, nickname );
131
132 if( libs->FindRow( nickname )->GetIsVisible() )
133 modelAdapter->AddLibrary( nickname, pinned );
134 }
135 }
136
137 modelAdapter->AssignIntrinsicRanks();
138
139 if( aFilter->GetFilterPowerSymbols() )
141 }
142
143 std::vector<LIB_SYMBOL> history_list_storage;
144 std::vector<LIB_TREE_ITEM*> history_list;
145
146 history_list_storage.reserve( aHistoryList.size() );
147
148 for( const PICKED_SYMBOL& i : aHistoryList )
149 {
150 LIB_SYMBOL* symbol = GetLibSymbol( i.LibId );
151
152 // This can be null, for example when a symbol has been deleted from a library
153 if( symbol )
154 {
155 history_list_storage.emplace_back( *symbol );
156
157 for( const std::pair<int, wxString>& fieldDef : i.Fields )
158 {
159 LIB_FIELD* field = history_list_storage.back().GetFieldById( fieldDef.first );
160
161 if( field )
162 field->SetText( fieldDef.second );
163 }
164
165 history_list.push_back( &history_list_storage.back() );
166 }
167 }
168
169 modelAdapter->DoAddLibrary( wxT( "-- " ) + _( "Recently Used" ) + wxT( " --" ), wxEmptyString,
170 history_list, false, true );
171
172 if( !aHistoryList.empty() )
173 modelAdapter->SetPreselectNode( aHistoryList[0].LibId, aHistoryList[0].Unit );
174
175 const std::vector< wxString > libNicknames = libs->GetLogicalLibs();
176
177 if( !loaded )
178 {
179 if( !modelAdapter->AddLibraries( libNicknames, this ) )
180 {
181 // loading cancelled by user
182 return PICKED_SYMBOL();
183 }
184 }
185
186 if( aHighlight && aHighlight->IsValid() )
187 modelAdapter->SetPreselectNode( *aHighlight, /* aUnit */ 0 );
188
189 wxString dialogTitle;
190
192 dialogTitle.Printf( _( "Choose Power Symbol (%d items loaded)" ), dataPtr->GetItemCount() );
193 else
194 dialogTitle.Printf( _( "Choose Symbol (%d items loaded)" ), dataPtr->GetItemCount() );
195
196 DIALOG_CHOOSE_SYMBOL dlg( this, dialogTitle, dataPtr, aConvert, aAllowFields, aShowFootprints,
197 aUseLibBrowser );
198
199 int ret = dlg.ShowModal();
200
201 // Save any changes to column widths, etc.
202 modelAdapter->SaveSettings();
203
204 if( ret == wxID_CANCEL )
205 return PICKED_SYMBOL();
206
207 PICKED_SYMBOL sel;
208 LIB_ID id = dlg.GetSelectedLibId( &sel.Unit );
209
210 if( dlg.IsExternalBrowserSelected() ) // User requested symbol browser.
211 {
212 sel = PickSymbolFromLibBrowser( this, aFilter, id, sel.Unit, sel.Convert );
213 id = sel.LibId;
214 }
215
216 if( !id.IsValid() ) // Dialog closed by OK button,
217 // or the selection by lib browser was requested,
218 // but no symbol selected
219 return PICKED_SYMBOL();
220
221 if( sel.Unit == 0 )
222 sel.Unit = 1;
223
224 sel.Fields = dlg.GetFields();
225 sel.LibId = id;
226
227 if( sel.LibId.IsValid() )
228 {
229 alg::delete_if( aHistoryList, [&sel]( PICKED_SYMBOL const& i )
230 {
231 return i.LibId == sel.LibId;
232 } );
233
234 aHistoryList.insert( aHistoryList.begin(), sel );
235 }
236
237 return sel;
238}
239
240
241void SCH_EDIT_FRAME::SelectUnit( SCH_SYMBOL* aSymbol, int aUnit )
242{
243 SCH_COMMIT commit( m_toolManager );
244 LIB_SYMBOL* symbol = GetLibSymbol( aSymbol->GetLibId() );
245
246 if( !symbol )
247 return;
248
249 int unitCount = symbol->GetUnitCount();
250
251 if( unitCount <= 1 || aSymbol->GetUnit() == aUnit )
252 return;
253
254 if( aUnit > unitCount )
255 aUnit = unitCount;
256
257 if( !aSymbol->GetEditFlags() ) // No command in progress: save in undo list
258 commit.Modify( aSymbol, GetScreen() );
259
260 /* Update the unit number. */
261 aSymbol->SetUnitSelection( &GetCurrentSheet(), aUnit );
262 aSymbol->SetUnit( aUnit );
263
264 if( !commit.Empty() )
265 {
266 if( eeconfig()->m_AutoplaceFields.enable )
267 aSymbol->AutoAutoplaceFields( GetScreen() );
268
269 commit.Push( _( "Change Unit" ) );
270 }
271}
272
273
275{
276 if( !aSymbol || !aSymbol->GetLibSymbolRef() )
277 return;
278
279 SCH_COMMIT commit( m_toolManager );
280 wxString msg;
281
282 if( !aSymbol->GetLibSymbolRef()->HasConversion() )
283 {
284 LIB_ID id = aSymbol->GetLibSymbolRef()->GetLibId();
285
286 msg.Printf( _( "No alternate body style found for symbol '%s' in library '%s'." ),
287 id.GetLibItemName().wx_str(),
288 id.GetLibNickname().wx_str() );
289 DisplayError( this, msg );
290 return;
291 }
292
293 commit.Modify( aSymbol, GetScreen() );
294
295 aSymbol->SetConvert( aSymbol->GetConvert() + 1 );
296
297 // ensure m_convert = 1 or 2
298 // 1 = shape 1 = not converted
299 // 2 = shape 2 = first converted shape
300 // > 2 is not currently supported
301 // When m_convert = val max, return to the first shape
304
305 // If selected make sure all the now-included pins are selected
306 if( aSymbol->IsSelected() )
308
309 commit.Push( _( "Convert Symbol" ) );
310}
311
312
313void SCH_EDIT_FRAME::SetAltPinFunction( SCH_PIN* aPin, const wxString& aFunction )
314{
315 if( !aPin )
316 return;
317
318 SCH_COMMIT commit( m_toolManager );
319 commit.Modify( aPin, GetScreen() );
320
321 if( aFunction == aPin->GetName() )
322 aPin->SetAlt( wxEmptyString );
323 else
324 aPin->SetAlt( aFunction );
325
326 commit.Push( _( "Set Pin Function" ) );
327}
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
bool Empty() const
Returns status of an item.
Definition: commit.h:142
Dialog class to select a symbol from the libraries.
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
static std::mutex g_Mutex
std::vector< std::pair< int, wxString > > GetFields() const
Get a list of fields edited by the user.
bool IsExternalBrowserSelected() const
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:129
bool IsSelected() const
Definition: eda_item.h:106
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:180
static TOOL_ACTION addItemToSel
Selects an item (specified as the event parameter).
Definition: ee_actions.h:59
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:432
Field object used in symbol libraries.
Definition: lib_field.h:62
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
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
@ BASE
Definition: lib_item.h:77
@ DEMORGAN
Definition: lib_item.h:77
Define a library symbol object.
Definition: lib_symbol.h:99
int GetUnitCount() const override
For items with units, return the number of units.
bool GetIsVisible() const
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
void SetPreselectNode(const LIB_ID &aLibId, int aUnit)
Set the symbol name to be selected if there are no search results.
void DoAddLibrary(const wxString &aNodeName, const wxString &aDesc, const std::vector< LIB_TREE_ITEM * > &aItemList, bool pinned, bool presorted)
Add the given list of symbols by alias.
void SetFilter(SYM_FILTER_TYPE aFilter)
Set the symbol filter type.
void AssignIntrinsicRanks()
Sort the tree and assign ranks after adding libraries.
SYM_FILTER_TYPE GetFilter() const
Return the active filter.
void SaveSettings()
Save the column widths to the config file.
@ SYM_FILTER_POWER
list symbols flagged PWR
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:69
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:158
PICKED_SYMBOL PickSymbolFromLibBrowser(wxTopLevelWindow *aParent, const SYMBOL_LIBRARY_FILTER *aFilter, const LIB_ID &aPreselectedLibId, int aUnit, int aConvert)
Call the library viewer to select symbol to import into schematic.
Definition: picksymbol.cpp:47
PICKED_SYMBOL PickSymbolFromLibTree(const SYMBOL_LIBRARY_FILTER *aFilter, std::vector< PICKED_SYMBOL > &aHistoryList, bool aUseLibBrowser, int aUnit, int aConvert, bool aShowFootprints, const LIB_ID *aHighlight=nullptr, bool aAllowFields=true)
Call the library viewer to select symbol to import into schematic.
Definition: picksymbol.cpp:93
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
Revert the commit by restoring the modified items state.
Definition: sch_commit.cpp:350
void ConvertPart(SCH_SYMBOL *aSymbol)
Definition: picksymbol.cpp:274
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)
Definition: picksymbol.cpp:313
SCH_SHEET_PATH & GetCurrentSheet() const
void SelectUnit(SCH_SYMBOL *aSymbol, int aUnit)
Definition: picksymbol.cpp:241
void AutoAutoplaceFields(SCH_SCREEN *aScreen)
Autoplace fields only if correct to do so automatically.
Definition: sch_item.h:455
void SetAlt(const wxString &aAlt)
Definition: sch_pin.h:65
wxString GetName() const
Definition: sch_pin.cpp:85
Schematic symbol object.
Definition: sch_symbol.h:81
void SetConvert(int aConvert)
Definition: sch_symbol.cpp:455
int GetConvert() const
Definition: sch_symbol.h:268
void SetUnit(int aUnit)
Change the unit number to aUnit.
Definition: sch_symbol.cpp:443
const LIB_ID & GetLibId() const
Definition: sch_symbol.h:173
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
Definition: sch_symbol.cpp:856
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:190
Helper object to filter a list of libraries.
const wxArrayString & GetAllowedLibList() const
SYMBOL_LIB_TABLE_ROW * FindRow(const wxString &aNickName, bool aCheckIfEnabled=false)
Return an SYMBOL_LIB_TABLE_ROW if aNickName is found in this table or in any chained fallBack table f...
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(EDA_BASE_FRAME *aParent, LIB_TABLE *aLibs)
Factory function: create a model adapter in a reference-counting container.
bool AddLibraries(const std::vector< wxString > &aNicknames, SCH_BASE_FRAME *aFrame)
Add all the libraries in a SYMBOL_LIB_TABLE to the model.
void AddLibrary(wxString const &aLibNickname, bool pinned)
Symbol library viewer main window.
void SetUnitAndConvert(int aUnit, int aConvert)
Set unit and convert, and set flag preventing them from automatically resetting to 1.
void SetSelectedLibrary(const wxString &aLibName, const wxString &aSubLibName=wxEmptyString)
Set the selected library in the library window.
bool ShowModal(wxString *aSymbol, wxWindow *aParent) override
Runs the symbol viewer as a modal dialog.
void SetSelectedSymbol(const wxString &aSymbolName)
Set the selected symbol.
void SetFilter(const SYMBOL_LIBRARY_FILTER *aFilter)
Set a filter to display only libraries and/or symbols which match the filter.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:165
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:145
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:280
This file is part of the common library.
#define _(s)
@ FRAME_SCH_VIEWER
Definition: frame_type.h:36
@ FRAME_SCH_VIEWER_MODAL
Definition: frame_type.h:37
KIWAY Kiway
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
Definition: kicad_algo.h:173
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:99
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:115
std::vector< wxString > pinned_symbol_libs
LIB_ID LibId
Definition: sch_screen.h:80
std::vector< std::pair< int, wxString > > Fields
Definition: sch_screen.h:84