1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "featuretreemodel.h"
#include "feature.h"
#include <QPalette>
#include <QColor>
#include <QApplication>
#include <QtDebug>
QT_BEGIN_NAMESPACE
class Node
{
public:
Node(Feature *f, Node *p = 0) : feature(f), parent(p) {}
~Node();
Node* find(const Feature *child) const;
bool contains(const Feature *child) const { return find(child) != 0; }
bool insert(Node *n);
Feature *feature;
Node *parent;
QList<Node*> children; // maybe convert to Map to get keys sorted
};
Node::~Node()
{
while (!children.isEmpty())
delete children.takeFirst();
}
Node* Node::find(const Feature *f) const
{
if (this->feature == f)
return const_cast<Node*>(this);
foreach (Node *n, children)
if (Node *m = n->find(f))
return m;
return 0;
}
static bool nodePtrLessThan(const Node *n1, const Node *n2)
{
return (n1->feature->key() < n2->feature->key());
}
/*
Try insert \a n into the tree with this node as root.
n is inserted as a child if it has a dependency to this node.
Returns true if child is inserted into the tree, false otherwise.
*/
bool Node::insert(Node *n)
{
Feature *f = const_cast<Feature*>(n->feature);
if (feature->supports().contains(f)) {
children.append(n);
qSort(children.begin(), children.end(), nodePtrLessThan);
n->parent = this;
return true;
}
foreach (Node *child, children)
if (child->insert(n))
return true;
return false;
}
static bool isSection(const QModelIndex &index)
{
return index.isValid() && (index.internalId() == 0);
}
FeatureTreeModel::FeatureTreeModel(QObject *parent)
: QAbstractItemModel(parent)
{
}
FeatureTreeModel::~FeatureTreeModel()
{
foreach (QString section, sections.keys())
while (!sections[section].isEmpty())
delete sections[section].takeFirst();
}
/*
Returns true if the model already contains \a in \a section, false otherwise.
*/
bool FeatureTreeModel::contains(const QString §ion, const Feature *f) const
{
return (find(section, f) != 0);
}
Node* FeatureTreeModel::find(const QString §ion, const Feature *f) const
{
QList<Node*> roots = sections[section];
foreach (Node *root, roots)
if (Node *n = root->find(f))
return n;
return 0;
}
/*
Add new \a feature to the tree.
When all feature is added, buildTree() must be called to build the
dependency tree.
*/
void FeatureTreeModel::addFeature(Feature *feature)
{
const QString section = feature->section();
Q_ASSERT(!contains(section, feature));
connect(feature, SIGNAL(changed()), this, SLOT(featureChanged()));
Node *node = new Node(feature, 0);
// try insert any toplevel nodes as child of this one
foreach (Node *n, sections[section])
if (node->insert(n))
sections[section].removeAll(n);
// try insert this node as a child of any existing node
foreach (Node *n, sections[section])
if (n->insert(node)) {
emit layoutChanged();
return;
}
// not a child, insert as a toplevel node
sections[section].append(node);
qSort(sections[section].begin(), sections[section].end(), nodePtrLessThan);
emit layoutChanged();
}
QModelIndex FeatureTreeModel::createIndex(int row, int column,
const QModelIndex &parent,
const Node *node) const
{
QModelIndex index = QAbstractItemModel::createIndex(row, column,
(void*)node);
if (parent.isValid())
parentMap[index] = parent;
if (node)
featureIndexMap[node->feature] = index;
return index;
}
QModelIndex FeatureTreeModel::index(int row, int column,
const QModelIndex &parent) const
{
if (!parent.isValid()) { // index is a section
if (row < sections.size() && column == 0)
return QAbstractItemModel::createIndex(row, column, 0);
return QModelIndex();
}
if (isSection(parent)) { // index is a toplevel feature
const int parentRow = parent.row();
if (parentRow < sections.size()) {
QString section = sections.keys().at(parentRow);
QList<Node*> nodes = sections[section];
if (row < nodes.size() && column < 2)
return createIndex(row, column, parent, nodes.at(row));
}
return QModelIndex();
}
// parent is a feature
Node *parentNode = static_cast<Node*>(parent.internalPointer());
QList<Node*> children = parentNode->children;
if (row < children.size() && column < 2)
return createIndex(row, column, parent, children.at(row));
return QModelIndex();
}
QModelIndex FeatureTreeModel::index(const QModelIndex &parent,
const Feature *feature) const
{
const int rows = rowCount(parent);
for (int i = 0; i < rows; ++i) {
QModelIndex child = index(i, 0, parent);
Node *node = static_cast<Node*>(child.internalPointer());
if (node && node->feature == feature)
return child;
QModelIndex childSearch = index(child, feature);
if (childSearch.isValid())
return childSearch;
}
return QModelIndex();
}
QModelIndex FeatureTreeModel::index(const Feature *feature) const
{
if (featureIndexMap.contains(feature))
return featureIndexMap.value(feature);
// exhaustive search
int sectionRow = sections.keys().indexOf(feature->section());
QModelIndex sectionIndex = index(sectionRow, 0, QModelIndex());
return index(sectionIndex, feature);
}
QModelIndex FeatureTreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
if (parentMap.contains(index))
return parentMap.value(index);
return QModelIndex();
}
int FeatureTreeModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return sections.size();
if (isSection(parent)) {
const QString section = sections.keys().at(parent.row());
return sections[section].size();
}
const Node *node = static_cast<Node*>(parent.internalPointer());
return node->children.size();
}
int FeatureTreeModel::columnCount(const QModelIndex &parent) const
{
#if 0
if (!parent.isValid())
return 0;
if (isSection(parent))
return 1;
#endif
Q_UNUSED(parent);
return 2; // Feature: [key, name]
}
QVariant FeatureTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
const Node *node = static_cast<Node*>(index.internalPointer());
switch (role) {
case Qt::DisplayRole: {
if (node == 0) // index is a section
return sections.keys().at(index.row());
if (index.column() == 0)
return node->feature->key();
Q_ASSERT(index.column() == 1);
return node->feature->title();
}
case Qt::CheckStateRole: {
if (node && index.column() == 0)
return (node->feature->enabled() ?
Qt::Checked : Qt::Unchecked);
break;
}
case Qt::TextColorRole: {
if (node && index.column() == 0) // feature key
if (node->feature->selectable())
return QApplication::palette().color(QPalette::Link);
break;
}
case Qt::TextAlignmentRole:
case Qt::BackgroundColorRole:
case Qt::FontRole:
case Qt::ToolTipRole: // TODO
case Qt::StatusTipRole: // TODO
case Qt::WhatsThisRole: // TODO
case Qt::DecorationRole:
case Qt::EditRole:
default:
break;
}
return QVariant();
}
bool FeatureTreeModel::setData(const QModelIndex &index,
const QVariant &value, int role)
{
if (!index.isValid())
return false;
Node *node = static_cast<Node*>(index.internalPointer());
if (!node)
return false;
if (role == Qt::CheckStateRole) {
Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
if (state == Qt::Checked)
node->feature->setEnabled(true);
else if (state == Qt::Unchecked)
node->feature->setEnabled(false);
emit dataChanged(index, index);
return true;
}
return false;
}
Qt::ItemFlags FeatureTreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid() || index.internalPointer() == 0)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
const Node *node = static_cast<Node*>(index.internalPointer());
const Feature *feature = node->feature;
Qt::ItemFlags flags = Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
if (feature->selectable())
flags |= Qt::ItemIsEnabled;
return flags;
}
QVariant FeatureTreeModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == 0)
return QString("Id");
else if (section == 1)
return QString("Name");
}
return QVariant();
}
Feature* FeatureTreeModel::getFeature(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
if (isSection(index))
return 0;
Node *node = static_cast<Node*>(index.internalPointer());
return const_cast<Feature*>(node->feature);
}
void FeatureTreeModel::featureChanged()
{
Feature *feature = qobject_cast<Feature*>(sender());
if (feature) {
QModelIndex featureIndex = index(feature);
emit dataChanged(featureIndex, featureIndex);
} else {
emit layoutChanged();
}
}
void FeatureTreeModel::readConfig(QTextStream &stream)
{
static QRegExp regexp("\\s*#\\s*define\\s+QT_NO_(\\S+)\\s*");
while (!stream.atEnd()) {
QString line = stream.readLine();
if (regexp.exactMatch(line)) {
Feature *f = Feature::getInstance(regexp.cap(1));
f->setEnabled(false);
}
}
}
/*
Search for all disabled child features of \a parent.
Returns a list of feature keys for the disabled items.
*/
QStringList FeatureTreeModel::findDisabled(const QModelIndex &parent) const
{
QStringList stringList;
const int rows = rowCount(parent);
for (int i = 0; i < rows; ++i) {
QModelIndex child = index(i, 0, parent);
Node *node = static_cast<Node*>(child.internalPointer());
if (node && node->feature && !node->feature->enabled())
stringList << node->feature->key();
stringList << findDisabled(child);
}
return stringList;
}
void FeatureTreeModel::writeConfig(QTextStream &stream) const
{
const int sectionCount = rowCount(QModelIndex());
for (int i = 0; i < sectionCount; ++i) {
QModelIndex section = index(i, 0, QModelIndex());
QStringList disabled = findDisabled(section);
if (disabled.size() > 0) {
stream << '\n' << "/* " << sections.keys().at(i) << " */" << '\n';
foreach (QString feature, disabled)
stream << "#ifndef QT_NO_" << feature << '\n'
<< "# define QT_NO_" << feature << '\n'
<< "#endif" << '\n';
}
}
}
void FeatureTreeModel::clear()
{
Feature::clear();
sections.clear();
parentMap.clear();
featureIndexMap.clear();
emit layoutChanged();
}
QT_END_NAMESPACE
|