20 #include "collectionview.h"
22 #include "collection.h"
23 #include "collectionmodel.h"
29 #include <klocalizedstring.h>
30 #include <kmessagebox.h>
32 #include <kxmlguifactory.h>
33 #include <kxmlguiwindow.h>
35 #include <QtCore/QDebug>
36 #include <QtCore/QTimer>
37 #include <QApplication>
38 #include <QDragMoveEvent>
39 #include <QHeaderView>
42 using namespace Akonadi;
47 class CollectionView::Private
58 void itemClicked(
const QModelIndex& );
59 void itemCurrentChanged(
const QModelIndex& );
63 QModelIndex dragOverIndex;
64 QTimer dragExpandTimer;
66 KXMLGUIClient *xmlGuiClient;
69 void CollectionView::Private::init()
71 mParent->header()->setClickable(
true );
72 mParent->header()->setStretchLastSection(
false );
74 mParent->setSortingEnabled(
true );
75 mParent->sortByColumn( 0, Qt::AscendingOrder );
76 mParent->setEditTriggers( QAbstractItemView::EditKeyPressed );
77 mParent->setAcceptDrops(
true );
78 mParent->setDropIndicatorShown(
true );
79 mParent->setDragDropMode( DragDrop );
80 mParent->setDragEnabled(
true );
82 dragExpandTimer.setSingleShot(
true );
83 mParent->connect( &dragExpandTimer, SIGNAL(timeout()), SLOT(dragExpand()) );
85 mParent->connect( mParent, SIGNAL(clicked(QModelIndex)),
86 mParent, SLOT(itemClicked(QModelIndex)) );
91 bool CollectionView::Private::hasParent(
const QModelIndex& idx,
Collection::Id parentId )
93 QModelIndex idx2 = idx;
94 while ( idx2.isValid() ) {
104 void CollectionView::Private::dragExpand()
106 mParent->setExpanded( dragOverIndex,
true );
107 dragOverIndex = QModelIndex();
110 void CollectionView::Private::itemClicked(
const QModelIndex &index )
112 if ( !index.isValid() ) {
121 emit mParent->clicked( collection );
124 void CollectionView::Private::itemCurrentChanged(
const QModelIndex &index )
126 if ( !index.isValid() ) {
135 emit mParent->currentChanged( collection );
139 : QTreeView( parent ),
140 d( new Private( this ) )
146 : QTreeView( parent ),
147 d( new Private( this ) )
149 d->xmlGuiClient = xmlGuiClient;
154 : QTreeView( parent ),
155 d( new Private( this ) )
157 d->xmlGuiClient =
static_cast<KXMLGUIClient*
>( xmlGuiWindow );
166 void CollectionView::setModel( QAbstractItemModel * model )
168 QTreeView::setModel( model );
169 header()->setStretchLastSection(
true );
171 connect( selectionModel(), SIGNAL(
currentChanged(QModelIndex,QModelIndex)),
172 this, SLOT(itemCurrentChanged(QModelIndex)) );
175 void CollectionView::dragMoveEvent( QDragMoveEvent * event )
177 QModelIndex index = indexAt( event->pos() );
178 if ( d->dragOverIndex != index ) {
179 d->dragExpandTimer.stop();
180 if ( index.isValid() && !isExpanded( index ) && itemsExpandable() ) {
181 d->dragExpandTimer.start( QApplication::startDragTime() );
182 d->dragOverIndex = index;
188 const QMimeData *mimeData =
event->mimeData();
189 const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
190 foreach (
const KUrl &url, urls ) {
194 if ( !supportedContentTypes.contains( QString::fromLatin1(
"inode/directory" ) ) ) {
199 if ( d->hasParent( index, collection.
id() ) ) {
203 const QString type = url.queryItems()[ QString::fromLatin1(
"type" ) ];
204 if ( !supportedContentTypes.contains( type ) ) {
209 QTreeView::dragMoveEvent( event );
213 event->setDropAction( Qt::IgnoreAction );
216 void CollectionView::dragLeaveEvent( QDragLeaveEvent * event )
218 d->dragExpandTimer.stop();
219 d->dragOverIndex = QModelIndex();
220 QTreeView::dragLeaveEvent( event );
223 void CollectionView::dropEvent( QDropEvent * event )
225 d->dragExpandTimer.stop();
226 d->dragOverIndex = QModelIndex();
231 QAction* moveDropAction = popup.addAction( KIcon( QString::fromLatin1(
"edit-rename" ) ), i18n(
"&Move here" ) );
232 QAction* copyDropAction = popup.addAction( KIcon( QString::fromLatin1(
"edit-copy" ) ), i18n(
"&Copy here" ) );
233 popup.addSeparator();
234 popup.addAction( KIcon( QString::fromLatin1(
"process-stop" ) ), i18n(
"Cancel" ) );
236 QAction *activatedAction = popup.exec( QCursor::pos() );
237 if ( activatedAction == moveDropAction ) {
238 event->setDropAction( Qt::MoveAction );
239 }
else if ( activatedAction == copyDropAction ) {
240 event->setDropAction( Qt::CopyAction );
245 QTreeView::dropEvent( event );
248 void CollectionView::contextMenuEvent( QContextMenuEvent * event )
250 if ( !d->xmlGuiClient ) {
253 QMenu *popup =
static_cast<QMenu*
>( d->xmlGuiClient->factory()->container(
254 QLatin1String(
"akonadi_collectionview_contextmenu" ), d->xmlGuiClient ) );
256 popup->exec( event->globalPos() );
262 d->xmlGuiClient = xmlGuiClient;
267 d->xmlGuiClient =
static_cast<KXMLGUIClient*
>( xmlGuiWindow );
270 #include "moc_collectionview.cpp"