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

List:       kde-commits
Subject:    branches/work/akonadi-ports/kdepim/kmail
From:       Constantin Berzan <exit3219 () gmail ! com>
Date:       2009-08-18 7:58:20
Message-ID: 1250582300.960627.8299.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 1012842 by cberzan:

Use QAbstractItemModel's API whenever possible, and avoid methods in AttachmentModel \
that take QModelIndexes as arguments.  This makes the sorting proxy in AttachmentView \
transparent, removing the need to convert proxy indexes to source model indexes.

Thanks Till for explaining how to do this properly.

(NOTE: requires recent libkdepim)



 M  +4 -2      attachmentcontroller.cpp  
 M  +16 -20    attachmentmodel.cpp  
 M  +15 -14    attachmentmodel.h  
 M  +9 -21     attachmentview.cpp  
 M  +0 -2      attachmentview.h  


--- branches/work/akonadi-ports/kdepim/kmail/attachmentcontroller.cpp \
#1012841:1012842 @@ -154,10 +154,12 @@
 
 void AttachmentController::Private::selectionChanged()
 {
-  QModelIndexList selectedRows = view->selectedSourceRows();
+  const QModelIndexList selectedRows = view->selectionModel()->selectedRows();
   selectedParts.clear();
   foreach( const QModelIndex &index, selectedRows ) {
-    selectedParts.append( model->attachment( index ) );
+    AttachmentPart::Ptr part = view->model()->data(
+        index, AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
+    selectedParts.append( part );
   }
   const int selectedCount = selectedParts.count();
 
--- branches/work/akonadi-ports/kdepim/kmail/attachmentmodel.cpp #1012841:1012842
@@ -156,11 +156,11 @@
     if( index.column() != 0 ) {
       // Avoid processing the same attachment more than once, since the entire
       // row is selected.
-      kWarning() << "Duplicate rows passed to mimeData().";
+      kWarning() << "column != 0. Possibly duplicate rows passed to mimeData().";
       continue;
     }
 
-    const AttachmentPart::Ptr part = attachment( index );
+    const AttachmentPart::Ptr part = d->parts[ index.row() ];
     QString attachmentName = part->fileName();
     if( attachmentName.isEmpty() ) {
       attachmentName = part->name();
@@ -291,6 +291,13 @@
       default:
         return QVariant();
     }
+  } else if( role == AttachmentPartRole ) {
+    if( index.column() == 0 ) {
+      return QVariant::fromValue( part );
+    } else {
+      kWarning() << "AttachmentPartRole and column != 0.";
+      return QVariant();
+    }
   } else {
     return QVariant();
   }
@@ -368,32 +375,21 @@
   return true;
 }
 
-bool AttachmentModel::removeAttachment( const QModelIndex &index )
+bool AttachmentModel::removeAttachment( AttachmentPart::Ptr part )
 {
-  if( !index.isValid() ) {
-    kWarning() << "Invalid index.";
+  int idx = d->parts.indexOf( part );
+  if( idx < 0 ) {
+    kWarning() << "Attachment not found.";
     return false;
   }
 
-  beginRemoveRows( QModelIndex(), index.row(), index.row() );
-  AttachmentPart::Ptr part = d->parts.takeAt( index.row() );
-  emit attachmentRemoved( part );
+  beginRemoveRows( QModelIndex(), idx, idx );
+  d->parts.removeAt( idx );
   endRemoveRows();
+  emit attachmentRemoved( part );
   return true;
 }
 
-bool AttachmentModel::removeAttachment( AttachmentPart::Ptr part )
-{
-  int idx = d->parts.indexOf( part );
-  return removeAttachment( index( idx, 0 ) );
-}
-
-AttachmentPart::Ptr AttachmentModel::attachment( const QModelIndex &index ) const
-{
-  Q_ASSERT( index.isValid() );
-  return d->parts[ index.row() ];
-}
-
 AttachmentPart::List AttachmentModel::attachments() const
 {
   return d->parts;
--- branches/work/akonadi-ports/kdepim/kmail/attachmentmodel.h #1012841:1012842
@@ -43,8 +43,11 @@
   Q_OBJECT
 
   public:
-    enum Column
-    {
+    enum {
+      AttachmentPartRole = Qt::UserRole
+    };
+
+    enum Column {
       NameColumn,
       SizeColumn,
       EncodingColumn,
@@ -79,25 +82,23 @@
     /// sets for all
     void setSignSelected( bool selected );
 
-    QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
-    bool setData( const QModelIndex &index, const QVariant &value, int role = \
Qt::EditRole ); +    virtual QVariant data( const QModelIndex &index, int role = \
Qt::DisplayRole ) const; +    virtual bool setData( const QModelIndex &index, const \
QVariant &value, int role = Qt::EditRole );  
     bool addAttachment( KPIM::AttachmentPart::Ptr part );
     bool updateAttachment( KPIM::AttachmentPart::Ptr part );
     bool replaceAttachment( KPIM::AttachmentPart::Ptr oldPart, \
                KPIM::AttachmentPart::Ptr newPart );
-    bool removeAttachment( const QModelIndex &index );
     bool removeAttachment( KPIM::AttachmentPart::Ptr part );
-    KPIM::AttachmentPart::Ptr attachment( const QModelIndex &index ) const;
     KPIM::AttachmentPart::List attachments() const;
 
-    Qt::ItemFlags flags( const QModelIndex &index ) const;
-    QVariant headerData( int section, Qt::Orientation orientation,
-                         int role = Qt::DisplayRole ) const;
-    QModelIndex index( int row, int column,
-                       const QModelIndex &parent = QModelIndex() ) const;
-    QModelIndex parent( const QModelIndex &index ) const;
-    int rowCount( const QModelIndex &parent = QModelIndex() ) const;
-    int columnCount( const QModelIndex &parent = QModelIndex() ) const;
+    virtual Qt::ItemFlags flags( const QModelIndex &index ) const;
+    virtual QVariant headerData( int section, Qt::Orientation orientation,
+                                 int role = Qt::DisplayRole ) const;
+    virtual QModelIndex index( int row, int column,
+                               const QModelIndex &parent = QModelIndex() ) const;
+    virtual QModelIndex parent( const QModelIndex &index ) const;
+    virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const;
+    virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const;
 
   signals:
     void encryptEnabled( bool enabled );
--- branches/work/akonadi-ports/kdepim/kmail/attachmentview.cpp #1012841:1012842
@@ -51,13 +51,13 @@
   d->model = model;
   connect( model, SIGNAL(encryptEnabled(bool)), this, SLOT(setEncryptEnabled(bool)) \
);  connect( model, SIGNAL(signEnabled(bool)), this, SLOT(setSignEnabled(bool)) );
-  connect( model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, \
                SLOT(hideIfEmpty()) );
-  connect( model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, \
SLOT(hideIfEmpty()) );  
   QSortFilterProxyModel *sortModel = new QSortFilterProxyModel( this );
   sortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
   sortModel->setSourceModel( model );
   setModel( sortModel );
+  connect( sortModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, \
SLOT(hideIfEmpty()) ); +  connect( sortModel, \
SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(hideIfEmpty()) );  
   setRootIsDecorated( false );
   setUniformRowHeights( true );
@@ -89,8 +89,10 @@
     // Indexes are based on row numbers, and row numbers change when items are \
                deleted.
     // Therefore, first we need to make a list of AttachmentParts to delete.
     AttachmentPart::List toRemove;
-    foreach( const QModelIndex &index, selectedSourceRows() ) {
-      toRemove.append( d->model->attachment( index ) );
+    foreach( const QModelIndex &index, selectionModel()->selectedRows() ) {
+      AttachmentPart::Ptr part = model()->data(
+          index, AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
+      toRemove.append( part );
     }
     foreach( const AttachmentPart::Ptr &part, toRemove ) {
       d->model->removeAttachment( part );
@@ -108,20 +110,6 @@
   }
 }
 
-QModelIndexList AttachmentView::selectedSourceRows() const
-{
-  Q_ASSERT( dynamic_cast<QSortFilterProxyModel*>( model() ) );
-  QSortFilterProxyModel *sortModel = static_cast<QSortFilterProxyModel*>( model() );
-  QModelIndexList selection = selectionModel()->selectedRows();
-
-  QModelIndexList sourceSelection;
-  foreach( const QModelIndex &index, selection ) {
-    const QModelIndex sourceIndex = sortModel->mapToSource( index );
-    sourceSelection.append( sourceIndex );
-  }
-  return sourceSelection;
-}
-
 void AttachmentView::setEncryptEnabled( bool enabled )
 {
   setColumnHidden( AttachmentModel::EncryptColumn, !enabled );
@@ -134,16 +122,16 @@
 
 void AttachmentView::hideIfEmpty()
 {
-  setVisible( d->model->rowCount() > 0 );
+  setVisible( model()->rowCount() > 0 );
 }
 
 void AttachmentView::startDrag( Qt::DropActions supportedActions )
 {
   Q_UNUSED( supportedActions );
 
-  QModelIndexList selection = selectedSourceRows();
+  const QModelIndexList selection = selectionModel()->selectedRows();
   if( !selection.isEmpty() ) {
-    QMimeData *mimeData = d->model->mimeData( selection );
+    QMimeData *mimeData = model()->mimeData( selection );
     QDrag *drag = new QDrag( this );
     drag->setMimeData( mimeData );
     drag->exec( Qt::CopyAction );
--- branches/work/akonadi-ports/kdepim/kmail/attachmentview.h #1012841:1012842
@@ -48,8 +48,6 @@
     /** reimpl to avoid drags from ourselves */
     virtual void dragEnterEvent( QDragEnterEvent *event );
 
-    QModelIndexList selectedSourceRows() const;
-
   public slots:
     /// model sets these
     void setEncryptEnabled( bool enabled );


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

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