KiCad PCB EDA Suite
symbol_tree_model_adapter.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) 2017 Chris Pavlina <[email protected]>
5  * Copyright (C) 2014 Henner Zeller <[email protected]>
6  * Copyright (C) 2014-2022 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <wx/log.h>
23 #include <wx/tokenzr.h>
24 #include <wx/window.h>
27 #include <eda_pattern_match.h>
28 #include <generate_alias_info.h>
29 #include <lib_symbol.h>
30 #include <locale_io.h>
31 #include <symbol_async_loader.h>
32 #include <symbol_lib_table.h>
34 
35 
37 
38 #define PROGRESS_INTERVAL_MILLIS 33 // 30 FPS refresh rate
39 
40 
41 wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
43 {
44  auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
45  return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
46 }
47 
48 
50  LIB_TREE_MODEL_ADAPTER( aParent, wxT( "pinned_symbol_libs" ) ),
51  m_libs( (SYMBOL_LIB_TABLE*) aLibs )
52 {}
53 
54 
56 {}
57 
58 
59 bool SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
60  wxWindow* aParent )
61 {
62  std::unique_ptr<WX_PROGRESS_REPORTER> prg = nullptr;
63 
64  if( m_show_progress )
65  {
66  prg = std::make_unique<WX_PROGRESS_REPORTER>( aParent, _( "Loading Symbol Libraries" ),
67  aNicknames.size(), true );
68  }
69 
70  // Disable KIID generation: not needed for library parts; sometimes very slow
71  KIID::CreateNilUuids( true );
72 
73  std::unordered_map<wxString, std::vector<LIB_SYMBOL*>> loadedSymbols;
74 
75  SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs,
77  &loadedSymbols, prg.get() );
78 
79  LOCALE_IO toggle;
80 
81  loader.Start();
82 
83  while( !loader.Done() )
84  {
85  if( prg && !prg->KeepRefreshing() )
86  break;
87 
88  wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
89  }
90 
91  loader.Join();
92 
93  bool cancelled = false;
94 
95  if( prg )
96  cancelled = prg->IsCancelled();
97 
98  if( !loader.GetErrors().IsEmpty() )
99  {
100  HTML_MESSAGE_BOX dlg( aParent, _( "Load Error" ) );
101 
102  dlg.MessageSet( _( "Errors loading symbols:" ) );
103 
104  wxString msg = loader.GetErrors();
105  msg.Replace( wxT( "\n" ), wxT( "<BR>" ) );
106 
107  dlg.AddHTML_Text( msg );
108  dlg.ShowModal();
109  }
110 
111  if( loadedSymbols.size() > 0 )
112  {
113  for( const std::pair<const wxString, std::vector<LIB_SYMBOL*>>& pair : loadedSymbols )
114  {
115  std::vector<LIB_TREE_ITEM*> treeItems( pair.second.begin(), pair.second.end() );
116  DoAddLibrary( pair.first, m_libs->GetDescription( pair.first ), treeItems, false );
117  }
118  }
119 
120  KIID::CreateNilUuids( false );
121 
123 
124  if( prg )
125  {
126  // Force immediate deletion of the APP_PROGRESS_DIALOG
127  // ( do not use Destroy(), or use Destroy() followed by wxSafeYield() )
128  // because on Windows, APP_PROGRESS_DIALOG has some side effects on the event loop
129  // manager. A side effect is the call of ShowModal() of a dialog following
130  // the use of SYMBOL_TREE_MODEL_ADAPTER creating a APP_PROGRESS_DIALOG
131  // has a broken behavior (incorrect modal behavior).
132  prg.reset();
133  m_show_progress = false;
134  }
135 
136  return !cancelled;
137 }
138 
139 
140 void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname )
141 {
142  bool onlyPowerSymbols = ( GetFilter() == SYM_FILTER_POWER );
143  std::vector<LIB_SYMBOL*> symbols;
144  std::vector<LIB_TREE_ITEM*> comp_list;
145 
146  try
147  {
148  m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
149  }
150  catch( const IO_ERROR& ioe )
151  {
152  wxLogError( _( "Error loading symbol library '%s'." ) + wxS( "\n%s" ),
153  aLibNickname,
154  ioe.What() );
155  return;
156  }
157 
158  if( symbols.size() > 0 )
159  {
160  comp_list.assign( symbols.begin(), symbols.end() );
161  DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, false );
162  }
163 }
164 
165 
166 wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
167 {
168  return GenerateAliasInfo( m_libs, aLibId, aUnit );
169 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
void AddLibrary(wxString const &aLibNickname)
wxString GenerateInfo(LIB_ID const &aLibId, int aUnit) override
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.
SYM_FILTER_TYPE GetFilter() const
Return the active filter.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
Abstract pattern-matching tool and implementations.
SYMBOL_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, LIB_TABLE *aLibs)
Constructor; takes a set of libraries to be included in the search.
wxString GenerateAliasInfo(SYMBOL_LIB_TABLE *aSymLibTable, LIB_ID const &aLibId, int aUnit)
Return an HTML page describing a LIB_ID in a SYMBOL_LIB_TABLE.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
const wxString GetDescription(const wxString &aNickname)
#define _(s)
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
void LoadSymbolLib(std::vector< LIB_SYMBOL * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
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.
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
static bool m_show_progress
Flag to only show the symbol library table load progress dialog the first time.
static void CreateNilUuids(bool aNil=true)
A performance optimization which disables/enables the generation of pseudo-random UUIDs.
Definition: kiid.cpp:273
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
The base frame for deriving all KiCad main window classes.
#define PROGRESS_INTERVAL_MILLIS
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
bool AddLibraries(const std::vector< wxString > &aNicknames, wxWindow *aParent)
Add all the libraries in a SYMBOL_LIB_TABLE to the model.
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.