[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: [Patch] KListWidgetSearchLine sorts QListWidget even if sorting
From:       "Nicolas Ternisien" <nicolas.ternisien () gmail ! com>
Date:       2008-04-10 12:41:18
Message-ID: ccba71b50804100541w2998eeffs84700c7dee13591c () mail ! gmail ! com
[Download RAW message or body]

And now, why not attaching the patch ? ;-)

On Thu, Apr 10, 2008 at 2:40 PM, Nicolas Ternisien
<nicolas.ternisien@gmail.com> wrote:
> In fact, there is an other problem in this class, that does not rely
>  on QListWidgetItem::setHidden() and QListWidgetItem::isHidden(), and
>  which changes list ordering when a filter is setted and removed.
>
>  This new patch uses now this feature, and KListWidgetSearchLine has
>  now the same behavior than KTreeWidgetSearchLine.
>
>  ++
>
>  Nicolas
>
>
>
>  On Thu, Apr 10, 2008 at 1:56 PM, Nicolas Ternisien
>  <nicolas.ternisien@gmail.com> wrote:
>  > Hi,
>  >
>  >  As the title said : KListWidgetSearchLine sorts the related
>  >  QListWidget even if sorting is disabled in this QListWidget. The
>  >  amazing attached patch fixes this problem. Any comment before applying
>  >  it ?
>  >
>  >  Nicolas.
>  >
>

["klistwidgetsearchline-sort-ifsorting-enabled.patch" (text/x-diff)]

Index: klistwidgetsearchline.cpp
===================================================================
--- klistwidgetsearchline.cpp	(révision 795466)
+++ klistwidgetsearchline.cpp	(copie de travail)
@@ -30,8 +30,6 @@
 
 #define DEFAULT_CASESENSITIVE Qt::CaseInsensitive
 
-typedef QList <QListWidgetItem *> QListWidgetItemList;
-
 class KListWidgetSearchLine::KListWidgetSearchLinePrivate
 {
 public:
@@ -48,8 +46,6 @@
     void _k_activateSearch();
 
     void init( QListWidget *listWidget = 0 );
-    void hideItem( QListWidgetItem *item );
-    void showItem( QListWidgetItem *item );
 
     KListWidgetSearchLine *q;
     QListWidget *listWidget;
@@ -57,7 +53,6 @@
     bool activeSearch;
     QString search;
     int queuedSearches;
-    QListWidgetItemList hiddenItems;
 };
 
 /******************************************************************************
@@ -73,7 +68,7 @@
 
 KListWidgetSearchLine::~KListWidgetSearchLine()
 {
-    clear(); // empty hiddenItems, returning items back to listWidget
+    clear(); // returning items back to listWidget
     delete d;
 }
 
@@ -98,38 +93,27 @@
 
     QString search = d->search = s.isNull() ? text() : s;
 
-    QListWidgetItemList *hi = &(d->hiddenItems);
-
     QListWidgetItem *currentItem = lw->currentItem();
 
-    // Remove Non-Matching items, add them them to hidden list
+    // Remove Non-Matching items
     int index = 0;
     while ( index < lw->count() ) {
         QListWidgetItem *item = lw->item(index);
         if ( ! itemMatches( item, search ) ) {
-            d->hideItem( item );
+        	item->setHidden( true );
 
             if ( item == currentItem ) {
                 currentItem = 0; // It's not in listWidget anymore.
             }
-        } else {
-            index++;
+        } else if ( item->isHidden() ){
+        	item->setHidden( false );
         }
-    }
-
-    // Add Matching items, remove from hidden list
-    index = 0;
-    while ( index < hi->count() ) {
-        QListWidgetItem *item = hi->at(index); 
         
-        if ( itemMatches( item, search ) ) {
-            d->showItem( item );
-        } else {
-            index++;
-        }
+        index++;
     }
 
-    lw->sortItems();
+    if ( lw->isSortingEnabled() )
+    	lw->sortItems();
 
     if ( currentItem != 0 )
         lw->scrollToItem( currentItem );
@@ -137,23 +121,13 @@
 
 void KListWidgetSearchLine::clear()
 {
-    // Clear hidden list, give items back to QListWidget, if it still exists
+    // Show items back to QListWidget
     QListWidgetItem *item = 0;
-    QListWidgetItemList::iterator it = d->hiddenItems.begin();
-    while ( it != d->hiddenItems.end() ) {
-        item = *it;
-        ++it;
-        if ( item != 0 ) {
-            if ( d->listWidget != 0 )
-                d->showItem( item );
-            else
-                delete item;
-        }
+    if ( d->listWidget != 0 ) {
+	    for (int i = 0 ; i < d->listWidget->count(); ++i) {
+            d->listWidget->item( i )->setHidden( false );
+	    }
     }
-    if ( ! d->hiddenItems.isEmpty() )
-        kDebug() << __FILE__ << ":" << __LINE__ <<
-        "hiddenItems is not empty as it should be. " <<
-        d->hiddenItems.count() << " items are still there.\n" << endl;
 
     d->search = "";
     d->queuedSearches = 0;
@@ -215,27 +189,6 @@
     q->setClearButtonShown(true);
 }
 
-void KListWidgetSearchLine::KListWidgetSearchLinePrivate::hideItem( QListWidgetItem *item )
-{
-    if ( ( item == 0 ) || ( listWidget == 0 ) )
-        return ;
-
-    hiddenItems.append( item );
-    listWidget->takeItem( listWidget->row( item ) );
-}
-
-void KListWidgetSearchLine::KListWidgetSearchLinePrivate::showItem( QListWidgetItem *item )
-{
-    if ( listWidget == 0 ) {
-        kDebug() << __FILE__ << ":" << __LINE__ <<
-        "showItem() could not be called while there's no listWidget set." <<
-        endl;
-        return ;
-    }
-    listWidget->addItem( item );
-    hiddenItems.removeAll( item );
-}
-
 bool KListWidgetSearchLine::event(QEvent *event) {
 
 	if (event->type() == QEvent::KeyPress) {


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic