Akonadi

entitylistview.cpp
1/*
2 SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
3 SPDX-FileCopyrightText: 2008 Stephen Kelly <steveire@gmail.com>
4 SPDX-FileCopyrightText: 2009 Kevin Ottens <ervin@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "entitylistview.h"
10
11#include "dragdropmanager_p.h"
12
13#include <QDragMoveEvent>
14#include <QMenu>
15
16#include "akonadiwidgets_debug.h"
17#include <KXMLGUIClient>
18#include <KXMLGUIFactory>
19
20#include "collection.h"
21#include "controlgui.h"
22#include "entitytreemodel.h"
23#include "item.h"
24#include "progressspinnerdelegate_p.h"
25
26using namespace Akonadi;
27
28/**
29 * @internal
30 */
31class Akonadi::EntityListViewPrivate
32{
33public:
34 explicit EntityListViewPrivate(EntityListView *parent)
35 : mParent(parent)
36#ifndef QT_NO_DRAGANDDROP
37 , mDragDropManager(new DragDropManager(mParent))
38#endif
39 {
40 }
41
42 void init();
43 void itemClicked(const QModelIndex &index) const;
44 void itemDoubleClicked(const QModelIndex &index) const;
45 void itemCurrentChanged(const QModelIndex &index) const;
46
47 EntityListView *const mParent;
48 DragDropManager *mDragDropManager = nullptr;
49 KXMLGUIClient *mXmlGuiClient = nullptr;
50};
51
52void EntityListViewPrivate::init()
53{
55 mParent->setAcceptDrops(true);
56#ifndef QT_NO_DRAGANDDROP
57 mParent->setDropIndicatorShown(true);
59 mParent->setDragEnabled(true);
60#endif
61 mParent->connect(mParent, &QAbstractItemView::clicked, mParent, [this](const auto &index) {
62 itemClicked(index);
63 });
64 mParent->connect(mParent, &QAbstractItemView::doubleClicked, mParent, [this](const auto &index) {
65 itemDoubleClicked(index);
66 });
67
68 auto animator = new DelegateAnimator(mParent);
69 auto customDelegate = new ProgressSpinnerDelegate(animator, mParent);
70 mParent->setItemDelegate(customDelegate);
71
73}
74
75void EntityListViewPrivate::itemClicked(const QModelIndex &index) const
76{
77 if (!index.isValid()) {
78 return;
79 }
80
81 const auto collection = index.model()->data(index, EntityTreeModel::CollectionRole).value<Collection>();
82 if (collection.isValid()) {
83 Q_EMIT mParent->clicked(collection);
84 } else {
85 const Item item = index.model()->data(index, EntityTreeModel::ItemRole).value<Item>();
86 if (item.isValid()) {
87 Q_EMIT mParent->clicked(item);
88 }
89 }
90}
91
92void EntityListViewPrivate::itemDoubleClicked(const QModelIndex &index) const
93{
94 if (!index.isValid()) {
95 return;
96 }
97
98 const auto collection = index.model()->data(index, EntityTreeModel::CollectionRole).value<Collection>();
99 if (collection.isValid()) {
100 Q_EMIT mParent->doubleClicked(collection);
101 } else {
102 const Item item = index.model()->data(index, EntityTreeModel::ItemRole).value<Item>();
103 if (item.isValid()) {
104 Q_EMIT mParent->doubleClicked(item);
105 }
106 }
107}
108
109void EntityListViewPrivate::itemCurrentChanged(const QModelIndex &index) const
110{
111 if (!index.isValid()) {
112 return;
113 }
114
115 const auto collection = index.model()->data(index, EntityTreeModel::CollectionRole).value<Collection>();
116 if (collection.isValid()) {
117 Q_EMIT mParent->currentChanged(collection);
118 } else {
119 const Item item = index.model()->data(index, EntityTreeModel::ItemRole).value<Item>();
120 if (item.isValid()) {
121 Q_EMIT mParent->currentChanged(item);
122 }
123 }
124}
125
127 : QListView(parent)
128 , d(new EntityListViewPrivate(this))
129{
131 d->init();
132}
133
135 : QListView(parent)
136 , d(new EntityListViewPrivate(this))
137{
138 d->mXmlGuiClient = xmlGuiClient;
139 d->init();
140}
141
143{
144 delete d->mDragDropManager;
145}
146
148{
149 if (selectionModel()) {
151 }
152
154
156 d->itemCurrentChanged(index);
157 });
158}
159
160#ifndef QT_NO_DRAGANDDROP
161void EntityListView::dragMoveEvent(QDragMoveEvent *event)
162{
163 if (d->mDragDropManager->dropAllowed(event)) {
164 // All urls are supported. process the event.
166 return;
167 }
168
169 event->setDropAction(Qt::IgnoreAction);
170}
171
172void EntityListView::dropEvent(QDropEvent *event)
173{
174 bool menuCanceled = false;
175 if (d->mDragDropManager->processDropEvent(event, menuCanceled) && !menuCanceled) {
177 }
178}
179#endif
180
181#ifndef QT_NO_CONTEXTMENU
182void EntityListView::contextMenuEvent(QContextMenuEvent *event)
183{
184 if (!d->mXmlGuiClient) {
185 return;
186 }
187
188 const QModelIndex index = indexAt(event->pos());
189
190 QMenu *popup = nullptr;
191
192 // check if the index under the cursor is a collection or item
193 const auto collection = model()->data(index, EntityTreeModel::CollectionRole).value<Collection>();
194 if (collection.isValid()) {
195 popup = static_cast<QMenu *>(d->mXmlGuiClient->factory()->container(QStringLiteral("akonadi_favoriteview_contextmenu"), d->mXmlGuiClient));
196 } else {
197 popup =
198 static_cast<QMenu *>(d->mXmlGuiClient->factory()->container(QStringLiteral("akonadi_favoriteview_emptyselection_contextmenu"), d->mXmlGuiClient));
199 }
200
201 if (popup) {
202 popup->exec(event->globalPos());
203 }
204}
205#endif
206
208{
209 d->mXmlGuiClient = xmlGuiClient;
210}
211
213{
214 return d->mXmlGuiClient;
215}
216
217#ifndef QT_NO_DRAGANDDROP
218void EntityListView::startDrag(Qt::DropActions supportedActions)
219{
220 d->mDragDropManager->startDrag(supportedActions);
221}
222#endif
223
225{
226#ifndef QT_NO_DRAGANDDROP
227 d->mDragDropManager->setShowDropActionMenu(enabled);
228#endif
229}
230
232{
233#ifndef QT_NO_DRAGANDDROP
234 return d->mDragDropManager->showDropActionMenu();
235#else
236 return false;
237#endif
238}
239
240#include "moc_entitylistview.cpp"
Represents a collection of PIM items.
Definition collection.h:62
static void widgetNeedsAkonadi(QWidget *widget)
Disable the given widget when Akonadi is not operational and show an error overlay (given enough spac...
A view to show an item/collection list provided by an EntityTreeModel.
void setDropActionMenuEnabled(bool enabled)
Sets whether the drop action menu is enabled and will be shown on drop operation.
void currentChanged(const Akonadi::Collection &collection)
This signal is emitted whenever the current collection in the view has changed.
void setXmlGuiClient(KXMLGUIClient *xmlGuiClient)
Sets the XML GUI client 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.
~EntityListView() override
Destroys the favorite collections view.
bool isDropActionMenuEnabled() const
Returns whether the drop action menu is enabled and will be shown on drop operation.
void setModel(QAbstractItemModel *model) override
void doubleClicked(const Akonadi::Collection &collection)
This signal is emitted whenever the user has double clicked a collection in the view.
KXMLGUIClient * xmlGuiClient() const
Return the XML GUI client which the view is used in.
EntityListView(QWidget *parent=nullptr)
Creates a new favorite collections view.
@ CollectionRole
The collection.
Helper integration between Akonadi and Qt.
virtual QVariant data(const QModelIndex &index, int role) const const=0
void clicked(const QModelIndex &index)
void doubleClicked(const QModelIndex &index)
void setDragDropMode(DragDropMode behavior)
void setDragEnabled(bool enable)
void setEditTriggers(EditTriggers triggers)
QAbstractItemModel * model() const const
void setSelectionMode(QAbstractItemView::SelectionMode mode)
QItemSelectionModel * selectionModel() const const
void setItemDelegate(QAbstractItemDelegate *delegate)
virtual void setModel(QAbstractItemModel *model)
void setDropIndicatorShown(bool enable)
void currentChanged(const QModelIndex &current, const QModelIndex &previous)
virtual void dragMoveEvent(QDragMoveEvent *e) override
virtual void dropEvent(QDropEvent *event) override
virtual bool event(QEvent *e) override
virtual QModelIndex indexAt(const QPoint &p) const const override
QAction * exec()
bool isValid() const const
const QAbstractItemModel * model() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
IgnoreAction
T value() const const
void setAcceptDrops(bool on)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.