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 <pavlina.chris@gmail.com>
5  * Copyright (C) 2014 Henner Zeller <h.zeller@acm.org>
6  * Copyright (C) 2014-2021 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 66
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, "pinned_symbol_libs" ),
51  m_libs( (SYMBOL_LIB_TABLE*) aLibs )
52 {}
53 
54 
56 {}
57 
58 
59 void 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 )
86  prg->KeepRefreshing();
87 
88  wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
89  }
90 
91  if( prg && prg->IsCancelled() )
92  {
93  loader.Abort();
94  }
95  else
96  {
97  loader.Join();
98  }
99 
100  if( !loader.GetErrors().IsEmpty() )
101  {
102  HTML_MESSAGE_BOX dlg( aParent, _( "Load Error" ) );
103 
104  dlg.MessageSet( _( "Errors loading symbols:" ) );
105 
106  wxString msg = loader.GetErrors();
107  msg.Replace( "\n", "<BR>" );
108 
109  dlg.AddHTML_Text( msg );
110  dlg.ShowModal();
111  }
112 
113  if( loadedSymbols.size() > 0 )
114  {
115  for( const std::pair<const wxString, std::vector<LIB_SYMBOL*>>& pair : loadedSymbols )
116  {
117  std::vector<LIB_TREE_ITEM*> treeItems( pair.second.begin(), pair.second.end() );
118  DoAddLibrary( pair.first, m_libs->GetDescription( pair.first ), treeItems, false );
119  }
120  }
121 
122  KIID::CreateNilUuids( false );
123 
125 
126  if( prg )
127  {
128  // Force immediate deletion of the APP_PROGRESS_DIALOG
129  // ( do not use Destroy(), or use Destroy() followed by wxSafeYield() )
130  // because on Windows, APP_PROGRESS_DIALOG has some side effects on the event loop
131  // manager. A side effect is the call of ShowModal() of a dialog following
132  // the use of SYMBOL_TREE_MODEL_ADAPTER creating a APP_PROGRESS_DIALOG
133  // has a broken behavior (incorrect modal behavior).
134  prg.reset();
135  m_show_progress = false;
136  }
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)
Definition: kiid.cpp:240
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.
void AddLibraries(const std::vector< wxString > &aNicknames, wxWindow *aParent)
Add all the libraries in a SYMBOL_LIB_TABLE to the model.
#define PROGRESS_INTERVAL_MILLIS
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.