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 // Remove the library if it no longer exists or it exists in both the global and the
86 // project library but the project library entry is disabled.
87 if( !m_libs->HasLibrary( name, true )
88 || m_libs->HasLibrary( name, true ) != m_libs->HasLibrary( name, false ) )
89 {
90 it = deleteLibrary( it );
91 continue;
92 }
93
94 updateLibrary( *(LIB_TREE_NODE_LIBRARY*) it->get() );
95 }
96 catch( ... )
97 {
98 // If the library isn't found, remove it
99 it = deleteLibrary( it );
100 continue;
101 }
102
103 ++it;
104 }
105
106 // Look for new libraries
108 PROJECT_FILE& project = m_frame->Prj().GetProjectFile();
109 size_t count = m_libMap.size();
110
111 for( const wxString& libName : m_libs->GetLibraryNames() )
112 {
113 if( m_libMap.count( libName ) == 0 )
114 {
115 if( std::optional<wxString> optDesc = PROJECT_PCB::FootprintLibAdapter( &m_frame->Prj() )->
116 GetLibraryDescription( libName ) )
117 {
118 bool pinned = alg::contains( cfg->m_Session.pinned_fp_libs, libName )
119 || alg::contains( project.m_PinnedFootprintLibs, libName );
120
121 std::vector<FOOTPRINT*> footprints = m_libs->GetFootprints( libName, true );
122 std::vector<LIB_TREE_ITEM*> treeItems;
123 treeItems.reserve( footprints.size() );
124
125 for( FOOTPRINT* fp : footprints )
126 treeItems.push_back( fp );
127
128 DoAddLibrary( libName, *optDesc, treeItems, pinned, true );
129
130 m_libMap.insert( libName );
131 }
132 }
133 }
134
135 if( m_libMap.size() > count )
136 m_tree.AssignIntrinsicRanks( m_shownColumns );
137}
138
139
141{
142 return m_libs->GetLibraryNames().size();
143}
144
145
147{
148 // Re-enumerate from disk if the library has changed since it was last preloaded.
149 // This picks up external modifications such as git branch switches.
150 m_libs->RefreshLibraryIfChanged( aLibNode.m_Name );
151
152 std::vector<FOOTPRINT*> footprints = m_libs->GetFootprints( aLibNode.m_Name, true );
153
154 // Build a map of footprint names for quick lookup
155 std::map<wxString, FOOTPRINT*> fpMap;
156
157 for( FOOTPRINT* fp : footprints )
158 fpMap[fp->GetFPID().GetLibItemName()] = fp;
159
160 // Remove items that no longer exist
161 for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); )
162 {
163 auto fpIt = fpMap.find( (*nodeIt)->m_Name );
164
165 if( fpIt != fpMap.end() )
166 {
167 static_cast<LIB_TREE_NODE_ITEM*>( nodeIt->get() )->Update( fpIt->second );
168 fpMap.erase( fpIt );
169 ++nodeIt;
170 }
171 else
172 {
173 nodeIt = aLibNode.m_Children.erase( nodeIt );
174 }
175 }
176
177 // Add new items
178 for( auto& [name, fp] : fpMap )
179 aLibNode.AddItem( fp );
180
182 m_libMap.insert( aLibNode.m_Name );
183}
184
185
186LIB_TREE_NODE::PTR_VECTOR::iterator
187FP_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary( LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
188{
189 LIB_TREE_NODE* node = aLibNodeIt->get();
190 m_libMap.erase( node->m_Name );
191 auto it = m_tree.m_Children.erase( aLibNodeIt );
192 return it;
193}
194
195
197{
198 return FindItem( m_frame->GetLoadedFPID() );
199}
200
201
202void FP_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
203 unsigned int aCol ) const
204{
205 if( IsFrozen() )
206 {
207 aVariant = wxEmptyString;
208 return;
209 }
210
211 LIB_TREE_NODE* node = ToNode( aItem );
212
213 switch( aCol )
214 {
215 case NAME_COL:
216 {
217 if( node->m_LibId == m_frame->GetLoadedFPID() && !m_frame->IsCurrentFPFromBoard() )
218 {
219 // Do not use GetLoadedFPID(); it returns m_footprintNameWhenLoaded.
220 node->m_Name =
221 m_frame->GetBoard()->GetFirstFootprint()->GetFPID().GetUniStringLibItemName();
222
223 // mark modified part with an asterisk
224 if( m_frame->GetScreen()->IsContentModified() )
225 aVariant = node->m_Name + wxT( " *" );
226 else
227 aVariant = node->m_Name;
228 }
229 else if( node->m_Pinned )
230 {
231 aVariant = GetPinningSymbol() + node->m_Name;
232 }
233 else
234 {
235 aVariant = node->m_Name;
236 }
237
238 break;
239 }
240
241 case DESC_COL:
242 {
243 if( node->m_LibId == m_frame->GetLoadedFPID() && !m_frame->IsCurrentFPFromBoard() )
244 {
245 node->m_Desc = m_frame->GetBoard()->GetFirstFootprint()->GetLibDescription();
246 }
247 else if( node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
248 {
249 if( std::optional<wxString> optDesc = PROJECT_PCB::FootprintLibAdapter( &m_frame->Prj() )->
250 GetLibraryDescription( node->m_LibId.GetLibNickname() ) )
251 {
252 node->m_Desc = *optDesc;
253 }
254 }
255
256 wxString descStr = UnescapeString( node->m_Desc );
257 descStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
258
259 aVariant = descStr;
260 break;
261 }
262
263 default: // column == -1 is used for default Compare function
264 aVariant = node->m_Name;
265 break;
266 }
267}
268
269
270bool FP_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
271 wxDataViewItemAttr& aAttr ) const
272{
273 if( IsFrozen() )
274 return false;
275
276 // change attributes only for the name field
277 if( aCol != 0 )
278 return false;
279
280 // don't link to a board footprint, even if the FPIDs match
281 if( m_frame->IsCurrentFPFromBoard() )
282 return false;
283
284 LIB_TREE_NODE* node = ToNode( aItem );
285 wxCHECK( node, false );
286
287 switch( node->m_Type )
288 {
289 case LIB_TREE_NODE::TYPE::LIBRARY:
290 if( node->m_Name == m_frame->GetLoadedFPID().GetLibNickname().wx_str() )
291 {
292 // mark the current library if it's collapsed
293 if( !m_widget->IsExpanded( ToItem( node ) ) )
294 {
295 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
296 // proxy for "is canvas item"
297 }
298
299 // mark modified libs with bold font
300 if( m_frame->GetScreen()->IsContentModified() && !m_frame->IsCurrentFPFromBoard() )
301 aAttr.SetBold( true );
302 }
303 break;
304
305 case LIB_TREE_NODE::TYPE::ITEM:
306 if( node->m_LibId == m_frame->GetLoadedFPID() )
307 {
308 // mark the current (on-canvas) part
309 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
310 // proxy for "is canvas item"
311
312 // mark modified part with bold font
313 if( m_frame->GetScreen()->IsContentModified() && !m_frame->IsCurrentFPFromBoard() )
314 aAttr.SetBold( true );
315 }
316 break;
317
318 default:
319 return false;
320 }
321
322 return true;
323}
324
325
326bool FP_TREE_SYNCHRONIZING_ADAPTER::HasPreview( const wxDataViewItem& aItem )
327{
328 LIB_TREE_NODE* node = ToNode( aItem );
329 wxCHECK( node, false );
330
331 return node->m_Type == LIB_TREE_NODE::TYPE::ITEM && node->m_LibId != m_frame->GetLoadedFPID();
332}
333
334
335static const wxString c_previewName = wxS( "fpHoverPreview" );
336
337
338void FP_TREE_SYNCHRONIZING_ADAPTER::ShowPreview( wxWindow* aParent, const wxDataViewItem& aItem )
339{
340 LIB_TREE_NODE* node = ToNode( aItem );
341 wxCHECK( node, /* void */ );
342
343 wxWindow* previewWindow = wxWindow::FindWindowByName( c_previewName, aParent );
344 FOOTPRINT_PREVIEW_PANEL* preview = dynamic_cast<FOOTPRINT_PREVIEW_PANEL*>( previewWindow );
345
346 if( !preview )
347 {
348 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
349 aParent->SetSizer( mainSizer );
350
351 preview = FOOTPRINT_PREVIEW_PANEL::New( &m_frame->Kiway(), aParent, m_frame );
352
353 preview->SetName( c_previewName );
354 preview->GetGAL()->SetAxesEnabled( false );
355
356 mainSizer->Add( preview, 1, wxEXPAND | wxALL, 1 );
357 aParent->Layout();
358 }
359
360 preview->DisplayFootprint( node->m_LibId );
361}
362
363
365{
366 wxWindow* previewWindow = wxWindow::FindWindowByName( c_previewName, aParent );
367
368 if( FOOTPRINT_PREVIEW_PANEL* preview = dynamic_cast<FOOTPRINT_PREVIEW_PANEL*>( previewWindow ) )
369 {
370 preview->GetCanvas()->SetEvtHandlerEnabled( false );
371 preview->GetCanvas()->StopDrawing();
372 }
373}
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:547
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