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 
39 wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
41 {
42  auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
43  return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
44 }
45 
46 
48  LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
49  m_libs( (SYMBOL_LIB_TABLE*) aLibs )
50 {}
51 
52 
54 {}
55 
56 
57 void SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
58  wxWindow* aParent )
59 {
60  APP_PROGRESS_DIALOG* prg = nullptr;
61  wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
62 
63  if( m_show_progress )
64  {
65  prg = new APP_PROGRESS_DIALOG( _( "Loading Symbol Libraries" ), wxEmptyString,
66  aNicknames.size(), aParent );
67  }
68 
69  unsigned int ii = 0;
70 
71  for( const auto& nickname : aNicknames )
72  {
73  if( prg && wxGetUTCTimeMillis() > nextUpdate )
74  {
75  prg->Update( ii, wxString::Format( _( "Loading library \"%s\"" ), nickname ) );
76 
77  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
78  }
79 
80  AddLibrary( nickname );
81  ii++;
82  }
83 
85 
86  if( prg )
87  {
88  prg->Destroy();
89  m_show_progress = false;
90  }
91 }
92 
93 
94 void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname )
95 {
96  bool onlyPowerSymbols = ( GetFilter() == CMP_FILTER_POWER );
97  std::vector<LIB_PART*> symbols;
98  std::vector<LIB_TREE_ITEM*> comp_list;
99 
100  try
101  {
102  m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
103  }
104  catch( const IO_ERROR& ioe )
105  {
106  wxLogError( wxString::Format( _( "Error loading symbol library %s.\n\n%s" ),
107  aLibNickname,
108  ioe.What() ) );
109  return;
110  }
111 
112  if( symbols.size() > 0 )
113  {
114  comp_list.assign( symbols.begin(), symbols.end() );
115  DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, false );
116  }
117 }
118 
119 
120 wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
121 {
122  return GenerateAliasInfo( m_libs, aLibId, aUnit );
123 }
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
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.
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: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 Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
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.
#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.
#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.