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/tokenzr.h>
23 #include <wx/window.h>
25 
26 #include <eda_pattern_match.h>
27 #include <symbol_lib_table.h>
28 #include <lib_symbol.h>
29 #include <generate_alias_info.h>
31 
32 
34 
35 #define PROGRESS_INTERVAL_MILLIS 66
36 
37 
38 wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
40 {
41  auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
42  return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
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  // Force immediate deletion of the APP_PROGRESS_DIALOG
88  // ( do not use Destroy(), or use Destroy() followed by wxSafeYield() )
89  // because on Windows, APP_PROGRESS_DIALOG has some side effects on the event loop
90  // manager. A side effect is the call of ShowModal() of a dialog following
91  // the use of SYMBOL_TREE_MODEL_ADAPTER creating a APP_PROGRESS_DIALOG
92  // has a broken behavior (incorrect modal behavior).
93  delete prg;
94  m_show_progress = false;
95  }
96 }
97 
98 
99 void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname )
100 {
101  bool onlyPowerSymbols = ( GetFilter() == CMP_FILTER_POWER );
102  std::vector<LIB_PART*> symbols;
103  std::vector<LIB_TREE_ITEM*> comp_list;
104 
105  try
106  {
107  m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
108  }
109  catch( const IO_ERROR& ioe )
110  {
111  wxLogError( wxString::Format( _( "Error loading symbol library %s.\n\n%s" ),
112  aLibNickname,
113  ioe.What() ) );
114  return;
115  }
116 
117  if( symbols.size() > 0 )
118  {
119  comp_list.assign( symbols.begin(), symbols.end() );
120  DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, false );
121  }
122 }
123 
124 
125 wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
126 {
127  return GenerateAliasInfo( m_libs, aLibId, aUnit );
128 }
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)
Output 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
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
CMP_FILTER_TYPE GetFilter() const
Return the active filter.
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.