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