diff --git a/retroshare-gui/src/gui/common/RSTreeWidget.cpp b/retroshare-gui/src/gui/common/RSTreeWidget.cpp index 17273928e..c5c4cae45 100644 --- a/retroshare-gui/src/gui/common/RSTreeWidget.cpp +++ b/retroshare-gui/src/gui/common/RSTreeWidget.cpp @@ -71,3 +71,38 @@ void RSTreeWidget::mousePressEvent(QMouseEvent *event) QTreeWidget::mousePressEvent(event); } + +void RSTreeWidget::filterItems(int filterColumn, const QString &text) +{ + int count = topLevelItemCount(); + for (int index = 0; index < count; ++index) { + filterItem(topLevelItem(index), filterColumn, text); + } +} + +bool RSTreeWidget::filterItem(QTreeWidgetItem *item, int filterColumn, const QString &text) +{ + bool itemVisible = true; + + if (!text.isEmpty()) { + if (!item->text(filterColumn).contains(text, Qt::CaseInsensitive)) { + itemVisible = false; + } + } + + int visibleChildCount = 0; + int count = item->childCount(); + for (int index = 0; index < count; ++index) { + if (filterItem(item->child(index), filterColumn, text)) { + ++visibleChildCount; + } + } + + if (itemVisible || visibleChildCount) { + item->setHidden(false); + } else { + item->setHidden(true); + } + + return (itemVisible || visibleChildCount); +} diff --git a/retroshare-gui/src/gui/common/RSTreeWidget.h b/retroshare-gui/src/gui/common/RSTreeWidget.h index d5bdb39ad..3cda62a13 100644 --- a/retroshare-gui/src/gui/common/RSTreeWidget.h +++ b/retroshare-gui/src/gui/common/RSTreeWidget.h @@ -34,9 +34,14 @@ public: void setPlaceholderText(const QString &text); + void filterItems(int filterColumn, const QString &text); + signals: void signalMouseMiddleButtonClicked(QTreeWidgetItem *item); +private: + bool filterItem(QTreeWidgetItem *item, int filterColumn, const QString &text); + protected: void paintEvent(QPaintEvent *event); virtual void mousePressEvent(QMouseEvent *event);