-
-
Notifications
You must be signed in to change notification settings - Fork 489
/
fp_tree_synchronizing_adapter.cpp
351 lines (286 loc) · 11.1 KB
/
fp_tree_synchronizing_adapter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 CERN
* Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
* @author Maciej Suminski <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* https://www.gnu.org/licenses/gpl-3.0.html
* or you may search the http://www.gnu.org website for the version 3 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <pgm_base.h>
#include <project/project_file.h>
#include <fp_tree_synchronizing_adapter.h>
#include <footprint_edit_frame.h>
#include <footprint_preview_panel.h>
#include <fp_lib_table.h>
#include <footprint_info_impl.h>
#include <gal/graphics_abstraction_layer.h>
#include <string_utils.h>
#include <board.h>
#include <footprint.h>
#include <tool/tool_manager.h>
#include <tools/footprint_editor_control.h>
#include <wx/settings.h>
wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
FP_TREE_SYNCHRONIZING_ADAPTER::Create( FOOTPRINT_EDIT_FRAME* aFrame, FP_LIB_TABLE* aLibs )
{
auto* adapter = new FP_TREE_SYNCHRONIZING_ADAPTER( aFrame, aLibs );
return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
}
FP_TREE_SYNCHRONIZING_ADAPTER::FP_TREE_SYNCHRONIZING_ADAPTER( FOOTPRINT_EDIT_FRAME* aFrame,
FP_LIB_TABLE* aLibs ) :
FP_TREE_MODEL_ADAPTER( aFrame, aLibs ),
m_frame( aFrame )
{
}
TOOL_INTERACTIVE* FP_TREE_SYNCHRONIZING_ADAPTER::GetContextMenuTool()
{
return m_frame->GetToolManager()->GetTool<FOOTPRINT_EDITOR_CONTROL>();
}
bool FP_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
{
const LIB_TREE_NODE* node = ToNode( aItem );
return node ? node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY : true;
}
#define PROGRESS_INTERVAL_MILLIS 33 // 30 FPS refresh rate
void FP_TREE_SYNCHRONIZING_ADAPTER::Sync( FP_LIB_TABLE* aLibs )
{
m_libs = aLibs;
// Process already stored libraries
for( auto it = m_tree.m_Children.begin(); it != m_tree.m_Children.end(); )
{
const wxString& name = it->get()->m_Name;
// Remove the library if it no longer exists or it exists in both the global and the
// project library but the project library entry is disabled.
if( !m_libs->HasLibrary( name, true )
|| m_libs->FindRow( name, true ) != m_libs->FindRow( name, false ) )
{
it = deleteLibrary( it );
continue;
}
updateLibrary( *(LIB_TREE_NODE_LIBRARY*) it->get() );
++it;
}
// Look for new libraries
COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
PROJECT_FILE& project = m_frame->Prj().GetProjectFile();
size_t count = m_libMap.size();
for( const wxString& libName : m_libs->GetLogicalLibs() )
{
if( m_libMap.count( libName ) == 0 )
{
try
{
const FP_LIB_TABLE_ROW* library = m_libs->FindRow( libName, true );
bool pinned = alg::contains( cfg->m_Session.pinned_fp_libs, libName )
|| alg::contains( project.m_PinnedFootprintLibs, libName );
DoAddLibrary( libName, library->GetDescr(), getFootprints( libName ), pinned, true );
m_libMap.insert( libName );
}
catch( ... )
{
// do nothing if libname is not found. Just skip it
}
}
}
if( m_libMap.size() > count )
m_tree.AssignIntrinsicRanks();
}
int FP_TREE_SYNCHRONIZING_ADAPTER::GetLibrariesCount() const
{
return GFootprintTable.GetCount();
}
void FP_TREE_SYNCHRONIZING_ADAPTER::updateLibrary( LIB_TREE_NODE_LIBRARY& aLibNode )
{
std::vector<LIB_TREE_ITEM*> footprints = getFootprints( aLibNode.m_Name );
// remove the common part from the footprints list
for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); )
{
// Since the list is sorted we can use a binary search to speed up searches within
// libraries with lots of footprints.
FOOTPRINT_INFO_IMPL dummy( wxEmptyString, (*nodeIt)->m_Name );
auto footprintIt = std::lower_bound( footprints.begin(), footprints.end(), &dummy,
[]( LIB_TREE_ITEM* a, LIB_TREE_ITEM* b )
{
return StrNumCmp( a->GetName(), b->GetName(), false ) < 0;
} );
if( footprintIt != footprints.end() && dummy.GetName() == (*footprintIt)->GetName() )
{
// footprint exists both in the lib tree and the footprint info list; just
// update the node data
static_cast<LIB_TREE_NODE_ITEM*>( nodeIt->get() )->Update( *footprintIt );
footprints.erase( footprintIt );
++nodeIt;
}
else
{
// node does not exist in the library manager, remove the corresponding node
nodeIt = aLibNode.m_Children.erase( nodeIt );
}
}
// now the footprint list contains only new aliases that need to be added to the tree
for( LIB_TREE_ITEM* footprint : footprints )
aLibNode.AddItem( footprint );
aLibNode.AssignIntrinsicRanks();
m_libMap.insert( aLibNode.m_Name );
}
LIB_TREE_NODE::PTR_VECTOR::iterator
FP_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary( LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
{
LIB_TREE_NODE* node = aLibNodeIt->get();
m_libMap.erase( node->m_Name );
auto it = m_tree.m_Children.erase( aLibNodeIt );
return it;
}
wxDataViewItem FP_TREE_SYNCHRONIZING_ADAPTER::GetCurrentDataViewItem()
{
return FindItem( m_frame->GetLoadedFPID() );
}
void FP_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
unsigned int aCol ) const
{
if( IsFrozen() )
{
aVariant = wxEmptyString;
return;
}
LIB_TREE_NODE* node = ToNode( aItem );
switch( aCol )
{
case NAME_COL:
{
if( node->m_LibId == m_frame->GetLoadedFPID() && !m_frame->IsCurrentFPFromBoard() )
{
// Do not use GetLoadedFPID(); it returns m_footprintNameWhenLoaded.
node->m_Name =
m_frame->GetBoard()->GetFirstFootprint()->GetFPID().GetUniStringLibItemName();
// mark modified part with an asterisk
if( m_frame->GetScreen()->IsContentModified() )
aVariant = node->m_Name + wxT( " *" );
else
aVariant = node->m_Name;
}
else if( node->m_Pinned )
{
aVariant = GetPinningSymbol() + node->m_Name;
}
else
{
aVariant = node->m_Name;
}
break;
}
case DESC_COL:
{
if( node->m_LibId == m_frame->GetLoadedFPID() && !m_frame->IsCurrentFPFromBoard() )
{
node->m_Desc = m_frame->GetBoard()->GetFirstFootprint()->GetLibDescription();
}
else if( node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
{
try
{
const FP_LIB_TABLE_ROW* lib =
GFootprintTable.FindRow( node->m_LibId.GetLibNickname() );
if( lib )
node->m_Desc = lib->GetDescr();
}
catch( IO_ERROR& )
{
}
}
wxString descStr = UnescapeString( node->m_Desc );
descStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
aVariant = descStr;
break;
}
default: // column == -1 is used for default Compare function
aVariant = node->m_Name;
break;
}
}
bool FP_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
wxDataViewItemAttr& aAttr ) const
{
if( IsFrozen() )
return false;
// change attributes only for the name field
if( aCol != 0 )
return false;
// don't link to a board footprint, even if the FPIDs match
if( m_frame->IsCurrentFPFromBoard() )
return false;
LIB_TREE_NODE* node = ToNode( aItem );
wxCHECK( node, false );
switch( node->m_Type )
{
case LIB_TREE_NODE::TYPE::LIBRARY:
if( node->m_Name == m_frame->GetLoadedFPID().GetLibNickname().wx_str() )
{
// mark the current library if it's collapsed
if( !m_widget->IsExpanded( ToItem( node ) ) )
{
aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
// proxy for "is canvas item"
}
// mark modified libs with bold font
if( m_frame->GetScreen()->IsContentModified() && !m_frame->IsCurrentFPFromBoard() )
aAttr.SetBold( true );
}
break;
case LIB_TREE_NODE::TYPE::ITEM:
if( node->m_LibId == m_frame->GetLoadedFPID() )
{
// mark the current (on-canvas) part
aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
// proxy for "is canvas item"
// mark modified part with bold font
if( m_frame->GetScreen()->IsContentModified() && !m_frame->IsCurrentFPFromBoard() )
aAttr.SetBold( true );
}
break;
default:
return false;
}
return true;
}
bool FP_TREE_SYNCHRONIZING_ADAPTER::HasPreview( const wxDataViewItem& aItem )
{
LIB_TREE_NODE* node = ToNode( aItem );
wxCHECK( node, false );
return node->m_Type == LIB_TREE_NODE::TYPE::ITEM && node->m_LibId != m_frame->GetLoadedFPID();
}
void FP_TREE_SYNCHRONIZING_ADAPTER::ShowPreview( wxWindow* aParent, const wxDataViewItem& aItem )
{
static const wxString c_previewName = wxS( "fpHoverPreview" );
LIB_TREE_NODE* node = ToNode( aItem );
wxCHECK( node, /* void */ );
wxWindow* previewWindow = wxWindow::FindWindowByName( c_previewName, aParent );
FOOTPRINT_PREVIEW_PANEL* preview = dynamic_cast<FOOTPRINT_PREVIEW_PANEL*>( previewWindow );
if( !preview )
{
wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
aParent->SetSizer( mainSizer );
preview = FOOTPRINT_PREVIEW_PANEL::New( &m_frame->Kiway(), aParent, m_frame );
preview->SetName( c_previewName );
preview->GetGAL()->SetAxesEnabled( false );
mainSizer->Add( preview, 1, wxEXPAND | wxALL, 1 );
aParent->Layout();
}
preview->DisplayFootprint( node->m_LibId );
}