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 <wx/log.h>
36#include <string_utils.h>
37
38
39static const int kDataViewIndent = 20;
40
41
42class LIB_TREE_RENDERER : public wxDataViewCustomRenderer
43{
44public:
46 m_canvasItem( false )
47 {}
48
49 wxSize GetSize() const override
50 {
51 wxSize size( GetOwner()->GetWidth(), GetTextExtent( m_text ).y + 2 );
52
53#if defined( __WXGTK__ ) && !wxCHECK_VERSION( 3, 2, 7 )
54 // Somehow returning 0 or negative width prevents the returned height from
55 // being taken into account at all, even if we return strictly positive
56 // width from later calls to GetSize(), meaning that it's enough to return
57 // 0 from it once to completely break the layout for the entire lifetime of
58 // the control.
59 //
60 // As this is completely unexpected, forcefully prevent this from happening
61 size.IncTo( wxSize( 1, 1 ) );
62#endif
63
64 return size;
65 }
66
67 bool GetValue( wxVariant& aValue ) const override
68 {
69 aValue = m_text;
70 return true;
71 }
72
73 bool SetValue( const wxVariant& aValue ) override
74 {
75 m_text = aValue.GetString();
76 return true;
77 }
78
79 void SetAttr( const wxDataViewItemAttr& aAttr ) override
80 {
81 // Use strikethrough as a proxy for is-canvas-item
82 m_canvasItem = aAttr.GetStrikethrough();
83
84 wxDataViewItemAttr realAttr = aAttr;
85 realAttr.SetStrikethrough( false );
86
87 wxDataViewCustomRenderer::SetAttr( realAttr );
88 }
89
90 bool Render( wxRect aRect, wxDC *dc, int aState ) override
91 {
92 RenderBackground( dc, aRect );
93
94 if( m_canvasItem )
95 {
96 wxPoint points[6];
97 points[0] = aRect.GetTopLeft();
98 points[1] = aRect.GetTopRight() + wxPoint( -4, 0 );
99 points[2] = aRect.GetTopRight() + wxPoint( 0, aRect.GetHeight() / 2 );
100 points[3] = aRect.GetBottomRight() + wxPoint( -4, 1 );
101 points[4] = aRect.GetBottomLeft() + wxPoint( 0, 1 );
102 points[5] = aRect.GetTopLeft();
103
104 dc->SetPen( KIPLATFORM::UI::IsDarkTheme() ? *wxWHITE_PEN : *wxBLACK_PEN );
105 dc->DrawLines( 6, points );
106 }
107
108 aRect.Deflate( 1 );
109
110#ifdef __WXOSX__
111 // We should be able to pass wxDATAVIEW_CELL_SELECTED into RenderText() and have it do
112 // the right thing -- but it picks wxSYS_COLOUR_HIGHLIGHTTEXT on MacOS (instead
113 // of wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT).
114 if( aState & wxDATAVIEW_CELL_SELECTED )
115 dc->SetTextForeground( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
116
117 RenderText( m_text, 0, aRect, dc, 0 );
118#else
119 RenderText( m_text, 0, aRect, dc, aState );
120#endif
121 return true;
122 }
123
124private:
126 wxString m_text;
127};
128
129
130wxDataViewItem LIB_TREE_MODEL_ADAPTER::ToItem( const LIB_TREE_NODE* aNode )
131{
132 return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
133}
134
135
137{
138 return static_cast<LIB_TREE_NODE*>( aItem.GetID() );
139}
140
141
143 APP_SETTINGS_BASE::LIB_TREE& aSettingsStruct ) :
144 m_parent( aParent ),
145 m_cfg( aSettingsStruct ),
146 m_widget( nullptr ),
147 m_lazyLoadHandler( nullptr ),
149 m_show_units( true ),
150 m_preselect_unit( 0 ),
151 m_freeze( 0 ),
152 m_filter( nullptr )
153{
154 // Default column widths. Do not translate these names.
155 m_colWidths[ _HKI( "Item" ) ] = 300;
156 m_colWidths[ _HKI( "Description" ) ] = 600;
157
158 m_availableColumns = { _HKI( "Item" ), _HKI( "Description" ) };
159
161}
162
163
166
167
169{
170 return m_parent->GetToolDispatcher();
171}
172
173
175{
176 for( const std::pair<const wxString, int>& pair : m_cfg.column_widths )
177 m_colWidths[pair.first] = pair.second;
178
179 m_shownColumns = m_cfg.columns;
180
181 if( m_shownColumns.empty() )
182 m_shownColumns = { _HKI( "Item" ), _HKI( "Description" ) };
183
184 if( m_shownColumns[0] != _HKI( "Item" ) )
185 m_shownColumns.insert( m_shownColumns.begin(), _HKI( "Item" ) );
186}
187
188
189std::vector<wxString> LIB_TREE_MODEL_ADAPTER::GetOpenLibs() const
190{
191 std::vector<wxString> openLibs;
192 wxDataViewItem rootItem( nullptr );
193 wxDataViewItemArray children;
194
195 GetChildren( rootItem, children );
196
197 for( const wxDataViewItem& child : children )
198 {
199 if( m_widget->IsExpanded( child ) )
200 openLibs.emplace_back( ToNode( child )->m_LibId.GetLibNickname().wx_str() );
201 }
202
203 return openLibs;
204}
205
206
207void LIB_TREE_MODEL_ADAPTER::OpenLibs( const std::vector<wxString>& aLibs )
208{
209 wxWindowUpdateLocker updateLock( m_widget );
210
211 for( const wxString& lib : aLibs )
212 {
213 wxDataViewItem item = FindItem( LIB_ID( lib, wxEmptyString ) );
214
215 if( item.IsOk() )
216 m_widget->Expand( item );
217 }
218}
219
220
222{
223 if( m_widget )
224 {
225 m_cfg.columns = GetShownColumns();
226 m_cfg.column_widths.clear();
227
228 for( const std::pair<const wxString, wxDataViewColumn*>& pair : m_colNameMap )
229 {
230 if( pair.second )
231 m_cfg.column_widths[pair.first] = pair.second->GetWidth();
232 }
233
234 m_cfg.open_libs = GetOpenLibs();
235 }
236}
237
238
240{
241 m_show_units = aShow;
242}
243
244
245void LIB_TREE_MODEL_ADAPTER::SetPreselectNode( const LIB_ID& aLibId, int aUnit )
246{
247 m_preselect_lib_id = aLibId;
248 m_preselect_unit = aUnit;
249}
250
251
252LIB_TREE_NODE_LIBRARY& LIB_TREE_MODEL_ADAPTER::DoAddLibraryNode( const wxString& aNodeName, const wxString& aDesc,
253 bool pinned )
254{
255 LIB_TREE_NODE_LIBRARY& lib_node = m_tree.AddLib( aNodeName, aDesc );
256
257 lib_node.m_Pinned = pinned;
258
259 return lib_node;
260}
261
262
263LIB_TREE_NODE_LIBRARY& LIB_TREE_MODEL_ADAPTER::DoAddLibrary( const wxString& aNodeName, const wxString& aDesc,
264 const std::vector<LIB_TREE_ITEM*>& aItemList,
265 bool pinned, bool presorted )
266{
267 LIB_TREE_NODE_LIBRARY& lib_node = DoAddLibraryNode( aNodeName, aDesc, pinned );
268
269 for( LIB_TREE_ITEM* item: aItemList )
270 {
271 if( item )
272 lib_node.AddItem( item );
273 }
274
275 lib_node.AssignIntrinsicRanks( m_shownColumns, presorted );
276
277 return lib_node;
278}
279
280
281void LIB_TREE_MODEL_ADAPTER::RemoveGroup( bool aRecentGroup, bool aPlacedGroup )
282{
283 m_tree.RemoveGroup( aRecentGroup, aPlacedGroup );
284}
285
286
287void LIB_TREE_MODEL_ADAPTER::UpdateSearchString( const wxString& aSearch, bool aState )
288{
289 const LIB_TREE_NODE* firstMatch = nullptr;
290
291 {
292 wxWindowUpdateLocker updateLock( m_widget );
293
294 // Even with the updateLock, wxWidgets sometimes ties its knickers in a knot trying to
295 // run a wxdataview_selection_changed_callback() on a row that has been deleted.
296 // https://bugs.launchpad.net/kicad/+bug/1756255
297 m_widget->UnselectAll();
298
299 // This collapse is required before the call to "Freeze()" below. Once Freeze()
300 // is called, GetParent() will return nullptr. While this works for some calls, it
301 // segfaults when we have any expanded elements b/c the sub units in the tree don't
302 // have explicit references that are maintained over a search
303 // The tree will be expanded again below when we get our matches
304 //
305 // Also note that this cannot happen when we have deleted a symbol as GTK will also
306 // iterate over the tree in this case and find a symbol that has an invalid link
307 // and crash https://gitlab.com/kicad/code/kicad/-/issues/6910
308 if( !aState && !aSearch.IsNull() && m_tree.m_Children.size() )
309 {
310 for( std::unique_ptr<LIB_TREE_NODE>& child: m_tree.m_Children )
311 m_widget->Collapse( wxDataViewItem( &*child ) );
312 }
313
314 // DO NOT REMOVE THE FREEZE/THAW. This freeze/thaw is a flag for this model adapter
315 // that tells it when it shouldn't trust any of the data in the model. When set, it will
316 // not return invalid data to the UI, since this invalid data can cause crashes.
317 // This is different than the update locker, which locks the UI aspects only.
318 Freeze();
319 BeforeReset();
320
321 // Don't cause KiCad to hang if someone accidentally pastes the PCB or schematic into
322 // the search box.
323 constexpr int MAX_TERMS = 100;
324
325 wxStringTokenizer tokenizer( aSearch, " \t\r\n", wxTOKEN_STRTOK );
326 std::vector<std::unique_ptr<EDA_COMBINED_MATCHER>> termMatchers;
327
328 while( tokenizer.HasMoreTokens() && termMatchers.size() < MAX_TERMS )
329 {
330 wxString term = tokenizer.GetNextToken().Lower();
331 termMatchers.emplace_back( std::make_unique<EDA_COMBINED_MATCHER>( term, CTX_LIBITEM ) );
332 }
333
334 m_tree.UpdateScore( termMatchers, m_filter );
335
336 m_tree.SortNodes( m_sort_mode == BEST_MATCH );
337 AfterReset();
338 Thaw();
339
340 // Move showResults inside the update locker to ensure all tree manipulation
341 // (including ExpandAncestors) happens while the window is frozen. This prevents
342 // GTK from rendering stale cached cell data during partial updates.
343 // https://gitlab.com/kicad/code/kicad/-/issues/18407
344 firstMatch = showResults();
345 }
346
347#ifdef __WXGTK__
348 // Ensure the control is repainted with the updated data. Without an explicit
349 // refresh the Gtk port can display stale rows until the user interacts with
350 // them, leading to mismatched tree contents.
351 m_widget->Refresh();
352 m_widget->Update();
353
354 // This causes crashes on Linux. Until someone can figure out why, please leave this commented
355 // out.
356 // wxSafeYield();
357#endif
358
359 if( firstMatch )
360 {
361 wxDataViewItem item = ToItem( firstMatch );
362 m_widget->Select( item );
363
364 // Make sure the *parent* item is visible. The selected item is the first (shown) child
365 // of the parent. So it's always right below the parent, and this way the user can also
366 // see what library the selected part belongs to, without having a case where the selection
367 // is off the screen (unless the window is a single row high, which is unlikely).
368 //
369 // This also happens to circumvent https://bugs.launchpad.net/kicad/+bug/1804400 which
370 // appears to be a GTK+3 bug.
371 {
372 wxDataViewItem parent = GetParent( item );
373
374 if( parent.IsOk() )
375 m_widget->EnsureVisible( parent );
376 }
377
378 m_widget->EnsureVisible( item );
379 }
380}
381
382
383void LIB_TREE_MODEL_ADAPTER::AttachTo( wxDataViewCtrl* aDataViewCtrl )
384{
385 m_widget = aDataViewCtrl;
386 aDataViewCtrl->SetIndent( kDataViewIndent );
387 aDataViewCtrl->AssociateModel( this );
389}
390
391
393{
394 m_widget->ClearColumns();
395
396 m_columns.clear();
397 m_colIdxMap.clear();
398 m_colNameMap.clear();
399
400 // The Item column is always shown
401 doAddColumn( wxT( "Item" ) );
403}
404
405
407{
408 for( const wxString& colName : m_shownColumns )
409 {
410 if( !m_colNameMap.count( colName ) )
411 doAddColumn( colName, colName == wxT( "Description" ) );
412 }
413}
414
415
417{
418 Freeze();
419 BeforeReset();
420
421 m_tree.SortNodes( m_sort_mode == BEST_MATCH );
422
423 AfterReset();
424 Thaw();
425}
426
427
429{
430 m_parent->Prj().PinLibrary( aTreeNode->m_LibId.GetLibNickname(), getLibType() );
431 aTreeNode->m_Pinned = true;
432
433 resortTree();
434 m_widget->EnsureVisible( ToItem( aTreeNode ) );
435}
436
437
439{
440 m_parent->Prj().UnpinLibrary( aTreeNode->m_LibId.GetLibNickname(), getLibType() );
441 aTreeNode->m_Pinned = false;
442
443 resortTree();
444 // Keep focus at top when unpinning
445}
446
447
449{
451
452 for( const std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
453 {
454 if( lib->m_IsRecentlyUsedGroup )
455 lib->m_Name = wxT( "-- " ) + _( "Recently Used" ) + wxT( " --" );
456 else if( lib->m_IsAlreadyPlacedGroup )
457 lib->m_Name = wxT( "-- " ) + _( "Already Placed" ) + wxT( " --" );
458 }
459}
460
461
462wxDataViewColumn* LIB_TREE_MODEL_ADAPTER::doAddColumn( const wxString& aHeader, bool aTranslate )
463{
464 wxString translatedHeader = aTranslate ? wxGetTranslation( aHeader ) : aHeader;
465
466 // The extent of the text doesn't take into account the space on either side
467 // in the header, so artificially pad it
468 wxSize headerMinWidth = KIUI::GetTextSize( translatedHeader + wxT( "MMM" ), m_widget );
469
470 if( !m_colWidths.count( aHeader ) || m_colWidths[aHeader] < headerMinWidth.x )
471 m_colWidths[aHeader] = headerMinWidth.x;
472
473 int index = (int) m_columns.size();
474
475 wxDataViewColumn* col = new wxDataViewColumn( translatedHeader, new LIB_TREE_RENDERER(), index,
476 m_colWidths[aHeader], wxALIGN_NOT,
477 wxDATAVIEW_CELL_INERT | (int) wxDATAVIEW_COL_RESIZABLE );
478 m_widget->AppendColumn( col );
479
480 col->SetMinWidth( headerMinWidth.x );
481
482 m_columns.emplace_back( col );
483 m_colNameMap[aHeader] = col;
484 m_colIdxMap[m_columns.size() - 1] = aHeader;
485
486 return col;
487}
488
489
490void LIB_TREE_MODEL_ADAPTER::addColumnIfNecessary( const wxString& aHeader )
491{
492 if( m_colNameMap.count( aHeader ) )
493 return;
494
495 // Columns will be created later
496 m_colNameMap[aHeader] = nullptr;
497 m_availableColumns.emplace_back( aHeader );
498}
499
500
501void LIB_TREE_MODEL_ADAPTER::SetShownColumns( const std::vector<wxString>& aColumnNames )
502{
503 bool recreate = m_shownColumns != aColumnNames;
504
505 m_shownColumns = aColumnNames;
506
507 if( recreate && m_widget )
509
510 for( std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
511 lib->AssignIntrinsicRanks( m_shownColumns );
512}
513
514
515LIB_ID LIB_TREE_MODEL_ADAPTER::GetAliasFor( const wxDataViewItem& aSelection ) const
516{
517 const LIB_TREE_NODE* node = ToNode( aSelection );
518 return node ? node->m_LibId : LIB_ID();
519}
520
521
522int LIB_TREE_MODEL_ADAPTER::GetUnitFor( const wxDataViewItem& aSelection ) const
523{
524 const LIB_TREE_NODE* node = ToNode( aSelection );
525 return node ? node->m_Unit : 0;
526}
527
528
529LIB_TREE_NODE::TYPE LIB_TREE_MODEL_ADAPTER::GetTypeFor( const wxDataViewItem& aSelection ) const
530{
531 const LIB_TREE_NODE* node = ToNode( aSelection );
532 return node ? node->m_Type : LIB_TREE_NODE::TYPE::INVALID;
533}
534
535
536LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::GetTreeNodeFor( const wxDataViewItem& aSelection ) const
537{
538 return ToNode( aSelection );
539}
540
541
543{
544 int n = 0;
545
546 for( const std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
547 n += lib->m_Children.size();
548
549 return n;
550}
551
552
553wxDataViewItem LIB_TREE_MODEL_ADAPTER::FindItem( const LIB_ID& aLibId )
554{
555 for( std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
556 {
557 if( lib->m_Name != aLibId.GetLibNickname().wx_str() )
558 continue;
559
560 // if part name is not specified, return the library node
561 if( aLibId.GetLibItemName() == "" )
562 return ToItem( lib.get() );
563
564 for( std::unique_ptr<LIB_TREE_NODE>& alias: lib->m_Children )
565 {
566 if( alias->m_Name == aLibId.GetLibItemName().wx_str() )
567 return ToItem( alias.get() );
568 }
569
570 break; // could not find the part in the requested library
571 }
572
573 return wxDataViewItem();
574}
575
576
581
582
583unsigned int LIB_TREE_MODEL_ADAPTER::GetChildren( const wxDataViewItem& aItem,
584 wxDataViewItemArray& aChildren ) const
585{
586 const LIB_TREE_NODE* node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
587 unsigned int count = 0;
588
589 if( node->m_Type == LIB_TREE_NODE::TYPE::ROOT
590 || node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY
591 || ( m_show_units && node->m_Type == LIB_TREE_NODE::TYPE::ITEM ) )
592 {
593 for( std::unique_ptr<LIB_TREE_NODE> const& child: node->m_Children )
594 {
595 if( child->m_Score > 0 )
596 {
597 aChildren.Add( ToItem( &*child ) );
598 ++count;
599 }
600 }
601 }
602
603 return count;
604}
605
606
608{
609 wxDataViewColumn* col = nullptr;
610 size_t idx = 0;
611 int totalWidth = 0;
612 wxString header;
613
614 for( ; idx < m_columns.size() - 1; idx++ )
615 {
616 wxASSERT( m_colIdxMap.count( idx ) );
617
618 col = m_columns[idx];
619 header = m_colIdxMap[idx];
620
621 wxASSERT( m_colWidths.count( header ) );
622
623 col->SetWidth( m_colWidths[header] );
624 totalWidth += col->GetWidth();
625 }
626
627 int remainingWidth = m_widget->GetSize().x - totalWidth;
628 header = m_columns[idx]->GetTitle();
629
630 m_columns[idx]->SetWidth( std::max( m_colWidths[header], remainingWidth ) );
631}
632
633
635{
636 // Yes, this is an enormous hack. But it works on all platforms, it doesn't suffer
637 // the On^2 sorting issues that ItemChanged() does on OSX, and it doesn't lose the
638 // user's scroll position (which re-attaching or deleting/re-inserting columns does).
639 static int walk = 1;
640
641 std::vector<int> widths;
642
643 for( const wxDataViewColumn* col : m_columns )
644 widths.emplace_back( col->GetWidth() );
645
646 wxASSERT( widths.size() );
647
648 // Only use the widths read back if they are non-zero.
649 // GTK returns the displayed width of the column, which is not calculated immediately
650 if( widths[0] > 0 )
651 {
652 size_t i = 0;
653
654 for( const auto& [ colName, colPtr ] : m_colNameMap )
655 {
656 if( i < widths.size() )
657 m_colWidths[ colName ] = widths[i++];
658 }
659 }
660
661 auto colIt = m_colWidths.begin();
662
663 colIt->second += walk;
664 colIt++;
665
666 if( colIt != m_colWidths.end() )
667 colIt->second -= walk;
668
669 for( const auto& [ colName, colPtr ] : m_colNameMap )
670 {
671 if( colPtr == m_columns[0] || colPtr == nullptr )
672 continue;
673
674 wxASSERT( m_colWidths.count( colName ) );
675 colPtr->SetWidth( m_colWidths[ colName ] );
676 }
677
678 walk = -walk;
679}
680
681
682bool LIB_TREE_MODEL_ADAPTER::HasContainerColumns( const wxDataViewItem& aItem ) const
683{
684 return IsContainer( aItem );
685}
686
687
688bool LIB_TREE_MODEL_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
689{
690 LIB_TREE_NODE* node = ToNode( aItem );
691 return node ? node->m_Children.size() : true;
692}
693
694
695wxDataViewItem LIB_TREE_MODEL_ADAPTER::GetParent( const wxDataViewItem& aItem ) const
696{
697 if( m_freeze )
698 return ToItem( nullptr );
699
700 LIB_TREE_NODE* node = ToNode( aItem );
701 LIB_TREE_NODE* parent = node ? node->m_Parent : nullptr;
702
703 if( node->m_Type == LIB_TREE_NODE::TYPE::INVALID )
704 return ToItem( nullptr );
705
706 // wxDataViewModel has no root node, but rather top-level elements have
707 // an invalid (null) parent.
708 if( !node || !parent || parent->m_Type == LIB_TREE_NODE::TYPE::ROOT )
709 return ToItem( nullptr );
710 else
711 return ToItem( parent );
712}
713
714
715void LIB_TREE_MODEL_ADAPTER::GetValue( wxVariant& aVariant, const wxDataViewItem& aItem,
716 unsigned int aCol ) const
717{
718 if( IsFrozen() )
719 {
720 aVariant = wxEmptyString;
721 return;
722 }
723
724 LIB_TREE_NODE* node = ToNode( aItem );
725 wxCHECK( node, /* void */ );
726 wxString valueStr;
727
728 switch( aCol )
729 {
730 case NAME_COL:
731 if( node->m_Pinned )
732 valueStr = GetPinningSymbol() + UnescapeString( node->m_Name );
733 else
734 valueStr = UnescapeString( node->m_Name );
735
736 break;
737
738 default:
739 if( m_colIdxMap.count( aCol ) )
740 {
741 const wxString& key = m_colIdxMap.at( aCol );
742
743 if( key == wxT( "Description" ) )
744 valueStr = UnescapeString( node->m_Desc );
745 else if( node->m_Fields.count( key ) )
746 valueStr = UnescapeString( node->m_Fields.at( key ) );
747 else
748 valueStr = wxEmptyString;
749 }
750
751 break;
752 }
753
754 valueStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
755
756 aVariant = valueStr;
757}
758
759
760bool LIB_TREE_MODEL_ADAPTER::GetAttr( const wxDataViewItem& aItem, unsigned int aCol,
761 wxDataViewItemAttr& aAttr ) const
762{
763 if( IsFrozen() )
764 return false;
765
766 LIB_TREE_NODE* node = ToNode( aItem );
767 wxCHECK( node, false );
768
769 if( node->m_Type == LIB_TREE_NODE::TYPE::ITEM )
770 {
771 if( !node->m_IsRoot && aCol == 0 )
772 {
773 // Names of non-root aliases are italicized
774 aAttr.SetItalic( true );
775 return true;
776 }
777 }
778
779 return false;
780}
781
782
783void recursiveDescent( LIB_TREE_NODE& aNode, const std::function<int( const LIB_TREE_NODE* )>& f )
784{
785 for( std::unique_ptr<LIB_TREE_NODE>& node: aNode.m_Children )
786 {
787 int r = f( node.get() );
788
789 if( r == 0 )
790 break;
791 else if( r == -1 )
792 continue;
793
794 recursiveDescent( *node, f );
795 }
796}
797
798
800{
801 const LIB_TREE_NODE* firstMatch = nullptr;
802
803 // Expand parents of leaf nodes with some level of matching
805 [&]( const LIB_TREE_NODE* n )
806 {
807 if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM && n->m_Score > 1 )
808 {
809 if( !firstMatch )
810 firstMatch = n;
811 else if( n->m_Score > firstMatch->m_Score )
812 firstMatch = n;
813
814 m_widget->ExpandAncestors( ToItem( n ) );
815 }
816
817 return 1; // keep going to expand ancestors of all found items
818 } );
819
820 // If no matches, find and show the preselect node
821 if( !firstMatch && m_preselect_lib_id.IsValid() )
822 {
824 [&]( const LIB_TREE_NODE* n )
825 {
826 // Don't match the recent and already placed libraries
827 if( n->m_Name.StartsWith( "-- " ) )
828 return -1; // Skip this node and its children
829
830 if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM
831 && ( n->m_Children.empty() || !m_preselect_unit )
832 && m_preselect_lib_id == n->m_LibId )
833 {
834 firstMatch = n;
835 m_widget->ExpandAncestors( ToItem( n ) );
836 return 0;
837 }
838 else if( n->m_Type == LIB_TREE_NODE::TYPE::UNIT
841 {
842 firstMatch = n;
843 m_widget->ExpandAncestors( ToItem( n ) );
844 return 0;
845 }
846
847 return 1;
848 } );
849 }
850
851 // If still no matches expand a single library if there is only one
852 if( !firstMatch )
853 {
854 int libraries = 0;
855
856 for( const std::unique_ptr<LIB_TREE_NODE>& child : m_tree.m_Children )
857 {
858 if( !child->m_Name.StartsWith( "-- " ) )
859 libraries++;
860 }
861
862 if( libraries != 1 )
863 return nullptr;
864
866 [&]( const LIB_TREE_NODE* n )
867 {
868 if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM )
869 {
870 firstMatch = n;
871 m_widget->ExpandAncestors( ToItem( n ) );
872 return 0;
873 }
874
875 return 1;
876 } );
877 }
878
879 return firstMatch;
880}
881
882
int index
The base frame for deriving all KiCad main window classes.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
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).
LIB_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, const wxString &aPinnedKey, APP_SETTINGS_BASE::LIB_TREE &aSettingsStruct)
Create the adapter.
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.
APP_SETTINGS_BASE::LIB_TREE & m_cfg
bool GetAttr(const wxDataViewItem &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
Get any formatting for an item.
std::map< wxString, int > m_colWidths
const LIB_TREE_NODE * showResults()
Find and expand successful search results.
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.
TOOL_DISPATCHER * GetToolDispatcher() const
void AttachTo(wxDataViewCtrl *aDataViewCtrl)
Attach to a wxDataViewCtrl and initialize it.
void SetShownColumns(const std::vector< wxString > &aColumnNames)
Sets which columns are shown in the widget.
std::function< void()> m_lazyLoadHandler
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.
std::vector< wxString > m_shownColumns
void RemoveGroup(bool aRecentlyUsedGroup, bool aAlreadyPlacedGroup)
Remove one of the system groups from the library.
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
LIB_TREE_NODE_LIBRARY & DoAddLibrary(const wxString &aNodeName, const wxString &aDesc, const std::vector< LIB_TREE_ITEM * > &aItemList, bool pinned, bool presorted)
Add the given list of symbols by alias.
wxDataViewItem FindItem(const LIB_ID &aLibId)
Returns tree item corresponding to part.
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.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
std::map< wxString, wxString > m_Fields
List of weighted search terms.
PTR_VECTOR m_Children
LIB_TREE_NODE * m_Parent
void AssignIntrinsicRanks(const std::vector< wxString > &aShownColumns, 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
wxString wx_str() const
Definition utf8.cpp:45
static void recursiveDescent(wxSizer *aSizer, std::map< int, wxString > &aLabels)
#define _(s)
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:49
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:78
#define _HKI(x)
Definition page_info.cpp:44
wxString UnescapeString(const wxString &aSource)
Functions to provide common constants and other functions to assist in making a consistent UI.