You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

823 lines
24 KiB

2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Chris Pavlina <pavlina.chris@gmail.com>
  5. * Copyright (C) 2014 Henner Zeller <h.zeller@acm.org>
  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. #include <eda_base_frame.h>
  23. #include <eda_pattern_match.h>
  24. #include <kiface_base.h>
  25. #include <kiplatform/ui.h>
  26. #include <lib_tree_model_adapter.h>
  27. #include <project/project_file.h>
  28. #include <settings/app_settings.h>
  29. #include <widgets/ui_common.h>
  30. #include <wx/tokenzr.h>
  31. #include <wx/wupdlock.h>
  32. #include <wx/settings.h>
  33. #include <wx/dc.h>
  34. #include <string_utils.h>
  35. static const int kDataViewIndent = 20;
  36. class LIB_TREE_RENDERER : public wxDataViewCustomRenderer
  37. {
  38. public:
  39. LIB_TREE_RENDERER() :
  40. m_canvasItem( false )
  41. {}
  42. wxSize GetSize() const override
  43. {
  44. return wxSize( GetOwner()->GetWidth(), GetTextExtent( m_text ).y + 2 );
  45. }
  46. bool GetValue( wxVariant& aValue ) const override
  47. {
  48. aValue = m_text;
  49. return true;
  50. }
  51. bool SetValue( const wxVariant& aValue ) override
  52. {
  53. m_text = aValue.GetString();
  54. return true;
  55. }
  56. void SetAttr( const wxDataViewItemAttr& aAttr ) override
  57. {
  58. // Use strikethrough as a proxy for is-canvas-item
  59. m_canvasItem = aAttr.GetStrikethrough();
  60. wxDataViewItemAttr realAttr = aAttr;
  61. realAttr.SetStrikethrough( false );
  62. wxDataViewCustomRenderer::SetAttr( realAttr );
  63. }
  64. bool Render( wxRect aRect, wxDC *dc, int aState ) override
  65. {
  66. RenderBackground( dc, aRect );
  67. if( m_canvasItem )
  68. {
  69. wxPoint points[6];
  70. points[0] = aRect.GetTopLeft();
  71. points[1] = aRect.GetTopRight() + wxPoint( -4, 0 );
  72. points[2] = aRect.GetTopRight() + wxPoint( 0, aRect.GetHeight() / 2 );
  73. points[3] = aRect.GetBottomRight() + wxPoint( -4, 1 );
  74. points[4] = aRect.GetBottomLeft() + wxPoint( 0, 1 );
  75. points[5] = aRect.GetTopLeft();
  76. dc->SetPen( KIPLATFORM::UI::IsDarkTheme() ? *wxWHITE_PEN : *wxBLACK_PEN );
  77. dc->DrawLines( 6, points );
  78. }
  79. aRect.Deflate( 1 );
  80. #ifdef __WXOSX__
  81. // We should be able to pass wxDATAVIEW_CELL_SELECTED into RenderText() and have it do
  82. // the right thing -- but it picks wxSYS_COLOUR_HIGHLIGHTTEXT on MacOS (instead
  83. // of wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT).
  84. if( aState & wxDATAVIEW_CELL_SELECTED )
  85. dc->SetTextForeground( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
  86. RenderText( m_text, 0, aRect, dc, 0 );
  87. #else
  88. RenderText( m_text, 0, aRect, dc, aState );
  89. #endif
  90. return true;
  91. }
  92. private:
  93. bool m_canvasItem;
  94. wxString m_text;
  95. };
  96. wxDataViewItem LIB_TREE_MODEL_ADAPTER::ToItem( const LIB_TREE_NODE* aNode )
  97. {
  98. return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
  99. }
  100. LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::ToNode( wxDataViewItem aItem )
  101. {
  102. return static_cast<LIB_TREE_NODE*>( aItem.GetID() );
  103. }
  104. LIB_TREE_MODEL_ADAPTER::LIB_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent,
  105. const wxString& aPinnedKey ) :
  106. m_widget( nullptr ),
  107. m_parent( aParent ),
  108. m_sort_mode( BEST_MATCH ),
  109. m_show_units( true ),
  110. m_preselect_unit( 0 ),
  111. m_freeze( 0 ),
  112. m_filter( nullptr )
  113. {
  114. // Default column widths. Do not translate these names.
  115. m_colWidths[ _HKI( "Item" ) ] = 300;
  116. m_colWidths[ _HKI( "Description" ) ] = 600;
  117. m_availableColumns = { _HKI( "Item" ), _HKI( "Description" ) };
  118. APP_SETTINGS_BASE* cfg = Kiface().KifaceSettings();
  119. for( const std::pair<const wxString, int>& pair : cfg->m_LibTree.column_widths )
  120. m_colWidths[pair.first] = pair.second;
  121. m_shownColumns = cfg->m_LibTree.columns;
  122. if( m_shownColumns.empty() )
  123. m_shownColumns = { _HKI( "Item" ), _HKI( "Description" ) };
  124. if( m_shownColumns[0] != _HKI( "Item" ) )
  125. m_shownColumns.insert( m_shownColumns.begin(), _HKI( "Item" ) );
  126. }
  127. LIB_TREE_MODEL_ADAPTER::~LIB_TREE_MODEL_ADAPTER()
  128. {}
  129. std::vector<wxString> LIB_TREE_MODEL_ADAPTER::GetOpenLibs() const
  130. {
  131. std::vector<wxString> openLibs;
  132. wxDataViewItem rootItem( nullptr );
  133. wxDataViewItemArray children;
  134. GetChildren( rootItem, children );
  135. for( const wxDataViewItem& child : children )
  136. {
  137. if( m_widget->IsExpanded( child ) )
  138. openLibs.emplace_back( ToNode( child )->m_LibId.GetLibNickname().wx_str() );
  139. }
  140. return openLibs;
  141. }
  142. void LIB_TREE_MODEL_ADAPTER::OpenLibs( const std::vector<wxString>& aLibs )
  143. {
  144. wxWindowUpdateLocker updateLock( m_widget );
  145. for( const wxString& lib : aLibs )
  146. {
  147. wxDataViewItem item = FindItem( LIB_ID( lib, wxEmptyString ) );
  148. if( item.IsOk() )
  149. m_widget->Expand( item );
  150. }
  151. }
  152. void LIB_TREE_MODEL_ADAPTER::SaveSettings()
  153. {
  154. if( m_widget )
  155. {
  156. APP_SETTINGS_BASE* cfg = Kiface().KifaceSettings();
  157. cfg->m_LibTree.columns = GetShownColumns();
  158. cfg->m_LibTree.column_widths.clear();
  159. for( const std::pair<const wxString, wxDataViewColumn*>& pair : m_colNameMap )
  160. cfg->m_LibTree.column_widths[pair.first] = pair.second->GetWidth();
  161. cfg->m_LibTree.open_libs = GetOpenLibs();
  162. }
  163. }
  164. void LIB_TREE_MODEL_ADAPTER::ShowUnits( bool aShow )
  165. {
  166. m_show_units = aShow;
  167. }
  168. void LIB_TREE_MODEL_ADAPTER::SetPreselectNode( const LIB_ID& aLibId, int aUnit )
  169. {
  170. m_preselect_lib_id = aLibId;
  171. m_preselect_unit = aUnit;
  172. }
  173. LIB_TREE_NODE_LIBRARY& LIB_TREE_MODEL_ADAPTER::DoAddLibraryNode( const wxString& aNodeName,
  174. const wxString& aDesc,
  175. bool pinned )
  176. {
  177. LIB_TREE_NODE_LIBRARY& lib_node = m_tree.AddLib( aNodeName, aDesc );
  178. lib_node.m_Pinned = pinned;
  179. return lib_node;
  180. }
  181. void LIB_TREE_MODEL_ADAPTER::DoAddLibrary( const wxString& aNodeName, const wxString& aDesc,
  182. const std::vector<LIB_TREE_ITEM*>& aItemList,
  183. bool pinned, bool presorted )
  184. {
  185. LIB_TREE_NODE_LIBRARY& lib_node = DoAddLibraryNode( aNodeName, aDesc, pinned );
  186. for( LIB_TREE_ITEM* item: aItemList )
  187. lib_node.AddItem( item );
  188. lib_node.AssignIntrinsicRanks( presorted );
  189. }
  190. void LIB_TREE_MODEL_ADAPTER::DoRemoveLibrary( const wxString& aNodeName )
  191. {
  192. m_tree.RemoveLib( aNodeName );
  193. }
  194. void LIB_TREE_MODEL_ADAPTER::UpdateSearchString( const wxString& aSearch, bool aState )
  195. {
  196. {
  197. wxWindowUpdateLocker updateLock( m_widget );
  198. // Even with the updateLock, wxWidgets sometimes ties its knickers in a knot trying to
  199. // run a wxdataview_selection_changed_callback() on a row that has been deleted.
  200. // https://bugs.launchpad.net/kicad/+bug/1756255
  201. m_widget->UnselectAll();
  202. // This collapse is required before the call to "Freeze()" below. Once Freeze()
  203. // is called, GetParent() will return nullptr. While this works for some calls, it
  204. // segfaults when we have any expanded elements b/c the sub units in the tree don't
  205. // have explicit references that are maintained over a search
  206. // The tree will be expanded again below when we get our matches
  207. //
  208. // Also note that this cannot happen when we have deleted a symbol as GTK will also
  209. // iterate over the tree in this case and find a symbol that has an invalid link
  210. // and crash https://gitlab.com/kicad/code/kicad/-/issues/6910
  211. if( !aState && !aSearch.IsNull() && m_tree.m_Children.size() )
  212. {
  213. for( std::unique_ptr<LIB_TREE_NODE>& child: m_tree.m_Children )
  214. m_widget->Collapse( wxDataViewItem( &*child ) );
  215. }
  216. // DO NOT REMOVE THE FREEZE/THAW. This freeze/thaw is a flag for this model adapter
  217. // that tells it when it shouldn't trust any of the data in the model. When set, it will
  218. // not return invalid data to the UI, since this invalid data can cause crashes.
  219. // This is different than the update locker, which locks the UI aspects only.
  220. Freeze();
  221. BeforeReset();
  222. m_tree.ResetScore();
  223. wxStringTokenizer tokenizer( aSearch );
  224. bool firstTerm = true;
  225. while( tokenizer.HasMoreTokens() )
  226. {
  227. // First search for the full token, in case it appears in a search string
  228. wxString term = tokenizer.GetNextToken().Lower();
  229. EDA_COMBINED_MATCHER termMatcher( term, CTX_LIBITEM );
  230. m_tree.UpdateScore( &termMatcher, wxEmptyString, m_filter );
  231. firstTerm = false;
  232. if( term.Contains( ":" ) )
  233. {
  234. // Next search for the library:item_name
  235. wxString lib = term.BeforeFirst( ':' );
  236. wxString itemName = term.AfterFirst( ':' );
  237. EDA_COMBINED_MATCHER itemNameMatcher( itemName, CTX_LIBITEM );
  238. m_tree.UpdateScore( &itemNameMatcher, lib, m_filter );
  239. }
  240. }
  241. if( firstTerm )
  242. {
  243. // No terms processed; just run the filter
  244. m_tree.UpdateScore( nullptr, wxEmptyString, m_filter );
  245. }
  246. m_tree.SortNodes( m_sort_mode == BEST_MATCH );
  247. AfterReset();
  248. Thaw();
  249. }
  250. const LIB_TREE_NODE* firstMatch = ShowResults();
  251. if( firstMatch )
  252. {
  253. wxDataViewItem item = ToItem( firstMatch );
  254. m_widget->Select( item );
  255. // Make sure the *parent* item is visible. The selected item is the first (shown) child
  256. // of the parent. So it's always right below the parent, and this way the user can also
  257. // see what library the selected part belongs to, without having a case where the selection
  258. // is off the screen (unless the window is a single row high, which is unlikely).
  259. //
  260. // This also happens to circumvent https://bugs.launchpad.net/kicad/+bug/1804400 which
  261. // appears to be a GTK+3 bug.
  262. {
  263. wxDataViewItem parent = GetParent( item );
  264. if( parent.IsOk() )
  265. m_widget->EnsureVisible( parent );
  266. }
  267. m_widget->EnsureVisible( item );
  268. }
  269. }
  270. void LIB_TREE_MODEL_ADAPTER::AttachTo( wxDataViewCtrl* aDataViewCtrl )
  271. {
  272. m_widget = aDataViewCtrl;
  273. aDataViewCtrl->SetIndent( kDataViewIndent );
  274. aDataViewCtrl->AssociateModel( this );
  275. recreateColumns();
  276. }
  277. void LIB_TREE_MODEL_ADAPTER::recreateColumns()
  278. {
  279. m_widget->ClearColumns();
  280. m_columns.clear();
  281. m_colIdxMap.clear();
  282. m_colNameMap.clear();
  283. // The Item column is always shown
  284. doAddColumn( wxT( "Item" ) );
  285. for( const wxString& colName : m_shownColumns )
  286. {
  287. if( !m_colNameMap.count( colName ) )
  288. doAddColumn( colName, colName == wxT( "Description" ) );
  289. }
  290. }
  291. void LIB_TREE_MODEL_ADAPTER::resortTree()
  292. {
  293. Freeze();
  294. BeforeReset();
  295. m_tree.SortNodes( m_sort_mode == BEST_MATCH );
  296. AfterReset();
  297. Thaw();
  298. }
  299. void LIB_TREE_MODEL_ADAPTER::PinLibrary( LIB_TREE_NODE* aTreeNode )
  300. {
  301. m_parent->Prj().PinLibrary( aTreeNode->m_LibId.GetLibNickname(), getLibType() );
  302. aTreeNode->m_Pinned = true;
  303. resortTree();
  304. m_widget->EnsureVisible( ToItem( aTreeNode ) );
  305. }
  306. void LIB_TREE_MODEL_ADAPTER::UnpinLibrary( LIB_TREE_NODE* aTreeNode )
  307. {
  308. m_parent->Prj().UnpinLibrary( aTreeNode->m_LibId.GetLibNickname(), getLibType() );
  309. aTreeNode->m_Pinned = false;
  310. resortTree();
  311. // Keep focus at top when unpinning
  312. }
  313. wxDataViewColumn* LIB_TREE_MODEL_ADAPTER::doAddColumn( const wxString& aHeader, bool aTranslate )
  314. {
  315. wxString translatedHeader = aTranslate ? wxGetTranslation( aHeader ) : aHeader;
  316. // The extent of the text doesn't take into account the space on either side
  317. // in the header, so artificially pad it
  318. wxSize headerMinWidth = KIUI::GetTextSize( translatedHeader + wxT( "MMM" ), m_widget );
  319. if( !m_colWidths.count( aHeader ) || m_colWidths[aHeader] < headerMinWidth.x )
  320. m_colWidths[aHeader] = headerMinWidth.x;
  321. int index = (int) m_columns.size();
  322. wxDataViewColumn* col = new wxDataViewColumn(
  323. translatedHeader, new LIB_TREE_RENDERER(), index, m_colWidths[aHeader], wxALIGN_NOT,
  324. wxDATAVIEW_CELL_INERT | static_cast<int>( wxDATAVIEW_COL_RESIZABLE ) );
  325. m_widget->AppendColumn( col );
  326. col->SetMinWidth( headerMinWidth.x );
  327. m_columns.emplace_back( col );
  328. m_colNameMap[aHeader] = col;
  329. m_colIdxMap[m_columns.size() - 1] = aHeader;
  330. return col;
  331. }
  332. void LIB_TREE_MODEL_ADAPTER::addColumnIfNecessary( const wxString& aHeader )
  333. {
  334. if( m_colNameMap.count( aHeader ) )
  335. return;
  336. // Columns will be created later
  337. m_colNameMap[aHeader] = nullptr;
  338. m_availableColumns.emplace_back( aHeader );
  339. }
  340. void LIB_TREE_MODEL_ADAPTER::SetShownColumns( const std::vector<wxString>& aColumnNames )
  341. {
  342. bool recreate = m_shownColumns != aColumnNames;
  343. m_shownColumns = aColumnNames;
  344. if( recreate && m_widget )
  345. recreateColumns();
  346. }
  347. LIB_ID LIB_TREE_MODEL_ADAPTER::GetAliasFor( const wxDataViewItem& aSelection ) const
  348. {
  349. const LIB_TREE_NODE* node = ToNode( aSelection );
  350. return node ? node->m_LibId : LIB_ID();
  351. }
  352. int LIB_TREE_MODEL_ADAPTER::GetUnitFor( const wxDataViewItem& aSelection ) const
  353. {
  354. const LIB_TREE_NODE* node = ToNode( aSelection );
  355. return node ? node->m_Unit : 0;
  356. }
  357. LIB_TREE_NODE::TYPE LIB_TREE_MODEL_ADAPTER::GetTypeFor( const wxDataViewItem& aSelection ) const
  358. {
  359. const LIB_TREE_NODE* node = ToNode( aSelection );
  360. return node ? node->m_Type : LIB_TREE_NODE::TYPE::INVALID;
  361. }
  362. LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::GetTreeNodeFor( const wxDataViewItem& aSelection ) const
  363. {
  364. return ToNode( aSelection );
  365. }
  366. int LIB_TREE_MODEL_ADAPTER::GetItemCount() const
  367. {
  368. int n = 0;
  369. for( const std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
  370. n += lib->m_Children.size();
  371. return n;
  372. }
  373. wxDataViewItem LIB_TREE_MODEL_ADAPTER::FindItem( const LIB_ID& aLibId )
  374. {
  375. for( std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
  376. {
  377. if( lib->m_Name != aLibId.GetLibNickname().wx_str() )
  378. continue;
  379. // if part name is not specified, return the library node
  380. if( aLibId.GetLibItemName() == "" )
  381. return ToItem( lib.get() );
  382. for( std::unique_ptr<LIB_TREE_NODE>& alias: lib->m_Children )
  383. {
  384. if( alias->m_Name == aLibId.GetLibItemName().wx_str() )
  385. return ToItem( alias.get() );
  386. }
  387. break; // could not find the part in the requested library
  388. }
  389. return wxDataViewItem();
  390. }
  391. wxDataViewItem LIB_TREE_MODEL_ADAPTER::GetCurrentDataViewItem()
  392. {
  393. return FindItem( m_preselect_lib_id );
  394. }
  395. unsigned int LIB_TREE_MODEL_ADAPTER::GetChildren( const wxDataViewItem& aItem,
  396. wxDataViewItemArray& aChildren ) const
  397. {
  398. const LIB_TREE_NODE* node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
  399. unsigned int count = 0;
  400. if( node->m_Type == LIB_TREE_NODE::TYPE::ROOT
  401. || node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY
  402. || ( m_show_units && node->m_Type == LIB_TREE_NODE::TYPE::ITEM ) )
  403. {
  404. for( std::unique_ptr<LIB_TREE_NODE> const& child: node->m_Children )
  405. {
  406. if( child->m_Score > 0 )
  407. {
  408. aChildren.Add( ToItem( &*child ) );
  409. ++count;
  410. }
  411. }
  412. }
  413. return count;
  414. }
  415. void LIB_TREE_MODEL_ADAPTER::FinishTreeInitialization()
  416. {
  417. wxDataViewColumn* col = nullptr;
  418. size_t idx = 0;
  419. int totalWidth = 0;
  420. wxString header;
  421. for( ; idx < m_columns.size() - 1; idx++ )
  422. {
  423. wxASSERT( m_colIdxMap.count( idx ) );
  424. col = m_columns[idx];
  425. header = m_colIdxMap[idx];
  426. wxASSERT( m_colWidths.count( header ) );
  427. col->SetWidth( m_colWidths[header] );
  428. totalWidth += col->GetWidth();
  429. }
  430. int remainingWidth = m_widget->GetSize().x - totalWidth;
  431. header = m_columns[idx]->GetTitle();
  432. m_columns[idx]->SetWidth( std::max( m_colWidths[header], remainingWidth ) );
  433. }
  434. void LIB_TREE_MODEL_ADAPTER::RefreshTree()
  435. {
  436. // Yes, this is an enormous hack. But it works on all platforms, it doesn't suffer
  437. // the On^2 sorting issues that ItemChanged() does on OSX, and it doesn't lose the
  438. // user's scroll position (which re-attaching or deleting/re-inserting columns does).
  439. static int walk = 1;
  440. std::vector<int> widths;
  441. for( const wxDataViewColumn* col : m_columns )
  442. widths.emplace_back( col->GetWidth() );
  443. wxASSERT( widths.size() );
  444. // Only use the widths read back if they are non-zero.
  445. // GTK returns the displayed width of the column, which is not calculated immediately
  446. if( widths[0] > 0 )
  447. {
  448. size_t i = 0;
  449. for( const auto& [ colName, colPtr ] : m_colNameMap )
  450. m_colWidths[ colName ] = widths[i++];
  451. }
  452. auto colIt = m_colWidths.begin();
  453. colIt->second += walk;
  454. colIt++;
  455. if( colIt != m_colWidths.end() )
  456. colIt->second -= walk;
  457. for( const auto& [ colName, colPtr ] : m_colNameMap )
  458. {
  459. if( colPtr == m_columns[0] )
  460. continue;
  461. wxASSERT( m_colWidths.count( colName ) );
  462. colPtr->SetWidth( m_colWidths[ colName ] );
  463. }
  464. walk = -walk;
  465. }
  466. bool LIB_TREE_MODEL_ADAPTER::HasContainerColumns( const wxDataViewItem& aItem ) const
  467. {
  468. return IsContainer( aItem );
  469. }
  470. bool LIB_TREE_MODEL_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
  471. {
  472. LIB_TREE_NODE* node = ToNode( aItem );
  473. return node ? node->m_Children.size() : true;
  474. }
  475. wxDataViewItem LIB_TREE_MODEL_ADAPTER::GetParent( const wxDataViewItem& aItem ) const
  476. {
  477. if( m_freeze )
  478. return ToItem( nullptr );
  479. LIB_TREE_NODE* node = ToNode( aItem );
  480. LIB_TREE_NODE* parent = node ? node->m_Parent : nullptr;
  481. // wxDataViewModel has no root node, but rather top-level elements have
  482. // an invalid (null) parent.
  483. if( !node || !parent || parent->m_Type == LIB_TREE_NODE::TYPE::ROOT )
  484. return ToItem( nullptr );
  485. else
  486. return ToItem( parent );
  487. }
  488. void LIB_TREE_MODEL_ADAPTER::GetValue( wxVariant& aVariant,
  489. const wxDataViewItem& aItem,
  490. unsigned int aCol ) const
  491. {
  492. if( IsFrozen() )
  493. {
  494. aVariant = wxEmptyString;
  495. return;
  496. }
  497. LIB_TREE_NODE* node = ToNode( aItem );
  498. wxCHECK( node, /* void */ );
  499. wxString valueStr;
  500. switch( aCol )
  501. {
  502. case NAME_COL:
  503. if( node->m_Pinned )
  504. valueStr = GetPinningSymbol() + UnescapeString( node->m_Name );
  505. else
  506. valueStr = UnescapeString( node->m_Name );
  507. break;
  508. default:
  509. if( m_colIdxMap.count( aCol ) )
  510. {
  511. const wxString& key = m_colIdxMap.at( aCol );
  512. if( key == wxT( "Description" ) )
  513. valueStr = UnescapeString( node->m_Desc );
  514. else if( node->m_Fields.count( key ) )
  515. valueStr = UnescapeString( node->m_Fields.at( key ) );
  516. else
  517. valueStr = wxEmptyString;
  518. }
  519. break;
  520. }
  521. valueStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
  522. aVariant = valueStr;
  523. }
  524. bool LIB_TREE_MODEL_ADAPTER::GetAttr( const wxDataViewItem& aItem,
  525. unsigned int aCol,
  526. wxDataViewItemAttr& aAttr ) const
  527. {
  528. if( IsFrozen() )
  529. return false;
  530. LIB_TREE_NODE* node = ToNode( aItem );
  531. wxCHECK( node, false );
  532. if( node->m_Type == LIB_TREE_NODE::TYPE::ITEM )
  533. {
  534. if( !node->m_IsRoot && aCol == 0 )
  535. {
  536. // Names of non-root aliases are italicized
  537. aAttr.SetItalic( true );
  538. return true;
  539. }
  540. }
  541. return false;
  542. }
  543. void recursiveDescent( LIB_TREE_NODE& aNode, const std::function<int( const LIB_TREE_NODE* )>& f )
  544. {
  545. for( std::unique_ptr<LIB_TREE_NODE>& node: aNode.m_Children )
  546. {
  547. int r = f( node.get() );
  548. if( r == 0 )
  549. break;
  550. else if( r == -1 )
  551. continue;
  552. recursiveDescent( *node, f );
  553. }
  554. }
  555. const LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::ShowResults()
  556. {
  557. const LIB_TREE_NODE* firstMatch = nullptr;
  558. // Expand parents of leaf nodes with some level of matching
  559. recursiveDescent( m_tree,
  560. [&]( const LIB_TREE_NODE* n )
  561. {
  562. if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM && n->m_Score > 1 )
  563. {
  564. if( !firstMatch )
  565. firstMatch = n;
  566. else if( n->m_Score > firstMatch->m_Score )
  567. firstMatch = n;
  568. m_widget->ExpandAncestors( ToItem( n ) );
  569. }
  570. return 1; // keep going to expand ancestors of all found items
  571. } );
  572. // If no matches, find and show the preselect node
  573. if( !firstMatch && m_preselect_lib_id.IsValid() )
  574. {
  575. recursiveDescent( m_tree,
  576. [&]( const LIB_TREE_NODE* n )
  577. {
  578. // Don't match the recent and already placed libraries
  579. if( n->m_Name.StartsWith( "-- " ) )
  580. return -1; // Skip this node and its children
  581. if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM
  582. && ( n->m_Children.empty() || !m_preselect_unit )
  583. && m_preselect_lib_id == n->m_LibId )
  584. {
  585. firstMatch = n;
  586. m_widget->ExpandAncestors( ToItem( n ) );
  587. return 0;
  588. }
  589. else if( n->m_Type == LIB_TREE_NODE::TYPE::UNIT
  590. && ( m_preselect_unit && m_preselect_unit == n->m_Unit )
  591. && m_preselect_lib_id == n->m_Parent->m_LibId )
  592. {
  593. firstMatch = n;
  594. m_widget->ExpandAncestors( ToItem( n ) );
  595. return 0;
  596. }
  597. return 1;
  598. } );
  599. }
  600. // If still no matches expand a single library if there is only one
  601. if( !firstMatch )
  602. {
  603. int libraries = 0;
  604. for( const std::unique_ptr<LIB_TREE_NODE>& child : m_tree.m_Children )
  605. {
  606. if( !child->m_Name.StartsWith( "-- " ) )
  607. libraries++;
  608. }
  609. if( libraries != 1 )
  610. return nullptr;
  611. recursiveDescent( m_tree,
  612. [&]( const LIB_TREE_NODE* n )
  613. {
  614. if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM )
  615. {
  616. firstMatch = n;
  617. m_widget->ExpandAncestors( ToItem( n ) );
  618. return 0;
  619. }
  620. return 1;
  621. } );
  622. }
  623. return firstMatch;
  624. }