From 481a85141169819eb7b3b619e5c31b58a512328b Mon Sep 17 00:00:00 2001 From: Aleksei Nikiforov Date: Fri, 28 Feb 2020 18:02:51 +0300 Subject: [PATCH] Contents tree: add fallback to URL without fragment If URL has fragment, but no such URL with specified fragment is present in contents tree, try to fallback to URL without any fragment if such URL is present. --- khelpcenter/navigator.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/khelpcenter/navigator.cpp b/khelpcenter/navigator.cpp index 770ef3fc..3debfcb9 100644 --- a/khelpcenter/navigator.cpp +++ b/khelpcenter/navigator.cpp @@ -291,10 +291,12 @@ void Navigator::selectItem( const QUrl &url ) // Make sure that we match both the original URL as well as // its counterpart. QUrl alternativeURL = url; + QUrl contentsItemURL = url; if (url.hasFragment()) { alternativeURL.setQuery(QStringLiteral("anchor=")+url.fragment()); alternativeURL.setFragment(QString()); + contentsItemURL.setFragment(QString()); } // If the navigator already has the given URL selected, do nothing. @@ -319,22 +321,32 @@ void Navigator::selectItem( const QUrl &url ) } } + NavigatorItem *contentsItem = nullptr; QTreeWidgetItemIterator it( mContentsTree ); while ( (*it) ) { NavigatorItem *item = static_cast( (*it) ); QUrl itemUrl( item->entry()->url() ); if ( (itemUrl == url) || (itemUrl == alternativeURL) ) { - mContentsTree->setCurrentItem( item ); // If the current item was not selected and remained unchanged it // needs to be explicitly selected mContentsTree->setCurrentItem(item); item->setExpanded( true ); break; } + if ( (contentsItem == nullptr) && (itemUrl == contentsItemURL) ) { + contentsItem = item; + } ++it; } if ( !(*it) ) { - clearSelection(); + // if search with fragment didn't find anything, but item without fragment was found, use it + if ( contentsItem != nullptr ) { + mContentsTree->setCurrentItem(contentsItem); + item->setExpanded( true ); + mSelected = true; + } else { + clearSelection(); + } } else { mSelected = true; } -- 2.24.1