summaryrefslogtreecommitdiff
path: root/src/backend/bookshelfmodel/btbookshelftreemodel.cpp
blob: a2a988c806afe53ef51de6a37caf43902637c949 (plain)
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*********
*
* In the name of the Father, and of the Son, and of the Holy Spirit.
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2009 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License
* version 2.0.
*
**********/

#include "backend/bookshelfmodel/btbookshelftreemodel.h"

#include <QSet>
#include "backend/bookshelfmodel/categoryitem.h"
#include "backend/bookshelfmodel/indexingitem.h"
#include "backend/bookshelfmodel/languageitem.h"
#include "backend/bookshelfmodel/moduleitem.h"


using namespace BookshelfModel;

BtBookshelfTreeModel::BtBookshelfTreeModel(QObject *parent)
        : QAbstractItemModel(parent), m_sourceModel(0), m_rootItem(new RootItem),
        m_defaultChecked(MODULE_HIDDEN), m_checkable(false) {
    m_groupingOrder.push_back(GROUP_CATEGORY);
    m_groupingOrder.push_back(GROUP_LANGUAGE);
}

BtBookshelfTreeModel::BtBookshelfTreeModel(const Grouping &g, QObject *parent)
        : QAbstractItemModel(parent), m_sourceModel(0), m_rootItem(new RootItem),
        m_groupingOrder(g), m_defaultChecked(MODULE_HIDDEN), m_checkable(false) {
    // Intentionally empty
}

BtBookshelfTreeModel::~BtBookshelfTreeModel() {
    delete m_rootItem;
}

int BtBookshelfTreeModel::rowCount(const QModelIndex &parent) const {
    return getItem(parent)->children().size();
}

int BtBookshelfTreeModel::columnCount(const QModelIndex &parent) const {
    Q_UNUSED(parent);

    return 1;
}

bool BtBookshelfTreeModel::hasChildren(const QModelIndex &parent) const {
    return !getItem(parent)->children().isEmpty();
}

QModelIndex BtBookshelfTreeModel::index(int row, int column,
                                        const QModelIndex &parent) const {
    if (!hasIndex(row, column, parent)) return QModelIndex();

    Item *parentItem(getItem(parent));
    Item *childItem(parentItem->children().at(row));
    if (childItem != 0) {
        return createIndex(row, column, childItem);
    }
    else {
        return QModelIndex();
    }
}

QModelIndex BtBookshelfTreeModel::parent(const QModelIndex &index) const {
    if (!index.isValid()) return QModelIndex();

    Item *childItem(static_cast<Item*>(index.internalPointer()));
    Q_ASSERT(childItem != 0);
    Item *parentItem(childItem->parent());
    Q_ASSERT(parentItem != 0);

    if (parentItem == m_rootItem) {
        return QModelIndex();
    }
    return createIndex(parentItem->childIndex(), 0, parentItem);
}

QVariant BtBookshelfTreeModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid() || index.column() != 0) {
        return QVariant();
    }

    Item *i(static_cast<Item*>(index.internalPointer()));
    Q_ASSERT(i != 0);
    switch (role) {
        case Qt::CheckStateRole:
            if (!m_checkable) break;
        case BtBookshelfTreeModel::CheckStateRole:
            return i->checkState();
        case BtBookshelfModel::ModulePointerRole:
            /* This case is just an optimization. */
            if (i->type() == Item::ITEM_MODULE) {
                ModuleItem *mi(static_cast<ModuleItem *>(i));
                return qVariantFromValue((void*) mi->moduleInfo());
            }
            return 0;
        case Qt::DisplayRole:
        case Qt::DecorationRole:
        case BtBookshelfModel::ModuleHiddenRole:
        default:
            if (i->type() == Item::ITEM_MODULE) {
                ModuleItem *item(static_cast<ModuleItem *>(i));
                CSwordModuleInfo* m(item->moduleInfo());
                return data(m, role);
            }
            else {
                return i->data(role);
            }
    }
    return QVariant();
}

QVariant BtBookshelfTreeModel::data(CSwordModuleInfo *module, int role) const {
    Q_ASSERT(m_sourceIndexMap.contains(module));
    return m_sourceModel->data(m_sourceIndexMap.value(module), role);
}

bool BtBookshelfTreeModel::setData(const QModelIndex &itemIndex,
                                   const QVariant &value,
                                   int role) {
    typedef QPair<Item *, QModelIndex> IP;

    Qt::CheckState newState;
    if (role == Qt::CheckStateRole) {
        bool ok;
        newState = (Qt::CheckState) value.toInt(&ok);
        if (!ok) return false;
    }
    else {
        return false;
    }

    // Handle partially checked as checked here in setData():
    if (newState == Qt::PartiallyChecked) newState = Qt::Checked;

    Item *item(static_cast<Item*>(itemIndex.internalPointer()));
    Q_ASSERT(item != 0);
    if (item->checkState() == newState) return false;

    // Recursively (un)check all children:
    QList<IP> q;
    IP p(item, itemIndex);
    for (;;) {
        if (item->checkState() != newState) {
            item->setCheckState(newState);
            emit dataChanged(p.second, p.second);
            if (item->type() == Item::ITEM_MODULE) {
                ModuleItem *mItem(static_cast<ModuleItem*>(item));
                CSwordModuleInfo *mInfo(mItem->moduleInfo());
                if (newState == Qt::Checked) {
                    m_checkedModulesCache.insert(mInfo);
                    emit moduleChecked(mInfo, true);
                }
                else {
                    m_checkedModulesCache.remove(mInfo);
                    emit moduleChecked(mInfo, false);
                }
            }
            else {
                const QList<Item*> &children(item->children());
                for (int i(0); i < children.size(); i++) {
                    q.append(IP(children.at(i), index(i, 0, p.second)));
                }
            }
        }
        if (q.empty()) break;
        p = q.takeFirst();
        item = p.first;
    }

    // Recursively change parent check states.
    resetParentCheckStates(itemIndex.parent());

    return true;
}

Qt::ItemFlags BtBookshelfTreeModel::flags(const QModelIndex &index) const {
    if (!index.isValid()) return 0;

    Qt::ItemFlags f(Qt::ItemIsEnabled | Qt::ItemIsSelectable);

    if (m_checkable) {
        f |= Qt::ItemIsUserCheckable;

        Item *i(static_cast<Item*>(index.internalPointer()));
        Q_ASSERT(i != 0);

        if (i->type() != Item::ITEM_MODULE) {
            f |= Qt::ItemIsTristate;
        }
    }

    return f;
}

QVariant BtBookshelfTreeModel::headerData(int section,
        Qt::Orientation orientation,
        int role) const {
    if (orientation == Qt::Horizontal) {
        return m_sourceModel->headerData(section, orientation, role);
    }
    return QVariant();
}

void BtBookshelfTreeModel::setSourceModel(QAbstractItemModel *sourceModel) {
    if (m_sourceModel == sourceModel) return;

    if (m_sourceModel != 0) {
        disconnect(this, SLOT(moduleInserted(QModelIndex, int, int)));
        disconnect(this, SLOT(moduleRemoved(QModelIndex, int, int)));
        disconnect(this, SLOT(moduleDataChanged(QModelIndex, QModelIndex)));
        beginRemoveRows(QModelIndex(), 0, m_rootItem->children().size() - 1);
        delete m_rootItem;
        m_modules.clear();
        m_sourceIndexMap.clear();
        m_checkedModulesCache.clear();
        m_rootItem = new RootItem;
        endRemoveRows();
    }

    m_sourceModel = sourceModel;

    if (sourceModel != 0) {
        connect(sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
                this,        SLOT(moduleRemoved(QModelIndex, int, int)));
        connect(sourceModel, SIGNAL(rowsInserted(QModelIndex, int, int)),
                this,        SLOT(moduleInserted(QModelIndex, int, int)));
        connect(sourceModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
                this,        SLOT(moduleDataChanged(QModelIndex, QModelIndex)));

        for (int i(0); i < sourceModel->rowCount(); i++) {
            typedef BtBookshelfModel::ModuleRole MRole;
            static const MRole HR(BtBookshelfModel::ModuleHiddenRole);
            static const MRole PR(BtBookshelfModel::ModulePointerRole);
            static const MRole IR(BtBookshelfModel::ModuleHasIndexRole);

            QModelIndex moduleIndex(sourceModel->index(i, 0));
            CSwordModuleInfo *module(
                static_cast<CSwordModuleInfo *>(
                    sourceModel->data(moduleIndex, PR).value<void*>()
                )
            );
            Q_ASSERT(module != 0);
            bool checked;
            if (m_defaultChecked == MODULE_HIDDEN) {
                checked = !sourceModel->data(moduleIndex, HR).toBool();
            }
            else if (m_defaultChecked == MODULE_INDEXED) {
                checked = !sourceModel->data(moduleIndex, IR).toBool();
            }
            else {
                checked = (m_defaultChecked == CHECKED);
            }
            m_sourceIndexMap[module] = moduleIndex;
            addModule(module, checked);
        }
    }
}

void BtBookshelfTreeModel::setGroupingOrder(const Grouping &groupingOrder) {
    if (m_groupingOrder == groupingOrder) return;
    m_groupingOrder = groupingOrder;

    if (m_sourceModel != 0) {
        QSet<CSwordModuleInfo*> checked(m_checkedModulesCache);
        m_checkedModulesCache.clear();

        beginRemoveRows(QModelIndex(), 0, m_rootItem->children().size() - 1);
        delete m_rootItem;
        m_modules.clear();
        m_rootItem = new RootItem;
        endRemoveRows();

        for (int i(0); i < m_sourceModel->rowCount(); i++) {
            QModelIndex sourceIndex(m_sourceModel->index(i, 0));
            CSwordModuleInfo *module(
                static_cast<CSwordModuleInfo *>(
                    m_sourceModel->data(
                        sourceIndex,
                        BtBookshelfModel::ModulePointerRole
                    ).value<void*>()
                )
            );
            Q_ASSERT(module != 0);
            m_sourceIndexMap[module] = sourceIndex;
            addModule(module, checked.contains(module));
        }
    }
}

void BtBookshelfTreeModel::setCheckable(bool checkable) {
    if (m_checkable == checkable) return;
    m_checkable = checkable;
    if (m_sourceModel == 0) return;

    // Notify views that flags changed for all items:
    QModelIndexList queue;
    queue.append(QModelIndex());
    do {
        QModelIndex parent(queue.takeFirst());
        int numChildren(rowCount(parent));
        emit dataChanged(index(0, 0, parent),
                         index(numChildren - 1, 0, parent));
        for (int i(0); i < numChildren; i++) {
            QModelIndex childIndex(index(i, 0, parent));
            if (rowCount(childIndex) > 0) {
                queue.append(childIndex);
            }
        }
    }
    while (!queue.isEmpty());
}

void BtBookshelfTreeModel::addModule(CSwordModuleInfo *module, bool checked) {
    if (m_modules.contains(module)) return;
    Grouping g(m_groupingOrder);
    addModule(module, QModelIndex(), g, checked);

    /**
      \bug Calling reset() shouldn't be necessary here, but omitting it will
           will break things like switching to a grouped layout or installing
           new modules. As a side effect, all attached views will also reset
           themselves.
    */
    reset();
}

void BtBookshelfTreeModel::addModule(CSwordModuleInfo *module,
                                     QModelIndex parentIndex,
                                     Grouping &intermediateGrouping,
                                     bool checked) {
    Q_ASSERT(module != 0);

    if (!intermediateGrouping.empty()) {
        QModelIndex newIndex;
        switch (intermediateGrouping.front()) {
            case GROUP_CATEGORY:
                newIndex = getGroup<CategoryItem>(module, parentIndex);
                break;
            case GROUP_LANGUAGE:
                newIndex = getGroup<LanguageItem>(module, parentIndex);
                break;
            case GROUP_INDEXING:
                newIndex = getGroup<IndexingItem>(module, parentIndex);
                break;
        }
        intermediateGrouping.pop_front();
        addModule(module, newIndex, intermediateGrouping, checked);
    }
    else {
        Item *parentItem(getItem(parentIndex));
        ModuleItem *newItem(new ModuleItem(module, this));
        newItem->setCheckState(checked ? Qt::Checked : Qt::Unchecked);
        const int newIndex(parentItem->indexFor(newItem));

        // Actually do the insertion:
        beginInsertRows(parentIndex, newIndex, newIndex);
        parentItem->insertChild(newIndex, newItem);
        m_modules.insert(module, newItem);
        if (checked) {
            // Add to checked modules cache
            m_checkedModulesCache.insert(module);
        }
        endInsertRows();

        // Reset parent item check states, if needed:
        resetParentCheckStates(parentIndex);
    }
}

void BtBookshelfTreeModel::removeModule(CSwordModuleInfo *module) {
    if (!m_modules.contains(module)) return;

    Item *i(m_modules[module]);

    // Set i to be the lowest item (including empty groups) to remove:
    Q_ASSERT(i->parent() != 0);
    while (i->parent() != m_rootItem && i->parent()->children().size() <= 1) {
        i = i->parent();
    }
    Q_ASSERT(i != 0);
    Q_ASSERT(i->parent() != 0);

    // Calculate item indexes:
    int index(i->childIndex());
    QModelIndex parentIndex(getIndex(i->parent()));

    // Actually remove the item:
    beginRemoveRows(parentIndex, index, index);
    delete i->parent()->children().takeAt(index);
    m_modules.remove(module);
    m_checkedModulesCache.remove(module);
    endRemoveRows();

    // Reset parent item check states, if needed:
    resetParentCheckStates(parentIndex);
}

Item *BtBookshelfTreeModel::getItem(const QModelIndex &index) const {
    if (index.isValid()) {
        Item *item(static_cast<Item*>(index.internalPointer()));
        Q_ASSERT(item != 0);
        return item;
    }
    else {
        return m_rootItem;
    }
}

QModelIndex BtBookshelfTreeModel::getIndex(const BookshelfModel::Item *item) {
    Q_ASSERT(item != 0);

    QList<int> indexes;
    for (;;) {
        int i(item->childIndex());
        if (i < 0) break;
        indexes.append(i);
        item = item->parent();
    }

    QModelIndex i;
    while (!indexes.isEmpty()) {
        i = index(indexes.takeLast(), 0, i);
    }
    return i;
}

void BtBookshelfTreeModel::resetParentCheckStates(QModelIndex parentIndex) {
    for ( ; parentIndex.isValid(); parentIndex = parentIndex.parent()) {
        Item *parentItem(static_cast<Item*>(parentIndex.internalPointer()));
        Q_ASSERT(parentItem != 0);

        Qt::CheckState oldState(parentItem->checkState());
        bool haveCheckedChildren(false);
        bool haveUncheckedChildren(false);
        for (int i(0); i < parentItem->children().size(); i++) {
            Qt::CheckState state(parentItem->children().at(i)->checkState());
            if (state == Qt::PartiallyChecked) {
                haveCheckedChildren = true;
                haveUncheckedChildren = true;
                break;
            }
            else if (state == Qt::Checked) {
                haveCheckedChildren = true;
                if (haveUncheckedChildren) break;
            }
            else if (state == Qt::Unchecked) {
                haveUncheckedChildren = true;
                if (haveCheckedChildren) break;
            }
        }

        Qt::CheckState newState;
        if (haveCheckedChildren) {
            if (haveUncheckedChildren) {
                newState = Qt::PartiallyChecked;
            }
            else {
                newState = Qt::Checked;
            }
        }
        else {
            newState = Qt::Unchecked;
        }
        if (newState == oldState) break;

        parentItem->setCheckState(newState);
        emit dataChanged(parentIndex, parentIndex);
    } // for ( ; parentIndex.isValid(); parentIndex = parentIndex.parent())
}

void BtBookshelfTreeModel::moduleDataChanged(const QModelIndex &topLeft,
        const QModelIndex &bottomRight) {
    typedef BtBookshelfModel BM;
    static const BM::ModuleRole PR(BM::ModulePointerRole);

    Q_ASSERT(!topLeft.parent().isValid());
    Q_ASSERT(!bottomRight.parent().isValid());
    Q_ASSERT(topLeft.column() == 0 && bottomRight.column() == 0);

    for (int i(topLeft.row()); i <= bottomRight.row(); i++) {
        QModelIndex moduleIndex(m_sourceModel->index(i, 0, topLeft.parent()));
        QVariant data(m_sourceModel->data(moduleIndex, PR));
        CSwordModuleInfo *module((CSwordModuleInfo *) (data.value<void*>()));
        QModelIndex itemIndex(getIndex(m_modules[module]));
        Q_ASSERT(itemIndex.isValid());

        emit dataChanged(itemIndex, itemIndex);

        /*
          Also emit signals for parent items because the change might alter them
          as well, e.g. isHidden()
        */
        do {
            itemIndex = itemIndex.parent();
            emit dataChanged(itemIndex, itemIndex);
        }
        while (itemIndex.isValid());
    }
}

void BtBookshelfTreeModel::moduleInserted(const QModelIndex &parent, int start,
        int end) {
    typedef BtBookshelfModel BM;
    static const BM::ModuleRole PR(BM::ModulePointerRole);
    static const BM::ModuleRole HR(BM::ModuleHiddenRole);
    static const BM::ModuleRole IR(BM::ModuleHasIndexRole);

    for (int i(start); i <= end; i++) {
        QModelIndex moduleIndex(m_sourceModel->index(i, 0, parent));
        QVariant data(m_sourceModel->data(moduleIndex, PR));
        CSwordModuleInfo *module((CSwordModuleInfo *) (data.value<void*>()));

        bool checked;
        if (m_defaultChecked == MODULE_HIDDEN) {
            checked = !m_sourceModel->data(moduleIndex, HR).toBool();
        }
        else if (m_defaultChecked == MODULE_INDEXED) {
            checked = !m_sourceModel->data(moduleIndex, IR).toBool();
        }
        else {
            checked = (m_defaultChecked == CHECKED);
        }
        m_sourceIndexMap[module] = moduleIndex;
        addModule(module, checked);
    }
}

void BtBookshelfTreeModel::moduleRemoved(const QModelIndex &parent, int start,
        int end) {
    typedef BtBookshelfModel BM;
    static const BM::ModuleRole PR(BM::ModulePointerRole);

    for (int i(start); i <= end; i++) {
        QModelIndex moduleIndex(m_sourceModel->index(i, 0, parent));
        QVariant data(m_sourceModel->data(moduleIndex, PR));
        CSwordModuleInfo *module((CSwordModuleInfo *) (data.value<void*>()));

        removeModule(module);
        m_sourceIndexMap.remove(module);
    }
}

QDataStream &operator<<(QDataStream &os, const BtBookshelfTreeModel::Grouping &o) {
    os << o.size();
    Q_FOREACH(BtBookshelfTreeModel::Group g, o) {
        os << (int) g;
    }
    return os;
}

QDataStream &operator>>(QDataStream &is, BtBookshelfTreeModel::Grouping &o) {
    int s;
    is >> s;
    for (int i(0); i < s; i++) {
        int g;
        is >> g;
        o.append((BtBookshelfTreeModel::Group) g);
    }
    return is;
}