KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_tree_model_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 Chris Pavlina <[email protected]>
5 * Copyright (C) 2014 Henner Zeller <[email protected]>
6 * Copyright (C) 2023 CERN
7 * Copyright (C) 2014-2023 KiCad Developers, see AUTHORS.txt for contributors.
8 *
9 * This program is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <eda_base_frame.h>
24#include <eda_pattern_match.h>
25#include <kiface_base.h>
26#include <kiplatform/ui.h>
30#include <widgets/ui_common.h>
31#include <wx/tokenzr.h>
32#include <wx/wupdlock.h>
33#include <wx/settings.h>
34#include <wx/dc.h>
35#include <string_utils.h>
36
37
38static const int kDataViewIndent = 20;
39
40
41class LIB_TREE_RENDERER : public wxDataViewCustomRenderer
42{
43public:
45 m_canvasItem( false )
46 {}
47
48 wxSize GetSize() const override
49 {
50 return wxSize( GetOwner()->GetWidth(), GetTextExtent( m_text ).y );
51 }
52
53 bool GetValue( wxVariant& aValue ) const override
54 {
55 aValue = m_text;
56 return true;
57 }
58
59 bool SetValue( const wxVariant& aValue ) override
60 {
61 m_text = aValue.GetString();
62 return true;
63 }
64
65 void SetAttr( const wxDataViewItemAttr& aAttr ) override
66 {
67 // Use strikethrough as a proxy for is-canvas-item
68 m_canvasItem = aAttr.GetStrikethrough();
69
70 wxDataViewItemAttr realAttr = aAttr;
71 realAttr.SetStrikethrough( false );
72
73 wxDataViewCustomRenderer::SetAttr( realAttr );
74 }
75
76 bool Render( wxRect aRect, wxDC *dc, int aState ) override
77 {
78 RenderBackground( dc, aRect );
79
80 if( m_canvasItem )
81 {
82 wxPoint points[6];
83 points[0] = aRect.GetTopLeft();
84 points[1] = aRect.GetTopRight() + wxPoint( -4, 0 );
85 points[2] = aRect.GetTopRight() + wxPoint( 0, aRect.GetHeight() / 2 );
86 points[3] = aRect.GetBottomRight() + wxPoint( -4, 1 );
87 points[4] = aRect.GetBottomLeft() + wxPoint( 0, 1 );
88 points[5] = aRect.GetTopLeft();
89
90 dc->SetPen( KIPLATFORM::UI::IsDarkTheme() ? *wxWHITE_PEN : *wxBLACK_PEN );
91 dc->DrawLines( 6, points );
92 }
93
94 // We should be able to pass wxDATAVIEW_CELL_SELECTED into RenderText() and have it do
95 // the right thing -- but it picks wxSYS_COLOUR_HIGHLIGHTTEXT on MacOS and GTK (instead
96 // of wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT).
97 if( aState & wxDATAVIEW_CELL_SELECTED )
98 dc->SetTextForeground( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
99
100 aRect.Deflate( 1 );
101 RenderText( m_text, 0, aRect, dc, 0 );
102 return true;
103 }
104
105private:
107 wxString m_text;
108};
109
110
111wxDataViewItem LIB_TREE_MODEL_ADAPTER::ToItem( const LIB_TREE_NODE* aNode )
112{
113 return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
114}
115
116
118{
119 return static_cast<LIB_TREE_NODE*>( aItem.GetID() );
120}
121
122
124 const wxString& aPinnedKey ) :
125 m_widget( nullptr ),
126 m_parent( aParent ),
127 m_sort_mode( BEST_MATCH ),
128 m_show_units( true ),
129 m_preselect_unit( 0 ),
130 m_freeze( 0 ),
131 m_filter( nullptr )
132{
133 // Default column widths. Do not translate these names.
134 m_colWidths[ _HKI( "Item" ) ] = 300;
135 m_colWidths[ _HKI( "Description" ) ] = 600;
136
137 m_availableColumns = { _HKI( "Item" ), _HKI( "Description" ) };
138
140
141 for( const std::pair<const wxString, int>& pair : cfg->m_LibTree.column_widths )
142 m_colWidths[pair.first] = pair.second;
143
145
146 if( m_shownColumns.empty() )
147 m_shownColumns = { _HKI( "Item" ), _HKI( "Description" ) };
148
149 if( m_shownColumns[0] != _HKI( "Item" ) )
150 m_shownColumns.insert( m_shownColumns.begin(), _HKI( "Item" ) );
151}
152
153
155{}
156
157
159{
160 if( m_widget )
161 {
163
165 cfg->m_LibTree.column_widths.clear();
166
167 for( const std::pair<const wxString, wxDataViewColumn*>& pair : m_colNameMap )
168 cfg->m_LibTree.column_widths[pair.first] = pair.second->GetWidth();
169 }
170}
171
172
174{
175 m_show_units = aShow;
176}
177
178
179void LIB_TREE_MODEL_ADAPTER::SetPreselectNode( const LIB_ID& aLibId, int aUnit )
180{
181 m_preselect_lib_id = aLibId;
182 m_preselect_unit = aUnit;
183}
184
185
187 const wxString& aDesc,
188 bool pinned )
189{
190 LIB_TREE_NODE_LIBRARY& lib_node = m_tree.AddLib( aNodeName, aDesc );
191
192 lib_node.m_Pinned = pinned;
193
194 return lib_node;
195}
196
197
198void LIB_TREE_MODEL_ADAPTER::DoAddLibrary( const wxString& aNodeName, const wxString& aDesc,
199 const std::vector<LIB_TREE_ITEM*>& aItemList,
200 bool pinned, bool presorted )
201{
202 LIB_TREE_NODE_LIBRARY& lib_node = DoAddLibraryNode( aNodeName, aDesc, pinned );
203
204 for( LIB_TREE_ITEM* item: aItemList )
205 lib_node.AddItem( item );
206
207 lib_node.AssignIntrinsicRanks( presorted );
208}
209
210
211void LIB_TREE_MODEL_ADAPTER::UpdateSearchString( const wxString& aSearch, bool aState )
212{
213 {
214 wxWindowUpdateLocker updateLock( m_widget );
215
216 // Even with the updateLock, wxWidgets sometimes ties its knickers in a knot trying to
217 // run a wxdataview_selection_changed_callback() on a row that has been deleted.
218 // https://bugs.launchpad.net/kicad/+bug/1756255
219 m_widget->UnselectAll();
220
221 // This collapse is required before the call to "Freeze()" below. Once Freeze()
222 // is called, GetParent() will return nullptr. While this works for some calls, it
223 // segfaults when we have any expanded elements b/c the sub units in the tree don't
224 // have explicit references that are maintained over a search
225 // The tree will be expanded again below when we get our matches
226 //
227 // Also note that this cannot happen when we have deleted a symbol as GTK will also
228 // iterate over the tree in this case and find a symbol that has an invalid link
229 // and crash https://gitlab.com/kicad/code/kicad/-/issues/6910
230 if( !aState && !aSearch.IsNull() && m_tree.m_Children.size() )
231 {
232 for( std::unique_ptr<LIB_TREE_NODE>& child: m_tree.m_Children )
233 m_widget->Collapse( wxDataViewItem( &*child ) );
234 }
235
236 // DO NOT REMOVE THE FREEZE/THAW. This freeze/thaw is a flag for this model adapter
237 // that tells it when it shouldn't trust any of the data in the model. When set, it will
238 // not return invalid data to the UI, since this invalid data can cause crashes.
239 // This is different than the update locker, which locks the UI aspects only.
240 Freeze();
241 BeforeReset();
242
244
245 wxStringTokenizer tokenizer( aSearch );
246 bool firstTerm = true;
247
248 while( tokenizer.HasMoreTokens() )
249 {
250 // First search for the full token, in case it appears in a search string
251 wxString term = tokenizer.GetNextToken().Lower();
252 EDA_COMBINED_MATCHER termMatcher( term, CTX_LIBITEM );
253
254 m_tree.UpdateScore( &termMatcher, wxEmptyString, firstTerm ? m_filter : nullptr );
255 firstTerm = false;
256
257 if( term.Contains( ":" ) )
258 {
259 // Next search for the library:item_name
260 wxString lib = term.BeforeFirst( ':' );
261 wxString itemName = term.AfterFirst( ':' );
262 EDA_COMBINED_MATCHER itemNameMatcher( itemName, CTX_LIBITEM );
263
264 m_tree.UpdateScore( &itemNameMatcher, lib, nullptr );
265 }
266 else
267 {
268 // In case the full token happens to be a library name
269 m_tree.UpdateScore( nullptr, term, nullptr );
270 }
271 }
272
273 if( firstTerm )
274 {
275 // No terms processed; just run the filter
276 m_tree.UpdateScore( nullptr, wxEmptyString, m_filter );
277 }
278
280 AfterReset();
281 Thaw();
282 }
283
284 const LIB_TREE_NODE* firstMatch = ShowResults();
285
286 if( firstMatch )
287 {
288 wxDataViewItem item = ToItem( firstMatch );
289 m_widget->Select( item );
290
291 // Make sure the *parent* item is visible. The selected item is the first (shown) child
292 // of the parent. So it's always right below the parent, and this way the user can also
293 // see what library the selected part belongs to, without having a case where the selection
294 // is off the screen (unless the window is a single row high, which is unlikely).
295 //
296 // This also happens to circumvent https://bugs.launchpad.net/kicad/+bug/1804400 which
297 // appears to be a GTK+3 bug.
298 {
299 wxDataViewItem parent = GetParent( item );
300
301 if( parent.IsOk() )
302 m_widget->EnsureVisible( parent );
303 }
304
305 m_widget->EnsureVisible( item );
306 }
307}
308
309
310void LIB_TREE_MODEL_ADAPTER::AttachTo( wxDataViewCtrl* aDataViewCtrl )
311{
312 m_widget = aDataViewCtrl;
313 aDataViewCtrl->SetIndent( kDataViewIndent );
314 aDataViewCtrl->AssociateModel( this );
316}
317
318
320{
321 m_widget->ClearColumns();
322
323 m_columns.clear();
324 m_colIdxMap.clear();
325 m_colNameMap.clear();
326
327 // The Item column is always shown
328 doAddColumn( wxT( "Item" ) );
329
330 for( const wxString& colName : m_shownColumns )
331 {
332 if( !m_colNameMap.count( colName ) )
333 doAddColumn( colName, colName == wxT( "Description" ) );
334 }
335}
336
337
339{
340 Freeze();
341 BeforeReset();
342
344
345 AfterReset();
346 Thaw();
347}
348
349
351{
353 aTreeNode->m_Pinned = true;
354
355 resortTree();
356 m_widget->EnsureVisible( ToItem( aTreeNode ) );
357}
358
359
361{
363 aTreeNode->m_Pinned = false;
364
365 resortTree();
366 // Keep focus at top when unpinning
367}
368
369
370wxDataViewColumn* LIB_TREE_MODEL_ADAPTER::doAddColumn( const wxString& aHeader, bool aTranslate )
371{
372 wxString translatedHeader = aTranslate ? wxGetTranslation( aHeader ) : aHeader;
373
374 // The extent of the text doesn't take into account the space on either side
375 // in the header, so artificially pad it
376 wxSize headerMinWidth = KIUI::GetTextSize( translatedHeader + wxT( "MMM" ), m_widget );
377
378 if( !m_colWidths.count( aHeader ) || m_colWidths[aHeader] < headerMinWidth.x )
379 m_colWidths[aHeader] = headerMinWidth.x;
380
381 int index = (int) m_columns.size();
382
383 wxDataViewColumn* col = new wxDataViewColumn( translatedHeader, new LIB_TREE_RENDERER(),
384 index, m_colWidths[aHeader], wxALIGN_NOT,
385 wxDATAVIEW_CELL_INERT );
386 m_widget->AppendColumn( col );
387
388 col->SetMinWidth( headerMinWidth.x );
389
390 m_columns.emplace_back( col );
391 m_colNameMap[aHeader] = col;
392 m_colIdxMap[m_columns.size() - 1] = aHeader;
393
394 return col;
395}
396
397
398void LIB_TREE_MODEL_ADAPTER::addColumnIfNecessary( const wxString& aHeader )
399{
400 if( m_colNameMap.count( aHeader ) )
401 return;
402
403 // Columns will be created later
404 m_colNameMap[aHeader] = nullptr;
405 m_availableColumns.emplace_back( aHeader );
406}
407
408
409void LIB_TREE_MODEL_ADAPTER::SetShownColumns( const std::vector<wxString>& aColumnNames )
410{
411 bool recreate = m_shownColumns != aColumnNames;
412
413 m_shownColumns = aColumnNames;
414
415 if( recreate && m_widget )
417}
418
419
420LIB_ID LIB_TREE_MODEL_ADAPTER::GetAliasFor( const wxDataViewItem& aSelection ) const
421{
422 const LIB_TREE_NODE* node = ToNode( aSelection );
423 return node ? node->m_LibId : LIB_ID();
424}
425
426
427int LIB_TREE_MODEL_ADAPTER::GetUnitFor( const wxDataViewItem& aSelection ) const
428{
429 const LIB_TREE_NODE* node = ToNode( aSelection );
430 return node ? node->m_Unit : 0;
431}
432
433
434LIB_TREE_NODE::TYPE LIB_TREE_MODEL_ADAPTER::GetTypeFor( const wxDataViewItem& aSelection ) const
435{
436 const LIB_TREE_NODE* node = ToNode( aSelection );
437 return node ? node->m_Type : LIB_TREE_NODE::INVALID;
438}
439
440
441LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::GetTreeNodeFor( const wxDataViewItem& aSelection ) const
442{
443 return ToNode( aSelection );
444}
445
446
448{
449 int n = 0;
450
451 for( const std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
452 n += lib->m_Children.size();
453
454 return n;
455}
456
457
458wxDataViewItem LIB_TREE_MODEL_ADAPTER::FindItem( const LIB_ID& aLibId )
459{
460 for( std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
461 {
462 if( lib->m_Name != aLibId.GetLibNickname() )
463 continue;
464
465 // if part name is not specified, return the library node
466 if( aLibId.GetLibItemName() == "" )
467 return ToItem( lib.get() );
468
469 for( std::unique_ptr<LIB_TREE_NODE>& alias: lib->m_Children )
470 {
471 if( alias->m_Name == aLibId.GetLibItemName() )
472 return ToItem( alias.get() );
473 }
474
475 break; // could not find the part in the requested library
476 }
477
478 return wxDataViewItem();
479}
480
481
483{
485}
486
487
488unsigned int LIB_TREE_MODEL_ADAPTER::GetChildren( const wxDataViewItem& aItem,
489 wxDataViewItemArray& aChildren ) const
490{
491 const LIB_TREE_NODE* node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
492 unsigned int count = 0;
493
494 if( node->m_Type == LIB_TREE_NODE::TYPE::ROOT
496 || ( m_show_units && node->m_Type == LIB_TREE_NODE::TYPE::ITEM ) )
497 {
498 for( std::unique_ptr<LIB_TREE_NODE> const& child: node->m_Children )
499 {
500 if( child->m_Score > 0 )
501 {
502 aChildren.Add( ToItem( &*child ) );
503 ++count;
504 }
505 }
506 }
507
508 return count;
509}
510
511
513{
514 wxDataViewColumn* col = nullptr;
515 size_t idx = 0;
516 int totalWidth = 0;
517 wxString header;
518
519 for( ; idx < m_columns.size() - 1; idx++ )
520 {
521 wxASSERT( m_colIdxMap.count( idx ) );
522
523 col = m_columns[idx];
524 header = m_colIdxMap[idx];
525
526 wxASSERT( m_colWidths.count( header ) );
527
528 col->SetWidth( m_colWidths[header] );
529 totalWidth += col->GetWidth();
530 }
531
532 int remainingWidth = m_widget->GetSize().x - totalWidth;
533 header = m_columns[idx]->GetTitle();
534
535 m_columns[idx]->SetWidth( std::max( m_colWidths[header], remainingWidth ) );
536}
537
538
540{
541 // Yes, this is an enormous hack. But it works on all platforms, it doesn't suffer
542 // the On^2 sorting issues that ItemChanged() does on OSX, and it doesn't lose the
543 // user's scroll position (which re-attaching or deleting/re-inserting columns does).
544 static int walk = 1;
545
546 std::vector<int> widths;
547
548 for( const wxDataViewColumn* col : m_columns )
549 widths.emplace_back( col->GetWidth() );
550
551 wxASSERT( widths.size() );
552
553 // Only use the widths read back if they are non-zero.
554 // GTK returns the displayed width of the column, which is not calculated immediately
555 if( widths[0] > 0 )
556 {
557 size_t i = 0;
558
559 for( const auto& [ colName, colPtr ] : m_colNameMap )
560 m_colWidths[ colName ] = widths[i++];
561 }
562
563 auto colIt = m_colWidths.begin();
564
565 colIt->second += walk;
566 colIt++;
567
568 if( colIt != m_colWidths.end() )
569 colIt->second -= walk;
570
571 for( const auto& [ colName, colPtr ] : m_colNameMap )
572 {
573 if( colPtr == m_columns[0] )
574 continue;
575
576 wxASSERT( m_colWidths.count( colName ) );
577 colPtr->SetWidth( m_colWidths[ colName ] );
578 }
579
580 walk = -walk;
581}
582
583
584bool LIB_TREE_MODEL_ADAPTER::HasContainerColumns( const wxDataViewItem& aItem ) const
585{
586 return IsContainer( aItem );
587}
588
589
590bool LIB_TREE_MODEL_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
591{
592 LIB_TREE_NODE* node = ToNode( aItem );
593 return node ? node->m_Children.size() : true;
594}
595
596
597wxDataViewItem LIB_TREE_MODEL_ADAPTER::GetParent( const wxDataViewItem& aItem ) const
598{
599 if( m_freeze )
600 return ToItem( nullptr );
601
602 LIB_TREE_NODE* node = ToNode( aItem );
603 LIB_TREE_NODE* parent = node ? node->m_Parent : nullptr;
604
605 // wxDataViewModel has no root node, but rather top-level elements have
606 // an invalid (null) parent.
607 if( !node || !parent || parent->m_Type == LIB_TREE_NODE::TYPE::ROOT )
608 return ToItem( nullptr );
609 else
610 return ToItem( parent );
611}
612
613
614void LIB_TREE_MODEL_ADAPTER::GetValue( wxVariant& aVariant,
615 const wxDataViewItem& aItem,
616 unsigned int aCol ) const
617{
618 if( IsFrozen() )
619 {
620 aVariant = wxEmptyString;
621 return;
622 }
623
624 LIB_TREE_NODE* node = ToNode( aItem );
625 wxCHECK( node, /* void */ );
626 wxString valueStr;
627
628 switch( aCol )
629 {
630 case NAME_COL:
631 if( node->m_Pinned )
632 valueStr = GetPinningSymbol() + UnescapeString( node->m_Name );
633 else
634 valueStr = UnescapeString( node->m_Name );
635
636 break;
637
638 default:
639 if( m_colIdxMap.count( aCol ) )
640 {
641 const wxString& key = m_colIdxMap.at( aCol );
642
643 if( node->m_Fields.count( key ) )
644 valueStr = UnescapeString( node->m_Fields.at( key ) );
645 else if( key == wxT( "Description" ) )
646 valueStr = UnescapeString( node->m_Desc );
647 else
648 valueStr = wxEmptyString;
649 }
650
651 break;
652 }
653
654 valueStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
655
656 aVariant = valueStr;
657}
658
659
660bool LIB_TREE_MODEL_ADAPTER::GetAttr( const wxDataViewItem& aItem,
661 unsigned int aCol,
662 wxDataViewItemAttr& aAttr ) const
663{
664 if( IsFrozen() )
665 return false;
666
667 LIB_TREE_NODE* node = ToNode( aItem );
668 wxCHECK( node, false );
669
670 if( node->m_Type == LIB_TREE_NODE::ITEM )
671 {
672 if( !node->m_IsRoot && aCol == 0 )
673 {
674 // Names of non-root aliases are italicized
675 aAttr.SetItalic( true );
676 return true;
677 }
678 }
679
680 return false;
681}
682
683
684void recursiveDescent( LIB_TREE_NODE& aNode, const std::function<bool( const LIB_TREE_NODE* )>& f )
685{
686 for( std::unique_ptr<LIB_TREE_NODE>& node: aNode.m_Children )
687 {
688 if( !f( node.get() ) )
689 break;
690
691 recursiveDescent( *node, f );
692 }
693}
694
695
697{
698 const LIB_TREE_NODE* firstMatch = nullptr;
699
700 // Expand parents of leaf nodes with some level of matching
702 [&]( const LIB_TREE_NODE* n )
703 {
704 if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM && n->m_Score > 1 )
705 {
706 if( !firstMatch )
707 firstMatch = n;
708 else if( n->m_Score > firstMatch->m_Score )
709 firstMatch = n;
710
711 m_widget->ExpandAncestors( ToItem( n ) );
712 }
713
714 return true; // keep going to expand ancestors of all found items
715 } );
716
717 // If no matches, find and show the preselect node
718 if( !firstMatch && m_preselect_lib_id.IsValid() )
719 {
721 [&]( const LIB_TREE_NODE* n )
722 {
724 && ( n->m_Children.empty() || !m_preselect_unit )
725 && m_preselect_lib_id == n->m_LibId )
726 {
727 firstMatch = n;
728 m_widget->ExpandAncestors( ToItem( n ) );
729 return false;
730 }
731 else if( n->m_Type == LIB_TREE_NODE::UNIT
734 {
735 firstMatch = n;
736 m_widget->ExpandAncestors( ToItem( n ) );
737 return false;
738 }
739
740 return true;
741 } );
742 }
743
744 // If still no matches expand a single library if there is only one
745 if( !firstMatch )
746 {
747 int libraries = 0;
748
749 for( const std::unique_ptr<LIB_TREE_NODE>& child : m_tree.m_Children )
750 {
751 if( !child->m_Name.StartsWith( "-- " ) )
752 libraries++;
753 }
754
755 if( libraries != 1 )
756 return nullptr;
757
759 [&]( const LIB_TREE_NODE* n )
760 {
761 if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM )
762 {
763 firstMatch = n;
764 m_widget->ExpandAncestors( ToItem( n ) );
765 return false;
766 }
767
768 return true;
769 } );
770 }
771
772 return firstMatch;
773}
774
775
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:92
The base frame for deriving all KiCad main window classes.
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:95
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
A mix-in to provide polymorphism between items stored in libraries (symbols, aliases and footprints).
Definition: lib_tree_item.h:41
int GetUnitFor(const wxDataViewItem &aSelection) const
Return the unit for the given item.
LIB_TREE_NODE::TYPE GetTypeFor(const wxDataViewItem &aSelection) const
Return node type for the given item.
bool GetAttr(const wxDataViewItem &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
Get any formatting for an item.
std::map< wxString, int > m_colWidths
void FinishTreeInitialization()
A final-stage initialization to be called after the window hierarchy has been realized and the window...
void addColumnIfNecessary(const wxString &aHeader)
void PinLibrary(LIB_TREE_NODE *aTreeNode)
virtual wxDataViewItem GetCurrentDataViewItem()
void SetPreselectNode(const LIB_ID &aLibId, int aUnit)
Set the symbol name to be selected if there are no search results.
static LIB_TREE_NODE * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> #SYM_TREE_NODE.
LIB_ID GetAliasFor(const wxDataViewItem &aSelection) const
Return the alias for the given item.
void AttachTo(wxDataViewCtrl *aDataViewCtrl)
Attach to a wxDataViewCtrl and initialize it.
void 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.
void SetShownColumns(const std::vector< wxString > &aColumnNames)
Sets which columns are shown in the widget.
static wxDataViewItem ToItem(const LIB_TREE_NODE *aNode)
Convert #SYM_TREE_NODE -> wxDataViewItem.
virtual bool isSymbolModel()=0
bool IsContainer(const wxDataViewItem &aItem) const override
Check whether an item can have children.
static const wxString GetPinningSymbol()
void ShowUnits(bool aShow)
Whether or not to show units.
LIB_TREE_NODE * GetTreeNodeFor(const wxDataViewItem &aSelection) const
unsigned int GetChildren(const wxDataViewItem &aItem, wxDataViewItemArray &aChildren) const override
Populate a list of all the children of an item.
void SaveSettings()
Save the column widths to the config file.
@ NAME_COL
Library or library item name column.
std::map< unsigned, wxString > m_colIdxMap
wxDataViewColumn * doAddColumn(const wxString &aHeader, bool aTranslate=true)
LIB_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, const wxString &aPinnedKey)
Create the adapter.
std::vector< wxDataViewColumn * > m_columns
std::vector< wxString > GetShownColumns() const
int GetItemCount() const
Return the number of symbols loaded in the tree.
const LIB_TREE_NODE * ShowResults()
Find and expand successful search results.
std::vector< wxString > m_shownColumns
void GetValue(wxVariant &aVariant, const wxDataViewItem &aItem, unsigned int aCol) const override
Get the value of an item.
void UnpinLibrary(LIB_TREE_NODE *aTreeNode)
std::map< wxString, wxDataViewColumn * > m_colNameMap
bool HasContainerColumns(const wxDataViewItem &aItem) const override
Check whether a container has columns too.
wxDataViewItem GetParent(const wxDataViewItem &aItem) const override
Get the parent of an item.
LIB_TREE_NODE_LIBRARY & DoAddLibraryNode(const wxString &aNodeName, const wxString &aDesc, bool pinned)
std::vector< wxString > m_availableColumns
wxDataViewItem FindItem(const LIB_ID &aLibId)
Returns tree item corresponding to part.
void UpdateSearchString(const wxString &aSearch, bool aState)
Set the search string provided by the user.
std::function< bool(LIB_TREE_NODE &aNode)> * m_filter
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.
LIB_TREE_NODE_LIBRARY & AddLib(wxString const &aName, wxString const &aDesc)
Construct an empty library node, add it to the root, and return it.
void UpdateScore(EDA_COMBINED_MATCHER *aMatcher, const wxString &aLib, std::function< bool(LIB_TREE_NODE &aNode)> *aFilter) override
Update the score for this part.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
void SortNodes(bool aUseScores)
Sort child nodes quickly and recursively (IntrinsicRanks must have been set).
enum TYPE m_Type
std::map< wxString, wxString > m_Fields
List of weighted search terms.
PTR_VECTOR m_Children
LIB_TREE_NODE * m_Parent
virtual void ResetScore()
Initialize scores recursively.
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
void SetAttr(const wxDataViewItemAttr &aAttr) override
bool SetValue(const wxVariant &aValue) override
wxSize GetSize() const override
bool GetValue(wxVariant &aValue) const override
bool Render(wxRect aRect, wxDC *dc, int aState) override
void UnpinLibrary(const wxString &aLibrary, bool isSymbolLibrary)
Definition: project.cpp:200
void PinLibrary(const wxString &aLibrary, bool isSymbolLibrary)
Definition: project.cpp:179
#define _HKI(x)
static void recursiveDescent(wxSizer *aSizer, std::map< int, wxString > &aLabels)
Base window classes and related definitions.
Abstract pattern-matching tool and implementations.
@ CTX_LIBITEM
void recursiveDescent(LIB_TREE_NODE &aNode, const std::function< bool(const LIB_TREE_NODE *)> &f)
static const int kDataViewIndent
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: gtk/ui.cpp:48
wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currentl...
Definition: ui_common.cpp:74
wxString UnescapeString(const wxString &aSource)
std::vector< wxString > columns
Ordered list of visible columns in the tree.
Definition: app_settings.h:121
std::map< wxString, int > column_widths
Column widths, keyed by header name.
Definition: app_settings.h:122
Functions to provide common constants and other functions to assist in making a consistent UI.