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-2017 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/tokenzr.h>
23 #include <wx/progdlg.h>
24 
25 #include <eda_pattern_match.h>
26 #include <symbol_lib_table.h>
27 #include <class_libentry.h>
28 #include <generate_alias_info.h>
29 
31 
32 
34 
35 #define PROGRESS_INTERVAL_MILLIS 66
36 
37 
39 {
40  return PTR( new SYMBOL_TREE_MODEL_ADAPTER( aLibs ) );
41 }
42 
43 
45  : m_libs( (SYMBOL_LIB_TABLE*) aLibs )
46 {}
47 
48 
50 {}
51 
52 
53 void SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
54  wxWindow* aParent )
55 {
56  wxProgressDialog* prg = nullptr;
57  wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
58 
59  if( m_show_progress )
60  {
61  prg = new wxProgressDialog( _( "Loading Symbol Libraries" ), wxEmptyString,
62  aNicknames.size(), aParent );
63  }
64 
65  unsigned int ii = 0;
66 
67  for( const auto& nickname : aNicknames )
68  {
69  if( prg && wxGetUTCTimeMillis() > nextUpdate )
70  {
71  prg->Update( ii, wxString::Format( _( "Loading library \"%s\"" ), nickname ) );
72  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
73  }
74 
75  AddLibrary( nickname );
76  ii++;
77  }
78 
80 
81  if( prg )
82  {
83  prg->Destroy();
84  m_show_progress = false;
85  }
86 }
87 
88 
89 void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname )
90 {
91  bool onlyPowerSymbols = ( GetFilter() == CMP_FILTER_POWER );
92  std::vector<LIB_ALIAS*> alias_list;
93  std::vector<LIB_TREE_ITEM*> comp_list;
94 
95  try
96  {
97  m_libs->LoadSymbolLib( alias_list, aLibNickname, onlyPowerSymbols );
98  }
99  catch( const IO_ERROR& ioe )
100  {
101  wxLogError( wxString::Format( _( "Error loading symbol library %s.\n\n%s" ),
102  aLibNickname,
103  ioe.What() ) );
104  return;
105  }
106 
107  if( alias_list.size() > 0 )
108  {
109  comp_list.assign( alias_list.begin(), alias_list.end() );
110  DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, false );
111  }
112 }
113 
114 
115 wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
116 {
117  return GenerateAliasInfo( m_libs, aLibId, aUnit );
118 }
void DoAddLibrary(wxString const &aNodeName, wxString const &aDesc, std::vector< LIB_TREE_ITEM * > const &aItemList, bool presorted)
Add the given list of components by alias.
void AddLibrary(wxString const &aLibNickname)
wxString GenerateInfo(LIB_ID const &aLibId, int aUnit) override
void LoadSymbolLib(std::vector< LIB_ALIAS * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
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.
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.
SYMBOL_TREE_MODEL_ADAPTER(LIB_TABLE *aLibs)
Constructor; takes a set of libraries to be included in the search.
CMP_FILTER_TYPE GetFilter() const
Return the active filter.
const wxString GetDescription(const wxString &aNickname)
Class LIB_ITEM definition.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
static PTR Create(LIB_TABLE *aLibs)
Factory function: create a model adapter in a reference-counting container.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
static bool m_show_progress
Flag to only show the symbol library table load progress dialog the first time.
void AddLibraries(const std::vector< wxString > &aNicknames, wxWindow *aParent)
Add all the libraries in a SYMBOL_LIB_TABLE to the model.
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.
#define PROGRESS_INTERVAL_MILLIS
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.