~ubuntu-branches/ubuntu/saucy/rocs/saucy-proposed

« back to all changes in this revision

Viewing changes to App/InterfacePlugins/ApiDoc/ApiDocModel.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg, Rohan Garg, Philip Muškovac
  • Date: 2013-06-21 02:04:20 UTC
  • mfrom: (1.1.27)
  • Revision ID: package-import@ubuntu.com-20130621020420-lzlui9y7qc6w3xog
Tags: 4:4.10.80-0ubuntu1
[ Rohan Garg ]
* New upstream release

[ Philip Muškovac ]
* Build-depend on libgrantlee-dev and libx11-dev
* Update rocs.install and not-installed 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of Rocs.
 
3
    Copyright 2013  Andreas Cord-Landwehr <cola@uni-paderborn.de>
 
4
 
 
5
    This program is free software; you can redistribute it and/or
 
6
    modify it under the terms of the GNU General Public License as
 
7
    published by the Free Software Foundation; either version 2 of
 
8
    the License, or (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
#include "ApiDocModel.h"
 
20
#include "ObjectDocumentation.h"
 
21
#include "MethodDocumentation.h"
 
22
#include "PropertyDocumentation.h"
 
23
 
 
24
#include <KDebug>
 
25
#include <KLocale>
 
26
 
 
27
class Item
 
28
{
 
29
public:
 
30
    Item(const QList<QVariant> &data, Item *parent = 0);
 
31
    ~Item();
 
32
 
 
33
    void appendChild(Item *child);
 
34
 
 
35
    Item *child(int row);
 
36
    int childCount() const;
 
37
    int columnCount() const;
 
38
    QVariant data(int column) const;
 
39
    void setDocumentAnchor(const QString &document, const QString &anchor);
 
40
    QString anchor();
 
41
    QString document();
 
42
    int row() const;
 
43
    Item *parent();
 
44
 
 
45
private:
 
46
    QList<Item*> _childItems;
 
47
    QList<QVariant> _itemData;
 
48
    QString _anchor;
 
49
    QString _document;
 
50
    Item *_parentItem;
 
51
};
 
52
 
 
53
Item::Item(const QList<QVariant> &data, Item *parent)
 
54
{
 
55
    _parentItem = parent;
 
56
    _itemData = data;
 
57
}
 
58
 
 
59
Item::~Item()
 
60
{
 
61
    qDeleteAll(_childItems);
 
62
}
 
63
 
 
64
void Item::appendChild(Item *item)
 
65
{
 
66
    _childItems.append(item);
 
67
}
 
68
 
 
69
Item * Item::child(int row)
 
70
{
 
71
    return _childItems.value(row);
 
72
}
 
73
 
 
74
int Item::childCount() const
 
75
{
 
76
    return _childItems.count();
 
77
}
 
78
 
 
79
int Item::row() const
 
80
{
 
81
    if (_parentItem) {
 
82
        return _parentItem->_childItems.indexOf(const_cast<Item*>(this));
 
83
    }
 
84
    return 0;
 
85
}
 
86
 
 
87
int Item::columnCount() const
 
88
{
 
89
    return _itemData.count();
 
90
}
 
91
 
 
92
QVariant Item::data(int column) const
 
93
{
 
94
    return _itemData.value(column);
 
95
}
 
96
 
 
97
void Item::setDocumentAnchor(const QString& document, const QString& anchor)
 
98
{
 
99
    _anchor = anchor;
 
100
    _document = document;
 
101
}
 
102
 
 
103
QString Item::anchor()
 
104
{
 
105
    return _anchor;
 
106
}
 
107
 
 
108
QString Item::document()
 
109
{
 
110
    return _document;
 
111
}
 
112
 
 
113
Item * Item::parent()
 
114
{
 
115
    return _parentItem;
 
116
}
 
117
 
 
118
 
 
119
// ApiDocModel methods
 
120
ApiDocModel::ApiDocModel(QList<ObjectDocumentation* > dataList, QObject *parent)
 
121
    : QAbstractItemModel(parent)
 
122
{
 
123
    QList<QVariant> rootData;
 
124
    rootData << i18n("Script Objects");
 
125
    rootItem = new Item(rootData);
 
126
    setupModelData(dataList, rootItem);
 
127
}
 
128
 
 
129
ApiDocModel::~ApiDocModel()
 
130
{
 
131
    delete rootItem;
 
132
}
 
133
 
 
134
QModelIndex ApiDocModel::index(int row, int column, const QModelIndex &parent) const
 
135
{
 
136
    if (!hasIndex(row, column, parent)) {
 
137
        return QModelIndex();
 
138
    }
 
139
 
 
140
    Item *parentItem;
 
141
    if (!parent.isValid()) {
 
142
        parentItem = rootItem;
 
143
    }
 
144
    else {
 
145
        parentItem = static_cast<Item*>(parent.internalPointer());
 
146
    }
 
147
 
 
148
    Item *childItem = parentItem->child(row);
 
149
    if (childItem) {
 
150
        return createIndex(row, column, childItem);
 
151
    }
 
152
    else {
 
153
        return QModelIndex();
 
154
    }
 
155
}
 
156
 
 
157
QModelIndex ApiDocModel::parent(const QModelIndex &index) const
 
158
{
 
159
    if (!index.isValid()) {
 
160
        return QModelIndex();
 
161
    }
 
162
 
 
163
    Item *childItem = static_cast<Item*>(index.internalPointer());
 
164
    Item *parentItem = childItem->parent();
 
165
 
 
166
    if (parentItem == rootItem) {
 
167
        return QModelIndex();
 
168
    }
 
169
 
 
170
    return createIndex(parentItem->row(), 0, parentItem);
 
171
}
 
172
 
 
173
int ApiDocModel::rowCount(const QModelIndex &parent) const
 
174
{
 
175
    Item *parentItem;
 
176
    if (parent.column() > 0) {
 
177
        return 0;
 
178
    }
 
179
 
 
180
    if (!parent.isValid()) {
 
181
        parentItem = rootItem;
 
182
    }
 
183
    else {
 
184
        parentItem = static_cast<Item*>(parent.internalPointer());
 
185
    }
 
186
 
 
187
    return parentItem->childCount();
 
188
}
 
189
 
 
190
int ApiDocModel::columnCount(const QModelIndex &parent) const
 
191
{
 
192
    if (parent.isValid()) {
 
193
        return static_cast<Item*>(parent.internalPointer())->columnCount();
 
194
    }
 
195
    else {
 
196
        return rootItem->columnCount();
 
197
    }
 
198
}
 
199
 
 
200
QVariant ApiDocModel::data(const QModelIndex &index, int role) const
 
201
{
 
202
    if (!index.isValid()) {
 
203
        return QVariant();
 
204
    }
 
205
 
 
206
    Item *item = static_cast<Item*>(index.internalPointer());
 
207
    if (role == DocumentRole) {
 
208
        return QVariant::fromValue<QString>(item->document());
 
209
    }
 
210
    if (role == AnchorRole) {
 
211
        return QVariant::fromValue<QString>(item->anchor());
 
212
    }
 
213
    if (role != Qt::DisplayRole) {
 
214
        return QVariant();
 
215
    }
 
216
    return item->data(index.column());
 
217
}
 
218
 
 
219
Qt::ItemFlags ApiDocModel::flags(const QModelIndex &index) const
 
220
{
 
221
    if (!index.isValid()) {
 
222
        return 0;
 
223
    }
 
224
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 
225
}
 
226
 
 
227
QVariant ApiDocModel::headerData(int section, Qt::Orientation orientation, int role) const
 
228
{
 
229
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
 
230
        return rootItem->data(section);
 
231
    }
 
232
 
 
233
    return QVariant();
 
234
}
 
235
 
 
236
void ApiDocModel::setupModelData(QList<ObjectDocumentation* > dataList, Item *parent)
 
237
{
 
238
    foreach (ObjectDocumentation *object, dataList) {
 
239
        QList<QVariant> columnData;
 
240
        columnData << object->id();
 
241
        Item *objectItem = new Item(columnData, parent);
 
242
        objectItem->setDocumentAnchor(object->apiDocumentIdentifier(), QString());
 
243
        parent->appendChild(objectItem);
 
244
 
 
245
        QList<QVariant> propertyColumnData;
 
246
        propertyColumnData << i18n("Properties");
 
247
        Item *propertyContainer = new Item(propertyColumnData, objectItem);
 
248
        propertyContainer->setDocumentAnchor(object->apiDocumentIdentifier(), "properties");
 
249
        objectItem->appendChild(propertyContainer);
 
250
        foreach (PropertyDocumentation *property, object->properties()) {
 
251
            QList<QVariant> columnData;
 
252
            columnData << property->name();
 
253
            Item *propertyItem = new Item(columnData, propertyContainer);
 
254
            propertyItem->setDocumentAnchor(object->apiDocumentIdentifier(),
 
255
                property->apiDocumentAnchor());
 
256
            propertyContainer->appendChild(propertyItem);
 
257
        }
 
258
 
 
259
        QList<QVariant> methodColumnData;
 
260
        methodColumnData << i18n("Methods");
 
261
        Item *methodContainer = new Item(methodColumnData, objectItem);
 
262
        methodContainer->setDocumentAnchor(object->apiDocumentIdentifier(), "methods");
 
263
        objectItem->appendChild(methodContainer);
 
264
        foreach (MethodDocumentation *method, object->methods()) {
 
265
            QList<QVariant> columnData;
 
266
            columnData << method->name();
 
267
            Item *methodProperty = new Item(columnData, methodContainer);
 
268
            methodProperty->setDocumentAnchor(object->apiDocumentIdentifier(),
 
269
                method->apiDocumentAnchor());
 
270
            methodContainer->appendChild(methodProperty);
 
271
        }
 
272
    }
 
273
}