1/*
2 Copyright (c) 2007 Tobias Koenig <tokoe@kde.org>
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 "control.h"
23#include "itemmodel.h"
24
25#include <KXMLGUIFactory>
26#include <KXmlGuiWindow>
27
28#include <QContextMenuEvent>
29#include <QHeaderView>
30#include <QMenu>
31
32using namespace Akonadi;
33
34/**
35 * @internal
36 */
37class ItemView::Private
38{
39public:
40 Private(ItemView *parent)
41 : xmlGuiClient(0)
42 , mParent(parent)
43 {
44 }
45
46 void init();
47 void itemActivated(const QModelIndex &index);
48 void itemCurrentChanged(const QModelIndex &index);
49 void itemClicked(const QModelIndex &index);
50 void itemDoubleClicked(const QModelIndex &index);
51
52 Item itemForIndex(const QModelIndex &index);
53
54 KXMLGUIClient *xmlGuiClient;
55
56private:
57 ItemView *mParent;
58};
59
60void ItemView::Private::init()
61{
62 mParent->setRootIsDecorated(false);
63
64 mParent->header()->setClickable(true);
65 mParent->header()->setStretchLastSection(true);
66
67 mParent->connect(mParent, SIGNAL(activated(QModelIndex)),
68 mParent, SLOT(itemActivated(QModelIndex)));
69 mParent->connect(mParent, SIGNAL(clicked(QModelIndex)),
70 mParent, SLOT(itemClicked(QModelIndex)));
71 mParent->connect(mParent, SIGNAL(doubleClicked(QModelIndex)),
72 mParent, SLOT(itemDoubleClicked(QModelIndex)));
73
74 Control::widgetNeedsAkonadi(mParent);
75}
76
77Item ItemView::Private::itemForIndex(const QModelIndex &index)
78{
79 if (!index.isValid()) {
80 return Item();
81 }
82
83 const Item::Id currentItem = index.sibling(index.row(), ItemModel::Id).data(ItemModel::IdRole).toLongLong();
84 if (currentItem <= 0) {
85 return Item();
86 }
87
88 const QString remoteId = index.sibling(index.row(), ItemModel::RemoteId).data(ItemModel::IdRole).toString();
89 const QString mimeType = index.sibling(index.row(), ItemModel::MimeType).data(ItemModel::MimeTypeRole).toString();
90
91 Item item(currentItem);
92 item.setRemoteId(remoteId);
93 item.setMimeType(mimeType);
94
95 return item;
96}
97
98void ItemView::Private::itemActivated(const QModelIndex &index)
99{
100 const Item item = itemForIndex(index);
101
102 if (!item.isValid()) {
103 return;
104 }
105
106 emit mParent->activated(item);
107}
108
109void ItemView::Private::itemCurrentChanged(const QModelIndex &index)
110{
111 const Item item = itemForIndex(index);
112
113 if (!item.isValid()) {
114 return;
115 }
116
117 emit mParent->currentChanged(item);
118}
119
120void ItemView::Private::itemClicked(const QModelIndex &index)
121{
122 const Item item = itemForIndex(index);
123
124 if (!item.isValid()) {
125 return;
126 }
127
128 emit mParent->clicked(item);
129}
130
131void ItemView::Private::itemDoubleClicked(const QModelIndex &index)
132{
133 const Item item = itemForIndex(index);
134
135 if (!item.isValid()) {
136 return;
137 }
138
139 emit mParent->doubleClicked(item);
140}
141
142ItemView::ItemView(QWidget *parent)
143 : QTreeView(parent)
144 , d(new Private(this))
145{
146 d->init();
147}
148
149ItemView::ItemView(KXmlGuiWindow *xmlGuiWindow, QWidget *parent)
150 : QTreeView(parent)
151 , d(new Private(this))
152{
153 d->xmlGuiClient = static_cast<KXMLGUIClient *>(xmlGuiWindow);
154 d->init();
155}
156
157ItemView::ItemView(KXMLGUIClient *xmlGuiClient, QWidget *parent)
158 : QTreeView(parent)
159 , d(new Private(this))
160{
161 d->xmlGuiClient = xmlGuiClient;
162 d->init();
163}
164
165ItemView::~ItemView()
166{
167 delete d;
168}
169
170void ItemView::setModel(QAbstractItemModel *model)
171{
172 QTreeView::setModel(model);
173
174 connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
175 this, SLOT(itemCurrentChanged(QModelIndex)));
176}
177
178void ItemView::contextMenuEvent(QContextMenuEvent *event)
179{
180 if (!d->xmlGuiClient) {
181 return;
182 }
183 QMenu *popup = static_cast<QMenu *>(d->xmlGuiClient->factory()->container(
184 QLatin1String("akonadi_itemview_contextmenu"), d->xmlGuiClient));
185 if (popup) {
186 popup->exec(event->globalPos());
187 }
188}
189
190void ItemView::setXmlGuiWindow(KXmlGuiWindow *xmlGuiWindow)
191{
192 d->xmlGuiClient = static_cast<KXMLGUIClient *>(xmlGuiWindow);
193}
194
195void ItemView::setXmlGuiClient(KXMLGUIClient *xmlGuiClient)
196{
197 d->xmlGuiClient = xmlGuiClient;
198}
199
200#include "moc_itemview.cpp"
201