Akonadi

entitylistview.cpp
1 /*
2  Copyright (c) 2006 - 2007 Volker Krause <[email protected]>
3  Copyright (c) 2008 Stephen Kelly <[email protected]>
4  Copyright (c) 2009 Kevin Ottens <[email protected]>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "entitylistview.h"
23 
24 #include "dragdropmanager_p.h"
25 
26 #include <QDragMoveEvent>
27 #include <QMenu>
28 
29 #include "akonadiwidgets_debug.h"
30 #include <KXMLGUIClient>
31 #include <KXMLGUIFactory>
32 
33 #include "collection.h"
34 #include "controlgui.h"
35 #include "item.h"
36 #include "entitytreemodel.h"
37 #include "progressspinnerdelegate_p.h"
38 
39 using namespace Akonadi;
40 
44 class Q_DECL_HIDDEN EntityListView::Private
45 {
46 public:
47  Private(EntityListView *parent)
48  : mParent(parent)
49 #ifndef QT_NO_DRAGANDDROP
50  , mDragDropManager(new DragDropManager(mParent))
51 #endif
52  {
53  }
54 
55  void init();
56  void itemClicked(const QModelIndex &index);
57  void itemDoubleClicked(const QModelIndex &index);
58  void itemCurrentChanged(const QModelIndex &index);
59 
60  EntityListView *mParent = nullptr;
61  DragDropManager *mDragDropManager = nullptr;
62  KXMLGUIClient *mXmlGuiClient = nullptr;
63 };
64 
65 void EntityListView::Private::init()
66 {
67  mParent->setEditTriggers(QAbstractItemView::EditKeyPressed);
68  mParent->setAcceptDrops(true);
69 #ifndef QT_NO_DRAGANDDROP
70  mParent->setDropIndicatorShown(true);
71  mParent->setDragDropMode(DragDrop);
72  mParent->setDragEnabled(true);
73 #endif
74  mParent->connect(mParent, SIGNAL(clicked(QModelIndex)), mParent, SLOT(itemClicked(QModelIndex)));
75  mParent->connect(mParent, SIGNAL(doubleClicked(QModelIndex)), mParent, SLOT(itemDoubleClicked(QModelIndex)));
76 
77  DelegateAnimator *animator = new DelegateAnimator(mParent);
78  ProgressSpinnerDelegate *customDelegate = new ProgressSpinnerDelegate(animator, mParent);
79  mParent->setItemDelegate(customDelegate);
80 
82 }
83 
84 void EntityListView::Private::itemClicked(const QModelIndex &index)
85 {
86  if (!index.isValid()) {
87  return;
88  }
89 
90  const Collection collection = index.model()->data(index, EntityTreeModel::CollectionRole).value<Collection>();
91  if (collection.isValid()) {
92  Q_EMIT mParent->clicked(collection);
93  } else {
94  const Item item = index.model()->data(index, EntityTreeModel::ItemRole).value<Item>();
95  if (item.isValid()) {
96  Q_EMIT mParent->clicked(item);
97  }
98  }
99 }
100 
101 void EntityListView::Private::itemDoubleClicked(const QModelIndex &index)
102 {
103  if (!index.isValid()) {
104  return;
105  }
106 
107  const Collection collection = index.model()->data(index, EntityTreeModel::CollectionRole).value<Collection>();
108  if (collection.isValid()) {
109  Q_EMIT mParent->doubleClicked(collection);
110  } else {
111  const Item item = index.model()->data(index, EntityTreeModel::ItemRole).value<Item>();
112  if (item.isValid()) {
113  Q_EMIT mParent->doubleClicked(item);
114  }
115  }
116 }
117 
118 void EntityListView::Private::itemCurrentChanged(const QModelIndex &index)
119 {
120  if (!index.isValid()) {
121  return;
122  }
123 
124  const Collection collection = index.model()->data(index, EntityTreeModel::CollectionRole).value<Collection>();
125  if (collection.isValid()) {
126  Q_EMIT mParent->currentChanged(collection);
127  } else {
128  const Item item = index.model()->data(index, EntityTreeModel::ItemRole).value<Item>();
129  if (item.isValid()) {
130  Q_EMIT mParent->currentChanged(item);
131  }
132  }
133 }
134 
136  : QListView(parent)
137  , d(new Private(this))
138 {
139  setSelectionMode(QAbstractItemView::SingleSelection);
140  d->init();
141 }
142 
144  : QListView(parent)
145  , d(new Private(this))
146 {
147  d->mXmlGuiClient = xmlGuiClient;
148  d->init();
149 }
150 
152 {
153  delete d->mDragDropManager;
154  delete d;
155 }
156 
158 {
159  if (selectionModel()) {
161  this, SLOT(itemCurrentChanged(QModelIndex)));
162  }
163 
164  QListView::setModel(model);
165 
167  SLOT(itemCurrentChanged(QModelIndex)));
168 }
169 
170 #ifndef QT_NO_DRAGANDDROP
171 void EntityListView::dragMoveEvent(QDragMoveEvent *event)
172 {
173  if (d->mDragDropManager->dropAllowed(event)) {
174  // All urls are supported. process the event.
176  return;
177  }
178 
179  event->setDropAction(Qt::IgnoreAction);
180 }
181 
182 void EntityListView::dropEvent(QDropEvent *event)
183 {
184  bool menuCanceled = false;
185  if (d->mDragDropManager->processDropEvent(event, menuCanceled) &&
186  !menuCanceled) {
187  QListView::dropEvent(event);
188  }
189 }
190 #endif
191 
192 #ifndef QT_NO_CONTEXTMENU
193 void EntityListView::contextMenuEvent(QContextMenuEvent *event)
194 {
195  if (!d->mXmlGuiClient) {
196  return;
197  }
198 
199  const QModelIndex index = indexAt(event->pos());
200 
201  QMenu *popup = nullptr;
202 
203  // check if the index under the cursor is a collection or item
204  const Collection collection = model()->data(index, EntityTreeModel::CollectionRole).value<Collection>();
205  if (collection.isValid()) {
206  popup = static_cast<QMenu *>(d->mXmlGuiClient->factory()->container(
207  QStringLiteral("akonadi_favoriteview_contextmenu"), d->mXmlGuiClient));
208  } else {
209  popup = static_cast<QMenu *>(d->mXmlGuiClient->factory()->container(
210  QStringLiteral("akonadi_favoriteview_emptyselection_contextmenu"), d->mXmlGuiClient));
211  }
212 
213  if (popup) {
214  popup->exec(event->globalPos());
215  }
216 }
217 #endif
218 
220 {
221  d->mXmlGuiClient = xmlGuiClient;
222 }
223 
225 {
226  return d->mXmlGuiClient;
227 }
228 
229 #ifndef QT_NO_DRAGANDDROP
230 void EntityListView::startDrag(Qt::DropActions supportedActions)
231 {
232  d->mDragDropManager->startDrag(supportedActions);
233 }
234 #endif
235 
237 {
238 #ifndef QT_NO_DRAGANDDROP
239  d->mDragDropManager->setShowDropActionMenu(enabled);
240 #endif
241 }
242 
244 {
245 #ifndef QT_NO_DRAGANDDROP
246  return d->mDragDropManager->showDropActionMenu();
247 #else
248  return false;
249 #endif
250 }
251 
252 #include "moc_entitylistview.cpp"
bool isValid() const
Returns whether the collection is valid.
Definition: collection.cpp:137
~EntityListView() override
Destroys the favorite collections view.
void setSelectionMode(QAbstractItemView::SelectionMode mode)
QItemSelectionModel * selectionModel() const const
KXMLGUIClient * xmlGuiClient() const
Return the XML GUI client which the view is used in.
virtual void setModel(QAbstractItemModel *model)
Represents a collection of PIM items.
Definition: collection.h:76
void currentChanged(const Akonadi::Collection &collection)
This signal is emitted whenever the current collection in the view has changed.
void setDropActionMenuEnabled(bool enabled)
Sets whether the drop action menu is enabled and will be shown on drop operation. ...
T value() const const
void setXmlGuiClient(KXMLGUIClient *xmlGuiClient)
Sets the XML GUI client which the view is used in.
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
const QPoint & globalPos() const const
bool isDropActionMenuEnabled() const
Returns whether the drop action menu is enabled and will be shown on drop operation.
QUICKADDONS_EXPORT void init()
bool isValid() const const
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:266
void setModel(QAbstractItemModel *model) override
EntityListView(QWidget *parent=nullptr)
Creates a new favorite collections view.
virtual QVariant data(const QModelIndex &index, int role) const const =0
QAction * exec()
virtual void dragMoveEvent(QDragMoveEvent *e) override
virtual bool event(QEvent *e) override
virtual void dropEvent(QDropEvent *e) override
A view to show an item/collection list provided by an EntityTreeModel.
const QPoint & pos() const const
virtual QModelIndex indexAt(const QPoint &p) const const override
const QAbstractItemModel * model() const const
Helper integration between Akonadi and Qt.
QAbstractItemModel * model() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
typedef DropActions
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue May 26 2020 22:46:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.