KiCad PCB EDA Suite
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-2022 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>
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 if( dlg.ShowModal() == wxID_CANCEL )
200 return PICKED_SYMBOL();
201
202 // Save any changes to column widths, etc.
203 modelAdapter->SaveSettings();
204
205 PICKED_SYMBOL sel;
206 LIB_ID id = dlg.GetSelectedLibId( &sel.Unit );
207
208 if( dlg.IsExternalBrowserSelected() ) // User requested symbol browser.
209 {
210 sel = PickSymbolFromLibBrowser( this, aFilter, id, sel.Unit, sel.Convert );
211 id = sel.LibId;
212 }
213
214 if( !id.IsValid() ) // Dialog closed by OK button,
215 // or the selection by lib browser was requested,
216 // but no symbol selected
217 return PICKED_SYMBOL();
218
219 if( sel.Unit == 0 )
220 sel.Unit = 1;
221
222 sel.Fields = dlg.GetFields();
223 sel.LibId = id;
224
225 if( sel.LibId.IsValid() )
226 {
227 alg::delete_if( aHistoryList, [&sel]( PICKED_SYMBOL const& i )
228 {
229 return i.LibId == sel.LibId;
230 } );
231
232 aHistoryList.insert( aHistoryList.begin(), sel );
233 }
234
235 return sel;
236}
237
238
239void SCH_EDIT_FRAME::SelectUnit( SCH_SYMBOL* aSymbol, int aUnit )
240{
241 LIB_SYMBOL* symbol = GetLibSymbol( aSymbol->GetLibId() );
242
243 if( !symbol )
244 return;
245
246 int unitCount = symbol->GetUnitCount();
247
248 if( unitCount <= 1 || aSymbol->GetUnit() == aUnit )
249 return;
250
251 if( aUnit > unitCount )
252 aUnit = unitCount;
253
254 EDA_ITEM_FLAGS savedFlags = aSymbol->GetFlags();
255
256 if( !aSymbol->GetEditFlags() ) // No command in progress: save in undo list
257 SaveCopyInUndoList( GetScreen(), aSymbol, UNDO_REDO::CHANGED, false );
258
259 /* Update the unit number. */
260 aSymbol->SetUnitSelection( &GetCurrentSheet(), aUnit );
261 aSymbol->SetUnit( aUnit );
262 aSymbol->ClearFlags();
263 aSymbol->SetFlags( savedFlags ); // Restore m_Flag modified by SetUnit()
264
265 if( !aSymbol->GetEditFlags() ) // No command in progress: update schematic
266 {
267 if( eeconfig()->m_AutoplaceFields.enable )
268 aSymbol->AutoAutoplaceFields( GetScreen() );
269
271
272 UpdateItem( aSymbol, false, true );
273 OnModify();
274 }
275}
276
277
279{
280 if( !aSymbol || !aSymbol->GetLibSymbolRef() )
281 return;
282
283 wxString msg;
284
285 if( !aSymbol->GetLibSymbolRef()->HasConversion() )
286 {
287 LIB_ID id = aSymbol->GetLibSymbolRef()->GetLibId();
288
289 msg.Printf( _( "No alternate body style found for symbol '%s' in library '%s'." ),
290 id.GetLibItemName().wx_str(),
291 id.GetLibNickname().wx_str() );
292 DisplayError( this, msg );
293 return;
294 }
295
296 EDA_ITEM_FLAGS savedFlags = aSymbol->GetFlags();
297
298 aSymbol->SetConvert( aSymbol->GetConvert() + 1 );
299
300 // ensure m_convert = 1 or 2
301 // 1 = shape 1 = not converted
302 // 2 = shape 2 = first converted shape
303 // > 2 is not currently supported
304 // When m_convert = val max, return to the first shape
305 if( aSymbol->GetConvert() > LIB_ITEM::LIB_CONVERT::DEMORGAN )
306 aSymbol->SetConvert( LIB_ITEM::LIB_CONVERT::BASE );
307
309 aSymbol->ClearFlags();
310 aSymbol->SetFlags( savedFlags ); // Restore m_flags (modified by SetConvert())
311
312 // If selected make sure all the now-included pins are selected
313 if( aSymbol->IsSelected() )
315
316 UpdateItem( aSymbol, false, true );
317 OnModify();
318}
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
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:147
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:142
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:143
bool IsSelected() const
Definition: eda_item.h:107
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:144
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:163
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.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
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:394
Field object used in symbol libraries.
Definition: lib_field.h:60
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:165
const UTF8 & GetLibItemName() const
Definition: lib_id.h:101
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
Define a library symbol object.
Definition: lib_symbol.h:98
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:65
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:148
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.
void ConvertPart(SCH_SYMBOL *aSymbol)
Definition: picksymbol.cpp:278
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag and update other data struc...
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend, bool aDirtyConnectivity=true)
Create a copy of the current schematic item, and put it in the undo list.
SCH_SHEET_PATH & GetCurrentSheet() const
void SelectUnit(SCH_SYMBOL *aSymbol, int aUnit)
Definition: picksymbol.cpp:239
void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false, bool aUpdateRtree=false) override
Mark an item for refresh.
void TestDanglingEnds()
Test all of the connectable objects in the schematic for unused connection points.
void AutoAutoplaceFields(SCH_SCREEN *aScreen)
Autoplace fields only if correct to do so automatically.
Definition: sch_item.h:436
Schematic symbol object.
Definition: sch_symbol.h:80
void SetConvert(int aConvert)
Definition: sch_symbol.cpp:402
int GetConvert() const
Definition: sch_symbol.h:267
void SetUnit(int aUnit)
Change the unit number to aUnit.
Definition: sch_symbol.cpp:386
const LIB_ID & GetLibId() const
Definition: sch_symbol.h:172
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
Definition: sch_symbol.cpp:744
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:189
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.
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.
void SetSelectedLibrary(const wxString &aLibName)
Set the selected library in the library window.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:170
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:142
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)
std::uint32_t EDA_ITEM_FLAGS
@ FRAME_SCH_VIEWER
Definition: frame_type.h:36
@ FRAME_SCH_VIEWER_MODAL
Definition: frame_type.h:37
bool IsValid(const std::string &aString, SIM_VALUE::TYPE aValueType=SIM_VALUE::TYPE_FLOAT, NOTATION aNotation=NOTATION::SI)
Definition: sim_value.cpp:127
PARSE_RESULT Parse(const std::string &aString, NOTATION aNotation=NOTATION::SI, SIM_VALUE::TYPE aValueType=SIM_VALUE::TYPE_FLOAT)
Definition: sim_value.cpp:189
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:111
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