00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "entitylistview.h"
00023
00024 #include "dragdropmanager_p.h"
00025
00026 #include <QtCore/QDebug>
00027 #include <QtCore/QTimer>
00028 #include <QtGui/QApplication>
00029 #include <QtGui/QDragMoveEvent>
00030 #include <QtGui/QHeaderView>
00031 #include <QtGui/QMenu>
00032
00033 #include <KAction>
00034 #include <KLocale>
00035 #include <KMessageBox>
00036 #include <KUrl>
00037 #include <KXMLGUIFactory>
00038
00039 #include <kdebug.h>
00040 #include <kxmlguiclient.h>
00041
00042 #include <akonadi/collection.h>
00043 #include <akonadi/control.h>
00044 #include <akonadi/item.h>
00045 #include <akonadi/entitytreemodel.h>
00046
00047 #include <progressspinnerdelegate_p.h>
00048
00049 using namespace Akonadi;
00050
00054 class EntityListView::Private
00055 {
00056 public:
00057 Private( EntityListView *parent )
00058 : mParent( parent ), mDragDropManager( new DragDropManager( mParent ) ), mXmlGuiClient( 0 )
00059 {
00060 }
00061
00062 void init();
00063 void itemClicked( const QModelIndex& );
00064 void itemDoubleClicked( const QModelIndex& );
00065 void itemCurrentChanged( const QModelIndex& );
00066
00067 EntityListView *mParent;
00068 DragDropManager *mDragDropManager;
00069 KXMLGUIClient *mXmlGuiClient;
00070 };
00071
00072 void EntityListView::Private::init()
00073 {
00074 mParent->setEditTriggers( QAbstractItemView::EditKeyPressed );
00075 mParent->setAcceptDrops( true );
00076 mParent->setDropIndicatorShown( true );
00077 mParent->setDragDropMode( DragDrop );
00078 mParent->setDragEnabled( true );
00079
00080 mParent->connect( mParent, SIGNAL( clicked( const QModelIndex& ) ),
00081 mParent, SLOT( itemClicked( const QModelIndex& ) ) );
00082 mParent->connect( mParent, SIGNAL( doubleClicked( const QModelIndex& ) ),
00083 mParent, SLOT( itemDoubleClicked( const QModelIndex& ) ) );
00084
00085 DelegateAnimator *animator = new DelegateAnimator(mParent);
00086 ProgressSpinnerDelegate *customDelegate = new ProgressSpinnerDelegate(animator, mParent);
00087 mParent->setItemDelegate(customDelegate);
00088
00089 Control::widgetNeedsAkonadi( mParent );
00090 }
00091
00092 void EntityListView::Private::itemClicked( const QModelIndex &index )
00093 {
00094 if ( !index.isValid() )
00095 return;
00096
00097 const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00098 if ( collection.isValid() ) {
00099 emit mParent->clicked( collection );
00100 } else {
00101 const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
00102 if ( item.isValid() )
00103 emit mParent->clicked( item );
00104 }
00105 }
00106
00107 void EntityListView::Private::itemDoubleClicked( const QModelIndex &index )
00108 {
00109 if ( !index.isValid() )
00110 return;
00111
00112 const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00113 if ( collection.isValid() ) {
00114 emit mParent->doubleClicked( collection );
00115 } else {
00116 const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
00117 if ( item.isValid() )
00118 emit mParent->doubleClicked( item );
00119 }
00120 }
00121
00122 void EntityListView::Private::itemCurrentChanged( const QModelIndex &index )
00123 {
00124 if ( !index.isValid() )
00125 return;
00126
00127 const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00128 if ( collection.isValid() ) {
00129 emit mParent->currentChanged( collection );
00130 } else {
00131 const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
00132 if ( item.isValid() )
00133 emit mParent->currentChanged( item );
00134 }
00135 }
00136
00137 EntityListView::EntityListView( QWidget * parent )
00138 : QListView( parent ),
00139 d( new Private( this ) )
00140 {
00141 setSelectionMode( QAbstractItemView::SingleSelection );
00142 d->init();
00143 }
00144
00145 EntityListView::EntityListView( KXMLGUIClient *xmlGuiClient, QWidget * parent )
00146 : QListView( parent ),
00147 d( new Private( this ) )
00148 {
00149 d->mXmlGuiClient = xmlGuiClient;
00150 d->init();
00151 }
00152
00153 EntityListView::~EntityListView()
00154 {
00155 delete d->mDragDropManager;
00156 delete d;
00157 }
00158
00159 void EntityListView::setModel( QAbstractItemModel * model )
00160 {
00161 if ( selectionModel() ) {
00162 disconnect( selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00163 this, SLOT( itemCurrentChanged( const QModelIndex& ) ) );
00164 }
00165
00166 QListView::setModel( model );
00167
00168 connect( selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00169 SLOT( itemCurrentChanged( const QModelIndex& ) ) );
00170 }
00171
00172 void EntityListView::dragMoveEvent( QDragMoveEvent * event )
00173 {
00174 if ( d->mDragDropManager->dropAllowed( event ) ) {
00175
00176 QListView::dragMoveEvent( event );
00177 return;
00178 }
00179
00180 event->setDropAction( Qt::IgnoreAction );
00181 }
00182
00183 void EntityListView::dropEvent( QDropEvent * event )
00184 {
00185 if ( d->mDragDropManager->processDropEvent( event ) ) {
00186 QListView::dropEvent( event );
00187 }
00188 }
00189
00190 void EntityListView::contextMenuEvent( QContextMenuEvent * event )
00191 {
00192 if ( !d->mXmlGuiClient )
00193 return;
00194
00195 const QModelIndex index = indexAt( event->pos() );
00196
00197 QMenu *popup = 0;
00198
00199
00200 const Collection collection = model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00201 if ( collection.isValid() ) {
00202 popup = static_cast<QMenu*>( d->mXmlGuiClient->factory()->container(
00203 QLatin1String( "akonadi_favoriteview_contextmenu" ), d->mXmlGuiClient ) );
00204 } else {
00205 popup = static_cast<QMenu*>( d->mXmlGuiClient->factory()->container(
00206 QLatin1String( "akonadi_favoriteview_emptyselection_contextmenu" ), d->mXmlGuiClient) );
00207 }
00208
00209 if ( popup )
00210 popup->exec( event->globalPos() );
00211 }
00212
00213 void EntityListView::setXmlGuiClient( KXMLGUIClient *xmlGuiClient )
00214 {
00215 d->mXmlGuiClient = xmlGuiClient;
00216 }
00217
00218 void EntityListView::startDrag( Qt::DropActions supportedActions )
00219 {
00220 d->mDragDropManager->startDrag( supportedActions );
00221 }
00222
00223 #include "entitylistview.moc"