Akonadi

itemview.cpp
1 /*
2  Copyright (c) 2007 Tobias Koenig <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "itemview.h"
21 
22 #include "controlgui.h"
23 #include "entitytreemodel.h"
24 
25 #include <KXMLGUIFactory>
26 #include <KXMLGUIClient>
27 #include <QContextMenuEvent>
28 #include <QHeaderView>
29 #include <QMenu>
30 
31 using namespace Akonadi;
32 
36 class Q_DECL_HIDDEN ItemView::Private
37 {
38 public:
39  Private(ItemView *parent)
40  : mParent(parent)
41  {
42  }
43 
44  void init();
45  void itemActivated(const QModelIndex &index);
46  void itemCurrentChanged(const QModelIndex &index);
47  void itemClicked(const QModelIndex &index);
48  void itemDoubleClicked(const QModelIndex &index);
49 
50  Item itemForIndex(const QModelIndex &index);
51 
52  KXMLGUIClient *xmlGuiClient = nullptr;
53 
54 private:
55  ItemView *mParent = nullptr;
56 };
57 
58 void ItemView::Private::init()
59 {
60  mParent->setRootIsDecorated(false);
61 
62  mParent->header()->setSectionsClickable(true);
63  mParent->header()->setStretchLastSection(true);
64 
65  mParent->connect(mParent, SIGNAL(activated(QModelIndex)), mParent, SLOT(itemActivated(QModelIndex)));
66  mParent->connect(mParent, SIGNAL(clicked(QModelIndex)), mParent, SLOT(itemClicked(QModelIndex)));
67  mParent->connect(mParent, QOverload<const QModelIndex &>::of(&QAbstractItemView::doubleClicked), mParent, [this](const QModelIndex &index) { itemDoubleClicked(index); });
68 
70 }
71 
72 Item ItemView::Private::itemForIndex(const QModelIndex &index)
73 {
74  if (!index.isValid()) {
75  return Item();
76  }
77 
78  return mParent->model()->data(index, EntityTreeModel::ItemRole).value<Item>();
79 }
80 
81 void ItemView::Private::itemActivated(const QModelIndex &index)
82 {
83  const Item item = itemForIndex(index);
84 
85  if (!item.isValid()) {
86  return;
87  }
88 
89  Q_EMIT mParent->activated(item);
90 }
91 
92 void ItemView::Private::itemCurrentChanged(const QModelIndex &index)
93 {
94  const Item item = itemForIndex(index);
95 
96  if (!item.isValid()) {
97  return;
98  }
99 
100  Q_EMIT mParent->currentChanged(item);
101 }
102 
103 void ItemView::Private::itemClicked(const QModelIndex &index)
104 {
105  const Item item = itemForIndex(index);
106 
107  if (!item.isValid()) {
108  return;
109  }
110 
111  Q_EMIT mParent->clicked(item);
112 }
113 
114 void ItemView::Private::itemDoubleClicked(const QModelIndex &index)
115 {
116  const Item item = itemForIndex(index);
117 
118  if (!item.isValid()) {
119  return;
120  }
121 
122  Q_EMIT mParent->doubleClicked(item);
123 }
124 
126  : QTreeView(parent)
127  , d(new Private(this))
128 {
129  d->init();
130 }
131 
133  : QTreeView(parent)
134  , d(new Private(this))
135 {
136  d->xmlGuiClient = xmlGuiClient;
137  d->init();
138 }
139 
141 {
142  delete d;
143 }
144 
145 void ItemView::setModel(QAbstractItemModel *model)
146 {
147  QTreeView::setModel(model);
148 
150  this, SLOT(itemCurrentChanged(QModelIndex)));
151 }
152 
153 void ItemView::contextMenuEvent(QContextMenuEvent *event)
154 {
155  if (!d->xmlGuiClient) {
156  return;
157  }
158  QMenu *popup = static_cast<QMenu *>(d->xmlGuiClient->factory()->container(
159  QStringLiteral("akonadi_itemview_contextmenu"), d->xmlGuiClient));
160  if (popup) {
161  popup->exec(event->globalPos());
162  }
163 }
164 
166 {
167  d->xmlGuiClient = xmlGuiClient;
168 }
169 
170 #include "moc_itemview.cpp"
void doubleClicked(const QModelIndex &index)
ItemView(QWidget *parent=nullptr)
Creates a new item view.
Definition: itemview.cpp:125
QItemSelectionModel * selectionModel() const const
void currentChanged(const Akonadi::Item &item)
This signal is emitted whenever the current item in the view has changed.
const QPoint & globalPos() const const
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
QAction * exec()
~ItemView() override
Destroys the item view.
Definition: itemview.cpp:140
A view to show an item list provided by an ItemModel.
Definition: itemview.h:63
virtual bool event(QEvent *event) override
Helper integration between Akonadi and Qt.
virtual void setModel(QAbstractItemModel *model) override
void setXmlGuiClient(KXMLGUIClient *xmlGuiClient)
Sets the KXMLGUIFactory which this view is used in.
Definition: itemview.cpp:165
QAbstractItemModel * model() const const
void setRootIsDecorated(bool show)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon May 25 2020 22:46:10 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.