20 #include "collectionview.h"
22 #include "collection.h"
23 #include "collectionmodel.h"
29 #include <kmessagebox.h>
31 #include <kxmlguifactory.h>
32 #include <kxmlguiwindow.h>
34 #include <QtCore/QDebug>
35 #include <QtCore/QTimer>
36 #include <QApplication>
37 #include <QDragMoveEvent>
38 #include <QHeaderView>
41 using namespace Akonadi;
46 class CollectionView::Private
57 void itemClicked(
const QModelIndex& );
58 void itemCurrentChanged(
const QModelIndex& );
62 QModelIndex dragOverIndex;
63 QTimer dragExpandTimer;
65 KXMLGUIClient *xmlGuiClient;
68 void CollectionView::Private::init()
70 mParent->header()->setClickable(
true );
71 mParent->header()->setStretchLastSection(
false );
73 mParent->setSortingEnabled(
true );
74 mParent->sortByColumn( 0, Qt::AscendingOrder );
75 mParent->setEditTriggers( QAbstractItemView::EditKeyPressed );
76 mParent->setAcceptDrops(
true );
77 mParent->setDropIndicatorShown(
true );
78 mParent->setDragDropMode( DragDrop );
79 mParent->setDragEnabled(
true );
81 dragExpandTimer.setSingleShot(
true );
82 mParent->connect( &dragExpandTimer, SIGNAL(timeout()), SLOT(dragExpand()) );
84 mParent->connect( mParent, SIGNAL(clicked(QModelIndex)),
85 mParent, SLOT(itemClicked(QModelIndex)) );
90 bool CollectionView::Private::hasParent(
const QModelIndex& idx,
Collection::Id parentId )
92 QModelIndex idx2 = idx;
93 while ( idx2.isValid() ) {
103 void CollectionView::Private::dragExpand()
105 mParent->setExpanded( dragOverIndex,
true );
106 dragOverIndex = QModelIndex();
109 void CollectionView::Private::itemClicked(
const QModelIndex &index )
111 if ( !index.isValid() ) {
120 emit mParent->clicked( collection );
123 void CollectionView::Private::itemCurrentChanged(
const QModelIndex &index )
125 if ( !index.isValid() ) {
134 emit mParent->currentChanged( collection );
138 : QTreeView( parent ),
139 d( new Private( this ) )
145 : QTreeView( parent ),
146 d( new Private( this ) )
148 d->xmlGuiClient = xmlGuiClient;
153 : QTreeView( parent ),
154 d( new Private( this ) )
156 d->xmlGuiClient =
static_cast<KXMLGUIClient*
>( xmlGuiWindow );
165 void CollectionView::setModel( QAbstractItemModel * model )
167 QTreeView::setModel( model );
168 header()->setStretchLastSection(
true );
170 connect( selectionModel(), SIGNAL(
currentChanged(QModelIndex,QModelIndex)),
171 this, SLOT(itemCurrentChanged(QModelIndex)) );
174 void CollectionView::dragMoveEvent( QDragMoveEvent * event )
176 QModelIndex index = indexAt( event->pos() );
177 if ( d->dragOverIndex != index ) {
178 d->dragExpandTimer.stop();
179 if ( index.isValid() && !isExpanded( index ) && itemsExpandable() ) {
180 d->dragExpandTimer.start( QApplication::startDragTime() );
181 d->dragOverIndex = index;
187 const QMimeData *mimeData =
event->mimeData();
188 const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
189 foreach (
const KUrl &url, urls ) {
193 if ( !supportedContentTypes.contains( QString::fromLatin1(
"inode/directory" ) ) ) {
198 if ( d->hasParent( index, collection.
id() ) ) {
202 const QString type = url.queryItems()[ QString::fromLatin1(
"type" ) ];
203 if ( !supportedContentTypes.contains( type ) ) {
208 QTreeView::dragMoveEvent( event );
212 event->setDropAction( Qt::IgnoreAction );
215 void CollectionView::dragLeaveEvent( QDragLeaveEvent * event )
217 d->dragExpandTimer.stop();
218 d->dragOverIndex = QModelIndex();
219 QTreeView::dragLeaveEvent( event );
222 void CollectionView::dropEvent( QDropEvent * event )
224 d->dragExpandTimer.stop();
225 d->dragOverIndex = QModelIndex();
230 QAction* moveDropAction = popup.addAction( KIcon( QString::fromLatin1(
"edit-rename" ) ), i18n(
"&Move here" ) );
231 QAction* copyDropAction = popup.addAction( KIcon( QString::fromLatin1(
"edit-copy" ) ), i18n(
"&Copy here" ) );
232 popup.addSeparator();
233 popup.addAction( KIcon( QString::fromLatin1(
"process-stop" ) ), i18n(
"Cancel" ) );
235 QAction *activatedAction = popup.exec( QCursor::pos() );
236 if ( activatedAction == moveDropAction ) {
237 event->setDropAction( Qt::MoveAction );
238 }
else if ( activatedAction == copyDropAction ) {
239 event->setDropAction( Qt::CopyAction );
244 QTreeView::dropEvent( event );
247 void CollectionView::contextMenuEvent( QContextMenuEvent * event )
249 if ( !d->xmlGuiClient ) {
252 QMenu *popup =
static_cast<QMenu*
>( d->xmlGuiClient->factory()->container(
253 QLatin1String(
"akonadi_collectionview_contextmenu" ), d->xmlGuiClient ) );
255 popup->exec( event->globalPos() );
261 d->xmlGuiClient = xmlGuiClient;
266 d->xmlGuiClient =
static_cast<KXMLGUIClient*
>( xmlGuiWindow );
269 #include "moc_collectionview.cpp"