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

List:       kde-commits
Subject:    koffice/libs/widgets
From:       Sven Langkamp <sven.langkamp () gmail ! com>
Date:       2010-02-18 12:09:01
Message-ID: 1266494941.796301.23360.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 1092143 by langkamp:

add support for proxy models in KoResourceItemChooser

 M  +26 -3     KoResourceItemChooser.cpp  
 M  +8 -0      KoResourceItemChooser.h  


--- trunk/koffice/libs/widgets/KoResourceItemChooser.cpp #1092142:1092143
@@ -24,6 +24,7 @@
 #include <QButtonGroup>
 #include <QPushButton>
 #include <QHeaderView>
+#include <QAbstractProxyModel>
 
 #include <kfiledialog.h>
 #include <kiconloader.h>
@@ -106,7 +107,7 @@
         QModelIndex index = d->view->currentIndex();
         if( index.isValid() ) {
 
-            KoResource * resource = static_cast<KoResource*>( index.internalPointer() );
+            KoResource * resource = resourceFromModelIndex(index);
             if( resource ) {
                 d->model->resourceServerAdapter()->removeResource(resource);
             }
@@ -140,7 +141,7 @@
 {
     QModelIndex index = d->view->currentIndex();
     if( index.isValid() )
-        return static_cast<KoResource*>( index.internalPointer() );
+        return resourceFromModelIndex(index);
 
     return 0;
 }
@@ -154,12 +155,18 @@
     d->view->setCurrentIndex(index);
 }
 
+void KoResourceItemChooser::setProxyModel( QAbstractProxyModel* proxyModel )
+{
+    proxyModel->setSourceModel(d->model);
+    d->view->setModel(proxyModel);
+}
+
 void KoResourceItemChooser::activated( const QModelIndex & index )
 {
     if( ! index.isValid() )
         return;
 
-    KoResource * resource = static_cast<KoResource*>( index.internalPointer() );
+    KoResource * resource = resourceFromModelIndex(index);
 
     if( resource ) {
         emit resourceSelected( resource );
@@ -182,4 +189,20 @@
     removeButton->setEnabled( false );
 }
 
+KoResource* KoResourceItemChooser::resourceFromModelIndex(const QModelIndex& index)
+{
+    if(!index.isValid())
+        return 0;
+    
+    const QAbstractProxyModel* proxyModel = dynamic_cast<const QAbstractProxyModel*>(index.model());
+    if(proxyModel) {
+        //Get original model index, because proxy models destroy the internalPointer
+        QModelIndex originalIndex = proxyModel->mapToSource(index);
+        return static_cast<KoResource*>( originalIndex.internalPointer() );
+    }
+
+    return static_cast<KoResource*>( index.internalPointer() );
+}
+
+
 #include <KoResourceItemChooser.moc>
--- trunk/koffice/libs/widgets/KoResourceItemChooser.h #1092142:1092143
@@ -27,6 +27,7 @@
 
 #include "kowidgets_export.h"
 
+class QAbstractProxyModel;
 class QButtonGroup;
 class QAbstractItemDelegate;
 class KoAbstractResourceServerAdapter;
@@ -65,6 +66,9 @@
     void setCurrentItem(int row, int column);
 
     void showButtons( bool show );
+    
+    ///Set a proxy model with will be used to filter the resources
+    void setProxyModel( QAbstractProxyModel* proxyModel );
 signals:
     /// Emitted when a resource was selected
     void resourceSelected( KoResource * resource );
@@ -78,6 +82,10 @@
 
     void updateRemoveButtonState();
 
+    /// Resource for a given model index
+    /// @returns the resource pointer, 0 is index not valid
+    KoResource* resourceFromModelIndex(const QModelIndex & index );
+
     class Private;
     Private * const d;
 };
[prev in list] [next in list] [prev in thread] [next in thread] 

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