Akonadi

entitylistview.cpp
1 /*
2  SPDX-FileCopyrightText: 2006-2007 Volker Krause <[email protected]>
3  SPDX-FileCopyrightText: 2008 Stephen Kelly <[email protected]>
4  SPDX-FileCopyrightText: 2009 Kevin Ottens <[email protected]>
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 
26 using namespace Akonadi;
27 
28 /**
29  * @internal
30  */
31 class Akonadi::EntityListViewPrivate
32 {
33 public:
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 
52 void EntityListViewPrivate::init()
53 {
54  mParent->setEditTriggers(QAbstractItemView::EditKeyPressed);
55  mParent->setAcceptDrops(true);
56 #ifndef QT_NO_DRAGANDDROP
57  mParent->setDropIndicatorShown(true);
58  mParent->setDragDropMode(EntityListView::DragDrop);
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 
75 void 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 
92 void 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 
109 void 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
161 void 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 
172 void 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
182 void 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
218 void 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"
virtual bool event(QEvent *e) override
bool isValid() const
Returns whether the item is valid.
Definition: item.cpp:88
static void widgetNeedsAkonadi(QWidget *widget)
Disable the given widget when Akonadi is not operational and show an error overlay (given enough spac...
Definition: controlgui.cpp:243
void doubleClicked(const QModelIndex &index)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
virtual void setModel(QAbstractItemModel *model)
virtual QVariant data(const QModelIndex &index, int role) const const=0
QAbstractItemModel * model() const const
QCA_EXPORT void init()
T value() const const
void setDropActionMenuEnabled(bool enabled)
Sets whether the drop action menu is enabled and will be shown on drop operation.
QItemSelectionModel * selectionModel() const const
Represents a collection of PIM items.
Definition: collection.h:61
~EntityListView() override
Destroys the favorite collections view.
KXMLGUIClient * xmlGuiClient() const
Return the XML GUI client which the view is used in.
void setSelectionMode(QAbstractItemView::SelectionMode mode)
bool isDropActionMenuEnabled() const
Returns whether the drop action menu is enabled and will be shown on drop operation.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
EntityListView(QWidget *parent=nullptr)
Creates a new favorite collections view.
virtual void dragMoveEvent(QDragMoveEvent *e) override
virtual void dropEvent(QDropEvent *e) override
@ CollectionRole
The collection.
void currentChanged(const QModelIndex &current, const QModelIndex &previous)
bool isValid() const const
IgnoreAction
virtual QModelIndex indexAt(const QPoint &p) const const override
void setModel(QAbstractItemModel *model) override
void setXmlGuiClient(KXMLGUIClient *xmlGuiClient)
Sets the XML GUI client which the view is used in.
void clicked(const QModelIndex &index)
const QAbstractItemModel * model() const const
QAction * exec()
Represents a PIM item stored in Akonadi storage.
Definition: item.h:104
A view to show an item/collection list provided by an EntityTreeModel.
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jun 30 2022 03:51:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.