KiCad PCB EDA Suite
Loading...
Searching...
No Matches
fp_tree_synchronizing_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 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
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 3
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 * https://www.gnu.org/licenses/gpl-3.0.html
21 * or you may search the http://www.gnu.org website for the version 3 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#include <pgm_base.h>
33#include <project_pcb.h>
34#include <string_utils.h>
35#include <board.h>
36#include <footprint.h>
37#include <tool/tool_manager.h>
39
40#include <map>
41
42#include <wx/settings.h>
43
44
45wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
47{
48 auto* adapter = new FP_TREE_SYNCHRONIZING_ADAPTER( aFrame, aLibs );
49 return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
50}
51
52
59
60
65
66
67bool FP_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
68{
69 const LIB_TREE_NODE* node = ToNode( aItem );
70 return node ? node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY : true;
71}
72
73
75{
76 m_libs = aLibs;
77
78 // Process already stored libraries
79 for( auto it = m_tree.m_Children.begin(); it != m_tree.m_Children.end(); )
80 {
81 const wxString& name = it->get()->m_Name;
82
83 try
84 {
85 // Check the table row directly
86 std::optional<LIBRARY_TABLE_ROW*> optRow = m_libs->GetRow( name );
87 std::optional<LIB_STATUS> libStatus = m_libs->GetLibraryStatus( name );
88
89 bool loadFailed = libStatus.has_value()
90 && libStatus->load_status == LOAD_STATUS::LOAD_ERROR;
91
92 if( !optRow.has_value()
93 || ( *optRow )->Disabled()
94 || ( *optRow )->Hidden()
95 || loadFailed )
96 {
97 it = deleteLibrary( it );
98 continue;
99 }
100
101 updateLibrary( *static_cast<LIB_TREE_NODE_LIBRARY*>( it->get() ) );
102 }
103 catch( ... )
104 {
105 // If the library isn't found, remove it
106 it = deleteLibrary( it );
107 continue;
108 }
109
110 ++it;
111 }
112
113 // Look for new libraries
115 PROJECT_FILE& project = m_frame->Prj().GetProjectFile();
116 size_t count = m_libMap.size();
117
118 for( const auto& [libName, status] : m_libs->GetLibraryStatuses() )
119 {
120 if( status.load_status != LOAD_STATUS::LOADED || status.error )
121 continue;
122
123 if( m_libMap.count( libName ) != 0 )
124 continue;
125
126 std::optional<LIBRARY_TABLE_ROW*> optRow = m_libs->GetRow( libName );
127
128 if( !optRow.has_value() || ( *optRow )->Disabled() || ( *optRow )->Hidden() )
129 continue;
130
131 bool pinned = alg::contains( cfg->m_Session.pinned_fp_libs, libName )
132 || alg::contains( project.m_PinnedFootprintLibs, libName );
133
134 std::vector<FOOTPRINT*> footprints = m_libs->GetFootprints( libName, true );
135 std::vector<LIB_TREE_ITEM*> treeItems;
136 treeItems.reserve( footprints.size() );
137
138 for( FOOTPRINT* fp : footprints )
139 treeItems.push_back( fp );
140
141 DoAddLibrary( libName, ( *optRow )->Description(), treeItems, pinned, true );
142 m_libMap.insert( libName );
143 }
144
145 if( m_libMap.size() > count )
146 m_tree.AssignIntrinsicRanks( m_shownColumns );
147}
148
149
151{
152 return m_libs->GetLibraryNames().size();
153}
154
155
157{
158 // Re-enumerate from disk if the library has changed since it was last preloaded.
159 // This picks up external modifications such as git branch switches.
160 m_libs->RefreshLibraryIfChanged( aLibNode.m_Name );
161
162 std::vector<FOOTPRINT*> footprints = m_libs->GetFootprints( aLibNode.m_Name, true );
163
164 // Build a map of footprint names for quick lookup
165 std::map<wxString, FOOTPRINT*> fpMap;
166
167 for( FOOTPRINT* fp : footprints )
168 fpMap[fp->GetFPID().GetLibItemName()] = fp;
169
170 // Remove items that no longer exist
171 for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); )
172 {
173 auto fpIt = fpMap.find( (*nodeIt)->m_Name );
174
175 if( fpIt != fpMap.end() )
176 {
177 static_cast<LIB_TREE_NODE_ITEM*>( nodeIt->get() )->Update( fpIt->second );
178 fpMap.erase( fpIt );
179 ++nodeIt;
180 }
181 else
182 {
183 nodeIt = aLibNode.m_Children.erase( nodeIt );
184 }
185 }
186
187 // Add new items
188 for( auto& [name, fp] : fpMap )
189 aLibNode.AddItem( fp );
190
192 m_libMap.insert( aLibNode.m_Name );
193}
194
195
196LIB_TREE_NODE::PTR_VECTOR::iterator
197FP_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary( LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
198{
199 LIB_TREE_NODE* node = aLibNodeIt->get();
200 m_libMap.erase( node->m_Name );
201 auto it = m_tree.m_Children.erase( aLibNodeIt );
202 return it;
203}
204
205
207{
208 return FindItem( m_frame->GetLoadedFPID() );
209}
210
211
212void FP_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
213 unsigned int aCol ) const
214{
215 if( IsFrozen() )
216 {
217 aVariant = wxEmptyString;
218 return;
219 }
220
221 LIB_TREE_NODE* node = ToNode( aItem );
222
223 switch( aCol )
224 {
225 case NAME_COL:
226 {
227 if( node->m_LibId == m_frame->GetLoadedFPID() && !m_frame->IsCurrentFPFromBoard() )
228 {
229 // Do not use GetLoadedFPID(); it returns m_footprintNameWhenLoaded.
230 node->m_Name =
231 m_frame->GetBoard()->GetFirstFootprint()->GetFPID().GetUniStringLibItemName();
232
233 // mark modified part with an asterisk
234 if( m_frame->GetScreen()->IsContentModified() )
235 aVariant = node->m_Name + wxT( " *" );
236 else
237 aVariant = node->m_Name;
238 }
239 else if( node->m_Pinned )
240 {
241 aVariant = GetPinningSymbol() + node->m_Name;
242 }
243 else
244 {
245 aVariant = node->m_Name;
246 }
247
248 break;
249 }
250
251 case DESC_COL:
252 {
253 if( node->m_LibId == m_frame->GetLoadedFPID() && !m_frame->IsCurrentFPFromBoard() )
254 {
255 node->m_Desc = m_frame->GetBoard()->GetFirstFootprint()->GetLibDescription();
256 }
257 else if( node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
258 {
259 if( std::optional<wxString> optDesc = PROJECT_PCB::FootprintLibAdapter( &m_frame->Prj() )->
260 GetLibraryDescription( node->m_LibId.GetLibNickname() ) )
261 {
262 node->m_Desc = *optDesc;
263 }
264 }
265
266 wxString descStr = UnescapeString( node->m_Desc );
267 descStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
268
269 aVariant = descStr;
270 break;
271 }
272
273 default: // column == -1 is used for default Compare function
274 aVariant = node->m_Name;
275 break;
276 }
277}
278
279
280bool FP_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
281 wxDataViewItemAttr& aAttr ) const
282{
283 if( IsFrozen() )
284 return false;
285
286 // change attributes only for the name field
287 if( aCol != 0 )
288 return false;
289
290 // don't link to a board footprint, even if the FPIDs match
291 if( m_frame->IsCurrentFPFromBoard() )
292 return false;
293
294 LIB_TREE_NODE* node = ToNode( aItem );
295 wxCHECK( node, false );
296
297 switch( node->m_Type )
298 {
299 case LIB_TREE_NODE::TYPE::LIBRARY:
300 if( node->m_Name == m_frame->GetLoadedFPID().GetLibNickname().wx_str() )
301 {
302 // mark the current library if it's collapsed
303 if( !m_widget->IsExpanded( ToItem( node ) ) )
304 {
305 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
306 // proxy for "is canvas item"
307 }
308
309 // mark modified libs with bold font
310 if( m_frame->GetScreen()->IsContentModified() && !m_frame->IsCurrentFPFromBoard() )
311 aAttr.SetBold( true );
312 }
313 break;
314
315 case LIB_TREE_NODE::TYPE::ITEM:
316 if( node->m_LibId == m_frame->GetLoadedFPID() )
317 {
318 // mark the current (on-canvas) part
319 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
320 // proxy for "is canvas item"
321
322 // mark modified part with bold font
323 if( m_frame->GetScreen()->IsContentModified() && !m_frame->IsCurrentFPFromBoard() )
324 aAttr.SetBold( true );
325 }
326 break;
327
328 default:
329 return false;
330 }
331
332 return true;
333}
334
335
336bool FP_TREE_SYNCHRONIZING_ADAPTER::HasPreview( const wxDataViewItem& aItem )
337{
338 LIB_TREE_NODE* node = ToNode( aItem );
339 wxCHECK( node, false );
340
341 return node->m_Type == LIB_TREE_NODE::TYPE::ITEM && node->m_LibId != m_frame->GetLoadedFPID();
342}
343
344
345static const wxString c_previewName = wxS( "fpHoverPreview" );
346
347
348void FP_TREE_SYNCHRONIZING_ADAPTER::ShowPreview( wxWindow* aParent, const wxDataViewItem& aItem )
349{
350 LIB_TREE_NODE* node = ToNode( aItem );
351 wxCHECK( node, /* void */ );
352
353 wxWindow* previewWindow = wxWindow::FindWindowByName( c_previewName, aParent );
354 FOOTPRINT_PREVIEW_PANEL* preview = dynamic_cast<FOOTPRINT_PREVIEW_PANEL*>( previewWindow );
355
356 if( !preview )
357 {
358 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
359 aParent->SetSizer( mainSizer );
360
361 preview = FOOTPRINT_PREVIEW_PANEL::New( &m_frame->Kiway(), aParent, m_frame );
362
363 preview->SetName( c_previewName );
364 preview->GetGAL()->SetAxesEnabled( false );
365
366 mainSizer->Add( preview, 1, wxEXPAND | wxALL, 1 );
367 aParent->Layout();
368 }
369
370 preview->DisplayFootprint( node->m_LibId );
371}
372
373
375{
376 wxWindow* previewWindow = wxWindow::FindWindowByName( c_previewName, aParent );
377
378 if( FOOTPRINT_PREVIEW_PANEL* preview = dynamic_cast<FOOTPRINT_PREVIEW_PANEL*>( previewWindow ) )
379 {
380 preview->GetCanvas()->SetEvtHandlerEnabled( false );
381 preview->GetCanvas()->StopDrawing();
382 }
383}
const char * name
KIGFX::GAL * GetGAL() const
Return a pointer to the GAL instance used in the panel.
Module editor specific tools.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
Panel that renders a single footprint via Cairo GAL, meant to be exported through Kiface.
bool DisplayFootprint(const LIB_ID &aFPID) override
Set the currently displayed footprint.
static FOOTPRINT_PREVIEW_PANEL * New(KIWAY *aKiway, wxWindow *aParent, UNITS_PROVIDER *aUnitsProvider)
FOOTPRINT_LIBRARY_ADAPTER * m_libs
FP_TREE_MODEL_ADAPTER(PCB_BASE_FRAME *aParent, FOOTPRINT_LIBRARY_ADAPTER *aLibs)
Constructor; takes a set of libraries to be included in the search.
FP_TREE_SYNCHRONIZING_ADAPTER(FOOTPRINT_EDIT_FRAME *aFrame, FOOTPRINT_LIBRARY_ADAPTER *aLibs)
bool HasPreview(const wxDataViewItem &aItem) override
void Sync(FOOTPRINT_LIBRARY_ADAPTER *aLibs)
bool GetAttr(wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
void updateLibrary(LIB_TREE_NODE_LIBRARY &aLibNode)
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(FOOTPRINT_EDIT_FRAME *aFrame, FOOTPRINT_LIBRARY_ADAPTER *aLibs)
int GetLibrariesCount() const override
Return the number of libraries loaded in the tree.
void ShutdownPreview(wxWindow *aParent) override
bool IsContainer(const wxDataViewItem &aItem) const override
LIB_TREE_NODE::PTR_VECTOR::iterator deleteLibrary(LIB_TREE_NODE::PTR_VECTOR::iterator &aLibNodeIt)
TOOL_INTERACTIVE * GetContextMenuTool() override
void ShowPreview(wxWindow *aParent, const wxDataViewItem &aItem) override
void GetValue(wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
void SetAxesEnabled(bool aAxesEnabled)
Enable drawing the axes.
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:87
static LIB_TREE_NODE * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> #SYM_TREE_NODE.
static wxDataViewItem ToItem(const LIB_TREE_NODE *aNode)
Convert #SYM_TREE_NODE -> wxDataViewItem.
static const wxString GetPinningSymbol()
@ NAME_COL
Library or library item name column.
@ DESC_COL
Library or library description column.
std::vector< wxString > m_shownColumns
LIB_TREE_NODE_LIBRARY & DoAddLibrary(const wxString &aNodeName, const wxString &aDesc, const std::vector< LIB_TREE_ITEM * > &aItemList, bool pinned, bool presorted)
Add the given list of symbols by alias.
wxDataViewItem FindItem(const LIB_ID &aLibId)
Returns tree item corresponding to part.
Node type: LIB_ID.
void Update(LIB_TREE_ITEM *aItem)
Update the node using data from a LIB_ALIAS object.
Node type: library.
LIB_TREE_NODE_ITEM & AddItem(LIB_TREE_ITEM *aItem)
Construct a new alias node, add it to this library, and return it.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
PTR_VECTOR m_Children
void AssignIntrinsicRanks(const std::vector< wxString > &aShownColumns, bool presorted=false)
Store intrinsic ranks on all children of this node.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:541
The backing store for a PROJECT, in JSON format.
static FOOTPRINT_LIBRARY_ADAPTER * FootprintLibAdapter(PROJECT *aProject)
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:100
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
wxString UnescapeString(const wxString &aSource)
std::vector< wxString > pinned_fp_libs
static const wxString c_previewName