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 <QtGui/QApplication>
37 #include <QtGui/QDragMoveEvent>
38 #include <QtGui/QHeaderView>
39 #include <QtGui/QMenu>
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() ) {
102 void CollectionView::Private::dragExpand()
104 mParent->setExpanded( dragOverIndex,
true );
105 dragOverIndex = QModelIndex();
108 void CollectionView::Private::itemClicked(
const QModelIndex &index )
110 if ( !index.isValid() )
117 emit mParent->clicked( collection );
120 void CollectionView::Private::itemCurrentChanged(
const QModelIndex &index )
122 if ( !index.isValid() )
129 emit mParent->currentChanged( collection );
133 : QTreeView( parent ),
134 d( new Private( this ) )
140 : QTreeView( parent ),
141 d( new Private( this ) )
143 d->xmlGuiClient = xmlGuiClient;
148 : QTreeView( parent ),
149 d( new Private( this ) )
151 d->xmlGuiClient =
static_cast<KXMLGUIClient*
>( xmlGuiWindow );
160 void CollectionView::setModel( QAbstractItemModel * model )
162 QTreeView::setModel( model );
163 header()->setStretchLastSection(
true );
165 connect( selectionModel(), SIGNAL(
currentChanged(QModelIndex,QModelIndex)),
166 this, SLOT(itemCurrentChanged(QModelIndex)) );
169 void CollectionView::dragMoveEvent( QDragMoveEvent * event )
171 QModelIndex index = indexAt( event->pos() );
172 if ( d->dragOverIndex != index ) {
173 d->dragExpandTimer.stop();
174 if ( index.isValid() && !isExpanded( index ) && itemsExpandable() ) {
175 d->dragExpandTimer.start( QApplication::startDragTime() );
176 d->dragOverIndex = index;
182 const QMimeData *mimeData =
event->mimeData();
183 const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
184 foreach (
const KUrl &url, urls ) {
188 if ( !supportedContentTypes.contains( QString::fromLatin1(
"inode/directory" ) ) )
192 if ( d->hasParent( index, collection.
id() ) )
195 const QString type = url.queryItems()[ QString::fromLatin1(
"type" ) ];
196 if ( !supportedContentTypes.contains( type ) )
200 QTreeView::dragMoveEvent( event );
204 event->setDropAction( Qt::IgnoreAction );
207 void CollectionView::dragLeaveEvent( QDragLeaveEvent * event )
209 d->dragExpandTimer.stop();
210 d->dragOverIndex = QModelIndex();
211 QTreeView::dragLeaveEvent( event );
214 void CollectionView::dropEvent( QDropEvent * event )
216 d->dragExpandTimer.stop();
217 d->dragOverIndex = QModelIndex();
222 QAction* moveDropAction = popup.addAction( KIcon( QString::fromLatin1(
"edit-rename" ) ), i18n(
"&Move here" ) );
223 QAction* copyDropAction = popup.addAction( KIcon( QString::fromLatin1(
"edit-copy" ) ), i18n(
"&Copy here" ) );
224 popup.addSeparator();
225 popup.addAction( KIcon( QString::fromLatin1(
"process-stop" ) ), i18n(
"Cancel" ) );
227 QAction *activatedAction = popup.exec( QCursor::pos() );
228 if ( activatedAction == moveDropAction ) {
229 event->setDropAction( Qt::MoveAction );
230 }
else if ( activatedAction == copyDropAction ) {
231 event->setDropAction( Qt::CopyAction );
236 QTreeView::dropEvent( event );
239 void CollectionView::contextMenuEvent( QContextMenuEvent * event )
241 if ( !d->xmlGuiClient )
243 QMenu *popup =
static_cast<QMenu*
>( d->xmlGuiClient->factory()->container(
244 QLatin1String(
"akonadi_collectionview_contextmenu" ), d->xmlGuiClient ) );
246 popup->exec( event->globalPos() );
251 d->xmlGuiClient = xmlGuiClient;
256 d->xmlGuiClient =
static_cast<KXMLGUIClient*
>( xmlGuiWindow );
259 #include "collectionview.moc"