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