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-2019 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  LIB_TABLE* aLibs )
40 {
41  return PTR( new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs ) );
42 }
43 
44 
46  LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
47  m_libs( (SYMBOL_LIB_TABLE*) aLibs )
48 {}
49 
50 
52 {}
53 
54 
55 void SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
56  wxWindow* aParent )
57 {
58  wxProgressDialog* prg = nullptr;
59  wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
60 
61  if( m_show_progress )
62  {
63  prg = new wxProgressDialog( _( "Loading Symbol Libraries" ), wxEmptyString,
64  aNicknames.size(), aParent );
65  }
66 
67  unsigned int ii = 0;
68 
69  for( const auto& nickname : aNicknames )
70  {
71  if( prg && wxGetUTCTimeMillis() > nextUpdate )
72  {
73  prg->Update( ii, wxString::Format( _( "Loading library \"%s\"" ), nickname ) );
74  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
75  }
76 
77  AddLibrary( nickname );
78  ii++;
79  }
80 
82 
83  if( prg )
84  {
85  prg->Destroy();
86  m_show_progress = false;
87  }
88 }
89 
90 
91 void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname )
92 {
93  bool onlyPowerSymbols = ( GetFilter() == CMP_FILTER_POWER );
94  std::vector<LIB_PART*> symbols;
95  std::vector<LIB_TREE_ITEM*> comp_list;
96 
97  try
98  {
99  m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
100  }
101  catch( const IO_ERROR& ioe )
102  {
103  wxLogError( wxString::Format( _( "Error loading symbol library %s.\n\n%s" ),
104  aLibNickname,
105  ioe.What() ) );
106  return;
107  }
108 
109  if( symbols.size() > 0 )
110  {
111  comp_list.assign( symbols.begin(), symbols.end() );
112  DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, false );
113  }
114 }
115 
116 
117 wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
118 {
119  return GenerateAliasInfo( m_libs, aLibId, aUnit );
120 }
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
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
static PTR Create(EDA_BASE_FRAME *aParent, LIB_TABLE *aLibs)
Factory function: create a model adapter in a reference-counting container.
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:33
const wxString GetDescription(const wxString &aNickname)
Class LIB_PIN definition.
void LoadSymbolLib(std::vector< LIB_PART * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
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.
#define _(s)
Definition: 3d_actions.cpp:33
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.
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
CMP_FILTER_TYPE GetFilter() const
Return the active filter.
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.