22 #include "entitylistview.h"
24 #include "dragdropmanager_p.h"
25 #include "favoritecollectionsmodel.h"
27 #include <QtCore/QDebug>
28 #include <QtCore/QTimer>
29 #include <QtGui/QApplication>
30 #include <QtGui/QDragMoveEvent>
31 #include <QtGui/QHeaderView>
32 #include <QtGui/QMenu>
36 #include <KMessageBox>
38 #include <KXMLGUIFactory>
41 #include <kxmlguiclient.h>
43 #include <akonadi/collection.h>
44 #include <akonadi/control.h>
45 #include <akonadi/item.h>
46 #include <akonadi/entitytreemodel.h>
48 #include <progressspinnerdelegate_p.h>
50 using namespace Akonadi;
55 class EntityListView::Private
58 Private( EntityListView *parent )
60 #ifndef QT_NO_DRAGANDDROP
61 , mDragDropManager( new DragDropManager( mParent ) )
68 void itemClicked(
const QModelIndex& );
69 void itemDoubleClicked(
const QModelIndex& );
70 void itemCurrentChanged(
const QModelIndex& );
72 EntityListView *mParent;
73 DragDropManager *mDragDropManager;
74 KXMLGUIClient *mXmlGuiClient;
77 void EntityListView::Private::init()
79 mParent->setEditTriggers( QAbstractItemView::EditKeyPressed );
80 mParent->setAcceptDrops(
true );
81 #ifndef QT_NO_DRAGANDDROP
82 mParent->setDropIndicatorShown(
true );
83 mParent->setDragDropMode( DragDrop );
84 mParent->setDragEnabled(
true );
86 mParent->connect( mParent, SIGNAL(clicked(QModelIndex)),
87 mParent, SLOT(itemClicked(QModelIndex)) );
88 mParent->connect( mParent, SIGNAL(doubleClicked(QModelIndex)),
89 mParent, SLOT(itemDoubleClicked(QModelIndex)) );
91 DelegateAnimator *animator =
new DelegateAnimator(mParent);
92 ProgressSpinnerDelegate *customDelegate =
new ProgressSpinnerDelegate(animator, mParent);
93 mParent->setItemDelegate(customDelegate);
95 Control::widgetNeedsAkonadi( mParent );
98 void EntityListView::Private::itemClicked(
const QModelIndex &index )
100 if ( !index.isValid() )
103 const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
104 if ( collection.isValid() ) {
105 emit mParent->clicked( collection );
107 const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
108 if ( item.isValid() )
109 emit mParent->clicked( item );
113 void EntityListView::Private::itemDoubleClicked(
const QModelIndex &index )
115 if ( !index.isValid() )
118 const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
119 if ( collection.isValid() ) {
120 emit mParent->doubleClicked( collection );
122 const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
123 if ( item.isValid() )
124 emit mParent->doubleClicked( item );
128 void EntityListView::Private::itemCurrentChanged(
const QModelIndex &index )
130 if ( !index.isValid() )
133 const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
134 if ( collection.isValid() ) {
135 emit mParent->currentChanged( collection );
137 const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
138 if ( item.isValid() )
139 emit mParent->currentChanged( item );
143 EntityListView::EntityListView( QWidget * parent )
144 : QListView( parent ),
145 d( new Private( this ) )
147 setSelectionMode( QAbstractItemView::SingleSelection );
151 EntityListView::EntityListView( KXMLGUIClient *xmlGuiClient, QWidget * parent )
152 : QListView( parent ),
153 d( new Private( this ) )
155 d->mXmlGuiClient = xmlGuiClient;
159 EntityListView::~EntityListView()
161 delete d->mDragDropManager;
165 void EntityListView::setModel( QAbstractItemModel * model )
167 if ( selectionModel() ) {
168 disconnect( selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
169 this, SLOT(itemCurrentChanged(QModelIndex)) );
172 QListView::setModel( model );
174 connect( selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
175 SLOT(itemCurrentChanged(QModelIndex)) );
178 #ifndef QT_NO_DRAGANDDROP
179 void EntityListView::dragMoveEvent( QDragMoveEvent * event )
181 if ( d->mDragDropManager->dropAllowed( event ) || qobject_cast<Akonadi::FavoriteCollectionsModel*>( model() ) ) {
183 QListView::dragMoveEvent( event );
187 event->setDropAction( Qt::IgnoreAction );
190 void EntityListView::dropEvent( QDropEvent * event )
192 bool menuCanceled =
false;
193 if ( d->mDragDropManager->processDropEvent( event, menuCanceled ) && !menuCanceled) {
196 QListView::dropEvent( event );
198 else if ( qobject_cast<Akonadi::FavoriteCollectionsModel*>( model() ) &&!menuCanceled )
200 QListView::dropEvent( event );
205 #ifndef QT_NO_CONTEXTMENU
206 void EntityListView::contextMenuEvent( QContextMenuEvent * event )
208 if ( !d->mXmlGuiClient )
211 const QModelIndex index = indexAt( event->pos() );
216 const Collection collection = model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
217 if ( collection.isValid() ) {
218 popup =
static_cast<QMenu*
>( d->mXmlGuiClient->factory()->container(
219 QLatin1String(
"akonadi_favoriteview_contextmenu" ), d->mXmlGuiClient ) );
221 popup =
static_cast<QMenu*
>( d->mXmlGuiClient->factory()->container(
222 QLatin1String(
"akonadi_favoriteview_emptyselection_contextmenu" ), d->mXmlGuiClient) );
226 popup->exec( event->globalPos() );
230 void EntityListView::setXmlGuiClient( KXMLGUIClient *xmlGuiClient )
232 d->mXmlGuiClient = xmlGuiClient;
235 #ifndef QT_NO_DRAGANDDROP
236 void EntityListView::startDrag( Qt::DropActions supportedActions )
238 d->mDragDropManager->startDrag( supportedActions );
242 void EntityListView::setDropActionMenuEnabled(
bool enabled )
244 #ifndef QT_NO_DRAGANDDROP
245 d->mDragDropManager->setShowDropActionMenu( enabled );
249 bool EntityListView::isDropActionMenuEnabled()
const
251 #ifndef QT_NO_DRAGANDDROP
252 return d->mDragDropManager->showDropActionMenu();
258 #include "entitylistview.moc"