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-2021 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>
28 #include <core/kicad_algo.h>
29 #include <symbol_library.h>
30 #include <confirm.h>
31 #include <eeschema_id.h>
32 #include <general.h>
33 #include <kiway.h>
34 #include <symbol_viewer_frame.h>
37 #include <sch_symbol.h>
38 #include <sch_edit_frame.h>
39 #include <symbol_lib_table.h>
40 #include <tool/tool_manager.h>
41 #include <tools/ee_actions.h>
42 
43 #include <dialog_choose_symbol.h>
44 
46  const SCHLIB_FILTER* aFilter,
47  const LIB_ID& aPreselectedLibId,
48  int aUnit, int aConvert )
49 {
50  // Close any open non-modal Lib browser, and open a new one, in "modal" mode:
52 
53  if( viewer )
54  viewer->Destroy();
55 
56  viewer = (SYMBOL_VIEWER_FRAME*) Kiway().Player( FRAME_SCH_VIEWER_MODAL, true, aParent );
57 
58  if( aFilter )
59  viewer->SetFilter( aFilter );
60 
61  if( aPreselectedLibId.IsValid() )
62  {
63  viewer->SetSelectedLibrary( aPreselectedLibId.GetLibNickname() );
64  viewer->SetSelectedSymbol( aPreselectedLibId.GetLibItemName());
65  }
66 
67  viewer->SetUnitAndConvert( aUnit, aConvert );
68 
69  viewer->Refresh();
70 
71  PICKED_SYMBOL sel;
72  wxString symbol;
73 
74  if( viewer->ShowModal( &symbol, aParent ) )
75  {
76  LIB_ID id;
77 
78  if( id.Parse( symbol ) == -1 )
79  sel.LibId = id;
80 
81  sel.Unit = viewer->GetUnit();
82  sel.Convert = viewer->GetConvert();
83  }
84 
85  viewer->Destroy();
86 
87  return sel;
88 }
89 
90 
92  std::vector<PICKED_SYMBOL>& aHistoryList,
93  bool aUseLibBrowser, int aUnit, int aConvert,
94  bool aShowFootprints, const LIB_ID* aHighlight,
95  bool aAllowFields )
96 {
97  std::unique_lock<std::mutex> dialogLock( DIALOG_CHOOSE_SYMBOL::g_Mutex, std::defer_lock );
98  SYMBOL_LIB_TABLE* libs = Prj().SchSymbolLibTable();
99 
100  // One DIALOG_CHOOSE_SYMBOL dialog at a time. User probably can't handle more anyway.
101  if( !dialogLock.try_lock() )
102  return PICKED_SYMBOL();
103 
104  // Make sure settings are loaded before we start running multi-threaded symbol loaders
105  Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
106  Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
107 
108  wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER> dataPtr
109  = SYMBOL_TREE_MODEL_ADAPTER::Create( this, libs );
110  SYMBOL_TREE_MODEL_ADAPTER* modelAdapter
111  = static_cast<SYMBOL_TREE_MODEL_ADAPTER*>( dataPtr.get() );
112  bool loaded = false;
113 
114  if( aFilter )
115  {
116  const wxArrayString& liblist = aFilter->GetAllowedLibList();
117 
118  for( unsigned ii = 0; ii < liblist.GetCount(); ii++ )
119  {
120  if( libs->HasLibrary( liblist[ii], true ) )
121  {
122  loaded = true;
123  modelAdapter->AddLibrary( liblist[ii] );
124  }
125  }
126 
127  modelAdapter->AssignIntrinsicRanks();
128 
129  if( aFilter->GetFilterPowerSymbols() )
131  }
132 
133  std::vector< LIB_TREE_ITEM* > history_list;
134 
135  for( const PICKED_SYMBOL& i : aHistoryList )
136  {
137  LIB_SYMBOL* symbol = GetLibSymbol( i.LibId );
138 
139  // This can be null, for example when a symbol has been deleted from a library
140  if( symbol )
141  history_list.push_back( symbol );
142  }
143 
144  modelAdapter->DoAddLibrary( wxT( "-- " ) + _( "Recently Used" ) + wxT( " --" ), wxEmptyString,
145  history_list, true );
146 
147  if( !aHistoryList.empty() )
148  modelAdapter->SetPreselectNode( aHistoryList[0].LibId, aHistoryList[0].Unit );
149 
150  const std::vector< wxString > libNicknames = libs->GetLogicalLibs();
151 
152  if( !loaded )
153  {
154  if( !modelAdapter->AddLibraries( libNicknames, this ) )
155  {
156  // loading cancelled by user
157  return PICKED_SYMBOL();
158  }
159  }
160 
161  if( aHighlight && aHighlight->IsValid() )
162  modelAdapter->SetPreselectNode( *aHighlight, /* aUnit */ 0 );
163 
164  wxString dialogTitle;
165 
167  dialogTitle.Printf( _( "Choose Power Symbol (%d items loaded)" ), dataPtr->GetItemCount() );
168  else
169  dialogTitle.Printf( _( "Choose Symbol (%d items loaded)" ), dataPtr->GetItemCount() );
170 
171  DIALOG_CHOOSE_SYMBOL dlg( this, dialogTitle, dataPtr, aConvert, aAllowFields, aShowFootprints,
172  aUseLibBrowser );
173 
174  if( dlg.ShowModal() == wxID_CANCEL )
175  return PICKED_SYMBOL();
176 
177  PICKED_SYMBOL sel;
178  LIB_ID id = dlg.GetSelectedLibId( &sel.Unit );
179 
180  if( dlg.IsExternalBrowserSelected() ) // User requested symbol browser.
181  {
182  sel = PickSymbolFromLibBrowser( this, aFilter, id, sel.Unit, sel.Convert );
183  id = sel.LibId;
184  }
185 
186  if( !id.IsValid() ) // Dialog closed by OK button,
187  // or the selection by lib browser was requested,
188  // but no symbol selected
189  return PICKED_SYMBOL();
190 
191  if( sel.Unit == 0 )
192  sel.Unit = 1;
193 
194  sel.Fields = dlg.GetFields();
195  sel.LibId = id;
196 
197  if( sel.LibId.IsValid() )
198  {
199  alg::delete_if( aHistoryList, [&sel]( PICKED_SYMBOL const& i )
200  {
201  return i.LibId == sel.LibId;
202  } );
203 
204  aHistoryList.insert( aHistoryList.begin(), sel );
205  }
206 
207  return sel;
208 }
209 
210 
211 void SCH_EDIT_FRAME::SelectUnit( SCH_SYMBOL* aSymbol, int aUnit )
212 {
213  LIB_SYMBOL* symbol = GetLibSymbol( aSymbol->GetLibId() );
214 
215  if( !symbol )
216  return;
217 
218  int unitCount = symbol->GetUnitCount();
219 
220  if( unitCount <= 1 || aSymbol->GetUnit() == aUnit )
221  return;
222 
223  if( aUnit > unitCount )
224  aUnit = unitCount;
225 
226  EDA_ITEM_FLAGS savedFlags = aSymbol->GetFlags();
227 
228  if( !aSymbol->GetEditFlags() ) // No command in progress: save in undo list
229  SaveCopyInUndoList( GetScreen(), aSymbol, UNDO_REDO::CHANGED, false );
230 
231  /* Update the unit number. */
232  aSymbol->SetUnitSelection( &GetCurrentSheet(), aUnit );
233  aSymbol->SetUnit( aUnit );
234  aSymbol->ClearFlags();
235  aSymbol->SetFlags( savedFlags ); // Restore m_Flag modified by SetUnit()
236 
237  if( !aSymbol->GetEditFlags() ) // No command in progress: update schematic
238  {
240  aSymbol->AutoAutoplaceFields( GetScreen() );
241 
243 
244  UpdateItem( aSymbol, false, true );
245  OnModify();
246  }
247 }
248 
249 
251 {
252  if( !aSymbol || !aSymbol->GetLibSymbolRef() )
253  return;
254 
255  wxString msg;
256 
257  if( !aSymbol->GetLibSymbolRef()->HasConversion() )
258  {
259  LIB_ID id = aSymbol->GetLibSymbolRef()->GetLibId();
260 
261  msg.Printf( _( "No alternate body style found for symbol '%s' in library '%s'." ),
262  id.GetLibItemName().wx_str(),
263  id.GetLibNickname().wx_str() );
264  DisplayError( this, msg );
265  return;
266  }
267 
268  EDA_ITEM_FLAGS savedFlags = aSymbol->GetFlags();
269 
270  aSymbol->SetConvert( aSymbol->GetConvert() + 1 );
271 
272  // ensure m_convert = 1 or 2
273  // 1 = shape 1 = not converted
274  // 2 = shape 2 = first converted shape
275  // > 2 is not currently supported
276  // When m_convert = val max, return to the first shape
277  if( aSymbol->GetConvert() > LIB_ITEM::LIB_CONVERT::DEMORGAN )
278  aSymbol->SetConvert( LIB_ITEM::LIB_CONVERT::BASE );
279 
281  aSymbol->ClearFlags();
282  aSymbol->SetFlags( savedFlags ); // Restore m_flags (modified by SetConvert())
283 
284  // If selected make sure all the now-included pins are selected
285  if( aSymbol->IsSelected() )
287 
288  UpdateItem( aSymbol, false, true );
289  OnModify();
290 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
void AssignIntrinsicRanks()
Sort the tree and assign ranks after adding libraries.
const UTF8 & GetLibItemName() const
Definition: lib_id.h:104
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
std::vector< std::pair< int, wxString > > GetFields() const
Get a list of fields edited by the user.
void SetFilter(SYM_FILTER_TYPE aFilter)
Set the symbol filter type.
bool IsSelected() const
Definition: eda_item.h:122
void SetUnit(int aUnit)
Change the unit number to aUnit.
Definition: sch_symbol.cpp:354
This file is part of the common library.
void ConvertPart(SCH_SYMBOL *aSymbol)
Definition: picksymbol.cpp:250
void AddLibrary(wxString const &aLibNickname)
void AutoAutoplaceFields(SCH_SCREEN *aScreen)
Autoplace fields only if correct to do so automatically.
Definition: sch_item.h:436
void SetPreselectNode(const LIB_ID &aLibId, int aUnit)
Set the symbol name to be selected if there are no search results.
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.
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:152
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:143
Symbol library viewer main window.
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:393
SYM_FILTER_TYPE GetFilter() const
Return the active filter.
std::uint32_t EDA_ITEM_FLAGS
void SelectUnit(SCH_SYMBOL *aSymbol, int aUnit)
Definition: picksymbol.cpp:211
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:159
Define a library symbol object.
Definition: lib_symbol.h:96
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
static std::mutex g_Mutex
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
Definition: sch_symbol.cpp:570
Dialog class to select a symbol from the libraries.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:165
EESCHEMA_SETTINGS * eeconfig() const
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
AUTOPLACE_FIELDS m_AutoplaceFields
static TOOL_ACTION addItemToSel
Selects an item (specified as the event parameter).
Definition: ee_actions.h:59
void TestDanglingEnds()
Test all of the connectable objects in the schematic for unused connection points.
PICKED_SYMBOL PickSymbolFromLibTree(const SCHLIB_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:91
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
int GetUnitCount() const override
For items with units, return the number of units.
void SetSelectedSymbol(const wxString &aSymbolName)
Set the selected symbol.
Definition for symbol library class.
#define _(s)
void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false, bool aUpdateRtree=false)
Mark an item for refresh.
void SetUnitAndConvert(int aUnit, int aConvert)
Set unit and convert, and set flag preventing them from automatically resetting to 1.
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:90
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:153
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:157
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
bool IsExternalBrowserSelected() const
PICKED_SYMBOL PickSymbolFromLibBrowser(wxTopLevelWindow *aParent, const SCHLIB_FILTER *aFilter, const LIB_ID &aPreselectedLibId, int aUnit, int aConvert)
Call the library viewer to select symbol to import into schematic.
Definition: picksymbol.cpp:45
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:154
void SetConvert(int aConvert)
Definition: sch_symbol.cpp:370
void SetFilter(const SCHLIB_FILTER *aFilter)
Set a filter to display only libraries and/or symbols which match the filter.
void DoAddLibrary(const wxString &aNodeName, const wxString &aDesc, const std::vector< LIB_TREE_ITEM * > &aItemList, bool presorted)
Add the given list of symbols by alias.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:158
see class PGM_BASE
void SetSelectedLibrary(const wxString &aLibName)
Set the selected library in the library window.
Schematic symbol object.
Definition: sch_symbol.h:78
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend)
Create a copy of the current schematic item, and put it in the undo list.
const wxArrayString & GetAllowedLibList() const
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
SCH_SHEET_PATH & GetCurrentSheet() const
LIB_SYMBOL * GetLibSymbol(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load symbol from symbol library table.
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current screen and u...
bool ShowModal(wxString *aSymbol, wxWindow *aParent) override
Runs the symbol viewer as a modal dialog.
LIB_ID LibId
Definition: sch_screen.h:82
int GetConvert() const
Definition: sch_symbol.h:224
std::vector< std::pair< int, wxString > > Fields
Definition: sch_screen.h:86
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
Definition: kicad_algo.h:173
bool AddLibraries(const std::vector< wxString > &aNicknames, wxWindow *aParent)
Add all the libraries in a SYMBOL_LIB_TABLE to the model.
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 GetFilterPowerSymbols() const
const LIB_ID & GetLibId() const
Definition: sch_symbol.h:148