diff --git a/sources/elementspanel.cpp b/sources/elementspanel.cpp index ae9acaa62..e9058fe62 100644 --- a/sources/elementspanel.cpp +++ b/sources/elementspanel.cpp @@ -120,6 +120,7 @@ ElementsPanel::ElementsPanel(QWidget *parent) : ); connect(this, SIGNAL(firstActivated()), this, SLOT(firstActivation())); + connect(this, SIGNAL(panelContentChanged()), this, SLOT(panelContentChange())); // emet un signal au lieu de gerer son menu contextuel setContextMenuPolicy(Qt::CustomContextMenu); @@ -370,6 +371,15 @@ void ElementsPanel::firstActivation() { QTimer::singleShot(250, this, SLOT(reload())); } +/** + Ensure the filter is applied again after the panel content has changed. +*/ +void ElementsPanel::panelContentChange() { + if (!filter_.isEmpty()) { + filter(filter_); + } +} + /** Methode permettant d'ajouter un projet au panel d'elements. @param qtwi_parent QTreeWidgetItem parent sous lequel sera insere le projet @@ -448,6 +458,20 @@ QTreeWidgetItem *ElementsPanel::updateElementItem(QTreeWidgetItem *element_qtwi, return(item); } +/** + @return true if \a item matches the current filter, false otherwise +*/ +bool ElementsPanel::matchesCurrentFilter(const QTreeWidgetItem *item) const { + if (!item) return(false); + + // no filter => we consider the item matches + if (filter_.isEmpty()) return(true); + + bool item_matches = item -> text(0).contains(filter_, Qt::CaseInsensitive); + + return(item_matches); +} + /** Reloads the following collections: * common collection @@ -628,15 +652,10 @@ void ElementsPanel::filter(const QString &m, QET::Filtering filtering) { } if (filtering != QET::EndFilter) { - // repere les items correspondant au filtre - QList matching_items; - foreach (QTreeWidgetItem *item, items) { - bool item_matches = item -> text(0).contains(m, Qt::CaseInsensitive); - if (item_matches) matching_items << item; - item -> setHidden(!item_matches); - } - ensureHierarchyIsVisible(matching_items); + filter_ = m; + applyCurrentFilter(items); } else { // filtering == QET::EndFilter + filter_ = QString(); QTreeWidgetItem *current_item = currentItem(); // restore the tree as it was before the filtering @@ -690,11 +709,25 @@ bool ElementsPanel::scrollToElement(const ElementsLocation &location) { return(true); } +/** + Apply the current filter to a given item. +*/ +void ElementsPanel::applyCurrentFilter(const QList &items) { + if (filter_.isEmpty()) return; + QList matching_items; + foreach (QTreeWidgetItem *item, items) { + bool item_matches = matchesCurrentFilter(item); + if (item_matches) matching_items << item; + item -> setHidden(!item_matches); + } + ensureHierarchyIsVisible(matching_items); +} + /** @param items une liste de QTreeWidgetItem pour lesquels il faut s'assurer que eux et leurs parents sont visibles */ -void ElementsPanel::ensureHierarchyIsVisible(QList items) { +void ElementsPanel::ensureHierarchyIsVisible(const QList &items) { // remonte l'arborescence pour lister les categories contenant les elements filtres QSet parent_items; foreach(QTreeWidgetItem *item, items) { diff --git a/sources/elementspanel.h b/sources/elementspanel.h index bd2c3b17b..0858d94cd 100644 --- a/sources/elementspanel.h +++ b/sources/elementspanel.h @@ -78,6 +78,8 @@ class ElementsPanel : public GenericPanel { void projectWasOpened(QETProject *); void projectWasClosed(QETProject *); bool scrollToElement(const ElementsLocation &); + void applyCurrentFilter(const QList &); + void ensureHierarchyIsVisible(const QList &); protected: void dragEnterEvent(QDragEnterEvent *); @@ -86,9 +88,11 @@ class ElementsPanel : public GenericPanel { void startDrag(Qt::DropActions); void startElementDrag(const ElementsLocation &); void startTitleBlockTemplateDrag(const TitleBlockTemplateLocation &); + bool matchesCurrentFilter(const QTreeWidgetItem *) const; protected slots: void firstActivation(); + void panelContentChange(); private: QTreeWidgetItem *addProject (QETProject *); @@ -97,8 +101,6 @@ class ElementsPanel : public GenericPanel { QTreeWidgetItem *updateElementsCategoryItem(QTreeWidgetItem *, ElementsCategory *, PanelOptions, bool = false); QTreeWidgetItem *updateElementItem (QTreeWidgetItem *, ElementDefinition *, PanelOptions, bool = false); - void ensureHierarchyIsVisible(QList); - // attributes private: QSet projects_to_display_; ///< list of projects that have been added to this panel @@ -108,5 +110,6 @@ class ElementsPanel : public GenericPanel { QTreeWidgetItem *custom_tbt_collection_item_; ///< pointer to the item representing the user templates collection int loading_progress_; ///< used to track the loading progress of elements collections bool first_reload_; ///< used to distinguish the first time this panel is reloaded + QString filter_; ///< Currently applied filter }; #endif