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

List:       kde-commits
Subject:    playground/games/kamala
From:       Stanislas Marquis <roundphi () gmail ! com>
Date:       2009-08-05 11:29:11
Message-ID: 1249471751.792343.9751.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 1007243 by smarquis:

Hopefully fixed horoscope proxy model filtering (and room left for further optimization).


 M  +2 -2      application.cpp  
 M  +15 -59    models/horoscopeproxymodel.cpp  
 M  +9 -12     models/horoscopeproxymodel.hpp  


--- trunk/playground/games/kamala/application.cpp #1007242:1007243
@@ -37,7 +37,8 @@
 #include <core.h>
 
 KamalaApplication::KamalaApplication()
-    : KApplication()
+    : KApplication(),
+    core( NULL )
 {
     // check for existence of our local data dir, usually in ~/.kde/share/apps/
     QString homeDir = KStandardDirs::locateLocal( "data", "kamala/", true );
@@ -66,7 +67,6 @@
     setenv( "ZODIACORE_DB_INSERTS", sqliteInserts.toUtf8().constData(), 1 );
 
     // init core
-    core = NULL;
     zodiacore_new( &core );
 
     // create models
--- trunk/playground/games/kamala/models/horoscopeproxymodel.cpp #1007242:1007243
@@ -25,7 +25,8 @@
 #include <QModelIndex>
 #include <QSortFilterProxyModel>
 
-#include "../application.hpp"
+#include <KDebug>
+
 #include "horoscopemodel.hpp"
 
 HoroscopeProxyModel::HoroscopeProxyModel( const HoroscopeModel* model, QObject* parent )
@@ -35,80 +36,35 @@
     setFilterWildcard( "" );
     setFilterKeyColumn( 0 );
     setDynamicSortFilter( true );
+
+    connect( model, SIGNAL( dataChanged( const QModelIndex&, const QModelIndex& ) ),
+        this, SLOT( buildIndexList() ) );
+    connect( model, SIGNAL( layoutChanged() ), this, SLOT( buildIndexList() ) );
 }
 
-#if 0
 QModelIndex HoroscopeProxyModel::mapToSource( const QModelIndex& index ) const
 {
-    if ( !index.isValid() )
+    if ( !index.isValid() || index.row() >= _indexList.count() )
         return QModelIndex();
+    return sourceModel()->index( _indexList.at( index.row() ), index.column() );
+}
 
+void HoroscopeProxyModel::buildIndexList()
+{ // todo: dont rebuild when data changed is not relevant to the horoscope name
+    _indexList.clear();
     const HoroscopeModel* src = (HoroscopeModel*) sourceModel();
     const uint srcCount = src->core->sortedHoroscopes->len;
-
-    if ( index.row() >= srcCount )
-        return QModelIndex();
-
-    int row = -1;
-    int x = -1;
     uint i = 0;
-
     for ( ; i < srcCount; ++i )
     {
         if ( filterAcceptsRow( i, QModelIndex() ) )
-            ++x;
-        else
-            continue;
-
-        if ( x == index.row() )
-        {
-            row = i;
-            break;
-        }
+            _indexList.append( src->index( i, 0 ).row() );
     }
-
-    if ( row == -1 )
-        return QModelIndex();
-
-    return src->index( row, index.column() );
+    invalidateFilter();
 }
-#endif
 
 void HoroscopeProxyModel::setFilterWildcard( const QString& pattern )
 {
-    buildIndexList( pattern );
     QSortFilterProxyModel::setFilterWildcard( pattern );
+    buildIndexList();
 }
-
-void HoroscopeProxyModel::buildIndexList( const QString& pattern )
-{
-    _indexList.clear();
-
-    const uint srcCount = KamalaApp->core->sortedHoroscopes->len;
-    uint i = 0;
-
-    if ( pattern.isEmpty() || pattern == "*" )
-    { // show all
-        for ( ; i < srcCount; ++i )
-            _indexList.append( true );
-    }
-    else
-    { // filter horoscopes
-        const char* pattrn = pattern.toUtf8().constData();
-        const Horoscope** vec = (const Horoscope**)KamalaApp->core->sortedHoroscopes->ptr;
-        while ( i < srcCount )
-        {
-            if ( !strstr( vec[i++]->name->ptr, pattrn ) )
-                _indexList.append( false );
-            else
-                _indexList.append( true );
-        }
-    }
-}
-
-bool HoroscopeProxyModel::filterAcceptsRow( int row, const QModelIndex& ) const
-{
-    if ( row < 0 || row >= _indexList.count() )
-        return false;
-    return _indexList.at( row );
-}
--- trunk/playground/games/kamala/models/horoscopeproxymodel.hpp #1007242:1007243
@@ -48,29 +48,26 @@
      */
     QModelIndex parent( const QModelIndex& ) const { return QModelIndex(); }
 
-    /** @brief Filter rows according to the pre-filtered list
-     *  @param src_row Source row
-     *  @param src_parent Source parent index
+    /** @brief Get a source index
      */
-    bool filterAcceptsRow( int src_row, const QModelIndex& src_parent ) const;
+    QModelIndex mapToSource( const QModelIndex& ) const;
 
   public Q_SLOTS:
 
-    /** @brief Rebuild the index list before reseting the pattern
-     *  @param pattern Wildcard pattern
+    /** @brief Build the pre-filtered index list
      */
+    void buildIndexList();
+
+    /** @brief Reset the pre-filtered index list when changing wildcard
+     *  @param pattern String pattern
+     */
     void setFilterWildcard( const QString& pattern );
 
   private:
 
-    /** @brief Recreate our filtered index list
-     *  @param pattern Wildcard pattern
-     */
-    void buildIndexList( const QString& pattern );
-
     /** @brief Pre-filtered index list
      */
-    QList<bool>        _indexList;
+    QList<int>      _indexList;
 
 };
 
[prev in list] [next in list] [prev in thread] [next in thread] 

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