akonadi
entitylistview.cpp
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 using namespace Akonadi;
00048
00052 class EntityListView::Private
00053 {
00054 public:
00055 Private( EntityListView *parent )
00056 : mParent( parent ), mDragDropManager( new DragDropManager( mParent ) ), mXmlGuiClient( 0 )
00057 {
00058 }
00059
00060 void init();
00061 void itemClicked( const QModelIndex& );
00062 void itemDoubleClicked( const QModelIndex& );
00063 void itemCurrentChanged( const QModelIndex& );
00064
00065 EntityListView *mParent;
00066 DragDropManager *mDragDropManager;
00067 KXMLGUIClient *mXmlGuiClient;
00068 };
00069
00070 void EntityListView::Private::init()
00071 {
00072 mParent->setEditTriggers( QAbstractItemView::EditKeyPressed );
00073 mParent->setAcceptDrops( true );
00074 mParent->setDropIndicatorShown( true );
00075 mParent->setDragDropMode( DragDrop );
00076 mParent->setDragEnabled( true );
00077
00078 mParent->connect( mParent, SIGNAL( clicked( const QModelIndex& ) ),
00079 mParent, SLOT( itemClicked( const QModelIndex& ) ) );
00080 mParent->connect( mParent, SIGNAL( doubleClicked( const QModelIndex& ) ),
00081 mParent, SLOT( itemDoubleClicked( const QModelIndex& ) ) );
00082
00083 Control::widgetNeedsAkonadi( mParent );
00084 }
00085
00086 void EntityListView::Private::itemClicked( const QModelIndex &index )
00087 {
00088 if ( !index.isValid() )
00089 return;
00090
00091 const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00092 if ( collection.isValid() ) {
00093 emit mParent->clicked( collection );
00094 } else {
00095 const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
00096 if ( item.isValid() )
00097 emit mParent->clicked( item );
00098 }
00099 }
00100
00101 void EntityListView::Private::itemDoubleClicked( const QModelIndex &index )
00102 {
00103 if ( !index.isValid() )
00104 return;
00105
00106 const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00107 if ( collection.isValid() ) {
00108 emit mParent->doubleClicked( collection );
00109 } else {
00110 const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
00111 if ( item.isValid() )
00112 emit mParent->doubleClicked( item );
00113 }
00114 }
00115
00116 void EntityListView::Private::itemCurrentChanged( const QModelIndex &index )
00117 {
00118 if ( !index.isValid() )
00119 return;
00120
00121 const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00122 if ( collection.isValid() ) {
00123 emit mParent->currentChanged( collection );
00124 } else {
00125 const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
00126 if ( item.isValid() )
00127 emit mParent->currentChanged( item );
00128 }
00129 }
00130
00131 EntityListView::EntityListView( QWidget * parent )
00132 : QListView( parent ),
00133 d( new Private( this ) )
00134 {
00135 setSelectionMode( QAbstractItemView::SingleSelection );
00136 d->init();
00137 }
00138
00139 EntityListView::EntityListView( KXMLGUIClient *xmlGuiClient, QWidget * parent )
00140 : QListView( parent ),
00141 d( new Private( this ) )
00142 {
00143 d->mXmlGuiClient = xmlGuiClient;
00144 d->init();
00145 }
00146
00147 EntityListView::~EntityListView()
00148 {
00149 delete d->mDragDropManager;
00150 delete d;
00151 }
00152
00153 void EntityListView::setModel( QAbstractItemModel * model )
00154 {
00155 if ( selectionModel() ) {
00156 disconnect( selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00157 this, SLOT( itemCurrentChanged( const QModelIndex& ) ) );
00158 }
00159
00160 QListView::setModel( model );
00161
00162 connect( selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00163 SLOT( itemCurrentChanged( const QModelIndex& ) ) );
00164 }
00165
00166 void EntityListView::dragMoveEvent( QDragMoveEvent * event )
00167 {
00168 if ( d->mDragDropManager->dropAllowed( event ) ) {
00169
00170 QListView::dragMoveEvent( event );
00171 return;
00172 }
00173
00174 event->setDropAction( Qt::IgnoreAction );
00175 }
00176
00177 void EntityListView::dropEvent( QDropEvent * event )
00178 {
00179 if ( d->mDragDropManager->processDropEvent( event ) ) {
00180 QListView::dropEvent( event );
00181 }
00182 }
00183
00184 void EntityListView::contextMenuEvent( QContextMenuEvent * event )
00185 {
00186 if ( !d->mXmlGuiClient )
00187 return;
00188
00189 const QModelIndex index = indexAt( event->pos() );
00190
00191 QMenu *popup = 0;
00192
00193
00194 const Collection collection = model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00195 if ( collection.isValid() ) {
00196 popup = static_cast<QMenu*>( d->mXmlGuiClient->factory()->container(
00197 QLatin1String( "akonadi_favoriteview_contextmenu" ), d->mXmlGuiClient ) );
00198 if ( popup )
00199 popup->exec( event->globalPos() );
00200 }
00201 }
00202
00203 void EntityListView::setXmlGuiClient( KXMLGUIClient *xmlGuiClient )
00204 {
00205 d->mXmlGuiClient = xmlGuiClient;
00206 }
00207
00208 void EntityListView::startDrag( Qt::DropActions supportedActions )
00209 {
00210 d->mDragDropManager->startDrag( supportedActions );
00211 }
00212
00213 #include "entitylistview.moc"