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>
26 
28 #include <eda_pattern_match.h>
29 #include <generate_alias_info.h>
30 #include <lib_symbol.h>
31 #include <locale_io.h>
32 #include <symbol_async_loader.h>
33 #include <symbol_lib_table.h>
35 
36 
38 
39 #define PROGRESS_INTERVAL_MILLIS 66
40 
41 
42 wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
44 {
45  auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
46  return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
47 }
48 
49 
51  LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
52  m_libs( (SYMBOL_LIB_TABLE*) aLibs )
53 {}
54 
55 
57 {}
58 
59 
60 void SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
61  wxWindow* aParent )
62 {
63  std::unique_ptr<WX_PROGRESS_REPORTER> prg = nullptr;
64 
65  if( m_show_progress )
66  {
67  prg = std::make_unique<WX_PROGRESS_REPORTER>( aParent, _( "Loading Symbol Libraries" ),
68  aNicknames.size(), true );
69  }
70 
71  // Disable KIID generation: not needed for library parts; sometimes very slow
72  KIID::CreateNilUuids( true );
73 
74  std::unordered_map<wxString, std::vector<LIB_SYMBOL*>> loadedSymbols;
75 
76  SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs,
78  &loadedSymbols, prg.get() );
79 
80  LOCALE_IO toggle;
81 
82  loader.Start();
83 
84  while( !loader.Done() )
85  {
86  if( prg )
87  prg->KeepRefreshing();
88 
89  wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
90  }
91 
92  if( prg && prg->IsCancelled() )
93  {
94  loader.Abort();
95  }
96  else
97  {
98  loader.Join();
99  }
100 
101  if( !loader.GetErrors().IsEmpty() )
102  {
103  HTML_MESSAGE_BOX dlg( aParent, _( "Load Error" ) );
104 
105  dlg.MessageSet( _( "Errors were encountered loading symbols:" ) );
106 
107  wxString msg = loader.GetErrors();
108  msg.Replace( "\n", "<BR>" );
109 
110  dlg.AddHTML_Text( msg );
111  dlg.ShowModal();
112  }
113 
114  if( loadedSymbols.size() > 0 )
115  {
116  for( const std::pair<const wxString, std::vector<LIB_SYMBOL*>>& pair : loadedSymbols )
117  {
118  std::vector<LIB_TREE_ITEM*> treeItems( pair.second.begin(), pair.second.end() );
119  DoAddLibrary( pair.first, m_libs->GetDescription( pair.first ), treeItems, false );
120  }
121  }
122 
123  KIID::CreateNilUuids( false );
124 
126 
127  if( prg )
128  {
129  // Force immediate deletion of the APP_PROGRESS_DIALOG
130  // ( do not use Destroy(), or use Destroy() followed by wxSafeYield() )
131  // because on Windows, APP_PROGRESS_DIALOG has some side effects on the event loop
132  // manager. A side effect is the call of ShowModal() of a dialog following
133  // the use of SYMBOL_TREE_MODEL_ADAPTER creating a APP_PROGRESS_DIALOG
134  // has a broken behavior (incorrect modal behavior).
135  prg.reset();
136  m_show_progress = false;
137  }
138 }
139 
140 
141 void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname )
142 {
143  bool onlyPowerSymbols = ( GetFilter() == SYM_FILTER_POWER );
144  std::vector<LIB_SYMBOL*> symbols;
145  std::vector<LIB_TREE_ITEM*> comp_list;
146 
147  try
148  {
149  m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
150  }
151  catch( const IO_ERROR& ioe )
152  {
153  wxLogError( wxString::Format( _( "Error loading symbol library %s.\n\n%s" ),
154  aLibNickname,
155  ioe.What() ) );
156  return;
157  }
158 
159  if( symbols.size() > 0 )
160  {
161  comp_list.assign( symbols.begin(), symbols.end() );
162  DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, false );
163  }
164 }
165 
166 
167 wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
168 {
169  return GenerateAliasInfo( m_libs, aLibId, aUnit );
170 }
void DoAddLibrary(wxString const &aNodeName, wxString const &aDesc, std::vector< LIB_TREE_ITEM * > const &aItemList, bool presorted)
Add the given list of symbols by alias.
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)
HTML_MESSAGE_BOX.
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void LoadSymbolLib(std::vector< LIB_SYMBOL * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
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.