akonadi
collectionmodel.cpp
00001 /* 00002 Copyright (c) 2006 - 2008 Volker Krause <vkrause@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "collectionmodel.h" 00021 #include "collectionmodel_p.h" 00022 00023 #include "collectionutils_p.h" 00024 #include "collectionmodifyjob.h" 00025 #include "entitydisplayattribute.h" 00026 #include "monitor.h" 00027 #include "pastehelper_p.h" 00028 #include "session.h" 00029 00030 #include <kdebug.h> 00031 #include <kurl.h> 00032 #include <kicon.h> 00033 00034 #include <QtCore/QMimeData> 00035 #include <QtGui/QPixmap> 00036 00037 using namespace Akonadi; 00038 00039 CollectionModel::CollectionModel( QObject * parent ) : 00040 QAbstractItemModel( parent ), 00041 d_ptr( new CollectionModelPrivate( this ) ) 00042 { 00043 Q_D( CollectionModel ); 00044 d->init(); 00045 } 00046 00047 //@cond PRIVATE 00048 CollectionModel::CollectionModel( CollectionModelPrivate *d, 00049 QObject *parent ) 00050 : QAbstractItemModel( parent ), 00051 d_ptr( d ) 00052 { 00053 d->init(); 00054 } 00055 //@endcond 00056 00057 CollectionModel::~CollectionModel() 00058 { 00059 Q_D( CollectionModel ); 00060 d->childCollections.clear(); 00061 d->collections.clear(); 00062 00063 delete d->monitor; 00064 d->monitor = 0; 00065 00066 delete d; 00067 } 00068 00069 int CollectionModel::columnCount( const QModelIndex & parent ) const 00070 { 00071 if (parent.isValid() && parent.column() != 0) 00072 return 0; 00073 return 1; 00074 } 00075 00076 QVariant CollectionModel::data( const QModelIndex & index, int role ) const 00077 { 00078 Q_D( const CollectionModel ); 00079 if ( !index.isValid() ) 00080 return QVariant(); 00081 00082 const Collection col = d->collections.value( index.internalId() ); 00083 if ( !col.isValid() ) 00084 return QVariant(); 00085 00086 if ( index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole) ) { 00087 if ( col.hasAttribute<EntityDisplayAttribute>() && 00088 !col.attribute<EntityDisplayAttribute>()->displayName().isEmpty() ) 00089 return col.attribute<EntityDisplayAttribute>()->displayName(); 00090 return col.name(); 00091 } 00092 00093 switch ( role ) { 00094 case Qt::DecorationRole: 00095 if ( index.column() == 0 ) { 00096 if ( col.hasAttribute<EntityDisplayAttribute>() && 00097 !col.attribute<EntityDisplayAttribute>()->iconName().isEmpty() ) 00098 return col.attribute<EntityDisplayAttribute>()->icon(); 00099 return KIcon( CollectionUtils::defaultIconName( col ) ); 00100 } 00101 break; 00102 case OldCollectionIdRole: // fall-through 00103 case CollectionIdRole: 00104 return col.id(); 00105 case OldCollectionRole: // fall-through 00106 case CollectionRole: 00107 return QVariant::fromValue( col ); 00108 } 00109 return QVariant(); 00110 } 00111 00112 QModelIndex CollectionModel::index( int row, int column, const QModelIndex & parent ) const 00113 { 00114 Q_D( const CollectionModel ); 00115 if (column >= columnCount() || column < 0) return QModelIndex(); 00116 00117 QVector<Collection::Id> list; 00118 if ( !parent.isValid() ) 00119 list = d->childCollections.value( Collection::root().id() ); 00120 else 00121 { 00122 if (parent.column() > 0) 00123 return QModelIndex(); 00124 list = d->childCollections.value( parent.internalId() ); 00125 } 00126 00127 if ( row < 0 || row >= list.size() ) 00128 return QModelIndex(); 00129 if ( !d->collections.contains( list.at(row) ) ) 00130 return QModelIndex(); 00131 return createIndex( row, column, reinterpret_cast<void*>( d->collections.value( list.at(row) ).id() ) ); 00132 } 00133 00134 QModelIndex CollectionModel::parent( const QModelIndex & index ) const 00135 { 00136 Q_D( const CollectionModel ); 00137 if ( !index.isValid() ) 00138 return QModelIndex(); 00139 00140 Collection col = d->collections.value( index.internalId() ); 00141 if ( !col.isValid() ) 00142 return QModelIndex(); 00143 00144 00145 Collection parentCol = d->collections.value( col.parentCollection().id() ); 00146 if ( !parentCol.isValid() ) 00147 { 00148 return QModelIndex(); 00149 } 00150 QVector<Collection::Id> list; 00151 list = d->childCollections.value( parentCol.parentCollection().id() ); 00152 00153 int parentRow = list.indexOf( parentCol.id() ); 00154 if ( parentRow < 0 ) 00155 return QModelIndex(); 00156 00157 return createIndex( parentRow, 0, reinterpret_cast<void*>( parentCol.id() ) ); 00158 } 00159 00160 int CollectionModel::rowCount( const QModelIndex & parent ) const 00161 { 00162 const Q_D( CollectionModel ); 00163 QVector<Collection::Id> list; 00164 if ( parent.isValid() ) 00165 list = d->childCollections.value( parent.internalId() ); 00166 else 00167 list = d->childCollections.value( Collection::root().id() ); 00168 00169 return list.size(); 00170 } 00171 00172 QVariant CollectionModel::headerData( int section, Qt::Orientation orientation, int role ) const 00173 { 00174 const Q_D( CollectionModel ); 00175 00176 if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ) 00177 return d->headerContent; 00178 return QAbstractItemModel::headerData( section, orientation, role ); 00179 } 00180 00181 bool CollectionModel::setHeaderData( int section, Qt::Orientation orientation, const QVariant &value, int role ) 00182 { 00183 Q_D( CollectionModel ); 00184 00185 if ( section == 0 && orientation == Qt::Horizontal && role == Qt::EditRole ) { 00186 d->headerContent = value.toString(); 00187 return true; 00188 } 00189 00190 return false; 00191 } 00192 00193 bool CollectionModel::setData( const QModelIndex & index, const QVariant & value, int role ) 00194 { 00195 Q_D( CollectionModel ); 00196 if ( index.column() == 0 && role == Qt::EditRole ) { 00197 // rename collection 00198 Collection col = d->collections.value( index.internalId() ); 00199 if ( !col.isValid() || value.toString().isEmpty() ) 00200 return false; 00201 col.setName( value.toString() ); 00202 CollectionModifyJob *job = new CollectionModifyJob( col, d->session ); 00203 connect( job, SIGNAL(result(KJob*)), SLOT(editDone(KJob*)) ); 00204 return true; 00205 } 00206 return QAbstractItemModel::setData( index, value, role ); 00207 } 00208 00209 Qt::ItemFlags CollectionModel::flags( const QModelIndex & index ) const 00210 { 00211 Q_D( const CollectionModel ); 00212 00213 // Pass modeltest. 00214 // http://labs.trolltech.com/forums/topic/79 00215 if (!index.isValid()) 00216 return 0; 00217 00218 Qt::ItemFlags flags = QAbstractItemModel::flags( index ); 00219 00220 flags = flags | Qt::ItemIsDragEnabled; 00221 00222 Collection col; 00223 if ( index.isValid() ) { 00224 col = d->collections.value( index.internalId() ); 00225 Q_ASSERT( col.isValid() ); 00226 } 00227 else 00228 return flags | Qt::ItemIsDropEnabled; // HACK Workaround for a probable bug in Qt 00229 00230 if ( col.isValid() ) { 00231 if ( col.rights() & (Collection::CanChangeCollection | 00232 Collection::CanCreateCollection | 00233 Collection::CanDeleteCollection | 00234 Collection::CanCreateItem) ) { 00235 if ( index.column() == 0 ) 00236 flags = flags | Qt::ItemIsEditable; 00237 00238 flags = flags | Qt::ItemIsDropEnabled; 00239 } 00240 } 00241 00242 return flags; 00243 } 00244 00245 Qt::DropActions CollectionModel::supportedDropActions() const 00246 { 00247 return Qt::CopyAction | Qt::MoveAction; 00248 } 00249 00250 QStringList CollectionModel::mimeTypes() const 00251 { 00252 return QStringList() << QLatin1String( "text/uri-list" ); 00253 } 00254 00255 QMimeData *CollectionModel::mimeData(const QModelIndexList &indexes) const 00256 { 00257 QMimeData *data = new QMimeData(); 00258 KUrl::List urls; 00259 foreach ( const QModelIndex &index, indexes ) { 00260 if ( index.column() != 0 ) 00261 continue; 00262 00263 urls << Collection( index.internalId() ).url(); 00264 } 00265 urls.populateMimeData( data ); 00266 00267 return data; 00268 } 00269 00270 bool CollectionModel::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) 00271 { 00272 Q_D( CollectionModel ); 00273 if ( !(action & supportedDropActions()) ) 00274 return false; 00275 00276 // handle drops onto items as well as drops between items 00277 QModelIndex idx; 00278 if ( row >= 0 && column >= 0 ) 00279 idx = index( row, column, parent ); 00280 else 00281 idx = parent; 00282 00283 if ( !idx.isValid() ) 00284 return false; 00285 00286 const Collection parentCol = d->collections.value( idx.internalId() ); 00287 if (!parentCol.isValid()) 00288 return false; 00289 00290 KJob *job = PasteHelper::paste( data, parentCol, action != Qt::MoveAction ); 00291 connect( job, SIGNAL(result(KJob*)), SLOT(dropResult(KJob*)) ); 00292 return true; 00293 } 00294 00295 Collection CollectionModel::collectionForId(Collection::Id id) const 00296 { 00297 Q_D( const CollectionModel ); 00298 return d->collections.value( id ); 00299 } 00300 00301 void CollectionModel::fetchCollectionStatistics(bool enable) 00302 { 00303 Q_D( CollectionModel ); 00304 d->fetchStatistics = enable; 00305 d->monitor->fetchCollectionStatistics( enable ); 00306 } 00307 00308 void CollectionModel::includeUnsubscribed(bool include) 00309 { 00310 Q_D( CollectionModel ); 00311 d->unsubscribed = include; 00312 } 00313 00314 00315 00316 #include "collectionmodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue May 8 2012 00:00:40 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue May 8 2012 00:00:40 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.