[request for approval] new small and useful kdevelop 3.4 feature

Flavio Castelli micron at bglug.it
Thu Dec 14 23:38:00 UTC 2006


I've wrote a small patch for kdevelop 3.4 that adds a filtering option 
to class browser.
Using this feature you can filter the class browser elements, showing only 
items matching a specified text.

It's something similar to the filtering option found in amarok collection 
browser or in kmail.

Here you can find some screenshots of kdevelop and this feature:
http://img321.imageshack.us/my.php?image=kdevelopclassfilter02vm9.jpg
http://img141.imageshack.us/my.php?image=kdevelopclassfilter01pb6.jpg

I've seen that the release of KDevelop 3.4 was planned for yesterday 
(14/Dec/2006), so kdevelop is in feature-freeze status.
The patch is stable and works fine, so I would like to know when I can commit 
it.

In the meantime here are the diff files (against kdevelop 3.4 branch). All 
changes are made into kdevelop/parts/classview directory.

Thanks in advance
	Flavio Castelli

Index: classviewwidget.cpp
===================================================================
--- classviewwidget.cpp (revision 612765)
+++ classviewwidget.cpp (working copy)
@@ -47,8 +47,8 @@

 // namespace ?!?

-ClassViewWidget::ClassViewWidget( ClassViewPart * part )
-    : KListView( 0, "ClassViewWidget" ), QToolTip( viewport() ), m_part( 
part ), m_projectDirectoryLength( 0 )
+ClassViewWidget::ClassViewWidget( ClassViewPart * part, QWidget * parent )
+    : KListView( parent, "ClassViewWidget" ), QToolTip( viewport() ), m_part( 
part ), m_projectDirectoryLength( 0 )
 {
     addColumn( "" );
     header()->hide();

Index: classviewwidget.h
===================================================================
--- classviewwidget.h   (revision 612765)
+++ classviewwidget.h   (working copy)
@@ -54,7 +54,7 @@
     };

 public:
-    ClassViewWidget( ClassViewPart *part );
+    ClassViewWidget( ClassViewPart *part, QWidget *parent);
     virtual ~ClassViewWidget();

     int viewMode() const;

Index: classviewpart.h
===================================================================
--- classviewpart.h     (revision 612765)
+++ classviewpart.h     (working copy)
@@ -48,6 +48,9 @@
 class ClassViewWidget;
 class KListViewAction;
 class QListViewItem;
+class QWidget;
+class KLineEdit;
+class KPushButton;
 class KToolBarPopupAction;
 class NamespaceItem;
 class Navigator;
@@ -64,7 +67,7 @@
     KListViewAction *m_functionsnav;
     Navigator *navigator;

-    inline ClassViewWidget* widget() {
+    inline QWidget* widget() {
         return &( *m_widget );
     }

@@ -73,15 +76,20 @@
 private slots:
     void slotProjectOpened();
     void slotProjectClosed();
+    void slotFilterItems(const QString&);
+    void slotShowItems();
     void graphicalClassView();
-               void refresh();
+    void refresh();

     void activePartChanged(KParts::Part*);

 private:
     void setupActions();

-    QGuardedPtr<ClassViewWidget> m_widget;
+    QGuardedPtr<QWidget> m_widget;
+    QGuardedPtr<ClassViewWidget> m_classViewWidget;
+    QGuardedPtr<KLineEdit> m_lineEdit;
+    QGuardedPtr<KPushButton> m_clearBtn;

     QString m_activeFileName;
     KTextEditor::Document* m_activeDocument;

Index: classviewpart.cpp
===================================================================
--- classviewpart.cpp   (revision 612765)
+++ classviewpart.cpp   (working copy)
@@ -20,11 +20,14 @@
  */

 #include <qwhatsthis.h>
+#include <qwidget.h>
+#include <qlayout.h>
 #include <qlistview.h>
 #include <qfileinfo.h>
 #include <qlineedit.h>

 #include <kiconloader.h>
+#include <kpushbutton.h>
 #include <klocale.h>
 #include <kdevgenericfactory.h>
 #include <kpopupmenu.h>
@@ -45,6 +48,7 @@

 #include "classviewwidget.h"
 #include "classviewpart.h"
+#include "clicklineedit.h"
 #include "hierarchydlg.h"
 #include "navigator.h"

@@ -137,7 +141,6 @@
         }
 };

-
 typedef KDevGenericFactory<ClassViewPart> ClassViewFactory;
 static const KDevPluginInfo data("kdevclassview");
 K_EXPORT_COMPONENT_FACTORY( libkdevclassview, ClassViewFactory( data ) )
@@ -154,30 +157,45 @@

     setupActions();

-    m_widget = new ClassViewWidget(this);
+    m_widget = new QWidget(0, "Class Browser");
     m_widget->setIcon( SmallIcon("view_tree") );
     m_widget->setCaption(i18n("Class Browser"));
     mainWindow()->embedSelectView( m_widget, i18n("Classes"), i18n("Class 
browser") );
     QWhatsThis::add(m_widget, i18n("<b>Class browser</b><p>"
             "The class browser shows all namespaces, classes and namespace 
and class members in a project."));
+
+    m_classViewWidget = new ClassViewWidget(this, m_widget);
+    m_lineEdit = new ClickLineEdit (i18n( "Enter search terms here" ), 
m_widget, 0);
+    m_clearBtn = new KPushButton (SmallIcon ("clear_left"),"", m_widget);
+
+    QVBoxLayout *vbox = new QVBoxLayout( m_widget );
+
+    QHBoxLayout *hlayout = new QHBoxLayout( vbox );
+    hlayout->addWidget ( m_lineEdit );
+    hlayout->addWidget ( m_clearBtn);
+
+    vbox->addWidget( m_classViewWidget);

     connect( core(), SIGNAL(projectOpened()), this, 
SLOT(slotProjectOpened()) );
     connect( core(), SIGNAL(projectClosed()), this, 
SLOT(slotProjectClosed()) );
     connect( core(), SIGNAL(languageChanged()), this, 
SLOT(slotProjectOpened()) );
     connect( partController(), SIGNAL(activePartChanged(KParts::Part*)),
         this, SLOT(activePartChanged(KParts::Part*)));
-    connect( m_widget, SIGNAL(removedNamespace(const QString&)), this, 
SLOT(removeNamespace(const QString& )));
+    connect( m_classViewWidget, SIGNAL(removedNamespace(const QString&)), 
this, SLOT(removeNamespace(const QString& )));
+    connect (m_clearBtn, SIGNAL(clicked ()), m_lineEdit, SLOT(clear()));
+    connect (m_clearBtn, SIGNAL(clicked ()), this, SLOT(slotShowItems()));
+    connect (m_lineEdit, SIGNAL(returnPressed(const QString &)), this, 
SLOT(slotFilterItems (const QString&)));
 }

 bool ClassViewPart::jumpedToItem( ItemDom item ) {
-    if(!m_widget) return false;
-    return m_widget->selectItem(item);
+    if(!m_classViewWidget) return false;
+    return m_classViewWidget->selectItem(item);
 }

 ClassViewPart::~ClassViewPart()
 {
     mainWindow()->removeView( m_widget );
-    delete (ClassViewWidget*) m_widget;
+    delete (ClassViewWidget*) m_classViewWidget;
 }

 void ClassViewPart::slotProjectOpened( )
@@ -214,6 +232,100 @@
     }
 }

+void ClassViewPart::slotShowItems()
+{
+    QListViewItemIterator it (m_classViewWidget);
+    while ( it.current() )
+    {
+        it.current()->setVisible(true);
+        ++it;
+    }
+}
+
+void findParents (QListViewItem* item, QPtrList<QListViewItem>& visibleItems)
+{
+    if (item == 0)
+        return;
+
+    QListViewItem* parent = item->parent();
+    if (parent)
+    {
+        if (visibleItems.find(parent) == -1)
+        {
+            visibleItems.append(parent);
+            findParents(parent, visibleItems);
+        }
+    }
+}
+
+void findChilds (QListViewItem* parent, QPtrList<QListViewItem>& 
visibleItems)
+{
+    QListViewItem* child = parent->firstChild();
+
+    if (child == 0)
+        return;
+
+    if (visibleItems.find(child) == -1)
+        visibleItems.append(child);
+
+    findChilds (child, visibleItems);
+
+    QListViewItem* brother = child->nextSibling();
+    while (brother)
+    {
+        if (visibleItems.find(brother) == -1)
+            visibleItems.append(brother);
+
+        findChilds (brother, visibleItems);
+        brother = brother->nextSibling();
+    }
+}
+
+void ClassViewPart::slotFilterItems(const QString& text)
+{
+    if (text.isEmpty())
+    {   // show all items and exit
+        slotShowItems();
+        return;
+    }
+
+    QPtrList<QListViewItem> visibleItems;
+    QListViewItemIterator it (m_classViewWidget->firstChild());
+    while ( it.current() ) // search interesting items
+    {
+        if ((visibleItems.find(it.current()) == -1) && 
(it.current()->text(0).contains(text, false) != 0))
+        {
+            visibleItems.append(it.current());
+            findParents (it.current(), visibleItems); //ensure all item's 
parents are visible
+            findChilds (it.current(), visibleItems); //ensure all item's 
children are visible
+
+            // if matched item is a class method we've to ensure other 
methods all be visible
+            ClassViewItem* temp = dynamic_cast<ClassViewItem*> 
(it.current()->parent());
+            if ((temp) && (temp->isClass()))
+                findChilds (temp, visibleItems);
+        }
+
+        ++it;
+    }
+
+    it = QListViewItemIterator (m_classViewWidget->firstChild());
+    while ( it.current() ) // show relevant items, hide others
+    {
+        if (visibleItems.find(it.current()) == -1)
+            it.current()->setVisible(false);
+        else
+            it.current()->setVisible(true);
+
+        ++it;
+    }
+
+//     if (visibleItems.find(m_classViewWidget->selectedItem()) == -1)
+//         m_classViewWidget->clearSelection();
+//
+//     if (visibleItems.find(m_classViewWidget->currentItem()) == -1)
+//         m_classViewWidget->setCurrentItem(0);
+}
+
 bool ClassViewPart::langHasFeature(KDevLanguageSupport::Features feature)
 {
     bool result = false;
@@ -229,8 +341,8 @@
 }

 void ClassViewPart::refresh() {
-       if( navigator )
-                       navigator->refresh();
+    if( navigator )
+        navigator->refresh();
 }

 void ClassViewPart::activePartChanged( KParts::Part * part)



-- 
|§ micron<- ICQ #118796665
|§ GPG Key:
|§  ~ Keyserver: pgp.mit.edu
|§  ~ KeyID: 6D632BED

~ "Progress is merely a realisation of utopias" ~




More information about the KDevelop-devel mailing list