[vlc-devel] [PATCH 3/5] Qt: Preferences tree filter

Yuval Tze yuvaltze at gmail.com
Tue Jul 26 23:17:35 CEST 2011


add filter function to expand and show matched tree items and hide the rest
---
 .../gui/qt4/components/complete_preferences.cpp    |   70 ++++++++++++++++++++
 .../gui/qt4/components/complete_preferences.hpp    |    3 +
 2 files changed, 73 insertions(+), 0 deletions(-)

diff --git a/modules/gui/qt4/components/complete_preferences.cpp b/modules/gui/qt4/components/complete_preferences.cpp
index 51f4a35..a4325e6 100644
--- a/modules/gui/qt4/components/complete_preferences.cpp
+++ b/modules/gui/qt4/components/complete_preferences.cpp
@@ -343,6 +343,75 @@ void PrefsTree::doAll( bool doclean )
     }
 }
 
+/* apply filter on tree item and recursively on its sub items
+ * returns whether the item was filtered */
+bool PrefsTree::filterItems( QTreeWidgetItem *item, const QString &text,
+                           Qt::CaseSensitivity cs )
+{
+    bool sub_filtered = true;
+
+    for( int i = 0; i < item->childCount(); i++ )
+    {
+        QTreeWidgetItem *sub_item = item->child( i );
+        if ( !filterItems( sub_item, text, cs ) )
+        {
+            /* not all the sub items were filtered */
+            sub_filtered = false;
+        }
+    }
+
+    PrefsItemData *data = item->data( 0, Qt::UserRole ).value<PrefsItemData *>();
+
+    bool filtered = sub_filtered && !data->contains( text, cs );
+    item->setExpanded( !sub_filtered );
+    item->setHidden( filtered );
+
+    return filtered;
+}
+
+
+/* collapse item if it's not selected or one of its sub items
+ * returns whether the item was collapsed */
+bool PrefsTree::collapseUnselectedItems( QTreeWidgetItem *item )
+{
+    bool sub_collapsed = true;
+
+    for( int i = 0; i < item->childCount(); i++ )
+    {
+        QTreeWidgetItem *sub_item = item->child( i );
+        if ( !collapseUnselectedItems( sub_item ) )
+        {
+            /* not all the sub items were collapsed */
+            sub_collapsed = false;
+        }
+    }
+
+    bool collapsed = sub_collapsed && !item->isSelected();
+    item->setExpanded( !sub_collapsed );
+    item->setHidden( false );
+
+    return collapsed;
+}
+
+/* apply filter on tree */
+void PrefsTree::filter( const QString &text )
+{
+    bool clear_filter = text.isEmpty();
+
+    for( int i = 0 ; i < topLevelItemCount(); i++ )
+    {
+        QTreeWidgetItem *cat_item = topLevelItem( i );
+        if ( clear_filter )
+        {
+            collapseUnselectedItems( cat_item );
+        }
+        else
+        {
+            filterItems( cat_item, text, Qt::CaseInsensitive );
+        }
+    }
+}
+
 /* go over the module config items and search text in psz_text
  * also search the module name and head */
 bool PrefsItemData::contains( const QString &text, Qt::CaseSensitivity cs )
@@ -484,6 +553,7 @@ AdvPrefsPanel::AdvPrefsPanel( intf_thread_t *_p_intf, QWidget *_parent,
         head = QString( qtr( module_GetLongName( p_module ) ) );
     }
 
+
     QLabel *titleLabel = new QLabel( head );
     QFont titleFont = QApplication::font();
     titleFont.setPointSize( titleFont.pointSize() + 6 );
diff --git a/modules/gui/qt4/components/complete_preferences.hpp b/modules/gui/qt4/components/complete_preferences.hpp
index f58b50f..2c24cd6 100644
--- a/modules/gui/qt4/components/complete_preferences.hpp
+++ b/modules/gui/qt4/components/complete_preferences.hpp
@@ -72,9 +72,12 @@ public:
 
     void applyAll();
     void cleanAll();
+    void filter( const QString &text );
 
 private:
     void doAll( bool );
+    bool filterItems( QTreeWidgetItem *item, const QString &text, Qt::CaseSensitivity cs );
+    bool collapseUnselectedItems( QTreeWidgetItem *item );
     intf_thread_t *p_intf;
 };
 
-- 
1.7.4.1




More information about the vlc-devel mailing list