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/window.h>
25 
26 #include <eda_pattern_match.h>
27 #include <symbol_lib_table.h>
28 #include <class_libentry.h>
29 #include <generate_alias_info.h>
30 
32 
33 
35 
36 #define PROGRESS_INTERVAL_MILLIS 66
37 
38 
40  LIB_TABLE* aLibs )
41 {
42  return PTR( new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs ) );
43 }
44 
45 
47  LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
48  m_libs( (SYMBOL_LIB_TABLE*) aLibs )
49 {}
50 
51 
53 {}
54 
55 
56 void SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
57  wxWindow* aParent )
58 {
59  APP_PROGRESS_DIALOG* prg = nullptr;
60  wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
61 
62  if( m_show_progress )
63  {
64  prg = new APP_PROGRESS_DIALOG( _( "Loading Symbol Libraries" ), wxEmptyString,
65  aNicknames.size(), aParent );
66  }
67 
68  unsigned int ii = 0;
69 
70  for( const auto& nickname : aNicknames )
71  {
72  if( prg && wxGetUTCTimeMillis() > nextUpdate )
73  {
74  prg->Update( ii, wxString::Format( _( "Loading library \"%s\"" ), nickname ) );
75 
76  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
77  }
78 
79  AddLibrary( nickname );
80  ii++;
81  }
82 
84 
85  if( prg )
86  {
87  prg->Destroy();
88  m_show_progress = false;
89  }
90 }
91 
92 
93 void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname )
94 {
95  bool onlyPowerSymbols = ( GetFilter() == CMP_FILTER_POWER );
96  std::vector<LIB_PART*> symbols;
97  std::vector<LIB_TREE_ITEM*> comp_list;
98 
99  try
100  {
101  m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
102  }
103  catch( const IO_ERROR& ioe )
104  {
105  wxLogError( wxString::Format( _( "Error loading symbol library %s.\n\n%s" ),
106  aLibNickname,
107  ioe.What() ) );
108  return;
109  }
110 
111  if( symbols.size() > 0 )
112  {
113  comp_list.assign( symbols.begin(), symbols.end() );
114  DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, false );
115  }
116 }
117 
118 
119 wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
120 {
121  return GenerateAliasInfo( m_libs, aLibId, aUnit );
122 }
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:29
const wxString GetDescription(const wxString &aNickname)
void LoadSymbolLib(std::vector< LIB_PART * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
virtual bool Update(int aValue, const wxString &aNewMsg=wxEmptyString, bool *aSkip=NULL) override
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:201
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.
wxProgressDialog with the option to also update the application progress on the taskbar
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.