KiCad PCB EDA Suite
getpart.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 <stambaughw@gmail.com>
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 <core/kicad_algo.h>
27 #include <symbol_library.h>
28 #include <confirm.h>
29 #include <eeschema_id.h>
30 #include <general.h>
31 #include <kiway.h>
32 #include <symbol_viewer_frame.h>
33 #include <sch_symbol.h>
34 #include <sch_edit_frame.h>
35 #include <symbol_lib_table.h>
36 #include <tool/tool_manager.h>
37 #include <tools/ee_actions.h>
38 
39 #include <dialog_choose_symbol.h>
41 
43  const SCHLIB_FILTER* aFilter,
44  const LIB_ID& aPreselectedLibId,
45  int aUnit, int aConvert )
46 {
47  // Close any open non-modal Lib browser, and open a new one, in "modal" mode:
49 
50  if( viewer )
51  viewer->Destroy();
52 
53  viewer = (SYMBOL_VIEWER_FRAME*) Kiway().Player( FRAME_SCH_VIEWER_MODAL, true, aParent );
54 
55  if( aFilter )
56  viewer->SetFilter( aFilter );
57 
58  if( aPreselectedLibId.IsValid() )
59  {
60  viewer->SetSelectedLibrary( aPreselectedLibId.GetLibNickname() );
61  viewer->SetSelectedSymbol( aPreselectedLibId.GetLibItemName());
62  }
63 
64  viewer->SetUnitAndConvert( aUnit, aConvert );
65 
66  viewer->Refresh();
67 
68  PICKED_SYMBOL sel;
69  wxString symbol;
70 
71  if( viewer->ShowModal( &symbol, aParent ) )
72  {
73  LIB_ID id;
74 
75  if( id.Parse( symbol ) == -1 )
76  sel.LibId = id;
77 
78  sel.Unit = viewer->GetUnit();
79  sel.Convert = viewer->GetConvert();
80  }
81 
82  viewer->Destroy();
83 
84  return sel;
85 }
86 
87 
89  std::vector<PICKED_SYMBOL>& aHistoryList,
90  bool aUseLibBrowser, int aUnit, int aConvert,
91  bool aShowFootprints, const LIB_ID* aHighlight,
92  bool aAllowFields )
93 {
94  std::unique_lock<std::mutex> dialogLock( DIALOG_CHOOSE_SYMBOL::g_Mutex, std::defer_lock );
95  SYMBOL_LIB_TABLE* libs = Prj().SchSymbolLibTable();
96 
97  // One DIALOG_CHOOSE_SYMBOL dialog at a time. User probably can't handle more anyway.
98  if( !dialogLock.try_lock() )
99  return PICKED_SYMBOL();
100 
101  wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER> adapter = SYMBOL_TREE_MODEL_ADAPTER::Create( this, libs );
102  bool loaded = false;
103 
104  if( aFilter )
105  {
106  const wxArrayString& liblist = aFilter->GetAllowedLibList();
107 
108  for( unsigned ii = 0; ii < liblist.GetCount(); ii++ )
109  {
110  if( libs->HasLibrary( liblist[ii], true ) )
111  {
112  loaded = true;
113  static_cast<SYMBOL_TREE_MODEL_ADAPTER*>( adapter.get() )->AddLibrary( liblist[ii] );
114  }
115  }
116 
117  adapter->AssignIntrinsicRanks();
118 
119  if( aFilter->GetFilterPowerSymbols() )
120  adapter->SetFilter( SYMBOL_TREE_MODEL_ADAPTER::SYM_FILTER_POWER );
121  }
122 
123  std::vector< LIB_TREE_ITEM* > history_list;
124 
125  for( const PICKED_SYMBOL& i : aHistoryList )
126  {
127  LIB_SYMBOL* symbol = GetLibSymbol( i.LibId );
128 
129  // This can be null, for example when a symbol has been deleted from a library
130  if( symbol )
131  history_list.push_back( symbol );
132  }
133 
134  adapter->DoAddLibrary( "-- " + _( "Recently Used" ) + " --", wxEmptyString, history_list,
135  true );
136 
137  if( !aHistoryList.empty() )
138  adapter->SetPreselectNode( aHistoryList[0].LibId, aHistoryList[0].Unit );
139 
140  const std::vector< wxString > libNicknames = libs->GetLogicalLibs();
141 
142  if( !loaded )
143  static_cast<SYMBOL_TREE_MODEL_ADAPTER*>( adapter.get() )->AddLibraries( libNicknames,
144  this );
145 
146  if( aHighlight && aHighlight->IsValid() )
147  adapter->SetPreselectNode( *aHighlight, /* aUnit */ 0 );
148 
149  wxString dialogTitle;
150 
151  if( adapter->GetFilter() == SYMBOL_TREE_MODEL_ADAPTER::SYM_FILTER_POWER )
152  dialogTitle.Printf( _( "Choose Power Symbol (%d items loaded)" ), adapter->GetItemCount() );
153  else
154  dialogTitle.Printf( _( "Choose Symbol (%d items loaded)" ), adapter->GetItemCount() );
155 
156  DIALOG_CHOOSE_SYMBOL dlg( this, dialogTitle, adapter, aConvert, aAllowFields, aShowFootprints,
157  aUseLibBrowser );
158 
159  if( dlg.ShowModal() == wxID_CANCEL )
160  return PICKED_SYMBOL();
161 
162  PICKED_SYMBOL sel;
163  LIB_ID id = dlg.GetSelectedLibId( &sel.Unit );
164 
165  if( dlg.IsExternalBrowserSelected() ) // User requested symbol browser.
166  {
167  sel = PickSymbolFromLibBrowser( this, aFilter, id, sel.Unit, sel.Convert );
168  id = sel.LibId;
169  }
170 
171  if( !id.IsValid() ) // Dialog closed by OK button,
172  // or the selection by lib browser was requested,
173  // but no symbol selected
174  return PICKED_SYMBOL();
175 
176  if( sel.Unit == 0 )
177  sel.Unit = 1;
178 
179  sel.Fields = dlg.GetFields();
180  sel.LibId = id;
181 
182  if( sel.LibId.IsValid() )
183  {
184  alg::delete_if( aHistoryList, [&sel]( PICKED_SYMBOL const& i )
185  {
186  return i.LibId == sel.LibId;
187  } );
188 
189  aHistoryList.insert( aHistoryList.begin(), sel );
190  }
191 
192  return sel;
193 }
194 
195 
196 void SCH_EDIT_FRAME::SelectUnit( SCH_SYMBOL* aSymbol, int aUnit )
197 {
198  LIB_SYMBOL* symbol = GetLibSymbol( aSymbol->GetLibId() );
199 
200  if( !symbol )
201  return;
202 
203  int unitCount = symbol->GetUnitCount();
204 
205  if( unitCount <= 1 || aSymbol->GetUnit() == aUnit )
206  return;
207 
208  if( aUnit > unitCount )
209  aUnit = unitCount;
210 
211  EDA_ITEM_FLAGS savedFlags = aSymbol->GetFlags();
212 
213  if( !aSymbol->GetEditFlags() ) // No command in progress: save in undo list
214  SaveCopyInUndoList( GetScreen(), aSymbol, UNDO_REDO::CHANGED, false );
215 
216  /* Update the unit number. */
217  aSymbol->SetUnitSelection( &GetCurrentSheet(), aUnit );
218  aSymbol->SetUnit( aUnit );
219  aSymbol->ClearFlags();
220  aSymbol->SetFlags( savedFlags ); // Restore m_Flag modified by SetUnit()
221 
222  if( !aSymbol->GetEditFlags() ) // No command in progress: update schematic
223  {
225  aSymbol->AutoAutoplaceFields( GetScreen() );
226 
228 
229  UpdateItem( aSymbol, false, true );
230  OnModify();
231  }
232 }
233 
234 
236 {
237  if( !aSymbol || !aSymbol->GetLibSymbolRef() )
238  return;
239 
240  wxString msg;
241 
242  if( !aSymbol->GetLibSymbolRef()->HasConversion() )
243  {
244  LIB_ID id = aSymbol->GetLibSymbolRef()->GetLibId();
245 
246  msg.Printf( _( "No alternate body style found for symbol '%s' in library '%s'." ),
247  id.GetLibItemName().wx_str(),
248  id.GetLibNickname().wx_str() );
249  DisplayError( this, msg );
250  return;
251  }
252 
253  EDA_ITEM_FLAGS savedFlags = aSymbol->GetFlags();
254 
255  aSymbol->SetConvert( aSymbol->GetConvert() + 1 );
256 
257  // ensure m_convert = 1 or 2
258  // 1 = shape 1 = not converted
259  // 2 = shape 2 = first converted shape
260  // > 2 is not currently supported
261  // When m_convert = val max, return to the first shape
262  if( aSymbol->GetConvert() > LIB_ITEM::LIB_CONVERT::DEMORGAN )
263  aSymbol->SetConvert( LIB_ITEM::LIB_CONVERT::BASE );
264 
266  aSymbol->ClearFlags();
267  aSymbol->SetFlags( savedFlags ); // Restore m_flags (modified by SetConvert())
268 
269  // If selected make sure all the now-included pins are selected
270  if( aSymbol->IsSelected() )
272 
273  UpdateItem( aSymbol, false, true );
274  OnModify();
275 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
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.
bool IsSelected() const
Definition: eda_item.h:123
void SetUnit(int aUnit)
Change the unit number to aUnit.
Definition: sch_symbol.cpp:331
This file is part of the common library.
void ConvertPart(SCH_SYMBOL *aSymbol)
Definition: getpart.cpp:235
void AutoAutoplaceFields(SCH_SCREEN *aScreen)
Autoplace fields only if correct to do so automatically.
Definition: sch_item.h:430
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:153
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:383
std::uint32_t EDA_ITEM_FLAGS
void SelectUnit(SCH_SYMBOL *aSymbol, int aUnit)
Definition: getpart.cpp:196
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
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:547
Dialog class to select a symbol from the libraries.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:164
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: getpart.cpp:88
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:154
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:158
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: getpart.cpp:42
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:155
void SetConvert(int aConvert)
Definition: sch_symbol.cpp:347
void SetFilter(const SCHLIB_FILTER *aFilter)
Set a filter to display only libraries and/or symbols which match the filter.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:158
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:223
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
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:147