From eab21078feec2b6afd4130f3c91fb642624b9322 2017-07-31 14:48:13 From: Alexandre Leroux Date: 2017-07-31 14:48:13 Subject: [PATCH] Implements filter function for data source An item is valid if one of its metadata matches the text in the seach box --- diff --git a/gui/include/DataSource/DataSourceTreeWidgetItem.h b/gui/include/DataSource/DataSourceTreeWidgetItem.h index 842e5ba..d58190f 100644 --- a/gui/include/DataSource/DataSourceTreeWidgetItem.h +++ b/gui/include/DataSource/DataSourceTreeWidgetItem.h @@ -21,6 +21,8 @@ public: explicit DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data, int type = Type); + const DataSourceItem *data() const; + virtual QVariant data(int column, int role) const override; virtual void setData(int column, int role, const QVariant &value) override; diff --git a/gui/src/DataSource/DataSourceTreeWidgetItem.cpp b/gui/src/DataSource/DataSourceTreeWidgetItem.cpp index 22a3cde..df831ad 100644 --- a/gui/src/DataSource/DataSourceTreeWidgetItem.cpp +++ b/gui/src/DataSource/DataSourceTreeWidgetItem.cpp @@ -131,6 +131,11 @@ DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const Da std::back_inserter(impl->m_Actions), createTreeAction); } +const DataSourceItem *DataSourceTreeWidgetItem::data() const +{ + return impl->m_Data; +} + QVariant DataSourceTreeWidgetItem::data(int column, int role) const { if (role == Qt::DisplayRole) { diff --git a/gui/src/DataSource/DataSourceWidget.cpp b/gui/src/DataSource/DataSourceWidget.cpp index 33f7cdb..e1de06d 100644 --- a/gui/src/DataSource/DataSourceWidget.cpp +++ b/gui/src/DataSource/DataSourceWidget.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -68,6 +69,21 @@ void DataSourceWidget::addDataSource(DataSourceItem *dataSource) noexcept void DataSourceWidget::filterChanged(const QString &text) noexcept { + auto validateItem = [&text](const DataSourceTreeWidgetItem &item) { + auto regExp = QRegExp{text, Qt::CaseInsensitive, QRegExp::Wildcard}; + + // An item is valid if any of its metadata validates the text filter + auto itemMetadata = item.data()->data(); + auto itemMetadataEnd = itemMetadata.cend(); + auto acceptFilter + = [®Exp](const auto &variant) { return variant.toString().contains(regExp); }; + + return std::find_if(itemMetadata.cbegin(), itemMetadataEnd, acceptFilter) + != itemMetadataEnd; + }; + + // Applies filter on tree widget + DataSourceTreeWidgetHelper::filter(*ui->treeWidget, validateItem); } void DataSourceWidget::onTreeMenuRequested(const QPoint &pos) noexcept