From a7a4ef87a701e65767870c3012809f0ec0961066 Mon Sep 17 00:00:00 2001 From: "S. Razi Alavizadeh" Date: Tue, 5 Mar 2013 00:04:43 +0330 Subject: [PATCH] Improved version of 'TreeWidget::filterString()' --- src/lib/other/browsinglibrary.cpp | 2 +- src/lib/tools/treewidget.cpp | 62 +++++++++++++------------------ 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/lib/other/browsinglibrary.cpp b/src/lib/other/browsinglibrary.cpp index 31672a554..f3bfcf2db 100644 --- a/src/lib/other/browsinglibrary.cpp +++ b/src/lib/other/browsinglibrary.cpp @@ -57,7 +57,7 @@ BrowsingLibrary::BrowsingLibrary(QupZilla* mainClass, QWidget* parent) ui->tabs->setFocus(); connect(ui->tabs, SIGNAL(CurrentChanged(int)), this, SLOT(currentIndexChanged(int))); - connect(ui->searchLine, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(search())); + connect(ui->searchLine, SIGNAL(textChanged(QString)), this, SLOT(search())); } void BrowsingLibrary::currentIndexChanged(int index) diff --git a/src/lib/tools/treewidget.cpp b/src/lib/tools/treewidget.cpp index af79b4396..db9f8156c 100644 --- a/src/lib/tools/treewidget.cpp +++ b/src/lib/tools/treewidget.cpp @@ -328,49 +328,39 @@ QList TreeWidget::allItems() void TreeWidget::filterString(const QString &string) { - expandAll(); QList _allItems = allItems(); - - if (string.isEmpty()) { - foreach(QTreeWidgetItem * item, _allItems) - item->setHidden(false); - for (int i = 0; i < topLevelItemCount(); i++) { - topLevelItem(i)->setHidden(false); + QList parents; + bool stringIsEmpty = string.isEmpty(); + foreach(QTreeWidgetItem * item, _allItems) { + bool containsString = stringIsEmpty || item->text(0).contains(string, Qt::CaseInsensitive); + if (containsString) { + item->setHidden(false); + if (item->parent()) { + if (!parents.contains(item->parent())) { + parents << item->parent(); + } + } } - if (m_showMode == ItemsCollapsed) { - collapseAll(); + else { + item->setHidden(true); + if (item->parent()) { + item->parent()->setHidden(true); + } } } - else { - foreach(QTreeWidgetItem * item, _allItems) { - item->setHidden(!item->text(0).contains(string, Qt::CaseInsensitive)); - item->setExpanded(true); + + for(int i = 0; i < parents.size(); ++i) { + QTreeWidgetItem* parentItem = parents.at(i); + parentItem->setHidden(false); + if (stringIsEmpty) { + parentItem->setExpanded(m_showMode == ItemsExpanded); } - for (int i = 0; i < topLevelItemCount(); i++) { - topLevelItem(i)->setHidden(false); + else { + parentItem->setExpanded(true); } - QTreeWidgetItem* firstItem = topLevelItem(0); - QTreeWidgetItem* belowItem = itemBelow(firstItem); - - int topLvlIndex = 0; - while (firstItem) { - if (firstItem->text(0).contains(string, Qt::CaseInsensitive)) { - firstItem->setHidden(false); - } - else if (!firstItem->parent() && !belowItem) { - firstItem->setHidden(true); - } - else if (!belowItem) { - break; - } - else if (!firstItem->parent() && !belowItem->parent()) { - firstItem->setHidden(true); - } - - topLvlIndex++; - firstItem = topLevelItem(topLvlIndex); - belowItem = itemBelow(firstItem); + if (parentItem->parent() && !parents.contains(parentItem->parent())) { + parents << parentItem->parent(); } } }