RFC: Patch for KListViewSearchLine

Frerich Raabe raabe at kde.org
Fri Apr 9 11:55:41 BST 2004


Moin,

I recently noticed that the new KListViewSearchLine class in libkdeui, while
being rather nifty, is unfortunately quite inflexible. In particular, I found
it quite annoying that KListViewSearchLine inherits KLineEdit, for the
following reasons:

- I could not use it at all for my Qt Designer dialogs. I suppose a
corresponding addition to kdewidgets would solve this, but the program I worked
on is supposed to work with KDE 3.1 libs, too.
- I could not use it in case my program uses custom lineedit widgets, widget
which were derived from KLineEdit themselves that is.
- Instead of just adding code, I had to change existing code to add
functionality.

IMHO these reasons are annoying enough to justify applying the attached
patch which changes KListViewSearchLine so that it does not inherit
KLineEdit anymore. Instead, it inherits QObject now and acts as a "connector"
between KListView and KLineEdit (and IMHO that reflects nicely what it
actually does). To be more concrete, a code snippet like

	KListView *listView = new KListView ..
	KLineEdit *lineEdit = new KLineEdit ..

currently needs to get changed into

	KListView *listView = new KListView
	KSearchLineEdit *lineEdit = new KSearchLineEdit( this, listView );

With the attached patch, you can do it like this:

	KListView *listView = new KListView ..
	KLineEdit *lineEdit = new KLineEdit ..
	new KListViewSearchLine( this, lineEdit, listView );

IMHO this is a lot nicer, but I'd appreciate comments on this (since I've
been told that this is a little "unintuitive").

One thing which I'd especially like to see comments on is the name - I think
it does not fit anymore (in case the patch would get applied), something like
"KIncrementalSearchConnector" or so would be better. I'm looking forward to
your creative input. :-)

- Frerich

-- 
if new true friend not protected for explicit private union, break
case and try using this.
-------------- next part --------------
Index: klistviewsearchline.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/klistviewsearchline.cpp,v
retrieving revision 1.2
diff -u -3 -p -r1.2 klistviewsearchline.cpp
--- klistviewsearchline.cpp	23 Mar 2004 03:10:20 -0000	1.2
+++ klistviewsearchline.cpp	8 Apr 2004 20:41:07 -0000
@@ -18,6 +18,7 @@
 
 #include "klistviewsearchline.h"
 
+#include <klineedit.h>
 #include <klistview.h>
 #include <kdebug.h>
 
@@ -25,11 +26,13 @@ class KListViewSearchLine::KListViewSear
 {
 public:
     KListViewSearchLinePrivate() :
+        lineEdit(0),
         listView(0),
         caseSensitive(false),
         activeSearch(false),
         keepParentsVisible(true) {}
     
+    KLineEdit *lineEdit;
     KListView *listView;
     bool caseSensitive;
     bool activeSearch;
@@ -43,25 +46,13 @@ public:
 // public methods
 ////////////////////////////////////////////////////////////////////////////////
 
-KListViewSearchLine::KListViewSearchLine(QWidget *parent, KListView *listView, const char *name) :
-    KLineEdit(parent, name)
+KListViewSearchLine::KListViewSearchLine(QWidget *parent, KLineEdit *lineEdit, KListView *listView, const char *name) :
+    QObject(parent, name)
 {
     d = new KListViewSearchLinePrivate;
 
-    d->listView = listView;
-
-    connect(this, SIGNAL(textChanged(const QString &)),
-            this, SLOT(updateSearch(const QString &)));
-
-    if(listView) {
-        connect(listView, SIGNAL(destroyed()),
-                this, SLOT(listViewDeleted()));
-
-        connect(listView, SIGNAL(itemAdded(QListViewItem *)),
-                this, SLOT(itemAdded(QListViewItem *)));
-    }
-    else
-        setEnabled(false);
+    setLineEdit(lineEdit);
+    setListView(listView);
 }
 
 KListViewSearchLine::~KListViewSearchLine()
@@ -89,16 +80,21 @@ KListView *KListViewSearchLine::listView
     return d->listView;
 }
 
+KLineEdit *KListViewSearchLine::lineEdit() const
+{
+    return d->lineEdit;
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // public slots
 ////////////////////////////////////////////////////////////////////////////////
 
 void KListViewSearchLine::updateSearch(const QString &s)
 {
-    if(!d->listView)
+    if(!d->listView || !d->lineEdit)
         return;
 
-    d->search = s.isNull() ? text() : s;
+    d->search = s.isNull() ? d->lineEdit->text() : s;
     checkItem(d->listView->firstChild());
 }
 
@@ -137,7 +133,29 @@ void KListViewSearchLine::setListView(KL
                 this, SLOT(itemAdded(QListViewItem *)));
     }
 
-    setEnabled(bool(lv));
+    if(d->lineEdit)
+        d->lineEdit->setEnabled(bool(lv));
+}
+
+void KListViewSearchLine::setLineEdit(KLineEdit *le)
+{
+    if(d->lineEdit) {
+        disconnect(d->lineEdit, SIGNAL(destroyed()),
+                   this, SLOT(lineEditDeleted()));
+
+        disconnect(d->lineEdit, SIGNAL(textChanged(const QString &)),
+                   this, SLOT(updateSearch(const QString &)));
+    }
+
+    d->lineEdit = le;
+
+    if(le) {
+        connect(d->lineEdit, SIGNAL(destroyed()),
+                this, SLOT(lineEditDeleted()));
+
+        connect(d->lineEdit, SIGNAL(textChanged(const QString &)),
+                this, SLOT(updateSearch(const QString &)));
+    }
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -178,13 +196,21 @@ bool KListViewSearchLine::itemMatches(co
 
 void KListViewSearchLine::itemAdded(QListViewItem *item) const
 {
-    item->setVisible(itemMatches(item, text()));
+    if(!d->lineEdit)
+        return;
+    item->setVisible(itemMatches(item, d->lineEdit->text()));
 }
 
 void KListViewSearchLine::listViewDeleted()
 {
     d->listView = 0;
-    setEnabled(false);
+    if(d->lineEdit)
+        d->lineEdit->setEnabled(false);
+}
+
+void KListViewSearchLine::lineEditDeleted()
+{
+    d->lineEdit = 0;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
Index: klistviewsearchline.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/klistviewsearchline.h,v
retrieving revision 1.2
diff -u -3 -p -r1.2 klistviewsearchline.h
--- klistviewsearchline.h	29 Feb 2004 04:02:09 -0000	1.2
+++ klistviewsearchline.h	8 Apr 2004 20:41:08 -0000
@@ -19,8 +19,9 @@
 #ifndef KLISTVIEWSEARCHLINE_H
 #define KLISTVIEWSEARCHLINE_H
 
-#include <klineedit.h>
+#include <qobject.h>
 
+class KLineEdit;
 class KListView;
 class QListViewItem;
 
@@ -28,24 +29,25 @@ class QListViewItem;
  * This class makes it easy to add a search line for filtering the items in a
  * listview based on a simple text search.
  *
- * No changes to the application other than instantiating this class with an
- * appropriate KListView should be needed.
+ * No changes other than instantiating this adaptor class with an appropriate
+ * KLineEdit and KListView instance pair should be needed.
  */
 
-class KListViewSearchLine : public KLineEdit
+class KListViewSearchLine : public QObject
 {
     Q_OBJECT
 
 public:
 
     /**
-     * Constructs a KListViewSearchLine with \a listView being the KListView to
-     * be filtered.
+     * Constructs a KListViewSearchLine with \a lineEdit being the lineedit
+     * to be used as the source of the search, and \a listView being the
+     * KListView to be filtered.
      *
-     * If \a listView is null then the widget will be disabled until a listview
-     * is set with setListView().
+     * If \a lineEdit and/or \a listView is null then the widget will be
+     * disabled until both widgets are set.
      */
-    KListViewSearchLine(QWidget *parent = 0, KListView *listView = 0, const char *name = 0);
+    KListViewSearchLine(QWidget *parent = 0, KLineEdit *lineEdit = 0, KListView *listView = 0, const char *name = 0);
 
     /**
      * Destroys the KListViewSearchLine.
@@ -82,6 +84,14 @@ public:
      */
     KListView *listView() const;
 
+    /**
+     * Returns the lineedit that is currently being used as the source for the
+     * search.
+     *
+     * @see setLineEdit()
+     */
+    KLineEdit *lineEdit() const;
+
 public slots:
     /**
      * Updates search to only make visible the items that match \a s.  If
@@ -124,6 +134,14 @@ public slots:
      */
     void setListView(KListView *lv);
 
+    /**
+     * Sets the KLineEdit that is filtered by this search line. If \a le is null
+     * then the widget will be disabled.
+     *
+     * @see lineEdit()
+     */
+    void setLineEdit(KLineEdit *le);
+
 protected:
 
     /**
@@ -147,6 +165,7 @@ private:
 private slots:
     void itemAdded(QListViewItem *item) const;
     void listViewDeleted();
+    void lineEditDeleted();
 
 private:
     class KListViewSearchLinePrivate;


More information about the kde-core-devel mailing list