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.

817 lines
24 KiB

2 years ago
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
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::UpdateSearchString( const wxString& aSearch, bool aState )
  191. {
  192. {
  193. wxWindowUpdateLocker updateLock( m_widget );
  194. // Even with the updateLock, wxWidgets sometimes ties its knickers in a knot trying to
  195. // run a wxdataview_selection_changed_callback() on a row that has been deleted.
  196. // https://bugs.launchpad.net/kicad/+bug/1756255
  197. m_widget->UnselectAll();
  198. // This collapse is required before the call to "Freeze()" below. Once Freeze()
  199. // is called, GetParent() will return nullptr. While this works for some calls, it
  200. // segfaults when we have any expanded elements b/c the sub units in the tree don't
  201. // have explicit references that are maintained over a search
  202. // The tree will be expanded again below when we get our matches
  203. //
  204. // Also note that this cannot happen when we have deleted a symbol as GTK will also
  205. // iterate over the tree in this case and find a symbol that has an invalid link
  206. // and crash https://gitlab.com/kicad/code/kicad/-/issues/6910
  207. if( !aState && !aSearch.IsNull() && m_tree.m_Children.size() )
  208. {
  209. for( std::unique_ptr<LIB_TREE_NODE>& child: m_tree.m_Children )
  210. m_widget->Collapse( wxDataViewItem( &*child ) );
  211. }
  212. // DO NOT REMOVE THE FREEZE/THAW. This freeze/thaw is a flag for this model adapter
  213. // that tells it when it shouldn't trust any of the data in the model. When set, it will
  214. // not return invalid data to the UI, since this invalid data can cause crashes.
  215. // This is different than the update locker, which locks the UI aspects only.
  216. Freeze();
  217. BeforeReset();
  218. m_tree.ResetScore();
  219. wxStringTokenizer tokenizer( aSearch );
  220. bool firstTerm = true;
  221. while( tokenizer.HasMoreTokens() )
  222. {
  223. // First search for the full token, in case it appears in a search string
  224. wxString term = tokenizer.GetNextToken().Lower();
  225. EDA_COMBINED_MATCHER termMatcher( term, CTX_LIBITEM );
  226. m_tree.UpdateScore( &termMatcher, wxEmptyString, firstTerm ? m_filter : nullptr );
  227. firstTerm = false;
  228. if( term.Contains( ":" ) )
  229. {
  230. // Next search for the library:item_name
  231. wxString lib = term.BeforeFirst( ':' );
  232. wxString itemName = term.AfterFirst( ':' );
  233. EDA_COMBINED_MATCHER itemNameMatcher( itemName, CTX_LIBITEM );
  234. m_tree.UpdateScore( &itemNameMatcher, lib, nullptr );
  235. }
  236. }
  237. if( firstTerm )
  238. {
  239. // No terms processed; just run the filter
  240. m_tree.UpdateScore( nullptr, wxEmptyString, m_filter );
  241. }
  242. m_tree.SortNodes( m_sort_mode == BEST_MATCH );
  243. AfterReset();
  244. Thaw();
  245. }
  246. const LIB_TREE_NODE* firstMatch = ShowResults();
  247. if( firstMatch )
  248. {
  249. wxDataViewItem item = ToItem( firstMatch );
  250. m_widget->Select( item );
  251. // Make sure the *parent* item is visible. The selected item is the first (shown) child
  252. // of the parent. So it's always right below the parent, and this way the user can also
  253. // see what library the selected part belongs to, without having a case where the selection
  254. // is off the screen (unless the window is a single row high, which is unlikely).
  255. //
  256. // This also happens to circumvent https://bugs.launchpad.net/kicad/+bug/1804400 which
  257. // appears to be a GTK+3 bug.
  258. {
  259. wxDataViewItem parent = GetParent( item );
  260. if( parent.IsOk() )
  261. m_widget->EnsureVisible( parent );
  262. }
  263. m_widget->EnsureVisible( item );
  264. }
  265. }
  266. void LIB_TREE_MODEL_ADAPTER::AttachTo( wxDataViewCtrl* aDataViewCtrl )
  267. {
  268. m_widget = aDataViewCtrl;
  269. aDataViewCtrl->SetIndent( kDataViewIndent );
  270. aDataViewCtrl->AssociateModel( this );
  271. recreateColumns();
  272. }
  273. void LIB_TREE_MODEL_ADAPTER::recreateColumns()
  274. {
  275. m_widget->ClearColumns();
  276. m_columns.clear();
  277. m_colIdxMap.clear();
  278. m_colNameMap.clear();
  279. // The Item column is always shown
  280. doAddColumn( wxT( "Item" ) );
  281. for( const wxString& colName : m_shownColumns )
  282. {
  283. if( !m_colNameMap.count( colName ) )
  284. doAddColumn( colName, colName == wxT( "Description" ) );
  285. }
  286. }
  287. void LIB_TREE_MODEL_ADAPTER::resortTree()
  288. {
  289. Freeze();
  290. BeforeReset();
  291. m_tree.SortNodes( m_sort_mode == BEST_MATCH );
  292. AfterReset();
  293. Thaw();
  294. }
  295. void LIB_TREE_MODEL_ADAPTER::PinLibrary( LIB_TREE_NODE* aTreeNode )
  296. {
  297. m_parent->Prj().PinLibrary( aTreeNode->m_LibId.GetLibNickname(), isSymbolModel() );
  298. aTreeNode->m_Pinned = true;
  299. resortTree();
  300. m_widget->EnsureVisible( ToItem( aTreeNode ) );
  301. }
  302. void LIB_TREE_MODEL_ADAPTER::UnpinLibrary( LIB_TREE_NODE* aTreeNode )
  303. {
  304. m_parent->Prj().UnpinLibrary( aTreeNode->m_LibId.GetLibNickname(), isSymbolModel() );
  305. aTreeNode->m_Pinned = false;
  306. resortTree();
  307. // Keep focus at top when unpinning
  308. }
  309. wxDataViewColumn* LIB_TREE_MODEL_ADAPTER::doAddColumn( const wxString& aHeader, bool aTranslate )
  310. {
  311. wxString translatedHeader = aTranslate ? wxGetTranslation( aHeader ) : aHeader;
  312. // The extent of the text doesn't take into account the space on either side
  313. // in the header, so artificially pad it
  314. wxSize headerMinWidth = KIUI::GetTextSize( translatedHeader + wxT( "MMM" ), m_widget );
  315. if( !m_colWidths.count( aHeader ) || m_colWidths[aHeader] < headerMinWidth.x )
  316. m_colWidths[aHeader] = headerMinWidth.x;
  317. int index = (int) m_columns.size();
  318. wxDataViewColumn* col = new wxDataViewColumn(
  319. translatedHeader, new LIB_TREE_RENDERER(), index, m_colWidths[aHeader], wxALIGN_NOT,
  320. wxDATAVIEW_CELL_INERT | wxDATAVIEW_COL_RESIZABLE | wxDATAVIEW_COL_SORTABLE );
  321. m_widget->AppendColumn( col );
  322. col->SetMinWidth( headerMinWidth.x );
  323. m_columns.emplace_back( col );
  324. m_colNameMap[aHeader] = col;
  325. m_colIdxMap[m_columns.size() - 1] = aHeader;
  326. return col;
  327. }
  328. void LIB_TREE_MODEL_ADAPTER::addColumnIfNecessary( const wxString& aHeader )
  329. {
  330. if( m_colNameMap.count( aHeader ) )
  331. return;
  332. // Columns will be created later
  333. m_colNameMap[aHeader] = nullptr;
  334. m_availableColumns.emplace_back( aHeader );
  335. }
  336. void LIB_TREE_MODEL_ADAPTER::SetShownColumns( const std::vector<wxString>& aColumnNames )
  337. {
  338. bool recreate = m_shownColumns != aColumnNames;
  339. m_shownColumns = aColumnNames;
  340. if( recreate && m_widget )
  341. recreateColumns();
  342. }
  343. LIB_ID LIB_TREE_MODEL_ADAPTER::GetAliasFor( const wxDataViewItem& aSelection ) const
  344. {
  345. const LIB_TREE_NODE* node = ToNode( aSelection );
  346. return node ? node->m_LibId : LIB_ID();
  347. }
  348. int LIB_TREE_MODEL_ADAPTER::GetUnitFor( const wxDataViewItem& aSelection ) const
  349. {
  350. const LIB_TREE_NODE* node = ToNode( aSelection );
  351. return node ? node->m_Unit : 0;
  352. }
  353. LIB_TREE_NODE::TYPE LIB_TREE_MODEL_ADAPTER::GetTypeFor( const wxDataViewItem& aSelection ) const
  354. {
  355. const LIB_TREE_NODE* node = ToNode( aSelection );
  356. return node ? node->m_Type : LIB_TREE_NODE::INVALID;
  357. }
  358. LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::GetTreeNodeFor( const wxDataViewItem& aSelection ) const
  359. {
  360. return ToNode( aSelection );
  361. }
  362. int LIB_TREE_MODEL_ADAPTER::GetItemCount() const
  363. {
  364. int n = 0;
  365. for( const std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
  366. n += lib->m_Children.size();
  367. return n;
  368. }
  369. wxDataViewItem LIB_TREE_MODEL_ADAPTER::FindItem( const LIB_ID& aLibId )
  370. {
  371. for( std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
  372. {
  373. if( lib->m_Name != aLibId.GetLibNickname() )
  374. continue;
  375. // if part name is not specified, return the library node
  376. if( aLibId.GetLibItemName() == "" )
  377. return ToItem( lib.get() );
  378. for( std::unique_ptr<LIB_TREE_NODE>& alias: lib->m_Children )
  379. {
  380. if( alias->m_Name == aLibId.GetLibItemName() )
  381. return ToItem( alias.get() );
  382. }
  383. break; // could not find the part in the requested library
  384. }
  385. return wxDataViewItem();
  386. }
  387. wxDataViewItem LIB_TREE_MODEL_ADAPTER::GetCurrentDataViewItem()
  388. {
  389. return FindItem( m_preselect_lib_id );
  390. }
  391. unsigned int LIB_TREE_MODEL_ADAPTER::GetChildren( const wxDataViewItem& aItem,
  392. wxDataViewItemArray& aChildren ) const
  393. {
  394. const LIB_TREE_NODE* node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
  395. unsigned int count = 0;
  396. if( node->m_Type == LIB_TREE_NODE::TYPE::ROOT
  397. || node->m_Type == LIB_TREE_NODE::LIBRARY
  398. || ( m_show_units && node->m_Type == LIB_TREE_NODE::TYPE::ITEM ) )
  399. {
  400. for( std::unique_ptr<LIB_TREE_NODE> const& child: node->m_Children )
  401. {
  402. if( child->m_Score > 0 )
  403. {
  404. aChildren.Add( ToItem( &*child ) );
  405. ++count;
  406. }
  407. }
  408. }
  409. return count;
  410. }
  411. void LIB_TREE_MODEL_ADAPTER::FinishTreeInitialization()
  412. {
  413. wxDataViewColumn* col = nullptr;
  414. size_t idx = 0;
  415. int totalWidth = 0;
  416. wxString header;
  417. for( ; idx < m_columns.size() - 1; idx++ )
  418. {
  419. wxASSERT( m_colIdxMap.count( idx ) );
  420. col = m_columns[idx];
  421. header = m_colIdxMap[idx];
  422. wxASSERT( m_colWidths.count( header ) );
  423. col->SetWidth( m_colWidths[header] );
  424. totalWidth += col->GetWidth();
  425. }
  426. int remainingWidth = m_widget->GetSize().x - totalWidth;
  427. header = m_columns[idx]->GetTitle();
  428. m_columns[idx]->SetWidth( std::max( m_colWidths[header], remainingWidth ) );
  429. }
  430. void LIB_TREE_MODEL_ADAPTER::RefreshTree()
  431. {
  432. // Yes, this is an enormous hack. But it works on all platforms, it doesn't suffer
  433. // the On^2 sorting issues that ItemChanged() does on OSX, and it doesn't lose the
  434. // user's scroll position (which re-attaching or deleting/re-inserting columns does).
  435. static int walk = 1;
  436. std::vector<int> widths;
  437. for( const wxDataViewColumn* col : m_columns )
  438. widths.emplace_back( col->GetWidth() );
  439. wxASSERT( widths.size() );
  440. // Only use the widths read back if they are non-zero.
  441. // GTK returns the displayed width of the column, which is not calculated immediately
  442. if( widths[0] > 0 )
  443. {
  444. size_t i = 0;
  445. for( const auto& [ colName, colPtr ] : m_colNameMap )
  446. m_colWidths[ colName ] = widths[i++];
  447. }
  448. auto colIt = m_colWidths.begin();
  449. colIt->second += walk;
  450. colIt++;
  451. if( colIt != m_colWidths.end() )
  452. colIt->second -= walk;
  453. for( const auto& [ colName, colPtr ] : m_colNameMap )
  454. {
  455. if( colPtr == m_columns[0] )
  456. continue;
  457. wxASSERT( m_colWidths.count( colName ) );
  458. colPtr->SetWidth( m_colWidths[ colName ] );
  459. }
  460. walk = -walk;
  461. }
  462. bool LIB_TREE_MODEL_ADAPTER::HasContainerColumns( const wxDataViewItem& aItem ) const
  463. {
  464. return IsContainer( aItem );
  465. }
  466. bool LIB_TREE_MODEL_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
  467. {
  468. LIB_TREE_NODE* node = ToNode( aItem );
  469. return node ? node->m_Children.size() : true;
  470. }
  471. wxDataViewItem LIB_TREE_MODEL_ADAPTER::GetParent( const wxDataViewItem& aItem ) const
  472. {
  473. if( m_freeze )
  474. return ToItem( nullptr );
  475. LIB_TREE_NODE* node = ToNode( aItem );
  476. LIB_TREE_NODE* parent = node ? node->m_Parent : nullptr;
  477. // wxDataViewModel has no root node, but rather top-level elements have
  478. // an invalid (null) parent.
  479. if( !node || !parent || parent->m_Type == LIB_TREE_NODE::TYPE::ROOT )
  480. return ToItem( nullptr );
  481. else
  482. return ToItem( parent );
  483. }
  484. void LIB_TREE_MODEL_ADAPTER::GetValue( wxVariant& aVariant,
  485. const wxDataViewItem& aItem,
  486. unsigned int aCol ) const
  487. {
  488. if( IsFrozen() )
  489. {
  490. aVariant = wxEmptyString;
  491. return;
  492. }
  493. LIB_TREE_NODE* node = ToNode( aItem );
  494. wxCHECK( node, /* void */ );
  495. wxString valueStr;
  496. switch( aCol )
  497. {
  498. case NAME_COL:
  499. if( node->m_Pinned )
  500. valueStr = GetPinningSymbol() + UnescapeString( node->m_Name );
  501. else
  502. valueStr = UnescapeString( node->m_Name );
  503. break;
  504. default:
  505. if( m_colIdxMap.count( aCol ) )
  506. {
  507. const wxString& key = m_colIdxMap.at( aCol );
  508. if( key == wxT( "Description" ) )
  509. valueStr = UnescapeString( node->m_Desc );
  510. else if( node->m_Fields.count( key ) )
  511. valueStr = UnescapeString( node->m_Fields.at( key ) );
  512. else
  513. valueStr = wxEmptyString;
  514. }
  515. break;
  516. }
  517. valueStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
  518. aVariant = valueStr;
  519. }
  520. bool LIB_TREE_MODEL_ADAPTER::GetAttr( const wxDataViewItem& aItem,
  521. unsigned int aCol,
  522. wxDataViewItemAttr& aAttr ) const
  523. {
  524. if( IsFrozen() )
  525. return false;
  526. LIB_TREE_NODE* node = ToNode( aItem );
  527. wxCHECK( node, false );
  528. if( node->m_Type == LIB_TREE_NODE::ITEM )
  529. {
  530. if( !node->m_IsRoot && aCol == 0 )
  531. {
  532. // Names of non-root aliases are italicized
  533. aAttr.SetItalic( true );
  534. return true;
  535. }
  536. }
  537. return false;
  538. }
  539. void recursiveDescent( LIB_TREE_NODE& aNode, const std::function<int( const LIB_TREE_NODE* )>& f )
  540. {
  541. for( std::unique_ptr<LIB_TREE_NODE>& node: aNode.m_Children )
  542. {
  543. int r = f( node.get() );
  544. if( r == 0 )
  545. break;
  546. else if( r == -1 )
  547. continue;
  548. recursiveDescent( *node, f );
  549. }
  550. }
  551. const LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::ShowResults()
  552. {
  553. const LIB_TREE_NODE* firstMatch = nullptr;
  554. // Expand parents of leaf nodes with some level of matching
  555. recursiveDescent( m_tree,
  556. [&]( const LIB_TREE_NODE* n )
  557. {
  558. if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM && n->m_Score > 1 )
  559. {
  560. if( !firstMatch )
  561. firstMatch = n;
  562. else if( n->m_Score > firstMatch->m_Score )
  563. firstMatch = n;
  564. m_widget->ExpandAncestors( ToItem( n ) );
  565. }
  566. return 1; // keep going to expand ancestors of all found items
  567. } );
  568. // If no matches, find and show the preselect node
  569. if( !firstMatch && m_preselect_lib_id.IsValid() )
  570. {
  571. recursiveDescent( m_tree,
  572. [&]( const LIB_TREE_NODE* n )
  573. {
  574. // Don't match the recent and already placed libraries
  575. if( n->m_Name.StartsWith( "-- " ) )
  576. return -1; // Skip this node and its children
  577. if( n->m_Type == LIB_TREE_NODE::ITEM
  578. && ( n->m_Children.empty() || !m_preselect_unit )
  579. && m_preselect_lib_id == n->m_LibId )
  580. {
  581. firstMatch = n;
  582. m_widget->ExpandAncestors( ToItem( n ) );
  583. return 0;
  584. }
  585. else if( n->m_Type == LIB_TREE_NODE::UNIT
  586. && ( m_preselect_unit && m_preselect_unit == n->m_Unit )
  587. && m_preselect_lib_id == n->m_Parent->m_LibId )
  588. {
  589. firstMatch = n;
  590. m_widget->ExpandAncestors( ToItem( n ) );
  591. return 0;
  592. }
  593. return 1;
  594. } );
  595. }
  596. // If still no matches expand a single library if there is only one
  597. if( !firstMatch )
  598. {
  599. int libraries = 0;
  600. for( const std::unique_ptr<LIB_TREE_NODE>& child : m_tree.m_Children )
  601. {
  602. if( !child->m_Name.StartsWith( "-- " ) )
  603. libraries++;
  604. }
  605. if( libraries != 1 )
  606. return nullptr;
  607. recursiveDescent( m_tree,
  608. [&]( const LIB_TREE_NODE* n )
  609. {
  610. if( n->m_Type == LIB_TREE_NODE::TYPE::ITEM )
  611. {
  612. firstMatch = n;
  613. m_widget->ExpandAncestors( ToItem( n ) );
  614. return 0;
  615. }
  616. return 1;
  617. } );
  618. }
  619. return firstMatch;
  620. }