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 + 2 );
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 aRect.Deflate( 1 );
95
96#ifdef __WXOSX__
97 // We should be able to pass wxDATAVIEW_CELL_SELECTED into RenderText() and have it do
98 // the right thing -- but it picks wxSYS_COLOUR_HIGHLIGHTTEXT on MacOS (instead
99 // of wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT).
100 if( aState & wxDATAVIEW_CELL_SELECTED )
101 dc->SetTextForeground( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
102
103 RenderText( m_text, 0, aRect, dc, 0 );
104#else
105 RenderText( m_text, 0, aRect, dc, aState );
106#endif
107 return true;
108 }
109
110private:
112 wxString m_text;
113};
114
115
116wxDataViewItem LIB_TREE_MODEL_ADAPTER::ToItem( const LIB_TREE_NODE* aNode )
117{
118 return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
119}
120
121
123{
124 return static_cast<LIB_TREE_NODE*>( aItem.GetID() );
125}
126
127
129 const wxString& aPinnedKey ) :
130 m_widget( nullptr ),
131 m_parent( aParent ),
132 m_sort_mode( BEST_MATCH ),
133 m_show_units( true ),
134 m_preselect_unit( 0 ),
135 m_freeze( 0 ),
136 m_filter( nullptr )
137{
138 // Default column widths. Do not translate these names.
139 m_colWidths[ _HKI( "Item" ) ] = 300;
140 m_colWidths[ _HKI( "Description" ) ] = 600;
141
142 m_availableColumns = { _HKI( "Item" ), _HKI( "Description" ) };
143
145
146 for( const std::pair<const wxString, int>& pair : cfg->m_LibTree.column_widths )
147 m_colWidths[pair.first] = pair.second;
148
150
151 if( m_shownColumns.empty() )
152 m_shownColumns = { _HKI( "Item" ), _HKI( "Description" ) };
153
154 if( m_shownColumns[0] != _HKI( "Item" ) )
155 m_shownColumns.insert( m_shownColumns.begin(), _HKI( "Item" ) );
156}
157
158
160{}
161
162
163std::vector<wxString> LIB_TREE_MODEL_ADAPTER::GetOpenLibs() const
164{
165 std::vector<wxString> openLibs;
166 wxDataViewItem rootItem( nullptr );
167 wxDataViewItemArray children;
168
169 GetChildren( rootItem, children );
170
171 for( const wxDataViewItem& child : children )
172 {
173 if( m_widget->IsExpanded( child ) )
174 openLibs.emplace_back( ToNode( child )->m_LibId.GetLibNickname().wx_str() );
175 }
176
177 return openLibs;
178}
179
180
181void LIB_TREE_MODEL_ADAPTER::OpenLibs( const std::vector<wxString>& aLibs )
182{
183 wxWindowUpdateLocker updateLock( m_widget );
184
185 for( const wxString& lib : aLibs )
186 {
187 wxDataViewItem item = FindItem( LIB_ID( lib, wxEmptyString ) );
188
189 if( item.IsOk() )
190 m_widget->Expand( item );
191 }
192}
193
194
196{
197 if( m_widget )
198 {
200
202 cfg->m_LibTree.column_widths.clear();
203
204 for( const std::pair<const wxString, wxDataViewColumn*>& pair : m_colNameMap )
205 cfg->m_LibTree.column_widths[pair.first] = pair.second->GetWidth();
206
208 }
209}
210
211
213{
214 m_show_units = aShow;
215}
216
217
218void LIB_TREE_MODEL_ADAPTER::SetPreselectNode( const LIB_ID& aLibId, int aUnit )
219{
220 m_preselect_lib_id = aLibId;
221 m_preselect_unit = aUnit;
222}
223
224
226 const wxString& aDesc,
227 bool pinned )
228{
229 LIB_TREE_NODE_LIBRARY& lib_node = m_tree.AddLib( aNodeName, aDesc );
230
231 lib_node.m_Pinned = pinned;
232
233 return lib_node;
234}
235
236
237void LIB_TREE_MODEL_ADAPTER::DoAddLibrary( const wxString& aNodeName, const wxString& aDesc,
238 const std::vector<LIB_TREE_ITEM*>& aItemList,
239 bool pinned, bool presorted )
240{
241 LIB_TREE_NODE_LIBRARY& lib_node = DoAddLibraryNode( aNodeName, aDesc, pinned );
242
243 for( LIB_TREE_ITEM* item: aItemList )
244 lib_node.AddItem( item );
245
246 lib_node.AssignIntrinsicRanks( presorted );
247}
248
249
250void LIB_TREE_MODEL_ADAPTER::UpdateSearchString( const wxString& aSearch, bool aState )
251{
252 {
253 wxWindowUpdateLocker updateLock( m_widget );
254
255 // Even with the updateLock, wxWidgets sometimes ties its knickers in a knot trying to
256 // run a wxdataview_selection_changed_callback() on a row that has been deleted.
257 // https://bugs.launchpad.net/kicad/+bug/1756255
258 m_widget->UnselectAll();
259
260 // This collapse is required before the call to "Freeze()" below. Once Freeze()
261 // is called, GetParent() will return nullptr. While this works for some calls, it
262 // segfaults when we have any expanded elements b/c the sub units in the tree don't
263 // have explicit references that are maintained over a search
264 // The tree will be expanded again below when we get our matches
265 //
266 // Also note that this cannot happen when we have deleted a symbol as GTK will also
267 // iterate over the tree in this case and find a symbol that has an invalid link
268 // and crash https://gitlab.com/kicad/code/kicad/-/issues/6910
269 if( !aState && !aSearch.IsNull() && m_tree.m_Children.size() )
270 {
271 for( std::unique_ptr<LIB_TREE_NODE>& child: m_tree.m_Children )
272 m_widget->Collapse( wxDataViewItem( &*child ) );
273 }
274
275 // DO NOT REMOVE THE FREEZE/THAW. This freeze/thaw is a flag for this model adapter
276 // that tells it when it shouldn't trust any of the data in the model. When set, it will
277 // not return invalid data to the UI, since this invalid data can cause crashes.
278 // This is different than the update locker, which locks the UI aspects only.
279 Freeze();
280 BeforeReset();
281
283
284 wxStringTokenizer tokenizer( aSearch );
285 bool firstTerm = true;
286
287 while( tokenizer.HasMoreTokens() )
288 {
289 // First search for the full token, in case it appears in a search string
290 wxString term = tokenizer.GetNextToken().Lower();
291 EDA_COMBINED_MATCHER termMatcher( term, CTX_LIBITEM );
292
293 m_tree.UpdateScore( &termMatcher, wxEmptyString, firstTerm ? m_filter : nullptr );
294 firstTerm = false;
295
296 if( term.Contains( ":" ) )
297 {
298 // Next search for the library:item_name
299 wxString lib = term.BeforeFirst( ':' );
300 wxString itemName = term.AfterFirst( ':' );
301 EDA_COMBINED_MATCHER itemNameMatcher( itemName, CTX_LIBITEM );
302
303 m_tree.UpdateScore( &itemNameMatcher, lib, nullptr );
304 }
305 }
306
307 if( firstTerm )
308 {
309 // No terms processed; just run the filter
310 m_tree.UpdateScore( nullptr, wxEmptyString, m_filter );
311 }
312
314 AfterReset();
315 Thaw();
316 }
317
318 const LIB_TREE_NODE* firstMatch = ShowResults();
319
320 if( firstMatch )
321 {
322 wxDataViewItem item = ToItem( firstMatch );
323 m_widget->Select( item );
324
325 // Make sure the *parent* item is visible. The selected item is the first (shown) child
326 // of the parent. So it's always right below the parent, and this way the user can also
327 // see what library the selected part belongs to, without having a case where the selection
328 // is off the screen (unless the window is a single row high, which is unlikely).
329 //
330 // This also happens to circumvent https://bugs.launchpad.net/kicad/+bug/1804400 which
331 // appears to be a GTK+3 bug.
332 {
333 wxDataViewItem parent = GetParent( item );
334
335 if( parent.IsOk() )
336 m_widget->EnsureVisible( parent );
337 }
338
339 m_widget->EnsureVisible( item );
340 }
341}
342
343
344void LIB_TREE_MODEL_ADAPTER::AttachTo( wxDataViewCtrl* aDataViewCtrl )
345{
346 m_widget = aDataViewCtrl;
347 aDataViewCtrl->SetIndent( kDataViewIndent );
348 aDataViewCtrl->AssociateModel( this );
350}
351
352
354{
355 m_widget->ClearColumns();
356
357 m_columns.clear();
358 m_colIdxMap.clear();
359 m_colNameMap.clear();
360
361 // The Item column is always shown
362 doAddColumn( wxT( "Item" ) );
363
364 for( const wxString& colName : m_shownColumns )
365 {
366 if( !m_colNameMap.count( colName ) )
367 doAddColumn( colName, colName == wxT( "Description" ) );
368 }
369}
370
371
373{
374 Freeze();
375 BeforeReset();
376
378
379 AfterReset();
380 Thaw();
381}
382
383
385{
387 aTreeNode->m_Pinned = true;
388
389 resortTree();
390 m_widget->EnsureVisible( ToItem( aTreeNode ) );
391}
392
393
395{
397 aTreeNode->m_Pinned = false;
398
399 resortTree();
400 // Keep focus at top when unpinning
401}
402
403
404wxDataViewColumn* LIB_TREE_MODEL_ADAPTER::doAddColumn( const wxString& aHeader, bool aTranslate )
405{
406 wxString translatedHeader = aTranslate ? wxGetTranslation( aHeader ) : aHeader;
407
408 // The extent of the text doesn't take into account the space on either side
409 // in the header, so artificially pad it
410 wxSize headerMinWidth = KIUI::GetTextSize( translatedHeader + wxT( "MMM" ), m_widget );
411
412 if( !m_colWidths.count( aHeader ) || m_colWidths[aHeader] < headerMinWidth.x )
413 m_colWidths[aHeader] = headerMinWidth.x;
414
415 int index = (int) m_columns.size();
416
417 wxDataViewColumn* col = new wxDataViewColumn(
418 translatedHeader, new LIB_TREE_RENDERER(), index, m_colWidths[aHeader], wxALIGN_NOT,
419 wxDATAVIEW_CELL_INERT | static_cast<int>( wxDATAVIEW_COL_RESIZABLE ) );
420 m_widget->AppendColumn( col );
421
422 col->SetMinWidth( headerMinWidth.x );
423
424 m_columns.emplace_back( col );
425 m_colNameMap[aHeader] = col;
426 m_colIdxMap[m_columns.size() - 1] = aHeader;
427
428 return col;
429}
430
431
432void LIB_TREE_MODEL_ADAPTER::addColumnIfNecessary( const wxString& aHeader )
433{
434 if( m_colNameMap.count( aHeader ) )
435 return;
436
437 // Columns will be created later
438 m_colNameMap[aHeader] = nullptr;
439 m_availableColumns.emplace_back( aHeader );
440}
441
442
443void LIB_TREE_MODEL_ADAPTER::SetShownColumns( const std::vector<wxString>& aColumnNames )
444{
445 bool recreate = m_shownColumns != aColumnNames;
446
447 m_shownColumns = aColumnNames;
448
449 if( recreate && m_widget )
451}
452
453
454LIB_ID LIB_TREE_MODEL_ADAPTER::GetAliasFor( const wxDataViewItem& aSelection ) const
455{
456 const LIB_TREE_NODE* node = ToNode( aSelection );
457 return node ? node->m_LibId : LIB_ID();
458}
459
460
461int LIB_TREE_MODEL_ADAPTER::GetUnitFor( const wxDataViewItem& aSelection ) const
462{
463 const LIB_TREE_NODE* node = ToNode( aSelection );
464 return node ? node->m_Unit : 0;
465}
466
467
468LIB_TREE_NODE::TYPE LIB_TREE_MODEL_ADAPTER::GetTypeFor( const wxDataViewItem& aSelection ) const
469{
470 const LIB_TREE_NODE* node = ToNode( aSelection );
471 return node ? node->m_Type : LIB_TREE_NODE::INVALID;
472}
473
474
475LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::GetTreeNodeFor( const wxDataViewItem& aSelection ) const
476{
477 return ToNode( aSelection );
478}
479
480
482{
483 int n = 0;
484
485 for( const std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
486 n += lib->m_Children.size();
487
488 return n;
489}
490
491
492wxDataViewItem LIB_TREE_MODEL_ADAPTER::FindItem( const LIB_ID& aLibId )
493{
494 for( std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
495 {
496 if( lib->m_Name != aLibId.GetLibNickname().wx_str() )
497 continue;
498
499 // if part name is not specified, return the library node
500 if( aLibId.GetLibItemName() == "" )
501 return ToItem( lib.get() );
502
503 for( std::unique_ptr<LIB_TREE_NODE>& alias: lib->m_Children )
504 {
505 if( alias->m_Name == aLibId.GetLibItemName().wx_str() )
506 return ToItem( alias.get() );
507 }
508
509 break; // could not find the part in the requested library
510 }
511
512 return wxDataViewItem();
513}
514
515
517{
519}
520
521
522unsigned int LIB_TREE_MODEL_ADAPTER::GetChildren( const wxDataViewItem& aItem,
523 wxDataViewItemArray& aChildren ) const
524{
525 const LIB_TREE_NODE* node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
526 unsigned int count = 0;
527
528 if( node->m_Type == LIB_TREE_NODE::TYPE::ROOT
530 || ( m_show_units && node->m_Type == LIB_TREE_NODE::TYPE::ITEM ) )
531 {
532 for( std::unique_ptr<LIB_TREE_NODE> const& child: node->m_Children )
533 {
534 if( child->m_Score > 0 )
535 {
536 aChildren.Add( ToItem( &*child ) );
537 ++count;
538 }
539 }
540 }
541
542 return count;
543}
544
545
547{
548 wxDataViewColumn* col = nullptr;
549 size_t idx = 0;
550 int totalWidth = 0;
551 wxString header;
552
553 for( ; idx < m_columns.size() - 1; idx++ )
554 {
555 wxASSERT( m_colIdxMap.count( idx ) );
556
557 col = m_columns[idx];
558 header = m_colIdxMap[idx];
559
560 wxASSERT( m_colWidths.count( header ) );
561
562 col->SetWidth( m_colWidths[header] );
563 totalWidth += col->GetWidth();
564 }
565
566 int remainingWidth = m_widget->GetSize().x - totalWidth;
567 header = m_columns[idx]->GetTitle();
568
569 m_columns[idx]->SetWidth( std::max( m_colWidths[header], remainingWidth ) );
570}
571
572
574{
575 // Yes, this is an enormous hack. But it works on all platforms, it doesn't suffer
576 // the On^2 sorting issues that ItemChanged() does on OSX, and it doesn't lose the
577 // user's scroll position (which re-attaching or deleting/re-inserting columns does).
578 static int walk = 1;
579
580 std::vector<int> widths;
581
582 for( const wxDataViewColumn* col : m_columns )
583 widths.emplace_back( col->GetWidth() );
584
585 wxASSERT( widths.size() );
586
587 // Only use the widths read back if they are non-zero.
588 // GTK returns the displayed width of the column, which is not calculated immediately
589 if( widths[0] > 0 )
590 {
591 size_t i = 0;
592
593 for( const auto& [ colName, colPtr ] : m_colNameMap )
594 m_colWidths[ colName ] = widths[i++];
595 }
596
597 auto colIt = m_colWidths.begin();
598
599 colIt->second += walk;
600 colIt++;
601
602 if( colIt != m_colWidths.end() )
603 colIt->second -= walk;
604
605 for( const auto& [ colName, colPtr ] : m_colNameMap )
606 {
607 if( colPtr == m_columns[0] )
608 continue;
609
610 wxASSERT( m_colWidths.count( colName ) );
611 colPtr->SetWidth( m_colWidths[ colName ] );
612 }
613
614 walk = -walk;
615}
616
617
618bool LIB_TREE_MODEL_ADAPTER::HasContainerColumns( const wxDataViewItem& aItem ) const
619{
620 return IsContainer( aItem );
621}
622
623
624bool LIB_TREE_MODEL_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
625{
626 LIB_TREE_NODE* node = ToNode( aItem );
627 return node ? node->m_Children.size() : true;
628}
629
630
631wxDataViewItem LIB_TREE_MODEL_ADAPTER::GetParent( const wxDataViewItem& aItem ) const
632{
633 if( m_freeze )
634 return ToItem( nullptr );
635
636 LIB_TREE_NODE* node = ToNode( aItem );
637 LIB_TREE_NODE* parent = node ? node->m_Parent : nullptr;
638
639 // wxDataViewModel has no root node, but rather top-level elements have
640 // an invalid (null) parent.
641 if( !node || !parent || parent->m_Type == LIB_TREE_NODE::TYPE::ROOT )
642 return ToItem( nullptr );
643 else
644 return ToItem( parent );
645}
646
647
648void LIB_TREE_MODEL_ADAPTER::GetValue( wxVariant& aVariant,
649 const wxDataViewItem& aItem,
650 unsigned int aCol ) const
651{
652 if( IsFrozen() )
653 {
654 aVariant = wxEmptyString;
655 return;
656 }
657
658 LIB_TREE_NODE* node = ToNode( aItem );
659 wxCHECK( node, /* void */ );
660 wxString valueStr;
661
662 switch( aCol )
663 {
664 case NAME_COL:
665 if( node->m_Pinned )
666 valueStr = GetPinningSymbol() + UnescapeString( node->m_Name );
667 else
668 valueStr = UnescapeString( node->m_Name );
669
670 break;
671
672 default:
673 if( m_colIdxMap.count( aCol ) )
674 {
675 const wxString& key = m_colIdxMap.at( aCol );
676
677 if( key == wxT( "Description" ) )
678 valueStr = UnescapeString( node->m_Desc );
679 else if( node->m_Fields.count( key ) )
680 valueStr = UnescapeString( node->m_Fields.at( key ) );
681 else
682 valueStr = wxEmptyString;
683 }
684
685 break;
686 }
687
688 valueStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
689
690 aVariant = valueStr;
691}
692
693
694bool LIB_TREE_MODEL_ADAPTER::GetAttr( const wxDataViewItem& aItem,
695 unsigned int aCol,
696 wxDataViewItemAttr& aAttr ) const
697{
698 if( IsFrozen() )
699 return false;
700
701 LIB_TREE_NODE* node = ToNode( aItem );
702 wxCHECK( node, false );
703
704 if( node->m_Type == LIB_TREE_NODE::ITEM )
705 {
706 if( !node->m_IsRoot && aCol == 0 )
707 {
708 // Names of non-root aliases are italicized
709 aAttr.SetItalic( true );
710 return true;
711 }
712 }
713
714 return false;
715}
716
717
718void recursiveDescent( LIB_TREE_NODE& aNode, const std::function<int( const LIB_TREE_NODE* )>& f )
719{
720 for( std::unique_ptr<LIB_TREE_NODE>& node: aNode.m_Children )
721 {
722 int r = f( node.get() );
723
724 if( r == 0 )
725 break;
726 else if( r == -1 )
727 continue;
728
729 recursiveDescent( *node, f );
730 }
731}
732
733
735{
736 const LIB_TREE_NODE* firstMatch = nullptr;
737
738 // Expand parents of leaf nodes with some level of matching
740 [&]( const LIB_TREE_NODE* n )
741 {
742 if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM && n->m_Score > 1 )
743 {
744 if( !firstMatch )
745 firstMatch = n;
746 else if( n->m_Score > firstMatch->m_Score )
747 firstMatch = n;
748
749 m_widget->ExpandAncestors( ToItem( n ) );
750 }
751
752 return 1; // keep going to expand ancestors of all found items
753 } );
754
755 // If no matches, find and show the preselect node
756 if( !firstMatch && m_preselect_lib_id.IsValid() )
757 {
759 [&]( const LIB_TREE_NODE* n )
760 {
761 // Don't match the recent and already placed libraries
762 if( n->m_Name.StartsWith( "-- " ) )
763 return -1; // Skip this node and its children
764
766 && ( n->m_Children.empty() || !m_preselect_unit )
767 && m_preselect_lib_id == n->m_LibId )
768 {
769 firstMatch = n;
770 m_widget->ExpandAncestors( ToItem( n ) );
771 return 0;
772 }
773 else if( n->m_Type == LIB_TREE_NODE::UNIT
776 {
777 firstMatch = n;
778 m_widget->ExpandAncestors( ToItem( n ) );
779 return 0;
780 }
781
782 return 1;
783 } );
784 }
785
786 // If still no matches expand a single library if there is only one
787 if( !firstMatch )
788 {
789 int libraries = 0;
790
791 for( const std::unique_ptr<LIB_TREE_NODE>& child : m_tree.m_Children )
792 {
793 if( !child->m_Name.StartsWith( "-- " ) )
794 libraries++;
795 }
796
797 if( libraries != 1 )
798 return nullptr;
799
801 [&]( const LIB_TREE_NODE* n )
802 {
803 if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM )
804 {
805 firstMatch = n;
806 m_widget->ExpandAncestors( ToItem( n ) );
807 return 0;
808 }
809
810 return 1;
811 } );
812 }
813
814 return firstMatch;
815}
816
817
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
std::vector< wxString > GetOpenLibs() const
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
void OpenLibs(const std::vector< wxString > &aLibs)
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:192
void PinLibrary(const wxString &aLibrary, bool isSymbolLibrary)
Definition: project.cpp:171
wxString wx_str() const
Definition: utf8.cpp:45
#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< int(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: wxgtk/ui.cpp:48
KICOMMON_API 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:77
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
std::vector< wxString > open_libs
list of libraries the user has open in the tree
Definition: app_settings.h:123
Functions to provide common constants and other functions to assist in making a consistent UI.