summaryrefslogtreecommitdiff
path: root/src/backend/bookshelfmodel
diff options
context:
space:
mode:
authorRoberto C. Sanchez <roberto@connexer.com>2014-10-21 22:58:34 -0400
committerRoberto C. Sanchez <roberto@connexer.com>2014-10-21 22:58:34 -0400
commit1ea03c0fce8066c1e22188447b4a6ca4dcef1201 (patch)
tree1ad46980fdca402062502b20b7e16468b89393f8 /src/backend/bookshelfmodel
parent579657c8cb4ecd8a313221e70bdbbc7267f20286 (diff)
Imported Upstream version 2.10.1
Diffstat (limited to 'src/backend/bookshelfmodel')
-rw-r--r--src/backend/bookshelfmodel/btbookshelffiltermodel.cpp158
-rw-r--r--src/backend/bookshelfmodel/btbookshelffiltermodel.h210
-rw-r--r--src/backend/bookshelfmodel/btbookshelfmodel.cpp130
-rw-r--r--src/backend/bookshelfmodel/btbookshelfmodel.h295
-rw-r--r--src/backend/bookshelfmodel/btbookshelftreemodel.cpp534
-rw-r--r--src/backend/bookshelfmodel/btbookshelftreemodel.h308
-rw-r--r--src/backend/bookshelfmodel/categoryitem.cpp24
-rw-r--r--src/backend/bookshelfmodel/categoryitem.h33
-rw-r--r--src/backend/bookshelfmodel/indexingitem.cpp24
-rw-r--r--src/backend/bookshelfmodel/indexingitem.h25
-rw-r--r--src/backend/bookshelfmodel/item.cpp48
-rw-r--r--src/backend/bookshelfmodel/item.h266
-rw-r--r--src/backend/bookshelfmodel/languageitem.cpp15
-rw-r--r--src/backend/bookshelfmodel/languageitem.h26
-rw-r--r--src/backend/bookshelfmodel/moduleitem.cpp11
-rw-r--r--src/backend/bookshelfmodel/moduleitem.h46
16 files changed, 1119 insertions, 1034 deletions
diff --git a/src/backend/bookshelfmodel/btbookshelffiltermodel.cpp b/src/backend/bookshelfmodel/btbookshelffiltermodel.cpp
index 182d337..320d693 100644
--- a/src/backend/bookshelfmodel/btbookshelffiltermodel.cpp
+++ b/src/backend/bookshelfmodel/btbookshelffiltermodel.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.
*
@@ -15,25 +15,26 @@
#include "backend/bookshelfmodel/btbookshelfmodel.h"
-BtBookshelfFilterModel::BtBookshelfFilterModel(QObject *parent)
- : QSortFilterProxyModel(parent), m_enabled(true),
- m_nameFilterRole(BtBookshelfModel::ModuleNameRole), m_nameFilterColumn(0),
- m_nameFilterCase(Qt::CaseInsensitive),
- m_hiddenFilterRole(BtBookshelfModel::ModuleHiddenRole),
- m_hiddenFilterColumn(0), m_showHidden(false), m_showShown(true),
- m_categoryFilter(CSwordModuleInfo::AllCategories),
- m_categoryFilterRole(BtBookshelfModel::ModuleCategoryRole),
- m_categoryFilterColumn(0)
+BtBookshelfFilterModel::BtBookshelfFilterModel(QObject * parent)
+ : QSortFilterProxyModel(parent)
+ , m_enabled(true)
+ , m_nameFilterRole(BtBookshelfModel::ModuleNameRole)
+ , m_nameFilterColumn(0)
+ , m_nameFilterCase(Qt::CaseInsensitive)
+ , m_hiddenFilterRole(BtBookshelfModel::ModuleHiddenRole)
+ , m_hiddenFilterColumn(0)
+ , m_showHidden(false)
+ , m_showShown(true)
+ , m_categoryFilter(CSwordModuleInfo::AllCategories)
+ , m_categoryFilterRole(BtBookshelfModel::ModuleCategoryRole)
+ , m_categoryFilterColumn(0)
{
setDynamicSortFilter(true);
}
-BtBookshelfFilterModel::~BtBookshelfFilterModel() {
- // Intentionally empty
-}
-
void BtBookshelfFilterModel::setEnabled(bool enable) {
- if (enable == m_enabled) return;
+ if (enable == m_enabled)
+ return;
m_enabled = enable;
invalidateFilter();
}
@@ -41,25 +42,29 @@ void BtBookshelfFilterModel::setEnabled(bool enable) {
// Name filter:
void BtBookshelfFilterModel::setNameFilterRole(int role) {
- if (m_nameFilterRole == role) return;
+ if (m_nameFilterRole == role)
+ return;
m_nameFilterRole = role;
invalidateFilter();
}
void BtBookshelfFilterModel::setNameFilterKeyColumn(int column) {
- if (m_nameFilterColumn == column) return;
+ if (m_nameFilterColumn == column)
+ return;
m_nameFilterColumn = column;
invalidateFilter();
}
void BtBookshelfFilterModel::setNameFilterFixedString(const QString &filter) {
- if (m_nameFilter == filter) return;
+ if (m_nameFilter == filter)
+ return;
m_nameFilter = filter;
invalidateFilter();
}
void BtBookshelfFilterModel::setNameFilterCase(Qt::CaseSensitivity value) {
- if (m_nameFilterCase == value) return;
+ if (m_nameFilterCase == value)
+ return;
m_nameFilterCase = value;
invalidateFilter();
}
@@ -67,25 +72,29 @@ void BtBookshelfFilterModel::setNameFilterCase(Qt::CaseSensitivity value) {
// Hidden filter:
void BtBookshelfFilterModel::setHiddenFilterRole(int role) {
- if (m_hiddenFilterRole == role) return;
+ if (m_hiddenFilterRole == role)
+ return;
m_hiddenFilterRole = role;
invalidateFilter();
}
void BtBookshelfFilterModel::setHiddenFilterKeyColumn(int column) {
- if (m_hiddenFilterColumn == column) return;
+ if (m_hiddenFilterColumn == column)
+ return;
m_hiddenFilterColumn = column;
invalidateFilter();
}
void BtBookshelfFilterModel::setShowHidden(bool show) {
- if (m_showHidden == show) return;
+ if (m_showHidden == show)
+ return;
m_showHidden = show;
invalidateFilter();
}
void BtBookshelfFilterModel::setShowShown(bool show) {
- if (m_showShown == show) return;
+ if (m_showShown == show)
+ return;
m_showShown = show;
invalidateFilter();
}
@@ -93,21 +102,24 @@ void BtBookshelfFilterModel::setShowShown(bool show) {
// Category filter:
void BtBookshelfFilterModel::setCategoryFilterRole(int role) {
- if (m_categoryFilterRole == role) return;
+ if (m_categoryFilterRole == role)
+ return;
m_categoryFilterRole = role;
invalidateFilter();
}
void BtBookshelfFilterModel::setCategoryFilterKeyColumn(int column) {
- if (m_categoryFilterColumn == column) return;
+ if (m_categoryFilterColumn == column)
+ return;
m_categoryFilterColumn = column;
invalidateFilter();
}
void BtBookshelfFilterModel::setShownCategories(
- const CSwordModuleInfo::Categories &categories)
+ const CSwordModuleInfo::Categories & categories)
{
- if (m_categoryFilter == categories) return;
+ if (m_categoryFilter == categories)
+ return;
m_categoryFilter = categories;
invalidateFilter();
}
@@ -115,20 +127,27 @@ void BtBookshelfFilterModel::setShownCategories(
// Filtering:
bool BtBookshelfFilterModel::filterAcceptsRow(int row,
- const QModelIndex &parent) const
+ const QModelIndex & parent) const
{
- if (!m_enabled) return true;
+ if (!m_enabled)
+ return true;
- if (!hiddenFilterAcceptsRow(row, parent)) return false;
- if (!nameFilterAcceptsRow(row, parent)) return false;
- if (!categoryFilterAcceptsRow(row, parent)) return false;
+ if (!hiddenFilterAcceptsRow(row, parent))
+ return false;
+ if (!nameFilterAcceptsRow(row, parent))
+ return false;
+ if (!categoryFilterAcceptsRow(row, parent))
+ return false;
return true;
}
-bool BtBookshelfFilterModel::nameFilterAcceptsRow(int row, const QModelIndex &parent) const {
- if (m_nameFilter.isEmpty()) return true;
+bool BtBookshelfFilterModel::nameFilterAcceptsRow(int row,
+ const QModelIndex & parent) const
+{
+ if (m_nameFilter.isEmpty())
+ return true;
- const QAbstractItemModel *m(sourceModel());
+ const QAbstractItemModel * const m = sourceModel();
Q_ASSERT(m != 0);
QModelIndex itemIndex(m->index(row, m_nameFilterColumn, parent));
@@ -137,59 +156,54 @@ bool BtBookshelfFilterModel::nameFilterAcceptsRow(int row, const QModelIndex &pa
QVariant data(m->data(itemIndex, m_nameFilterRole));
return data.toString().contains(m_nameFilter, m_nameFilterCase);
}
- else {
- for (int i(0); i < numChildren; i++) {
- if (filterAcceptsRow(i, itemIndex)) return true;
- }
- return false;
- }
+
+ for (int i = 0; i < numChildren; i++)
+ if (filterAcceptsRow(i, itemIndex))
+ return true;
+ return false;
}
-bool BtBookshelfFilterModel::hiddenFilterAcceptsRow(int row, const QModelIndex &parent) const
+bool BtBookshelfFilterModel::hiddenFilterAcceptsRow(int row,
+ const QModelIndex & parent) const
{
- if (m_showHidden && m_showShown) return true;
+ if (m_showHidden && m_showShown)
+ return true;
- typedef Qt::CheckState CS;
-
- QAbstractItemModel *m(sourceModel());
+ const QAbstractItemModel * const m = sourceModel();
Q_ASSERT(m != 0);
- QModelIndex itemIndex(m->index(row, m_hiddenFilterColumn, parent));
- int numChildren(m->rowCount(itemIndex));
+ const QModelIndex itemIndex = m->index(row, m_hiddenFilterColumn, parent);
+ const int numChildren = m->rowCount(itemIndex);
if (numChildren == 0) {
- if ((CS) m->data(itemIndex, m_hiddenFilterRole).toBool()) {
+ if (static_cast<Qt::CheckState>(m->data(itemIndex, m_hiddenFilterRole).toBool()))
return m_showHidden;
- }
- else {
- return m_showShown;
- }
- }
- else {
- for (int i(0); i < numChildren; i++) {
- if (filterAcceptsRow(i, itemIndex)) return true;
- }
- return false;
+ return m_showShown;
}
+
+ for (int i = 0; i < numChildren; i++)
+ if (filterAcceptsRow(i, itemIndex))
+ return true;
+ return false;
}
bool BtBookshelfFilterModel::categoryFilterAcceptsRow(int row,
- const QModelIndex &parent) const
+ const QModelIndex & parent) const
{
- if (m_categoryFilter == CSwordModuleInfo::AllCategories) return true;
+ if (m_categoryFilter == CSwordModuleInfo::AllCategories)
+ return true;
- QAbstractItemModel *m(sourceModel());
+ const QAbstractItemModel * const m = sourceModel();
Q_ASSERT(m != 0);
- QModelIndex itemIndex(m->index(row, m_categoryFilterColumn, parent));
- int numChildren(m->rowCount(itemIndex));
+ const QModelIndex itemIndex(m->index(row, m_categoryFilterColumn, parent));
+ const int numChildren(m->rowCount(itemIndex));
if (numChildren == 0) {
- int cat = m->data(itemIndex, m_categoryFilterRole).toInt();
- return m_categoryFilter.testFlag((CSwordModuleInfo::Category) cat);
- }
- else {
- for (int i(0); i < numChildren; i++) {
- if (filterAcceptsRow(i, itemIndex)) return true;
- }
- return false;
+ const int cat = m->data(itemIndex, m_categoryFilterRole).toInt();
+ return m_categoryFilter.testFlag(static_cast<CSwordModuleInfo::Category>(cat));
}
+
+ for (int i = 0; i < numChildren; i++)
+ if (filterAcceptsRow(i, itemIndex))
+ return true;
+ return false;
}
diff --git a/src/backend/bookshelfmodel/btbookshelffiltermodel.h b/src/backend/bookshelfmodel/btbookshelffiltermodel.h
index 55322ed..b1b22e6 100644
--- a/src/backend/bookshelfmodel/btbookshelffiltermodel.h
+++ b/src/backend/bookshelfmodel/btbookshelffiltermodel.h
@@ -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.
*
@@ -19,109 +19,113 @@
class BtBookshelfFilterModel: public QSortFilterProxyModel {
+
Q_OBJECT
- public:
- BtBookshelfFilterModel(QObject *parent = 0);
- virtual ~BtBookshelfFilterModel();
-
- // Common methods:
- inline bool enabled() const {
- return m_enabled;
- }
-
- virtual bool filterAcceptsRow(int row, const QModelIndex &parent) const;
-
- // Name filter:
- inline int nameFilterRole() const {
- return m_nameFilterRole;
- }
-
- inline int nameFilterKeyColumn() const {
- return m_nameFilterColumn;
- }
-
- inline const QString &nameFilter() const {
- return m_nameFilter;
- }
-
- inline const Qt::CaseSensitivity nameFilterCase() const {
- return m_nameFilterCase;
- }
-
- // Hidden filter:
- int hiddenFilterRole() const {
- return m_hiddenFilterRole;
- }
-
- int hiddenFilterKeyColumn() const {
- return m_hiddenFilterColumn;
- }
-
- inline bool showHidden() const {
- return m_showHidden;
- }
-
- inline bool showShown() const {
- return m_showShown;
- }
-
- // Category filter:
- int categoryFilterRole() const {
- return m_categoryFilterRole;
- }
-
- int categoryFilterKeyColumn() const {
- return m_categoryFilterColumn;
- }
-
- inline CSwordModuleInfo::Categories shownCategories() const {
- return m_categoryFilter;
- }
-
- public slots:
- void setEnabled(bool enable);
-
- // Name filter:
- void setNameFilterRole(int role);
- void setNameFilterKeyColumn(int column);
- void setNameFilterFixedString(const QString &nameFilter);
- void setNameFilterCase(Qt::CaseSensitivity value);
-
- // Hidden filter:
- void setHiddenFilterRole(int role);
- void setHiddenFilterKeyColumn(int column);
- void setShowHidden(bool show);
- void setShowShown(bool show);
-
- // Category filter:
- void setCategoryFilterRole(int role);
- void setCategoryFilterKeyColumn(int column);
- void setShownCategories(const CSwordModuleInfo::Categories &categories);
-
- protected:
- bool nameFilterAcceptsRow(int row, const QModelIndex &parent) const;
- bool hiddenFilterAcceptsRow(int row, const QModelIndex &parent) const;
- bool categoryFilterAcceptsRow(int row, const QModelIndex &parent) const;
-
- protected:
- bool m_enabled;
-
- // Name filter:
- QString m_nameFilter;
- int m_nameFilterRole;
- int m_nameFilterColumn;
- Qt::CaseSensitivity m_nameFilterCase;
-
- // Hidden filter:
- int m_hiddenFilterRole;
- int m_hiddenFilterColumn;
- bool m_showHidden;
- bool m_showShown;
-
- // Categories filter:
- CSwordModuleInfo::Categories m_categoryFilter;
- int m_categoryFilterRole;
- int m_categoryFilterColumn;
+
+public: /* Methods: */
+
+ BtBookshelfFilterModel(QObject * parent = 0);
+
+ inline bool enabled() const {
+ return m_enabled;
+ }
+
+ virtual bool filterAcceptsRow(int row, const QModelIndex & parent) const;
+
+ // Name filter:
+ inline int nameFilterRole() const {
+ return m_nameFilterRole;
+ }
+
+ inline int nameFilterKeyColumn() const {
+ return m_nameFilterColumn;
+ }
+
+ inline const QString &nameFilter() const {
+ return m_nameFilter;
+ }
+
+ inline Qt::CaseSensitivity nameFilterCase() const {
+ return m_nameFilterCase;
+ }
+
+ // Hidden filter:
+ int hiddenFilterRole() const {
+ return m_hiddenFilterRole;
+ }
+
+ int hiddenFilterKeyColumn() const {
+ return m_hiddenFilterColumn;
+ }
+
+ inline bool showHidden() const {
+ return m_showHidden;
+ }
+
+ inline bool showShown() const {
+ return m_showShown;
+ }
+
+ // Category filter:
+ int categoryFilterRole() const {
+ return m_categoryFilterRole;
+ }
+
+ int categoryFilterKeyColumn() const {
+ return m_categoryFilterColumn;
+ }
+
+ inline CSwordModuleInfo::Categories shownCategories() const {
+ return m_categoryFilter;
+ }
+
+public slots:
+ void setEnabled(bool enable);
+
+ // Name filter:
+ void setNameFilterRole(int role);
+ void setNameFilterKeyColumn(int column);
+ void setNameFilterFixedString(const QString & nameFilter);
+ void setNameFilterCase(Qt::CaseSensitivity value);
+
+ // Hidden filter:
+ void setHiddenFilterRole(int role);
+ void setHiddenFilterKeyColumn(int column);
+ void setShowHidden(bool show);
+ void setShowShown(bool show);
+
+ // Category filter:
+ void setCategoryFilterRole(int role);
+ void setCategoryFilterKeyColumn(int column);
+ void setShownCategories(const CSwordModuleInfo::Categories & categories);
+
+private: /* Methods: */
+
+ bool nameFilterAcceptsRow(int row, const QModelIndex & parent) const;
+ bool hiddenFilterAcceptsRow(int row, const QModelIndex & parent) const;
+ bool categoryFilterAcceptsRow(int row, const QModelIndex & parent) const;
+
+private: /* Fields: */
+
+ bool m_enabled;
+
+ // Name filter:
+ QString m_nameFilter;
+ int m_nameFilterRole;
+ int m_nameFilterColumn;
+ Qt::CaseSensitivity m_nameFilterCase;
+
+ // Hidden filter:
+ int m_hiddenFilterRole;
+ int m_hiddenFilterColumn;
+ bool m_showHidden;
+ bool m_showShown;
+
+ // Categories filter:
+ CSwordModuleInfo::Categories m_categoryFilter;
+ int m_categoryFilterRole;
+ int m_categoryFilterColumn;
+
};
#endif // BTBOOKSHELFFILTERMODEL_H
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));
diff --git a/src/backend/bookshelfmodel/btbookshelfmodel.h b/src/backend/bookshelfmodel/btbookshelfmodel.h
index 20f2b3d..0884b4c 100644
--- a/src/backend/bookshelfmodel/btbookshelfmodel.h
+++ b/src/backend/bookshelfmodel/btbookshelfmodel.h
@@ -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.
*
@@ -25,148 +25,157 @@
BtBookshelfTreeModel might be a better choice, since it also provides sorting and grouping.
*/
class BtBookshelfModel: public QAbstractListModel {
- Q_OBJECT
- public:
- enum ModuleRole {
- ModuleNameRole = Qt::DisplayRole,
- ModuleIconRole = Qt::DecorationRole,
- ModulePointerRole = Qt::UserRole,
- ModuleCategoryRole = Qt::UserRole + 1,
- ModuleLanguageRole = Qt::UserRole + 2,
- ModuleHiddenRole = Qt::UserRole + 3,
- ModuleInstallPathRole = Qt::UserRole + 4,
- ModuleHasIndexRole = Qt::UserRole + 5,
- ModuleIndexSizeRole = Qt::UserRole + 6,
- ModuleDescriptionRole = Qt::UserRole + 7,
- UserRole = Qt::UserRole + 100
- };
-
- public:
- BtBookshelfModel(QObject *parent = 0);
- ~BtBookshelfModel();
-
- // Virtual methods implemented from QAbstractListModel:
- int rowCount(const QModelIndex &parent = QModelIndex()) const;
- QVariant data(CSwordModuleInfo *module, int role) const;
- QVariant data(const QModelIndex &index, int role) const;
- QVariant headerData(int section, Qt::Orientation orientation,
- int role = Qt::DisplayRole) const;
- bool setData(const QModelIndex &index, const QVariant &value,
- int role = ModuleHiddenRole);
-
- /**
- Given an index of this model, this method returns a pointer to the underlying
- CSwordModuleInfo instance corresponding to the given index.
- \param[in] index An index to this model.
- */
- inline CSwordModuleInfo *module(const QModelIndex &index) const {
- return (CSwordModuleInfo *)
- data(index, BtBookshelfModel::ModulePointerRole)
- .value<void *>();
- }
-
- /**
- Clears the data of the whole model by removing all items.
- \param[in] destroy If true, all CSwordModuleInfo instances in this model are also
- destroyed.
- */
- void clear(bool destroy = false);
-
- /**
- Appends the given module to this model.
- \param[in] module Module to add.
- */
- void addModule(CSwordModuleInfo * const module);
-
- /**
- Appends the all the modules in the given set to this model.
- \param[in] modules Set of modules to add.
- */
- void addModules(const QSet<CSwordModuleInfo *> &modules);
-
- /**
- Appends the all the modules in the given list to this model.
- \param[in] modules Set of modules to add.
- */
- void addModules(const QList<CSwordModuleInfo *> &modules);
-
- /**
- Removes the given module from this model and optionally destroys it.
- \param[in] module The module to remove from this model.
- \param[in] destroy If true, the given CSwordModuleInfo instance is destroyed.
- */
- void removeModule(CSwordModuleInfo * const module,
- bool destroy = false);
-
- /**
- Removes all modules from the given set from this model and optionally destroys
- them.
- \param[in] modules The set of modules to remove from this model.
- \param[in] destroy If true, the given CSwordModuleInfo instances are destroyed.
- */
- void removeModules(const QSet<CSwordModuleInfo *> &modules,
- bool destroy = false);
-
- /**
- Removes all modules from the given list from this model and optionally destroys
- them.
- \param[in] modules The list of modules to remove from this model.
- \param[in] destroy If true, the given CSwordModuleInfo instances are destroyed.
- */
- void removeModules(const QList<CSwordModuleInfo *> &modules,
- bool destroy = false);
-
- /**
- Returns the first module found with the given name.
- \param[in] name Name of the module to find.
- */
- CSwordModuleInfo* getModule(const QString &name) const;
-
- /**
- Returns the list of handled modules as a list of CSwordModuleInfo* pointers.
- */
- inline const QList<CSwordModuleInfo *> &moduleList() const {
- return m_data;
- }
-
- protected slots:
- /**
- Slot DIRECTLY called by CSwordModuleInfo when the hidden status of the respective
- module changes.
- \param[in] hidden True, if the module was hidden; false, if the module was shown.
- */
- void moduleHidden(bool hidden);
-
- /**
- Slot DIRECTLY called by CSwordModuleInfo when the indexed status of the respective
- module changes.
- \param[in] indexed True, if the module was indexed; false if the index was deleted.
- */
- void moduleIndexed(bool indexed);
-
- /**
- Slot DIRECTLY called by CSwordModuleInfo when the locked status of the respective
- module changes.
- \param[in] unlocked True, if the module was unlocked; false if the module was
- locked.
- */
- void moduleUnlocked(bool unlocked);
-
- protected:
- /**
- Called internally when module data changes. This method emits any neccessary
- signals for this model.
- \pre The givem module is handled by this model.
- \param[in] module The module that changed status.
- */
- void moduleDataChanged(CSwordModuleInfo *module);
-
- protected:
- /**
- The underlying data as a list of pointers to the respective CSwordModuleInfo
- instances.
- */
- QList<CSwordModuleInfo *> m_data;
+
+ Q_OBJECT
+
+public: /* Types: */
+
+ enum ModuleRole {
+ ModuleNameRole = Qt::DisplayRole,
+ ModuleIconRole = Qt::DecorationRole,
+ ModulePointerRole = Qt::UserRole,
+ ModuleCategoryRole = Qt::UserRole + 1,
+ ModuleLanguageRole = Qt::UserRole + 2,
+ ModuleHiddenRole = Qt::UserRole + 3,
+ ModuleInstallPathRole = Qt::UserRole + 4,
+ ModuleHasIndexRole = Qt::UserRole + 5,
+ ModuleIndexSizeRole = Qt::UserRole + 6,
+ ModuleDescriptionRole = Qt::UserRole + 7,
+ UserRole = Qt::UserRole + 100
+ };
+
+public: /* Methods: */
+
+ inline BtBookshelfModel(QObject * const parent = 0)
+ : QAbstractListModel(parent) {}
+
+ // Virtual methods implemented from QAbstractListModel:
+ int rowCount(const QModelIndex & parent = QModelIndex()) const;
+ QVariant data(CSwordModuleInfo * module, int role) const;
+ QVariant data(const QModelIndex & index, int role) const;
+ QVariant headerData(int section,
+ Qt::Orientation orientation,
+ int role = Qt::DisplayRole) const;
+ bool setData(const QModelIndex & index,
+ const QVariant & value,
+ int role = ModuleHiddenRole);
+
+ /**
+ Given an index of this model, this method returns a pointer to the underlying
+ CSwordModuleInfo instance corresponding to the given index.
+ \param[in] index An index to this model.
+ */
+ inline CSwordModuleInfo * module(const QModelIndex & index) const {
+ return static_cast<CSwordModuleInfo *>(
+ data(index, BtBookshelfModel::ModulePointerRole).value<void *>());
+ }
+
+ /**
+ Clears the data of the whole model by removing all items.
+ \param[in] destroy If true, all CSwordModuleInfo instances in this model are also
+ destroyed.
+ */
+ void clear(bool destroy = false);
+
+ /**
+ Appends the given module to this model.
+ \param[in] module Module to add.
+ */
+ void addModule(CSwordModuleInfo * const module);
+
+ /**
+ Appends the all the modules in the given set to this model.
+ \param[in] modules Set of modules to add.
+ */
+ void addModules(const QSet<CSwordModuleInfo *> & modules);
+
+ /**
+ Appends the all the modules in the given list to this model.
+ \param[in] modules Set of modules to add.
+ */
+ void addModules(const QList<CSwordModuleInfo *> & modules);
+
+ /**
+ Removes the given module from this model and optionally destroys it.
+ \param[in] module The module to remove from this model.
+ \param[in] destroy If true, the given CSwordModuleInfo instance is destroyed.
+ */
+ void removeModule(CSwordModuleInfo * const module,
+ bool destroy = false);
+
+ /**
+ Removes all modules from the given set from this model and optionally destroys
+ them.
+ \param[in] modules The set of modules to remove from this model.
+ \param[in] destroy If true, the given CSwordModuleInfo instances are destroyed.
+ */
+ void removeModules(const QSet<CSwordModuleInfo *> & modules,
+ bool destroy = false);
+
+ /**
+ Removes all modules from the given list from this model and optionally destroys
+ them.
+ \param[in] modules The list of modules to remove from this model.
+ \param[in] destroy If true, the given CSwordModuleInfo instances are destroyed.
+ */
+ void removeModules(const QList<CSwordModuleInfo *> & modules,
+ bool destroy = false);
+
+ /**
+ Returns the first module found with the given name.
+ \param[in] name Name of the module to find.
+ */
+ CSwordModuleInfo* getModule(const QString & name) const;
+
+ /**
+ Returns the list of handled modules as a list of CSwordModuleInfo* pointers.
+ */
+ inline const QList<CSwordModuleInfo *> & moduleList() const {
+ return m_data;
+ }
+
+protected slots:
+
+ /**
+ Slot DIRECTLY called by CSwordModuleInfo when the hidden status of the respective
+ module changes.
+ \param[in] hidden True, if the module was hidden; false, if the module was shown.
+ */
+ void moduleHidden(bool hidden);
+
+ /**
+ Slot DIRECTLY called by CSwordModuleInfo when the indexed status of the respective
+ module changes.
+ \param[in] indexed True, if the module was indexed; false if the index was deleted.
+ */
+ void moduleIndexed(bool indexed);
+
+ /**
+ Slot DIRECTLY called by CSwordModuleInfo when the locked status of the respective
+ module changes.
+ \param[in] unlocked True, if the module was unlocked; false if the module was
+ locked.
+ */
+ void moduleUnlocked(bool unlocked);
+
+private: /* Methods: */
+
+ /**
+ Called internally when module data changes. This method emits any neccessary
+ signals for this model.
+ \pre The givem module is handled by this model.
+ \param[in] module The module that changed status.
+ */
+ void moduleDataChanged(CSwordModuleInfo * module);
+
+private: /* Fields: */
+
+ /**
+ The underlying data as a list of pointers to the respective CSwordModuleInfo
+ instances.
+ */
+ QList<CSwordModuleInfo *> m_data;
+
};
#endif // BTBOOKSHELFMODEL_H
diff --git a/src/backend/bookshelfmodel/btbookshelftreemodel.cpp b/src/backend/bookshelfmodel/btbookshelftreemodel.cpp
index 0fcd14e..f3dbc4a 100644
--- a/src/backend/bookshelfmodel/btbookshelftreemodel.cpp
+++ b/src/backend/bookshelfmodel/btbookshelftreemodel.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.
*
@@ -13,20 +13,19 @@
#include "backend/bookshelfmodel/btbookshelftreemodel.h"
#include <QSet>
-#include <QSettings>
#include "backend/bookshelfmodel/categoryitem.h"
#include "backend/bookshelfmodel/indexingitem.h"
#include "backend/bookshelfmodel/languageitem.h"
#include "backend/bookshelfmodel/moduleitem.h"
-#include "backend/config/cbtconfig.h"
+#include "backend/config/btconfig.h"
#include "util/macros.h"
using namespace BookshelfModel;
-bool BtBookshelfTreeModel::Grouping::loadFrom(const QString &configKey) {
+bool BtBookshelfTreeModel::Grouping::loadFrom(const QString & configKey) {
Q_ASSERT(!configKey.isNull());
- QVariant v = CBTConfig::getConfig()->value(configKey);
+ QVariant v = btConfig().qVariantValue(configKey, QVariant());
if (!v.canConvert<Grouping>())
return false;
@@ -34,141 +33,142 @@ bool BtBookshelfTreeModel::Grouping::loadFrom(const QString &configKey) {
return true;
}
-void BtBookshelfTreeModel::Grouping::saveTo(const QString &configKey) const {
+void BtBookshelfTreeModel::Grouping::saveTo(const QString & configKey) const {
Q_ASSERT(!configKey.isNull());
- CBTConfig::getConfig()->setValue(configKey, QVariant::fromValue(*this));
-}
-
-
-BtBookshelfTreeModel::BtBookshelfTreeModel(QObject *parent)
- : QAbstractItemModel(parent), m_sourceModel(0), m_rootItem(new RootItem),
- m_defaultChecked(MODULE_HIDDEN), m_checkable(false)
-{
- // Intentionally empty
-}
-
-BtBookshelfTreeModel::BtBookshelfTreeModel(const QString &configKey,
- QObject *parent)
- : QAbstractItemModel(parent), m_sourceModel(0), m_rootItem(new RootItem),
- m_groupingOrder(configKey), m_defaultChecked(MODULE_HIDDEN),
- m_checkable(false)
-{
- // Intentionally empty
-}
-
-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
-}
+ btConfig().setValue(configKey, QVariant::fromValue(*this));
+}
+
+BtBookshelfTreeModel::BtBookshelfTreeModel(QObject * parent)
+ : QAbstractItemModel(parent)
+ , m_sourceModel(0)
+ , m_rootItem(new RootItem)
+ , m_defaultChecked(MODULE_HIDDEN)
+ , m_checkable(false) {}
+
+BtBookshelfTreeModel::BtBookshelfTreeModel(const QString & configKey,
+ QObject * parent)
+ : QAbstractItemModel(parent)
+ , m_sourceModel(0)
+ , m_rootItem(new RootItem)
+ , m_groupingOrder(configKey)
+ , m_defaultChecked(MODULE_HIDDEN)
+ , m_checkable(false) {}
+
+BtBookshelfTreeModel::BtBookshelfTreeModel(const Grouping & grouping,
+ QObject * parent)
+ : QAbstractItemModel(parent)
+ , m_sourceModel(0)
+ , m_rootItem(new RootItem)
+ , m_groupingOrder(grouping)
+ , m_defaultChecked(MODULE_HIDDEN)
+ , m_checkable(false) {}
BtBookshelfTreeModel::~BtBookshelfTreeModel() {
delete m_rootItem;
}
-int BtBookshelfTreeModel::rowCount(const QModelIndex &parent) const {
- return getItem(parent)->children().size();
+int BtBookshelfTreeModel::rowCount(const QModelIndex & parent) const {
+ return getItem(parent).children().size();
}
-int BtBookshelfTreeModel::columnCount(const QModelIndex &parent) const {
+int BtBookshelfTreeModel::columnCount(const QModelIndex & parent) const {
Q_UNUSED(parent);
return 1;
}
-bool BtBookshelfTreeModel::hasChildren(const QModelIndex &parent) const {
- return !getItem(parent)->children().isEmpty();
+bool BtBookshelfTreeModel::hasChildren(const QModelIndex & parent) const {
+ return !getItem(parent).children().isEmpty();
}
QModelIndex BtBookshelfTreeModel::index(int row, int column,
- const QModelIndex &parent) const {
+ 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 {
+ Item & parentItem = getItem(parent);
+ Item * const childItem = parentItem.children().at(row);
+ if (!childItem)
return QModelIndex();
- }
+
+ return createIndex(row, column, childItem);
}
-QModelIndex BtBookshelfTreeModel::parent(const QModelIndex &index) const {
- if (!index.isValid()) return QModelIndex();
+QModelIndex BtBookshelfTreeModel::parent(const QModelIndex & index) const {
+ if (!index.isValid())
+ return QModelIndex();
- Item *childItem(static_cast<Item*>(index.internalPointer()));
+ Item * childItem(static_cast<Item*>(index.internalPointer()));
Q_ASSERT(childItem != 0);
- Item *parentItem(childItem->parent());
+ Item * parentItem(childItem->parent());
Q_ASSERT(parentItem != 0);
- if (parentItem == m_rootItem) {
+ 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) {
+QVariant BtBookshelfTreeModel::data(const QModelIndex & index, int role) const {
+ if (!index.isValid() || index.column() != 0)
return QVariant();
- }
- Item *i(static_cast<Item*>(index.internalPointer()));
+ const Item * const i = static_cast<Item*>(index.internalPointer());
Q_ASSERT(i != 0);
switch (role) {
+
case Qt::CheckStateRole:
- if (!m_checkable) break;
+ 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());
+ const ModuleItem & mi = *static_cast<const ModuleItem *>(i);
+ return qVariantFromValue(static_cast<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);
- }
+ if (i->type() == Item::ITEM_MODULE)
+ return data(static_cast<const ModuleItem *>(i)->moduleInfo(), role);
+
+ 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);
+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,
+bool BtBookshelfTreeModel::setData(const QModelIndex & itemIndex,
+ const QVariant & value,
int role) {
Q_ASSERT(itemIndex.isValid());
typedef QPair<Item *, QModelIndex> IP;
- Qt::CheckState newState;
- if (LIKELY(role == Qt::CheckStateRole)) {
- bool ok;
- newState = (Qt::CheckState) value.toInt(&ok);
- if (UNLIKELY(!ok)) return false;
- }
- else {
+ if (UNLIKELY(role != Qt::CheckStateRole))
+ return false;
+
+ bool ok;
+ Qt::CheckState newState = static_cast<Qt::CheckState>(value.toInt(&ok));
+ if (UNLIKELY(!ok))
return false;
- }
// Handle partially checked as checked here in setData():
- if (newState == Qt::PartiallyChecked) newState = Qt::Checked;
+ if (newState == Qt::PartiallyChecked)
+ newState = Qt::Checked;
- Item *item(static_cast<Item*>(itemIndex.internalPointer()));
+ Item * item = static_cast<Item *>(itemIndex.internalPointer());
Q_ASSERT(item != 0);
if (item->checkState() == newState) return false;
@@ -180,25 +180,23 @@ bool BtBookshelfTreeModel::setData(const QModelIndex &itemIndex,
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());
+ 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);
+ 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++) {
+ } 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;
+ if (q.empty())
+ break;
p = q.takeFirst();
item = p.first;
}
@@ -209,36 +207,36 @@ bool BtBookshelfTreeModel::setData(const QModelIndex &itemIndex,
return true;
}
-Qt::ItemFlags BtBookshelfTreeModel::flags(const QModelIndex &index) const {
- if (!index.isValid()) return 0;
+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) {
+ const Item & i = *static_cast<Item*>(index.internalPointer());
+ 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) {
+ 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;
+void BtBookshelfTreeModel::setSourceModel(QAbstractItemModel * sourceModel) {
+ if (m_sourceModel == sourceModel)
+ return;
if (m_sourceModel != 0) {
disconnect(this, SLOT(moduleInserted(QModelIndex, int, int)));
@@ -263,43 +261,38 @@ void BtBookshelfTreeModel::setSourceModel(QAbstractItemModel *sourceModel) {
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);
+ for (int i = 0; i < sourceModel->rowCount(); i++) {
+ const QModelIndex moduleIndex(sourceModel->index(i, 0));
+ CSwordModuleInfo & module = *static_cast<CSwordModuleInfo *>(
+ sourceModel->data(moduleIndex,
+ BtBookshelfModel::ModulePointerRole).value<void*>());
+
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 = !sourceModel->data(moduleIndex,
+ BtBookshelfModel::ModuleHiddenRole).toBool();
+ } else if (m_defaultChecked == MODULE_INDEXED) {
+ checked = !sourceModel->data(moduleIndex,
+ BtBookshelfModel::ModuleHasIndexRole).toBool();
+ } else {
checked = (m_defaultChecked == CHECKED);
}
- m_sourceIndexMap[module] = moduleIndex;
+ m_sourceIndexMap[&module] = moduleIndex;
addModule(module, checked);
}
}
}
-void BtBookshelfTreeModel::setGroupingOrder(const Grouping &groupingOrder,
+void BtBookshelfTreeModel::setGroupingOrder(const Grouping & groupingOrder,
bool emitSignal)
{
- if (m_groupingOrder == groupingOrder) return;
+ if (m_groupingOrder == groupingOrder)
+ return;
+
m_groupingOrder = groupingOrder;
if (m_sourceModel != 0) {
- QSet<CSwordModuleInfo*> checked(m_checkedModulesCache);
+ const QSet<CSwordModuleInfo *> checked(m_checkedModulesCache);
m_checkedModulesCache.clear();
beginRemoveRows(QModelIndex(), 0, m_rootItem->children().size() - 1);
@@ -308,42 +301,39 @@ void BtBookshelfTreeModel::setGroupingOrder(const Grouping &groupingOrder,
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));
+ for (int i = 0; i < m_sourceModel->rowCount(); i++) {
+ const QModelIndex sourceIndex(m_sourceModel->index(i, 0));
+ CSwordModuleInfo & module = *static_cast<CSwordModuleInfo *>(
+ m_sourceModel->data(sourceIndex,
+ BtBookshelfModel::ModulePointerRole).value<void *>());
+ m_sourceIndexMap[&module] = sourceIndex;
+ addModule(module, checked.contains(&module));
}
}
- if (emitSignal) emit groupingOrderChanged(groupingOrder);
+ if (emitSignal)
+ emit groupingOrderChanged(groupingOrder);
}
void BtBookshelfTreeModel::setCheckable(bool checkable) {
- if (m_checkable == checkable) return;
+ if (m_checkable == checkable)
+ return;
m_checkable = checkable;
- if (m_sourceModel == 0) return;
+ if (m_sourceModel == 0)
+ return;
// Notify views that flags changed for all items:
resetData();
}
-void BtBookshelfTreeModel::setCheckedModules(const QSet<CSwordModuleInfo*> &modules) {
+void BtBookshelfTreeModel::setCheckedModules(const QSet<CSwordModuleInfo *> & modules) {
typedef ModuleItemMap::const_iterator MIMCI;
- for (MIMCI it(m_modules.constBegin()); it != m_modules.constEnd(); it++) {
+ for (MIMCI it = m_modules.constBegin(); it != m_modules.constEnd(); ++it) {
if (modules.contains(it.key())) {
- setData(getIndex(it.value()), Qt::Checked, Qt::CheckStateRole);
+ setData(getIndex(*it.value()), Qt::Checked, Qt::CheckStateRole);
} else {
- setData(getIndex(it.value()), Qt::Unchecked, Qt::CheckStateRole);
+ setData(getIndex(*it.value()), Qt::Unchecked, Qt::CheckStateRole);
}
}
}
@@ -355,18 +345,17 @@ void BtBookshelfTreeModel::resetData() {
QModelIndex parent(queue.takeFirst());
emit dataChanged(index(0, 0, parent),
index(rowCount(parent) - 1, columnCount() - 1, parent));
- for (int i(0); i < rowCount(parent); i++) {
- QModelIndex childIndex(index(i, 0, parent));
- if (rowCount(childIndex) > 0) {
+ for (int i = 0; i < rowCount(parent); i++) {
+ const QModelIndex childIndex(index(i, 0, parent));
+ if (rowCount(childIndex) > 0)
queue.append(childIndex);
- }
}
- }
- while (!queue.isEmpty());
+ } while (!queue.isEmpty());
}
-void BtBookshelfTreeModel::addModule(CSwordModuleInfo *module, bool checked) {
- if (m_modules.contains(module)) return;
+void BtBookshelfTreeModel::addModule(CSwordModuleInfo & module, bool checked) {
+ if (m_modules.contains(&module))
+ return;
#if QT_VERSION >= 0x040600
beginResetModel();
@@ -388,42 +377,43 @@ void BtBookshelfTreeModel::addModule(CSwordModuleInfo *module, bool checked) {
#endif
}
-void BtBookshelfTreeModel::addModule(CSwordModuleInfo *module,
+void BtBookshelfTreeModel::addModule(CSwordModuleInfo & module,
QModelIndex parentIndex,
- Grouping &intermediateGrouping,
- bool checked) {
- Q_ASSERT(module != 0);
-
+ Grouping & intermediateGrouping,
+ bool checked)
+{
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));
+ } else {
+ Item & parentItem = getItem(parentIndex);
+ ModuleItem * const newItem = new ModuleItem(module, *this);
newItem->setCheckState(checked ? Qt::Checked : Qt::Unchecked);
- const int newIndex(parentItem->indexFor(newItem));
+ 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);
- }
+ 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:
@@ -431,85 +421,84 @@ void BtBookshelfTreeModel::addModule(CSwordModuleInfo *module,
}
}
-void BtBookshelfTreeModel::removeModule(CSwordModuleInfo *module) {
- if (!m_modules.contains(module)) return;
+void BtBookshelfTreeModel::removeModule(CSwordModuleInfo & module) {
+ typedef ModuleItemMap::iterator MIMI;
+ const MIMI it = m_modules.find(&module);
+ if (it == m_modules.end())
+ return;
- Item *i(m_modules[module]);
+ Item * i = it.value();
// 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) {
+ Q_ASSERT(i->parent());
+ while (i->parent() != m_rootItem && i->parent()->children().size() <= 1)
i = i->parent();
- }
- Q_ASSERT(i != 0);
- Q_ASSERT(i->parent() != 0);
+ Q_ASSERT(i);
+ Q_ASSERT(i->parent());
// Calculate item indexes:
- int index(i->childIndex());
- QModelIndex parentIndex(getIndex(i->parent()));
+ const int index = i->childIndex();
+ const 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);
+ m_modules.erase(it);
+ m_checkedModulesCache.remove(&module);
endRemoveRows();
// Reset parent item check states, if needed:
resetParentCheckStates(parentIndex);
}
-Item *BtBookshelfTreeModel::getItem(const QModelIndex &index) const {
- if (LIKELY(index.isValid())) {
- Item *item(static_cast<Item*>(index.internalPointer()));
- Q_ASSERT(item != 0);
- return item;
- }
- else {
- return m_rootItem;
- }
-}
+Item & BtBookshelfTreeModel::getItem(const QModelIndex & index) const {
+ if (UNLIKELY(!index.isValid()))
+ return *m_rootItem;
-QModelIndex BtBookshelfTreeModel::getIndex(const BookshelfModel::Item *item) {
- Q_ASSERT(item != 0);
+ Item * const item = static_cast<Item *>(index.internalPointer());
+ Q_ASSERT(item);
+ return *item;
+}
+QModelIndex BtBookshelfTreeModel::getIndex(const BookshelfModel::Item & item) {
+ const BookshelfModel::Item * it = &item;
QList<int> indexes;
for (;;) {
- int i(item->childIndex());
- if (i < 0) break;
+ const int i = it->childIndex();
+ if (i < 0)
+ break;
indexes.append(i);
- item = item->parent();
+ it = it->parent();
}
QModelIndex i;
- while (!indexes.isEmpty()) {
+ 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());
+ Item & parentItem = *static_cast<Item *>(parentIndex.internalPointer());
+
+ const Qt::CheckState oldState = parentItem.checkState();
+ bool haveCheckedChildren = false;
+ bool haveUncheckedChildren = false;
+ for (int i = 0; i < parentItem.children().size(); i++) {
+ const Qt::CheckState state = parentItem.children().at(i)->checkState();
if (state == Qt::PartiallyChecked) {
haveCheckedChildren = true;
haveUncheckedChildren = true;
break;
- }
- else if (state == Qt::Checked) {
+ } else if (state == Qt::Checked) {
haveCheckedChildren = true;
- if (haveUncheckedChildren) break;
- }
- else if (state == Qt::Unchecked) {
+ if (haveUncheckedChildren)
+ break;
+ } else {
+ Q_ASSERT(state == Qt::Unchecked);
haveUncheckedChildren = true;
- if (haveCheckedChildren) break;
+ if (haveCheckedChildren)
+ break;
}
}
@@ -517,35 +506,34 @@ void BtBookshelfTreeModel::resetParentCheckStates(QModelIndex parentIndex) {
if (haveCheckedChildren) {
if (haveUncheckedChildren) {
newState = Qt::PartiallyChecked;
- }
- else {
+ } else {
newState = Qt::Checked;
}
- }
- else {
+ } else {
newState = Qt::Unchecked;
}
- if (newState == oldState) break;
+ if (newState == oldState)
+ break;
- parentItem->setCheckState(newState);
+ 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);
-
+void BtBookshelfTreeModel::moduleDataChanged(const QModelIndex & topLeft,
+ const QModelIndex & bottomRight)
+{
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(topLeft.column() == 0);
+ Q_ASSERT(bottomRight.column() == 0);
+
+ for (int i = topLeft.row(); i <= bottomRight.row(); i++) {
+ const QModelIndex moduleIndex(m_sourceModel->index(i, 0, topLeft.parent()));
+ const QVariant data(m_sourceModel->data(moduleIndex,
+ BtBookshelfModel::ModulePointerRole));
+ CSwordModuleInfo & module = *static_cast<CSwordModuleInfo *>(data.value<void *>());
+ QModelIndex itemIndex(getIndex(*m_modules[&module]));
Q_ASSERT(itemIndex.isValid());
emit dataChanged(itemIndex, itemIndex);
@@ -557,67 +545,73 @@ void BtBookshelfTreeModel::moduleDataChanged(const QModelIndex &topLeft,
do {
itemIndex = itemIndex.parent();
emit dataChanged(itemIndex, itemIndex);
- }
- while (itemIndex.isValid());
+ } 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);
+void BtBookshelfTreeModel::moduleInserted(const QModelIndex & parent,
+ int start,
+ int end)
+{
+ Q_ASSERT(start <= end);
- 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*>()));
+ for (int i = start; i <= end; i++) {
+ const QModelIndex moduleIndex(m_sourceModel->index(i, 0, parent));
+ const QVariant data(m_sourceModel->data(moduleIndex,
+ BtBookshelfModel::ModulePointerRole));
+ CSwordModuleInfo & module = *static_cast<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_sourceModel->data(moduleIndex,
+ BtBookshelfModel::ModuleHiddenRole).toBool();
+ } else if (m_defaultChecked == MODULE_INDEXED) {
+ checked = !m_sourceModel->data(moduleIndex,
+ BtBookshelfModel::ModuleHasIndexRole).toBool();
+ } else {
+ Q_ASSERT(m_defaultChecked == CHECKED || m_defaultChecked == UNCHECKED);
checked = (m_defaultChecked == CHECKED);
}
- m_sourceIndexMap[module] = moduleIndex;
+ 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*>()));
+void BtBookshelfTreeModel::moduleRemoved(const QModelIndex & parent,
+ int start,
+ int end)
+{
+ Q_ASSERT(start <= end);
+ for (int i = start; i <= end; i++) {
+ const QModelIndex moduleIndex(m_sourceModel->index(i, 0, parent));
+ const QVariant data(m_sourceModel->data(moduleIndex,
+ BtBookshelfModel::ModulePointerRole));
+ CSwordModuleInfo & module = *static_cast<CSwordModuleInfo *>(data.value<void*>());
removeModule(module);
- m_sourceIndexMap.remove(module);
+ m_sourceIndexMap.remove(&module);
}
}
-QDataStream &operator<<(QDataStream &os, const BtBookshelfTreeModel::Grouping &o) {
+QDataStream & operator <<(QDataStream & os,
+ const BtBookshelfTreeModel::Grouping & o)
+{
os << o.size();
- Q_FOREACH(BtBookshelfTreeModel::Group g, o) {
- os << (int) g;
- }
+ Q_FOREACH(BtBookshelfTreeModel::Group g, o)
+ os << static_cast<int>(g);
return os;
}
-QDataStream &operator>>(QDataStream &is, BtBookshelfTreeModel::Grouping &o) {
+QDataStream & operator >>(QDataStream & is,
+ BtBookshelfTreeModel::Grouping & o)
+{
int s;
is >> s;
o.clear();
- for (int i(0); i < s; i++) {
+ for (int i = 0; i < s; i++) {
int g;
is >> g;
- o.append((BtBookshelfTreeModel::Group) g);
+ o.append(static_cast<BtBookshelfTreeModel::Group>(g));
}
return is;
}
diff --git a/src/backend/bookshelfmodel/btbookshelftreemodel.h b/src/backend/bookshelfmodel/btbookshelftreemodel.h
index 085095d..14c3289 100644
--- a/src/backend/bookshelfmodel/btbookshelftreemodel.h
+++ b/src/backend/bookshelfmodel/btbookshelftreemodel.h
@@ -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.
*
@@ -23,157 +23,181 @@
#include "backend/bookshelfmodel/item.h"
-namespace BookshelfModel {
-class ModuleItem;
-}
+namespace BookshelfModel { class ModuleItem; }
class CSwordModuleInfo;
class QDataStream;
class BtBookshelfTreeModel: public QAbstractItemModel {
- Q_OBJECT
- Q_ENUMS(Group)
-
- typedef QMap<CSwordModuleInfo*, BookshelfModel::ModuleItem*> ModuleItemMap;
- typedef QMap<CSwordModuleInfo*, QPersistentModelIndex> SourceIndexMap;
-
- public: /* Types: */
- enum ModuleRole {
- CheckStateRole = BtBookshelfModel::UserRole,
- UserRole = BtBookshelfModel::UserRole + 100
- };
- enum Group {
- GROUP_CATEGORY = 0,
- GROUP_LANGUAGE = 1,
- GROUP_INDEXING
- };
- enum CheckedBehavior {
- CHECKED, /**< Check all added modules by default. */
- UNCHECKED, /**< Uncheck all added modules by default. */
- MODULE_HIDDEN, /**< By default, check only added modules that are not hidden. */
- MODULE_INDEXED /**< By default, check only added modules that are indexed. */
- };
-
- class Grouping: public QList<Group> {
- public:
- /**
- \warning Be careful using this constructor!
- */
- explicit inline Grouping(bool empty = false) {
- if (empty)
- return;
- push_back(GROUP_CATEGORY);
- push_back(GROUP_LANGUAGE);
- }
- explicit inline Grouping(Group group) { push_back(group); }
- explicit inline Grouping(const QString &configKey) {
- if (loadFrom(configKey))
- return;
- push_back(GROUP_CATEGORY);
- push_back(GROUP_LANGUAGE);
- }
- inline Grouping(const Grouping &copy)
- : QList<Group>(copy) {}
-
- bool loadFrom(const QString &configKey);
- void saveTo(const QString &configKey) const;
- };
-
- public: /* Methods: */
- BtBookshelfTreeModel(QObject *parent = 0);
- BtBookshelfTreeModel(const QString &configKey, QObject *parent = 0);
- BtBookshelfTreeModel(const Grouping &grouping, QObject *parent = 0);
- virtual ~BtBookshelfTreeModel();
-
- virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
- virtual int columnCount(const QModelIndex &parent = QModelIndex())
- const;
- virtual bool hasChildren(const QModelIndex &parent = QModelIndex())
- const;
- virtual QModelIndex index(int row, int column,
- const QModelIndex &parent = QModelIndex())
- const;
- virtual QModelIndex parent(const QModelIndex &index) const;
- virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
- QVariant data(CSwordModuleInfo *module, int role = Qt::DisplayRole) const;
- virtual Qt::ItemFlags flags(const QModelIndex &index) const;
- virtual QVariant headerData(int section, Qt::Orientation orientation,
- int role = Qt::DisplayRole) const;
- virtual bool setData(const QModelIndex &index, const QVariant &value,
- int role);
-
- inline QAbstractItemModel *sourceModel() const { return m_sourceModel; }
- inline const Grouping &groupingOrder() const { return m_groupingOrder; }
- inline bool checkable() const { return m_checkable; }
- inline CheckedBehavior defaultChecked() const { return m_defaultChecked; }
- inline QList<CSwordModuleInfo*> modules() const { return m_modules.keys(); }
- inline const QSet<CSwordModuleInfo*> &checkedModules() const {
- return m_checkedModulesCache;
- }
- public slots:
- void setSourceModel(QAbstractItemModel *sourceModel);
- void setGroupingOrder(const BtBookshelfTreeModel::Grouping &groupingOrder,
- bool emitSignal = true);
- void setCheckable(bool checkable);
- inline void setDefaultChecked(CheckedBehavior b) {
- m_defaultChecked = b;
- }
- void setCheckedModules(const QSet<CSwordModuleInfo*> &modules);
-
- signals:
- void groupingOrderChanged(BtBookshelfTreeModel::Grouping newGrouping);
- void moduleChecked(CSwordModuleInfo *module, bool checked);
-
- protected:
- void resetData();
- QVariant parentData(BookshelfModel::ModuleItem *item, int role) const;
- void addModule(CSwordModuleInfo *module, bool checked);
- void addModule(CSwordModuleInfo *module, QModelIndex parentIndex,
- Grouping &intermediateGrouping, bool checked);
- void removeModule(CSwordModuleInfo *module);
-
- BookshelfModel::Item *getItem(const QModelIndex &index) const;
- QModelIndex getIndex(const BookshelfModel::Item *item);
- void resetParentCheckStates(QModelIndex parentIndex);
-
- template <class T>
- QModelIndex getGroup(CSwordModuleInfo *module,
- QModelIndex parentIndex) {
- BookshelfModel::Item *parentItem(getItem(parentIndex));
- int groupIndex;
- T *groupItem(parentItem->getGroupItem<T>(module, groupIndex));
-
- if (groupItem == 0) {
- groupItem = new T(module);
- groupIndex = parentItem->indexFor(groupItem);
- beginInsertRows(parentIndex, groupIndex, groupIndex);
- parentItem->insertChild(groupIndex, groupItem);
- endInsertRows();
+ Q_OBJECT
+ Q_ENUMS(Group)
+
+private: /* Types: */
+
+ typedef QMap<CSwordModuleInfo *, BookshelfModel::ModuleItem *> ModuleItemMap;
+ typedef QMap<CSwordModuleInfo *, QPersistentModelIndex> SourceIndexMap;
+
+public: /* Types: */
+
+ enum ModuleRole {
+ CheckStateRole = BtBookshelfModel::UserRole,
+ UserRole = BtBookshelfModel::UserRole + 100
+ };
+
+ enum Group {
+ GROUP_CATEGORY = 0,
+ GROUP_LANGUAGE = 1,
+ GROUP_INDEXING
+ };
+
+ enum CheckedBehavior {
+ CHECKED, /**< Check all added modules by default. */
+ UNCHECKED, /**< Uncheck all added modules by default. */
+ MODULE_HIDDEN, /**< By default, check only added modules that are not hidden. */
+ MODULE_INDEXED /**< By default, check only added modules that are indexed. */
+ };
+
+ class Grouping: public QList<Group> {
+
+ public: /* Methods: */
+
+ /**
+ \warning Be careful using this constructor!
+ */
+ explicit inline Grouping(bool empty = false) {
+ if (empty)
+ return;
+ push_back(GROUP_CATEGORY);
+ push_back(GROUP_LANGUAGE);
+ }
+
+ explicit inline Grouping(Group group) { push_back(group); }
+
+ explicit inline Grouping(const QString & configKey) {
+ if (loadFrom(configKey))
+ return;
+ push_back(GROUP_CATEGORY);
+ push_back(GROUP_LANGUAGE);
}
- return index(groupIndex, 0, parentIndex);
+
+ inline Grouping(const Grouping & copy)
+ : QList<Group>(copy) {}
+
+ bool loadFrom(const QString & configKey);
+ void saveTo(const QString & configKey) const;
+
+ };
+
+public: /* Methods: */
+
+ BtBookshelfTreeModel(QObject * parent = 0);
+ BtBookshelfTreeModel(const QString & configKey, QObject * parent = 0);
+ BtBookshelfTreeModel(const Grouping & grouping, QObject * parent = 0);
+ virtual ~BtBookshelfTreeModel();
+
+ virtual int rowCount(const QModelIndex & parent = QModelIndex()) const;
+ virtual int columnCount(const QModelIndex & parent = QModelIndex())
+ const;
+ virtual bool hasChildren(const QModelIndex & parent = QModelIndex())
+ const;
+ virtual QModelIndex index(int row,
+ int column,
+ const QModelIndex & parent = QModelIndex())
+ const;
+ virtual QModelIndex parent(const QModelIndex & index) const;
+ virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
+ QVariant data(CSwordModuleInfo & module, int role = Qt::DisplayRole) const;
+ virtual Qt::ItemFlags flags(const QModelIndex & index) const;
+ virtual QVariant headerData(int section, Qt::Orientation orientation,
+ int role = Qt::DisplayRole) const;
+ virtual bool setData(const QModelIndex & index,
+ const QVariant & value,
+ int role);
+
+ inline QAbstractItemModel * sourceModel() const { return m_sourceModel; }
+ inline const Grouping & groupingOrder() const { return m_groupingOrder; }
+ inline bool checkable() const { return m_checkable; }
+ inline CheckedBehavior defaultChecked() const { return m_defaultChecked; }
+ inline QList<CSwordModuleInfo *> modules() const { return m_modules.keys(); }
+ inline const QSet<CSwordModuleInfo *> & checkedModules() const {
+ return m_checkedModulesCache;
+ }
+
+public slots:
+
+ void setSourceModel(QAbstractItemModel * sourceModel);
+ void setGroupingOrder(const BtBookshelfTreeModel::Grouping & groupingOrder,
+ bool emitSignal = true);
+ void setCheckable(bool checkable);
+ inline void setDefaultChecked(CheckedBehavior b) {
+ m_defaultChecked = b;
+ }
+ void setCheckedModules(const QSet<CSwordModuleInfo *> & modules);
+
+signals:
+
+ void groupingOrderChanged(BtBookshelfTreeModel::Grouping newGrouping);
+ void moduleChecked(CSwordModuleInfo * module, bool checked);
+
+protected: /* Methods: */
+
+ void resetData();
+
+protected slots:
+
+ void moduleDataChanged(const QModelIndex & topLeft,
+ const QModelIndex & bottomRight);
+ void moduleInserted(const QModelIndex & parent, int start, int end);
+ void moduleRemoved(const QModelIndex & parent, int start, int end);
+
+private: /* Methods: */
+
+ void addModule(CSwordModuleInfo & module, bool checked);
+ void addModule(CSwordModuleInfo & module,
+ QModelIndex parentIndex,
+ Grouping & intermediateGrouping,
+ bool checked);
+ void removeModule(CSwordModuleInfo & module);
+
+ BookshelfModel::Item & getItem(const QModelIndex & index) const;
+ QModelIndex getIndex(const BookshelfModel::Item & item);
+ void resetParentCheckStates(QModelIndex parentIndex);
+
+ template <class T>
+ QModelIndex getGroup(CSwordModuleInfo & module,
+ QModelIndex parentIndex)
+ {
+ BookshelfModel::Item & parentItem = getItem(parentIndex);
+ int groupIndex;
+ T * groupItem = parentItem.getGroupItem<T>(module, groupIndex);
+
+ if (!groupItem) {
+ groupItem = new T(module);
+ groupIndex = parentItem.indexFor(groupItem);
+ beginInsertRows(parentIndex, groupIndex, groupIndex);
+ parentItem.insertChild(groupIndex, groupItem);
+ endInsertRows();
}
+ return index(groupIndex, 0, parentIndex);
+ }
+
+private: /* Fields: */
+
+ QAbstractItemModel * m_sourceModel;
+ BookshelfModel::Item * m_rootItem;
+ ModuleItemMap m_modules;
+ SourceIndexMap m_sourceIndexMap;
+ Grouping m_groupingOrder;
+ CheckedBehavior m_defaultChecked;
+ bool m_checkable;
+
+ QSet<CSwordModuleInfo *> m_checkedModulesCache;
- protected slots:
- void moduleDataChanged(const QModelIndex &topLeft,
- const QModelIndex &bottomRight);
- void moduleInserted(const QModelIndex &parent, int start, int end);
- void moduleRemoved(const QModelIndex &parent, int start, int end);
-
- protected:
- QAbstractItemModel *m_sourceModel;
- BookshelfModel::Item *m_rootItem;
- ModuleItemMap m_modules;
- SourceIndexMap m_sourceIndexMap;
- Grouping m_groupingOrder;
- CheckedBehavior m_defaultChecked;
- bool m_checkable;
-
- QSet<CSwordModuleInfo*> m_checkedModulesCache;
};
-QDataStream &operator<<(QDataStream &os, const BtBookshelfTreeModel::Grouping &o);
-QDataStream &operator>>(QDataStream &is, BtBookshelfTreeModel::Grouping &o);
+QDataStream & operator <<(QDataStream & os, const BtBookshelfTreeModel::Grouping & o);
+QDataStream & operator >>(QDataStream & is, BtBookshelfTreeModel::Grouping & o);
-Q_DECLARE_METATYPE(BtBookshelfTreeModel::Grouping);
+Q_DECLARE_METATYPE(BtBookshelfTreeModel::Grouping)
#endif // BTBOOKSHELFTREEMODEL_H
diff --git a/src/backend/bookshelfmodel/categoryitem.cpp b/src/backend/bookshelfmodel/categoryitem.cpp
index aaf5566..20ba8e4 100644
--- a/src/backend/bookshelfmodel/categoryitem.cpp
+++ b/src/backend/bookshelfmodel/categoryitem.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.
*
@@ -15,29 +15,31 @@
namespace BookshelfModel {
-CategoryItem::CategoryItem(CSwordModuleInfo *module)
- : Item(ITEM_CATEGORY), m_category(module->category()) {
- // Intentionally empty
-}
QVariant CategoryItem::data(int role) const {
switch (role) {
+
case Qt::DisplayRole:
return CSwordModuleInfo::categoryName(m_category);
+
case Qt::DecorationRole:
return CSwordModuleInfo::categoryIcon(m_category);
+
default:
return Item::data(role);
+
}
}
-bool CategoryItem::operator<(const Item &other) const {
- if (other.type() != ITEM_CATEGORY) {
+bool CategoryItem::operator<(const Item & other) const {
+ if (other.type() != ITEM_CATEGORY)
return ITEM_CATEGORY < other.type();
- }
- const CategoryItem &o(static_cast<const CategoryItem &>(other));
- if (m_category == CSwordModuleInfo::UnknownCategory) return false;
- if (o.m_category == CSwordModuleInfo::UnknownCategory) return true;
+
+ const CategoryItem & o = static_cast<const CategoryItem &>(other);
+ if (m_category == CSwordModuleInfo::UnknownCategory)
+ return false;
+ if (o.m_category == CSwordModuleInfo::UnknownCategory)
+ return true;
return m_category < o.m_category;
}
diff --git a/src/backend/bookshelfmodel/categoryitem.h b/src/backend/bookshelfmodel/categoryitem.h
index 75d5a7c..135f337 100644
--- a/src/backend/bookshelfmodel/categoryitem.h
+++ b/src/backend/bookshelfmodel/categoryitem.h
@@ -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.
*
@@ -22,28 +22,29 @@
namespace BookshelfModel {
-class CategoryItem: public Item {
- Q_DECLARE_TR_FUNCTIONS(CategoryItem);
+class CategoryItem: public GroupItem<Item::ITEM_CATEGORY> {
- public:
- static const Item::Type GROUP_TYPE = Item::ITEM_CATEGORY;
+public: /* Methods: */
- CategoryItem(CSwordModuleInfo *module);
+ inline CategoryItem(const CSwordModuleInfo & module)
+ : m_category(module.category()) {}
- inline const CSwordModuleInfo::Category &category() const {
- return m_category;
- }
+ inline const CSwordModuleInfo::Category & category() const {
+ return m_category;
+ }
- QVariant data(int role = Qt::DisplayRole) const;
+ QVariant data(int role = Qt::DisplayRole) const;
- inline bool fitFor(CSwordModuleInfo *module) const {
- return module->category() == m_category;
- }
+ inline bool fitFor(const CSwordModuleInfo & module) const {
+ return module.category() == m_category;
+ }
- bool operator<(const Item &other) const;
+ bool operator<(const Item & other) const;
+
+private: /* Fields: */
+
+ const CSwordModuleInfo::Category m_category;
- protected:
- CSwordModuleInfo::Category m_category;
};
} // namespace BookshelfModel
diff --git a/src/backend/bookshelfmodel/indexingitem.cpp b/src/backend/bookshelfmodel/indexingitem.cpp
index 5491ca5..46488a4 100644
--- a/src/backend/bookshelfmodel/indexingitem.cpp
+++ b/src/backend/bookshelfmodel/indexingitem.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.
*
@@ -15,23 +15,13 @@
namespace BookshelfModel {
-IndexingItem::IndexingItem(CSwordModuleInfo *module)
- : Item(Item::ITEM_INDEXING), m_indexed(module->hasIndex()) {
- // Intentionally empty
-}
-
QVariant IndexingItem::data(int role) const {
- switch (role) {
- case Qt::DisplayRole:
- if (m_indexed) {
- return QObject::tr("Indexed works");
- }
- else {
- return QObject::tr("Unindexed works");
- }
- default:
- return Item::data(role);
- }
+ if (role != Qt::DisplayRole)
+ return Item::data(role);
+
+ return m_indexed
+ ? QObject::tr("Indexed works")
+ : QObject::tr("Unindexed works");
}
} // namespace BookshelfModel
diff --git a/src/backend/bookshelfmodel/indexingitem.h b/src/backend/bookshelfmodel/indexingitem.h
index a1eb812..3e1e0cd 100644
--- a/src/backend/bookshelfmodel/indexingitem.h
+++ b/src/backend/bookshelfmodel/indexingitem.h
@@ -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.
*
@@ -20,20 +20,23 @@
namespace BookshelfModel {
-class IndexingItem: public Item {
- public:
- static const Item::Type GROUP_TYPE = Item::ITEM_INDEXING;
+class IndexingItem: public GroupItem<Item::ITEM_INDEXING> {
- IndexingItem(CSwordModuleInfo *module);
+public: /* Methods: */
- QVariant data(int role = Qt::DisplayRole) const;
+ inline IndexingItem(const CSwordModuleInfo & module)
+ : m_indexed(module.hasIndex()) {}
- inline bool fitFor(CSwordModuleInfo *module) const {
- return module->hasIndex() == m_indexed;
- }
+ QVariant data(int role = Qt::DisplayRole) const;
+
+ inline bool fitFor(const CSwordModuleInfo & module) const {
+ return module.hasIndex() == m_indexed;
+ }
+
+private: /* Fields: */
+
+ bool m_indexed;
- protected:
- bool m_indexed;
};
} // namespace BookshelfModel
diff --git a/src/backend/bookshelfmodel/item.cpp b/src/backend/bookshelfmodel/item.cpp
index 02cd991..9d6e5af 100644
--- a/src/backend/bookshelfmodel/item.cpp
+++ b/src/backend/bookshelfmodel/item.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,56 +17,56 @@
namespace BookshelfModel {
-Item::Item(Type type)
- : m_type(type), m_parent(0), m_checkState(Qt::Unchecked) {
- // Intentionally empty
-}
-
Item::~Item() {
qDeleteAll(m_children);
}
-int Item::indexFor(Item *newItem) {
- Q_ASSERT(newItem != 0);
+int Item::indexFor(Item * newItem) {
+ Q_ASSERT(newItem);
- if (m_children.empty()) return 0;
+ if (m_children.empty())
+ return 0;
- int i(0);
+ int i = 0;
for (;;) {
- Item *nextItem(m_children.at(i));
+ Item * const nextItem(m_children.at(i));
Q_ASSERT(nextItem->type() == newItem->type());
- if (*newItem < *nextItem) {
+ if (*newItem < *nextItem)
return i;
- }
+
i++;
- if (i >= m_children.size()) {
+ if (i >= m_children.size())
return i;
- }
}
}
QVariant Item::data(int role) const {
switch (role) {
+
case Qt::CheckStateRole:
return m_checkState;
+
case BtBookshelfModel::ModuleHiddenRole:
- if (m_children.empty()) return true;
+ if (m_children.empty())
+ return true;
- foreach (Item *child, m_children) {
- if (!child->data(role).toBool()) return false;
- }
+ Q_FOREACH (Item * child, m_children)
+ if (!child->data(role).toBool())
+ return false;
return true;
+
default:
return QVariant();
+
}
}
-bool Item::operator<(const Item &other) const {
- if (m_type != other.type()) {
+bool Item::operator<(const Item & other) const {
+ if (m_type != other.type())
return m_type < other.type();
- }
- QString first(data(Qt::DisplayRole).toString().toLower());
- QString second(other.data(Qt::DisplayRole).toString().toLower());
+
+ const QString first(data(Qt::DisplayRole).toString().toLower());
+ const QString second(other.data(Qt::DisplayRole).toString().toLower());
return first.localeAwareCompare(second) < 0;
}
diff --git a/src/backend/bookshelfmodel/item.h b/src/backend/bookshelfmodel/item.h
index 652120b..712d4a2 100644
--- a/src/backend/bookshelfmodel/item.h
+++ b/src/backend/bookshelfmodel/item.h
@@ -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.
*
@@ -13,10 +13,7 @@
#ifndef ITEM_H
#define ITEM_H
-#include <QIcon>
#include <QList>
-#include <QString>
-#include <QtGlobal>
#include <QVariant>
@@ -25,134 +22,165 @@ class CSwordModuleInfo;
namespace BookshelfModel {
class Item {
- public:
- enum Type {
- ITEM_ROOT = 0,
- ITEM_CATEGORY = 1,
- ITEM_LANGUAGE = 2,
- ITEM_MODULE = 3,
- ITEM_INDEXING = 4
- };
-
- Item(Type type);
- virtual ~Item();
-
- /**
- \brief Returns the type of this item.
- */
- inline Type type() const {
- return m_type;
- }
- /**
- \brief Returns a pointer to the parent item of this item.
- \retval 0 if this item has no parent.
- */
- inline Item *parent() const {
- return m_parent;
+public: /* Types: */
+
+ enum Type {
+ ITEM_ROOT = 0,
+ ITEM_CATEGORY = 1,
+ ITEM_LANGUAGE = 2,
+ ITEM_MODULE = 3,
+ ITEM_INDEXING = 4
+ };
+
+public: /* Methods: */
+
+ inline Item(Type type)
+ : m_type(type)
+ , m_parent(0)
+ , m_checkState(Qt::Unchecked) {}
+
+ virtual ~Item();
+
+ /**
+ \brief Returns the type of this item.
+ */
+ inline Type type() const {
+ return m_type;
+ }
+
+ /**
+ \brief Returns a pointer to the parent item of this item.
+ \retval 0 if this item has no parent.
+ */
+ inline Item * parent() const {
+ return m_parent;
+ }
+
+ /**
+ \brief Returns the list of child items of this node.
+ */
+ inline QList<Item *> & children() {
+ return m_children;
+ }
+
+ /**
+ \brief Returns the index of this item under its parent.
+ \retval -1 if this item has no parent.
+ */
+ inline int childIndex() const {
+ return m_parent == 0
+ ? -1
+ : m_parent->m_children.indexOf(const_cast<Item *>(this));
+ }
+
+ /**
+ \brief Returns the position for where the given child item would be
+ inserted.
+ \param[in] newItem Pointer to the item that would be inserted.
+ */
+ int indexFor(Item * newItem);
+
+ /**
+ \brief Inserts the given item as a child at the given index.
+ \pre The given index is a valid position for the item.
+ \param[in] index The child index to insert the item at.
+ \param[in] newItem The item to insert.
+ */
+ inline void insertChild(int index, Item * newItem) {
+ Q_ASSERT(newItem != 0);
+ Q_ASSERT(index >= 0 && index <= m_children.size());
+ m_children.insert(index, newItem);
+ newItem->setParent(this);
+ }
+
+ template <class T>
+ inline T * getGroupItem(CSwordModuleInfo & module, int & outIndex) {
+ for (int i = 0; i < m_children.size(); i++) {
+ Q_ASSERT(m_children.at(i)->type() == T::staticItemType());
+ T * item = static_cast<T *>(m_children.at(i));
+ if (item->fitFor(module)) {
+ outIndex = i;
+ return item;
+ }
}
+ return 0;
+ }
+
+ /**
+ \brief Returns data for this item.
+ */
+ virtual QVariant data(int role = Qt::DisplayRole) const;
+
+ /**
+ \brief Returns the check state of this item.
+ */
+ inline Qt::CheckState checkState() const {
+ return m_checkState;
+ }
+
+ /**
+ \brief Sets the check state of this item.
+ \param[in] state new check state.
+ */
+ inline void setCheckState(const Qt::CheckState state) {
+ m_checkState = state;
+ }
+
+ /**
+ \brief Returns whether this item is fit to contain the given module.
+ \param[in] module The module to check with.
+ \retval true If this item is a group and can contain the given module.
+ \retval false This item is not a group or is a wrong group.
+ */
+ inline virtual bool fitFor(const CSwordModuleInfo & module) const = 0;
+
+ /**
+ \brief Comparsion operator used sorting child items.
+ */
+ virtual bool operator<(const Item & other) const;
+
+private: /* Methods: */
+
+ inline void setParent(Item * parent) {
+ Q_ASSERT(parent != 0);
+ m_parent = parent;
+ }
+
+private: /* Fields: */
+
+ Type m_type;
+ Item * m_parent;
+ QList<Item *> m_children;
+ Qt::CheckState m_checkState;
- /**
- \brief Returns the list of child items of this node.
- */
- inline QList<Item*> &children() {
- return m_children;
- }
+};
- /**
- \brief Returns the index of this item under its parent.
- \retval -1 if this item has no parent.
- */
- inline int childIndex() const {
- if (m_parent == 0) return -1;
- return m_parent->m_children.indexOf(const_cast<Item*>(this));
- }
+class RootItem: public Item {
- /**
- \brief Returns the position for where the given child item would be
- inserted.
- \param[in] newItem Pointer to the item that would be inserted.
- */
- int indexFor(Item *newItem);
-
- /**
- \brief Inserts the given item as a child at the given index.
- \pre The given index is a valid position for the item.
- \param[in] index The child index to insert the item at.
- \param[in] newItem The item to insert.
- */
- inline void insertChild(int index, Item *newItem) {
- Q_ASSERT(newItem != 0);
- Q_ASSERT(index >= 0 && index <= m_children.size());
- m_children.insert(index, newItem);
- newItem->setParent(this);
- }
+public: /* Methods: */
- template <class T>
- T *getGroupItem(CSwordModuleInfo *module, int &outIndex) {
- for (int i(0); i < m_children.size(); i++) {
- Q_ASSERT(m_children.at(i)->type() == T::GROUP_TYPE);
- T *item(static_cast<T*>(m_children.at(i)));
- if (item->fitFor(module)) {
- outIndex = i;
- return item;
- }
- }
- return 0;
- }
+ inline RootItem()
+ : Item(Item::ITEM_ROOT) {}
- /**
- \brief Returns data for this item.
- */
- virtual QVariant data(int role = Qt::DisplayRole) const;
+ inline virtual bool fitFor(const CSwordModuleInfo &) const {
+ return true;
+ }
- /**
- \brief Returns the check state of this item.
- */
- inline Qt::CheckState checkState() const {
- return m_checkState;
- }
-
- /**
- \brief Sets the check state of this item.
- \param[in] state new check state.
- */
- inline void setCheckState(const Qt::CheckState state) {
- m_checkState = state;
- }
+};
- /**
- \brief Returns whether this item is fit to contain the given module.
- \param[in] module The module to check with.
- \retval true If this item is a group and can contain the given module.
- \retval false This item is not a group or is a wrong group.
- */
- inline virtual bool fitFor(CSwordModuleInfo *module) const {
- Q_UNUSED(module);
- return false;
- }
+template <Item::Type TYPE>
+class GroupItem: public Item {
- /**
- \brief Comparsion operator used sorting child items.
- */
- virtual bool operator<(const Item &other) const;
+public: /* Methods: */
- protected:
- inline void setParent(Item *parent) {
- Q_ASSERT(parent != 0);
- m_parent = parent;
- }
+ inline GroupItem()
+ : Item(TYPE) {}
- protected:
- Type m_type;
- Item *m_parent;
- QList<Item*> m_children;
- Qt::CheckState m_checkState;
-};
+ inline static Item::Type staticItemType() {
+ return TYPE;
+ }
-class RootItem: public Item {
- public:
- inline RootItem() : Item(Item::ITEM_ROOT) {}
};
} // Namespace BookshelfModel
diff --git a/src/backend/bookshelfmodel/languageitem.cpp b/src/backend/bookshelfmodel/languageitem.cpp
index 2fdab8b..072d720 100644
--- a/src/backend/bookshelfmodel/languageitem.cpp
+++ b/src/backend/bookshelfmodel/languageitem.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.
*
@@ -12,22 +12,23 @@
#include "backend/bookshelfmodel/languageitem.h"
+#include "util/geticon.h"
-namespace BookshelfModel {
-LanguageItem::LanguageItem(CSwordModuleInfo *module)
- : Item(ITEM_LANGUAGE), m_language(module->language()) {
- // Intentionally empty
-}
+namespace BookshelfModel {
QVariant LanguageItem::data(int role) const {
switch (role) {
+
case Qt::DisplayRole:
return m_language->translatedName();
+
case Qt::DecorationRole:
- return util::directory::getIcon("flag.svg");
+ return util::getIcon("flag.svg");
+
default:
return Item::data(role);
+
}
}
diff --git a/src/backend/bookshelfmodel/languageitem.h b/src/backend/bookshelfmodel/languageitem.h
index 03a9ce3..1d1d5fa 100644
--- a/src/backend/bookshelfmodel/languageitem.h
+++ b/src/backend/bookshelfmodel/languageitem.h
@@ -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,25 +17,27 @@
#include "backend/bookshelfmodel/btbookshelfmodel.h"
#include "backend/drivers/cswordmoduleinfo.h"
-#include "util/directory.h"
namespace BookshelfModel {
-class LanguageItem: public Item {
- public:
- static const Item::Type GROUP_TYPE = Item::ITEM_LANGUAGE;
+class LanguageItem: public GroupItem<Item::ITEM_LANGUAGE> {
- LanguageItem(CSwordModuleInfo *module);
+public: /* Methods: */
- QVariant data(int role = Qt::DisplayRole) const;
+ inline LanguageItem(const CSwordModuleInfo & module)
+ : m_language(module.language()) {}
- inline bool fitFor(CSwordModuleInfo *module) const {
- return module->language() == m_language;
- }
+ QVariant data(int role = Qt::DisplayRole) const;
+
+ inline bool fitFor(const CSwordModuleInfo & module) const {
+ return module.language() == m_language;
+ }
+
+private: /* Fields: */
+
+ const CLanguageMgr::Language * m_language;
- protected:
- const CLanguageMgr::Language *m_language;
};
} // namespace BookshelfModel
diff --git a/src/backend/bookshelfmodel/moduleitem.cpp b/src/backend/bookshelfmodel/moduleitem.cpp
index ca9c3fb..ffe8eb8 100644
--- a/src/backend/bookshelfmodel/moduleitem.cpp
+++ b/src/backend/bookshelfmodel/moduleitem.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.
*
@@ -18,16 +18,9 @@
namespace BookshelfModel {
-ModuleItem::ModuleItem(CSwordModuleInfo *module,
- BtBookshelfTreeModel *parentModel)
- : Item(ITEM_MODULE), m_moduleInfo(module), m_parentModel(parentModel) {
- Q_ASSERT(module != 0);
- Q_ASSERT(parentModel != 0);
-}
-
QVariant ModuleItem::data(int role) const {
// Dispatch request to tree model:
- return m_parentModel->data(m_moduleInfo, role);
+ return m_parentModel.data(m_moduleInfo, role);
}
} // namespace BookshelfModel
diff --git a/src/backend/bookshelfmodel/moduleitem.h b/src/backend/bookshelfmodel/moduleitem.h
index 5e6e1ba..071e3fe 100644
--- a/src/backend/bookshelfmodel/moduleitem.h
+++ b/src/backend/bookshelfmodel/moduleitem.h
@@ -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.
*
@@ -24,22 +24,34 @@ class BtBookshelfTreeModel;
namespace BookshelfModel {
class ModuleItem: public Item {
- public:
- ModuleItem(CSwordModuleInfo *module, BtBookshelfTreeModel *parentModel);
-
- /**
- Reimplementation of Item::data() which dispatches all
- requests to the parent model (BtBookshelfTreeModel).
- */
- virtual QVariant data(int role = Qt::DisplayRole) const;
-
- inline CSwordModuleInfo *moduleInfo() const {
- return m_moduleInfo;
- }
-
- protected:
- CSwordModuleInfo *m_moduleInfo;
- BtBookshelfTreeModel *m_parentModel;
+
+public: /* Methods: */
+
+ ModuleItem(CSwordModuleInfo & module,
+ BtBookshelfTreeModel & parentModel)
+ : Item(ITEM_MODULE)
+ , m_moduleInfo(module)
+ , m_parentModel(parentModel) {}
+
+ /**
+ Reimplementation of Item::data() which dispatches all
+ requests to the parent model (BtBookshelfTreeModel).
+ */
+ virtual QVariant data(int role = Qt::DisplayRole) const;
+
+ inline CSwordModuleInfo & moduleInfo() const {
+ return m_moduleInfo;
+ }
+
+ inline virtual bool fitFor(const CSwordModuleInfo &) const {
+ return false;
+ }
+
+private: /* Fields: */
+
+ CSwordModuleInfo & m_moduleInfo;
+ BtBookshelfTreeModel & m_parentModel;
+
};
} // namespace BookshelfModel