diff options
Diffstat (limited to 'src/backend/bookshelfmodel/btbookshelfmodel.cpp')
-rw-r--r-- | src/backend/bookshelfmodel/btbookshelfmodel.cpp | 130 |
1 files changed, 69 insertions, 61 deletions
diff --git a/src/backend/bookshelfmodel/btbookshelfmodel.cpp b/src/backend/bookshelfmodel/btbookshelfmodel.cpp index 322b22a..c19e429 100644 --- a/src/backend/bookshelfmodel/btbookshelfmodel.cpp +++ b/src/backend/bookshelfmodel/btbookshelfmodel.cpp @@ -4,7 +4,7 @@ * * This file is part of BibleTime's source code, http://www.bibletime.info/. * -* Copyright 1999-2011 by the BibleTime developers. +* Copyright 1999-2014 by the BibleTime developers. * The BibleTime source code is licensed under the GNU General Public License * version 2.0. * @@ -17,29 +17,22 @@ #include "util/macros.h" -BtBookshelfModel::BtBookshelfModel(QObject *parent) - : QAbstractListModel(parent) { - // Intentionally empty -} - -BtBookshelfModel::~BtBookshelfModel() { - // Intentionally empty -} - -int BtBookshelfModel::rowCount(const QModelIndex &parent) const { - if (parent.isValid()) return 0; +int BtBookshelfModel::rowCount(const QModelIndex & parent) const { + if (parent.isValid()) + return 0; return m_data.size(); } -QVariant BtBookshelfModel::data(CSwordModuleInfo *module, int role) const { +QVariant BtBookshelfModel::data(CSwordModuleInfo * module, int role) const { + Q_ASSERT(module); switch (role) { case ModuleNameRole: // Qt::DisplayRole return module->name(); case ModuleIconRole: // Qt::DecorationRole - return CSwordModuleInfo::moduleIcon(module); + return CSwordModuleInfo::moduleIcon(*module); case ModulePointerRole: - return qVariantFromValue((void*) module); + return qVariantFromValue(static_cast<void *>(module)); case ModuleCategoryRole: return QVariant::fromValue(module->category()); case ModuleLanguageRole: @@ -64,30 +57,40 @@ QVariant BtBookshelfModel::data(CSwordModuleInfo *module, int role) const { } QVariant BtBookshelfModel::data(const QModelIndex &index, int role) const { - if (!index.isValid() || index.column() != 0 || index.parent().isValid()) { + if (!index.isValid() || index.column() != 0 || index.parent().isValid()) + return QVariant(); + + int row = index.row(); + if (row >= m_data.size()) 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) { +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) { +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 @@ -100,12 +103,12 @@ bool BtBookshelfModel::setData(const QModelIndex &index, const QVariant &value, } void BtBookshelfModel::clear(bool destroy) { - if (m_data.size() <= 0) return; + if (m_data.size() <= 0) + return; beginRemoveRows(QModelIndex(), 0, m_data.size() - 1); - if (destroy) { + if (destroy) qDeleteAll(m_data); - } m_data.clear(); endRemoveRows(); } @@ -113,7 +116,8 @@ void BtBookshelfModel::clear(bool destroy) { void BtBookshelfModel::addModule(CSwordModuleInfo * const module) { Q_ASSERT(module != 0); - if (m_data.contains(module)) return; + if (m_data.contains(module)) + return; const int index(m_data.size()); beginInsertRows(QModelIndex(), index, index); @@ -127,23 +131,23 @@ void BtBookshelfModel::addModule(CSwordModuleInfo * const module) { endInsertRows(); } -void BtBookshelfModel::addModules(const QList<CSwordModuleInfo *> &modules) { +void BtBookshelfModel::addModules(const QList<CSwordModuleInfo *> & modules) { addModules(modules.toSet()); } -void BtBookshelfModel::addModules(const QSet<CSwordModuleInfo *> &modules) { +void BtBookshelfModel::addModules(const QSet<CSwordModuleInfo *> & modules) { QList<CSwordModuleInfo *> newModules; - Q_FOREACH(CSwordModuleInfo *module, modules) { - if (!m_data.contains(module)) { + Q_FOREACH (CSwordModuleInfo * module, modules) + if (!m_data.contains(module)) newModules.append(module); - } - } - if (newModules.isEmpty()) return; + if (newModules.isEmpty()) + return; - beginInsertRows(QModelIndex(), m_data.size(), + beginInsertRows(QModelIndex(), + m_data.size(), m_data.size() + newModules.size() - 1); - Q_FOREACH(CSwordModuleInfo *module, newModules) { + Q_FOREACH (CSwordModuleInfo * module, newModules) { m_data.append(module); connect(module, SIGNAL(hiddenChanged(bool)), this, SLOT(moduleHidden(bool))); @@ -157,8 +161,9 @@ void BtBookshelfModel::addModules(const QSet<CSwordModuleInfo *> &modules) { void BtBookshelfModel::removeModule(CSwordModuleInfo * const module, bool destroy) { - const int index(m_data.indexOf(module)); - if (index == -1) return; + const int index = m_data.indexOf(module); + if (index == -1) + return; beginRemoveRows(QModelIndex(), index, index); disconnect(module, SIGNAL(hiddenChanged(bool)), @@ -169,48 +174,51 @@ void BtBookshelfModel::removeModule(CSwordModuleInfo * const module, this, SLOT(moduleUnlocked(bool))); m_data.removeAt(index); endRemoveRows(); - if (destroy) delete module; + if (destroy) + delete module; } -void BtBookshelfModel::removeModules(const QList<CSwordModuleInfo *> &modules, - bool destroy) { +void BtBookshelfModel::removeModules(const QList<CSwordModuleInfo *> & modules, + bool destroy) +{ removeModules(modules.toSet(), destroy); } -void BtBookshelfModel::removeModules(const QSet<CSwordModuleInfo *> &modules, - bool 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) { + Q_FOREACH (CSwordModuleInfo * module, modules) removeModule(module, destroy); - } } -CSwordModuleInfo* BtBookshelfModel::getModule(const QString &name) const { - Q_FOREACH(CSwordModuleInfo *module, m_data) { - if (UNLIKELY(module->name() == name)) return module; - } +CSwordModuleInfo * BtBookshelfModel::getModule(const QString & name) const { + Q_FOREACH (CSwordModuleInfo * module, m_data) + if (UNLIKELY(module->name() == name)) + return module; + return 0; } void BtBookshelfModel::moduleHidden(bool) { - Q_ASSERT(qobject_cast<CSwordModuleInfo*>(sender()) != 0); + Q_ASSERT(qobject_cast<CSwordModuleInfo *>(sender()) != 0); - moduleDataChanged(static_cast<CSwordModuleInfo*>(sender())); + moduleDataChanged(static_cast<CSwordModuleInfo *>(sender())); } void BtBookshelfModel::moduleIndexed(bool) { - Q_ASSERT(qobject_cast<CSwordModuleInfo*>(sender()) != 0); + Q_ASSERT(qobject_cast<CSwordModuleInfo *>(sender()) != 0); - moduleDataChanged(static_cast<CSwordModuleInfo*>(sender())); + moduleDataChanged(static_cast<CSwordModuleInfo *>(sender())); } void BtBookshelfModel::moduleUnlocked(bool) { - Q_ASSERT(qobject_cast<CSwordModuleInfo*>(sender()) != 0); + Q_ASSERT(qobject_cast<CSwordModuleInfo *>(sender()) != 0); - moduleDataChanged(static_cast<CSwordModuleInfo*>(sender())); + moduleDataChanged(static_cast<CSwordModuleInfo *>(sender())); } -void BtBookshelfModel::moduleDataChanged(CSwordModuleInfo *module) { +void BtBookshelfModel::moduleDataChanged(CSwordModuleInfo * module) { Q_ASSERT(m_data.count(module) == 1); QModelIndex i(index(m_data.indexOf(module), 0)); |