KiCad PCB EDA Suite
footprint_info.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) 2011 Jean-Pierre Charras, <jp.charras@wanadoo.fr>
5  * Copyright (C) 2013-2016 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 
32 /*
33  * Functions to read footprint libraries and fill m_footprints by available footprints names
34  * and their documentation (comments and keywords)
35  */
36 
37 #include <class_module.h>
38 #include <common.h>
39 #include <fctsys.h>
40 #include <footprint_info.h>
41 #include <fp_lib_table.h>
42 #include <html_messagebox.h>
43 #include <io_mgr.h>
44 #include <kiface_ids.h>
45 #include <kiway.h>
46 #include <lib_id.h>
47 #include <macros.h>
48 #include <pgm_base.h>
49 #include <thread>
51 
52 
53 FOOTPRINT_INFO* FOOTPRINT_LIST::GetModuleInfo( const wxString& aFootprintName )
54 {
55  if( aFootprintName.IsEmpty() )
56  return NULL;
57 
58  for( auto& fp : m_list )
59  {
60  LIB_ID fpid;
61 
62  wxCHECK_MSG( fpid.Parse( TO_UTF8( aFootprintName ) ) < 0, NULL,
64  "'%s' is not a valid LIB_ID.", aFootprintName ) );
65 
66  wxString libNickname = FROM_UTF8( fpid.GetLibNickname() );
67  wxString footprintName = FROM_UTF8( fpid.GetLibItemName() );
68 
69  if( libNickname == fp->GetNickname() && footprintName == fp->GetFootprintName() )
70  return &*fp;
71  }
72 
73  return NULL;
74 }
75 
76 
77 bool FOOTPRINT_INFO::InLibrary( const wxString& aLibrary ) const
78 {
79  return aLibrary == m_nickname;
80 }
81 
82 
83 void FOOTPRINT_LIST::DisplayErrors( wxTopLevelWindow* aWindow )
84 {
85  // @todo: go to a more HTML !<table>! ? centric output, possibly with
86  // recommendations for remedy of errors. Add numeric error codes
87  // to PARSE_ERROR, and switch on them for remedies, etc. Full
88  // access is provided to everything in every exception!
89 
90  HTML_MESSAGE_BOX dlg( aWindow, _( "Load Error" ) );
91 
92  dlg.MessageSet( _( "Errors were encountered loading footprints:" ) );
93 
94  wxString msg;
95 
96  while( auto error = PopError() )
97  {
98  msg += wxT( "<p>" ) + error->Problem() + wxT( "</p>" );
99  }
100 
101  dlg.AddHTML_Text( msg );
102 
103  dlg.ShowModal();
104 }
105 
106 
107 static std::unique_ptr<FOOTPRINT_LIST> get_instance_from_id( KIWAY& aKiway, int aId )
108 {
109  void* ptr = nullptr;
110 
111  try
112  {
113  KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
114 
115  if( !kiface )
116  return nullptr;
117 
118  ptr = kiface->IfaceOrAddress( aId );
119 
120  if( !ptr )
121  return nullptr;
122  }
123  catch( ... )
124  {
125  return nullptr;
126  }
127 
128  return std::unique_ptr<FOOTPRINT_LIST>( (FOOTPRINT_LIST*) ( ptr ) );
129 }
130 
131 
132 std::unique_ptr<FOOTPRINT_LIST> FOOTPRINT_LIST::GetInstance( KIWAY& aKiway )
133 {
135 }
136 
137 
139 {
140 }
141 
142 
144 {
145  m_list = aList;
146 }
147 
148 
150  FP_LIB_TABLE* aTable, wxString const* aNickname, unsigned aNThreads )
151 {
152  m_started = true;
153 
154  // Capture the FP_LIB_TABLE into m_last_table. Formatting it as a string instead of storing the
155  // raw data avoids having to pull in the FP-specific parts.
156  STRING_FORMATTER sof;
157  aTable->Format( &sof, 0 );
158  m_last_table = sof.GetString();
159 
160  m_list->StartWorkers( aTable, aNickname, this, aNThreads );
161 }
162 
163 
165 {
166  if( m_list )
167  {
168  bool rv = m_list->JoinWorkers();
169  m_list = nullptr;
170  return rv;
171  }
172  else
173  return true;
174 }
175 
176 
178 {
179  if( !m_started )
180  return 0;
181  else if( m_total_libs == 0 || !m_list )
182  return 100;
183  else
184  {
185  int loaded = m_list->CountFinished();
186  int prog = ( 100 * loaded ) / m_total_libs;
187 
188  if( loaded == m_total_libs )
189  return 100;
190  else if( loaded < m_total_libs && prog >= 100 )
191  return 99;
192  else if( prog <= 0 )
193  return 1;
194  else
195  return prog;
196  }
197 }
198 
199 
200 void FOOTPRINT_ASYNC_LOADER::SetCompletionCallback( std::function<void()> aCallback )
201 {
202  m_completion_cb = aCallback;
203 }
204 
205 
207 {
208  STRING_FORMATTER sof;
209  aOther->Format( &sof, 0 );
210  return m_last_table == sof.GetString();
211 }
Return a new instance of FOOTPRINT_LIST from pcbnew.
Definition: kiface_ids.h:39
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
void SetCompletionCallback(std::function< void()> aCallback)
Set a callback to receive notice when loading is complete.
void DisplayErrors(wxTopLevelWindow *aCaller=NULL)
int Parse(const UTF8 &aId)
Function Parse.
Definition: lib_id.cpp:122
virtual bool JoinWorkers()=0
Join worker threads.
Class LIB_ID.
Definition: lib_id.h:56
FOOTPRINT_LIST * m_list
VTBL_ENTRY void * IfaceOrAddress(int aDataId)=0
Function IfaceOrAddress returns a pointer to the requested object.
static std::unique_ptr< FOOTPRINT_LIST > GetInstance(KIWAY &aKiway)
Factory function to return a new FOOTPRINT_LIST via Kiway.
FOOTPRINT_ASYNC_LOADER()
Construct an asynchronous loader.
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
FOOTPRINT_INFO * GetModuleInfo(const wxString &aFootprintName)
Get info for a module by name.
bool m_started
True if Start() has been called - does not reset.
void Start(FP_LIB_TABLE *aTable, wxString const *aNickname=nullptr, unsigned aNThreads=DEFAULT_THREADS)
Launch the worker threads.
bool InLibrary(const wxString &aLibrary) const
Test if the FOOTPRINT_INFO object was loaded from aLibrary.
const UTF8 & GetLibItemName() const
Function GetLibItemName.
Definition: lib_id.h:129
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
The common library.
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:257
pcbnew DSO
Definition: kiway.h:266
bool Join()
Wait until the worker threads are finished, and then perform any required single-threaded finishing o...
const std::string & GetString()
Definition: richio.h:475
Class HTML_MESSAGE_BOX.
VTBL_ENTRY KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Function KiFACE returns the KIFACE* given a FACE_T.
Definition: kiway.cpp:150
void MessageSet(const wxString &message)
Function MessageSet adds a message (in bold) to message list.
std::unique_ptr< IO_ERROR > PopError()
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const override
Function Format.
see class PGM_BASE
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
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
int GetProgress() const
Get the current completion percentage.
virtual size_t CountFinished()=0
Return the number of libraries finished (successfully or otherwise).
void AddHTML_Text(const wxString &message)
Function AddHTML_Text adds html text (without any change) to message list.
std::function< void()> m_completion_cb
Class KIFACE is used by a participant in the KIWAY alchemy.
Definition: kiway.h:150
FOOTPRINT_LIST * m_list
The common library.
return & kiface
Definition: pcbnew.cpp:207
Module description (excepted pads)
wxString m_nickname
library as known in FP_LIB_TABLE
const UTF8 & GetLibNickname() const
Function GetLibNickname.
Definition: lib_id.h:108
void SetList(FOOTPRINT_LIST *aList)
Assign a FOOTPRINT_LIST to the loader.
Class STRING_FORMATTER implements OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:445
static std::unique_ptr< FOOTPRINT_LIST > get_instance_from_id(KIWAY &aKiway, int aId)
bool IsSameTable(FP_LIB_TABLE *aOther)
Return true if the given table is the same as the last table loaded.
virtual void StartWorkers(FP_LIB_TABLE *aTable, wxString const *aNickname, FOOTPRINT_ASYNC_LOADER *aLoader, unsigned aNThreads)=0
Launch worker threads to load footprints.