summaryrefslogtreecommitdiff
path: root/src/backend/bookshelfmodel/btbookshelfmodel.cpp
blob: 6882b900e4463b6a4b9a9c75cf6aaa97c1a24d2d (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
/*********
*
* 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/btbookshelfmodel.h"

#include <QSet>
#include "util/cresmgr.h"
#include "util/directory.h"


BtBookshelfModel::BtBookshelfModel(QObject *parent)
        : QAbstractListModel(parent) {
    // Intentionally empty
}

BtBookshelfModel::~BtBookshelfModel() {
    // Intentionally empty
}

int BtBookshelfModel::rowCount(const QModelIndex &parent) const {
    return m_data.size();
}

QVariant BtBookshelfModel::data(CSwordModuleInfo *module, int role) const {
    switch (role) {
        case ModuleNameRole: // Qt::DisplayRole
            return module->name();
        case ModuleIconRole: // Qt::DecorationRole
            return moduleIcon(module);
        case ModulePointerRole:
            return qVariantFromValue((void*) module);
        case ModuleCategoryRole:
            return QVariant::fromValue(module->category());
        case ModuleLanguageRole:
            return QVariant(); /// \todo Unimplemented
        case ModuleHiddenRole:
            return module->isHidden();
        default:
            return QVariant();
    }
}

QVariant BtBookshelfModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid() || index.column() != 0 || index.parent().isValid()) {
        return QVariant();
    }
    int row(index.row());
    if (row >= m_data.size()) return QVariant();

    return data(m_data.at(row), role);
}

QVariant BtBookshelfModel::headerData(int section, Qt::Orientation orientation,
                                      int role) const {
    if (role == Qt::DisplayRole && orientation == Qt::Horizontal &&
            section == 0) {
        return tr("Module");
    }

    return QVariant();
}

bool BtBookshelfModel::setData(const QModelIndex &index, const QVariant &value,
                               int role) {
    int row(index.row());
    if (role == ModuleHiddenRole && row >= 0 && row < m_data.size()
        && index.column() == 0)
    {
        /*
          Emitting dataChanged here is actually mandatory, but were not doing it
          directly. Since we're connected to the module, changing its hidden
          state will emit a signal we catch in moduleHidden(), which in turn is
          what will actually emit dataChanged().
        */
        return m_data.at(row)->setHidden(value.toBool());
    }
    return false;
}

QIcon BtBookshelfModel::moduleIcon(const CSwordModuleInfo *m) {
    namespace DU = util::directory;

    /// \todo Make CSwordModuleInfo::isLocked() const and remove const_cast:
    CSwordModuleInfo *module(const_cast<CSwordModuleInfo*>(m));

    CSwordModuleInfo::Category cat(module->category());
    switch (cat) {
        case CSwordModuleInfo::Bibles:
            if (module->isLocked()) {
                return DU::getIcon(CResMgr::modules::bible::icon_locked);
            } else {
                return DU::getIcon(CResMgr::modules::bible::icon_unlocked);
            }
        case CSwordModuleInfo::Commentaries:
            if (module->isLocked()) {
                return DU::getIcon(CResMgr::modules::commentary::icon_locked);
            } else {
                return DU::getIcon(CResMgr::modules::commentary::icon_unlocked);
            }
        case CSwordModuleInfo::Lexicons:
            if (module->isLocked()) {
                return DU::getIcon(CResMgr::modules::lexicon::icon_locked);
            } else {
                return DU::getIcon(CResMgr::modules::lexicon::icon_unlocked);
            }
        case CSwordModuleInfo::Books:
            if (module->isLocked()) {
                return DU::getIcon(CResMgr::modules::book::icon_locked);
            } else {
                return DU::getIcon(CResMgr::modules::book::icon_unlocked);
            }
        case CSwordModuleInfo::Cult:
        case CSwordModuleInfo::Images:
        case CSwordModuleInfo::DailyDevotional:
        case CSwordModuleInfo::Glossary:
        case CSwordModuleInfo::UnknownCategory:
        default:
            return categoryIcon(cat);
    }
}

QIcon BtBookshelfModel::categoryIcon(const CSwordModuleInfo::Category &category) {
    namespace DU = util::directory;

    switch (category) {
        case CSwordModuleInfo::Bibles:
            return DU::getIcon(CResMgr::categories::bibles::icon);
        case CSwordModuleInfo::Commentaries:
            return DU::getIcon(CResMgr::categories::commentaries::icon);
        case CSwordModuleInfo::Books:
            return DU::getIcon(CResMgr::categories::books::icon);
        case CSwordModuleInfo::Cult:
            return DU::getIcon(CResMgr::categories::cults::icon);
        case CSwordModuleInfo::Images:
            return DU::getIcon(CResMgr::categories::images::icon);
        case CSwordModuleInfo::DailyDevotional:
            return DU::getIcon(CResMgr::categories::dailydevotional::icon);
        case CSwordModuleInfo::Lexicons:
            return DU::getIcon(CResMgr::categories::lexicons::icon);
        case CSwordModuleInfo::Glossary:
            return DU::getIcon(CResMgr::categories::glossary::icon);
        case CSwordModuleInfo::UnknownCategory:
        default:
            return QIcon();
    }
}

QString BtBookshelfModel::categoryName(
    const CSwordModuleInfo::Category &category) {
    switch (category) {
        case CSwordModuleInfo::Bibles:
            return tr("Bibles");
        case CSwordModuleInfo::Commentaries:
            return tr("Commentaries");
        case CSwordModuleInfo::Books:
            return tr("Books");
        case CSwordModuleInfo::Cult:
            return tr("Cults/Unorthodox");
        case CSwordModuleInfo::Images:
            return tr("Maps and Images");
        case CSwordModuleInfo::DailyDevotional:
            return tr("Daily Devotionals");
        case CSwordModuleInfo::Lexicons:
            return  tr("Lexicons and Dictionaries");
        case CSwordModuleInfo::Glossary:
            return tr("Glossaries");
        default:
            return tr("Unknown");
    }
}

QString BtBookshelfModel::languageName(
    const CLanguageMgr::Language *language) {
    return language->translatedName();
}

void BtBookshelfModel::clear(bool destroy) {
    if (m_data.size() <= 0) return;

    beginRemoveRows(QModelIndex(), 0, m_data.size() - 1);
    if (destroy) {
        qDeleteAll(m_data);
    }
    m_data.clear();
    endRemoveRows();
}

void BtBookshelfModel::addModule(CSwordModuleInfo * const module) {
    Q_ASSERT(module != 0);

    if (m_data.contains(module)) return;

    const int index(m_data.size());
    beginInsertRows(QModelIndex(), index, index);
    m_data.append(module);
    connect(module, SIGNAL(hiddenChanged(bool)),
            this, SLOT(moduleHidden(bool)));
    endInsertRows();
}

void BtBookshelfModel::addModules(const QList<CSwordModuleInfo *> &modules) {
    addModules(modules.toSet());
}

void BtBookshelfModel::addModules(const QSet<CSwordModuleInfo *> &modules) {
    QList<CSwordModuleInfo *> newModules;
    Q_FOREACH(CSwordModuleInfo *module, modules) {
        if (!m_data.contains(module)) {
            newModules.append(module);
        }
    }

    if (newModules.isEmpty()) return;

    beginInsertRows(QModelIndex(), m_data.size(),
                    m_data.size() + newModules.size() - 1);
    Q_FOREACH(CSwordModuleInfo *module, newModules) {
        m_data.append(module);
        connect(module, SIGNAL(hiddenChanged(bool)),
                this, SLOT(moduleHidden(bool)));
    }
    endInsertRows();
}

void BtBookshelfModel::removeModule(CSwordModuleInfo * const module,
                                    bool destroy)
{
    const int index(m_data.indexOf(module));
    if (index == -1) return;

    beginRemoveRows(QModelIndex(), index, index);
    disconnect(module, SIGNAL(hiddenChanged(bool)),
               this, SLOT(moduleHidden(bool)));
    m_data.removeAt(index);
    endRemoveRows();
    if (destroy) delete module;
}

void BtBookshelfModel::removeModules(const QList<CSwordModuleInfo *> &modules,
                                     bool destroy)
{
    removeModules(modules.toSet(), destroy);
}

void BtBookshelfModel::removeModules(const QSet<CSwordModuleInfo *> &modules,
                                     bool destroy)
{
    // This is inefficient, since signals are emitted for each removed module:
    Q_FOREACH(CSwordModuleInfo *module, modules) {
        removeModule(module, destroy);
    }
}

CSwordModuleInfo* BtBookshelfModel::getModule(const QString &name) const {
    Q_FOREACH(CSwordModuleInfo *module, m_data) {
        if (module->name() == name) return module;
    }
    return 0;
}

void BtBookshelfModel::moduleHidden(bool) {
    Q_ASSERT(qobject_cast<CSwordModuleInfo*>(sender()) != 0);

    CSwordModuleInfo *module(static_cast<CSwordModuleInfo*>(sender()));
    Q_ASSERT(m_data.count(module) == 1);

    QModelIndex i(index(m_data.indexOf(module), 0));
    emit dataChanged(i, i);
}