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>
27#include <kiplatform/ui.h>
34#include <project_pcb.h>
35#include <string_utils.h>
36#include <board.h>
37#include <footprint.h>
38#include <tool/tool_manager.h>
40
41#include <map>
42
43#include <wx/log.h>
44#include <wx/settings.h>
45
46
47wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
49{
50 auto* adapter = new FP_TREE_SYNCHRONIZING_ADAPTER( aFrame, aLibs );
51 return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
52}
53
54
61
62
67
68
69bool FP_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
70{
71 const LIB_TREE_NODE* node = ToNode( aItem );
72 return node ? node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY : true;
73}
74
75
77{
78 m_libs = aLibs;
79
80 wxLogTrace( wxT( "KICAD_TABS_DBG" ), wxT( "FpSyncAdapter::Sync enter" ) );
81
82 // The work below frees nodes while the caller yields the event loop, so a GtkTreeView scroll
83 // queued earlier would point at freed rows and crash the next frame-clock tick. Drop it first.
85
86 // Detach the GtkTreeView from the model before freeing any node so the frame-clock tick during
87 // the caller's yield has no stale rows to validate. The RAII guard re-attaches on all paths.
88 ResetTreeView resetGuard( *this );
89
90 wxLogTrace( wxT( "KICAD_TABS_DBG" ), wxT( "FpSyncAdapter::Sync freeing/updating nodes" ) );
91
92 // Process already stored libraries
93 for( auto it = m_tree.m_Children.begin(); it != m_tree.m_Children.end(); )
94 {
95 const wxString& name = it->get()->m_Name;
96
97 try
98 {
99 // Check the table row directly
100 std::optional<LIBRARY_TABLE_ROW*> optRow = m_libs->GetRow( name );
101 std::optional<LIB_STATUS> libStatus = m_libs->GetLibraryStatus( name );
102
103 bool loadFailed = libStatus.has_value()
104 && libStatus->load_status == LOAD_STATUS::LOAD_ERROR;
105
106 if( !optRow.has_value()
107 || ( *optRow )->Disabled()
108 || ( *optRow )->Hidden()
109 || loadFailed )
110 {
111 it = deleteLibrary( it );
112 continue;
113 }
114
115 updateLibrary( *static_cast<LIB_TREE_NODE_LIBRARY*>( it->get() ) );
116 }
117 catch( ... )
118 {
119 // If the library isn't found, remove it
120 it = deleteLibrary( it );
121 continue;
122 }
123
124 ++it;
125 }
126
127 // Look for new libraries
129 PROJECT_FILE& project = m_frame->Prj().GetProjectFile();
130 size_t count = m_libMap.size();
131
132 for( const auto& [libName, status] : m_libs->GetLibraryStatuses() )
133 {
134 if( status.load_status != LOAD_STATUS::LOADED || status.error )
135 continue;
136
137 if( m_libMap.count( libName ) != 0 )
138 continue;
139
140 std::optional<LIBRARY_TABLE_ROW*> optRow = m_libs->GetRow( libName );
141
142 if( !optRow.has_value() || ( *optRow )->Disabled() || ( *optRow )->Hidden() )
143 continue;
144
145 bool pinned = alg::contains( cfg->m_Session.pinned_fp_libs, libName )
146 || alg::contains( project.m_PinnedFootprintLibs, libName );
147
148 std::vector<FOOTPRINT*> footprints = m_libs->GetFootprints( libName, true );
149 std::vector<LIB_TREE_ITEM*> treeItems;
150 treeItems.reserve( footprints.size() );
151
152 for( FOOTPRINT* fp : footprints )
153 treeItems.push_back( fp );
154
155 DoAddLibrary( libName, ( *optRow )->Description(), treeItems, pinned, true );
156 m_libMap.insert( libName );
157 }
158
159 if( m_libMap.size() > count )
160 m_tree.AssignIntrinsicRanks( m_shownColumns );
161
162 wxLogTrace( wxT( "KICAD_TABS_DBG" ), wxT( "FpSyncAdapter::Sync exit" ) );
163}
164
165
167{
168 return m_libs->GetLibraryNames().size();
169}
170
171
173{
174 // Re-enumerate from disk if the library has changed since it was last preloaded.
175 // This picks up external modifications such as git branch switches.
176 m_libs->RefreshLibraryIfChanged( aLibNode.m_Name );
177
178 std::vector<FOOTPRINT*> footprints = m_libs->GetFootprints( aLibNode.m_Name, true );
179
180 // Build a map of footprint names for quick lookup
181 std::map<wxString, FOOTPRINT*> fpMap;
182
183 for( FOOTPRINT* fp : footprints )
184 fpMap[fp->GetFPID().GetLibItemName()] = fp;
185
186 // Remove items that no longer exist
187 for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); )
188 {
189 auto fpIt = fpMap.find( (*nodeIt)->m_Name );
190
191 if( fpIt != fpMap.end() )
192 {
193 static_cast<LIB_TREE_NODE_ITEM*>( nodeIt->get() )->Update( fpIt->second );
194 fpMap.erase( fpIt );
195 ++nodeIt;
196 }
197 else
198 {
199 nodeIt = aLibNode.m_Children.erase( nodeIt );
200 }
201 }
202
203 // Add new items
204 for( auto& [name, fp] : fpMap )
205 aLibNode.AddItem( fp );
206
208 m_libMap.insert( aLibNode.m_Name );
209}
210
211
212LIB_TREE_NODE::PTR_VECTOR::iterator
213FP_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary( LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
214{
215 LIB_TREE_NODE* node = aLibNodeIt->get();
216 m_libMap.erase( node->m_Name );
217 auto it = m_tree.m_Children.erase( aLibNodeIt );
218 return it;
219}
220
221
223{
224 return FindItem( m_frame->GetLoadedFPID() );
225}
226
227
228void FP_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
229 unsigned int aCol ) const
230{
231 if( IsFrozen() )
232 {
233 aVariant = wxEmptyString;
234 return;
235 }
236
237 LIB_TREE_NODE* node = ToNode( aItem );
238
239 switch( aCol )
240 {
241 case NAME_COL:
242 {
243 if( node->m_LibId == m_frame->GetLoadedFPID() && !m_frame->IsCurrentFPFromBoard() )
244 {
245 // Do not use GetLoadedFPID(); it returns m_footprintNameWhenLoaded.
246 node->m_Name =
247 m_frame->GetBoard()->GetFirstFootprint()->GetFPID().GetUniStringLibItemName();
248
249 // mark modified part with an asterisk
250 if( m_frame->GetScreen()->IsContentModified() )
251 aVariant = node->m_Name + wxT( " *" );
252 else
253 aVariant = node->m_Name;
254 }
255 else if( node->m_Pinned )
256 {
257 aVariant = GetPinningSymbol() + node->m_Name;
258 }
259 else
260 {
261 aVariant = node->m_Name;
262 }
263
264 break;
265 }
266
267 case DESC_COL:
268 {
269 if( node->m_LibId == m_frame->GetLoadedFPID() && !m_frame->IsCurrentFPFromBoard() )
270 {
271 node->m_Desc = m_frame->GetBoard()->GetFirstFootprint()->GetLibDescription();
272 }
273 else if( node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
274 {
275 if( std::optional<wxString> optDesc = PROJECT_PCB::FootprintLibAdapter( &m_frame->Prj() )->
276 GetLibraryDescription( node->m_LibId.GetLibNickname() ) )
277 {
278 node->m_Desc = *optDesc;
279 }
280 }
281
282 wxString descStr = UnescapeString( node->m_Desc );
283 descStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
284
285 aVariant = descStr;
286 break;
287 }
288
289 default: // column == -1 is used for default Compare function
290 aVariant = node->m_Name;
291 break;
292 }
293}
294
295
296bool FP_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
297 wxDataViewItemAttr& aAttr ) const
298{
299 if( IsFrozen() )
300 return false;
301
302 // change attributes only for the name field
303 if( aCol != 0 )
304 return false;
305
306 // don't link to a board footprint, even if the FPIDs match
307 if( m_frame->IsCurrentFPFromBoard() )
308 return false;
309
310 LIB_TREE_NODE* node = ToNode( aItem );
311 wxCHECK( node, false );
312
313 switch( node->m_Type )
314 {
315 case LIB_TREE_NODE::TYPE::LIBRARY:
316 if( node->m_Name == m_frame->GetLoadedFPID().GetLibNickname().wx_str() )
317 {
318 // mark the current library if it's collapsed
319 if( !m_widget->IsExpanded( ToItem( node ) ) )
320 {
321 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
322 // proxy for "is canvas item"
323 }
324
325 // mark modified libs with bold font
326 if( m_frame->GetScreen()->IsContentModified() && !m_frame->IsCurrentFPFromBoard() )
327 aAttr.SetBold( true );
328 }
329 break;
330
331 case LIB_TREE_NODE::TYPE::ITEM:
332 if( node->m_LibId == m_frame->GetLoadedFPID() )
333 {
334 // mark the current (on-canvas) part
335 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
336 // proxy for "is canvas item"
337
338 // mark modified part with bold font
339 if( m_frame->GetScreen()->IsContentModified() && !m_frame->IsCurrentFPFromBoard() )
340 aAttr.SetBold( true );
341 }
342 break;
343
344 default:
345 return false;
346 }
347
348 return true;
349}
350
351
352bool FP_TREE_SYNCHRONIZING_ADAPTER::HasPreview( const wxDataViewItem& aItem )
353{
354 LIB_TREE_NODE* node = ToNode( aItem );
355 wxCHECK( node, false );
356
357 return node->m_Type == LIB_TREE_NODE::TYPE::ITEM && node->m_LibId != m_frame->GetLoadedFPID();
358}
359
360
361static const wxString c_previewName = wxS( "fpHoverPreview" );
362
363
364void FP_TREE_SYNCHRONIZING_ADAPTER::ShowPreview( wxWindow* aParent, const wxDataViewItem& aItem )
365{
366 LIB_TREE_NODE* node = ToNode( aItem );
367 wxCHECK( node, /* void */ );
368
369 wxWindow* previewWindow = wxWindow::FindWindowByName( c_previewName, aParent );
370 FOOTPRINT_PREVIEW_PANEL* preview = dynamic_cast<FOOTPRINT_PREVIEW_PANEL*>( previewWindow );
371
372 if( !preview )
373 {
374 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
375 aParent->SetSizer( mainSizer );
376
377 preview = FOOTPRINT_PREVIEW_PANEL::New( &m_frame->Kiway(), aParent, m_frame );
378
379 preview->SetName( c_previewName );
380 preview->GetGAL()->SetAxesEnabled( false );
381
382 mainSizer->Add( preview, 1, wxEXPAND | wxALL, 1 );
383 aParent->Layout();
384 }
385
386 preview->DisplayFootprint( node->m_LibId );
387}
388
389
391{
392 wxWindow* previewWindow = wxWindow::FindWindowByName( c_previewName, aParent );
393
394 if( FOOTPRINT_PREVIEW_PANEL* preview = dynamic_cast<FOOTPRINT_PREVIEW_PANEL*>( previewWindow ) )
395 {
396 preview->GetCanvas()->SetEvtHandlerEnabled( false );
397 preview->GetCanvas()->StopDrawing();
398 }
399}
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
RAII guard that detaches the GtkTreeView from the model across a tree rebuild so a deferred frame-clo...
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)
void CancelPendingScroll(wxDataViewCtrl *aCtrl)
Cancel any pending scroll-to-item request on aCtrl.
Definition wxgtk/ui.cpp:457
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