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>
47class CollectionView::Private
58 void itemClicked(
const QModelIndex &);
59 void itemCurrentChanged(
const QModelIndex &);
63 QModelIndex dragOverIndex;
64 QTimer dragExpandTimer;
66 KXMLGUIClient *xmlGuiClient;
69void 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)));
91bool CollectionView::Private::hasParent(
const QModelIndex &idx,
Collection::Id parentId)
93 QModelIndex idx2 = idx;
94 while (idx2.isValid()) {
104void CollectionView::Private::dragExpand()
106 mParent->setExpanded(dragOverIndex,
true);
107 dragOverIndex = QModelIndex();
110void CollectionView::Private::itemClicked(
const QModelIndex &index)
112 if (!index.isValid()) {
117 if (!collection.isValid()) {
121 emit mParent->clicked(collection);
124void CollectionView::Private::itemCurrentChanged(
const QModelIndex &index)
126 if (!index.isValid()) {
131 if (!collection.isValid()) {
135 emit mParent->currentChanged(collection);
140 , d(new Private(this))
147 , d(new Private(this))
149 d->xmlGuiClient = xmlGuiClient;
155 , d(new Private(this))
157 d->xmlGuiClient =
static_cast<KXMLGUIClient *
>(xmlGuiWindow);
166void CollectionView::setModel(QAbstractItemModel *model)
168 QTreeView::setModel(model);
169 header()->setStretchLastSection(
true);
171 connect(selectionModel(), SIGNAL(
currentChanged(QModelIndex,QModelIndex)),
172 this, SLOT(itemCurrentChanged(QModelIndex)));
175void 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) {
193 if (collection.isValid()) {
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);
216void CollectionView::dragLeaveEvent(QDragLeaveEvent *event)
218 d->dragExpandTimer.stop();
219 d->dragOverIndex = QModelIndex();
220 QTreeView::dragLeaveEvent(event);
223void 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);
248void 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"
@ CollectionIdRole
The collection identifier.
@ CollectionRole
The actual collection object.
A view to show a collection tree provided by a CollectionModel.
AKONADI_DEPRECATED void setXmlGuiWindow(KXmlGuiWindow *xmlGuiWindow)
Sets the KXmlGuiWindow which the view is used in.
void clicked(const Akonadi::Collection &collection)
This signal is emitted whenever the user has clicked a collection in the view.
CollectionView(QWidget *parent=0)
Creates a new collection view.
void setXmlGuiClient(KXMLGUIClient *xmlGuiClient)
Sets the KXMLGUIClient which the view is used in.
virtual ~CollectionView()
Destroys the collection view.
void currentChanged(const Akonadi::Collection &collection)
This signal is emitted whenever the current collection in the view has changed.
Represents a collection of PIM items.
static Collection fromUrl(const KUrl &url)
Creates a collection from the given url.
static void widgetNeedsAkonadi(QWidget *widget)
Disable the given widget when Akonadi is not operational and show an error overlay (given enough spac...
qint64 Id
Describes the unique id type.
FreeBusyManager::Singleton.