diff options
Diffstat (limited to 'src/backend/bookshelfmodel/btbookshelfmodel.cpp')
-rw-r--r-- | src/backend/bookshelfmodel/btbookshelfmodel.cpp | 74 |
1 files changed, 57 insertions, 17 deletions
diff --git a/src/backend/bookshelfmodel/btbookshelfmodel.cpp b/src/backend/bookshelfmodel/btbookshelfmodel.cpp index 99f83ba..6882b90 100644 --- a/src/backend/bookshelfmodel/btbookshelfmodel.cpp +++ b/src/backend/bookshelfmodel/btbookshelfmodel.cpp @@ -14,7 +14,8 @@ #include <QSet> #include "util/cresmgr.h" -#include "util/directoryutil.h" +#include "util/directory.h" + BtBookshelfModel::BtBookshelfModel(QObject *parent) : QAbstractListModel(parent) { @@ -29,25 +30,35 @@ int BtBookshelfModel::rowCount(const QModelIndex &parent) const { return m_data.size(); } -QVariant BtBookshelfModel::data(const QModelIndex &index, int role) const { - if (!index.isValid() || index.column() != 0 || index.parent().isValid()) { - return QVariant(); - } - int row(index.row()); - if (row >= m_data.size()) return QVariant(); - +QVariant BtBookshelfModel::data(CSwordModuleInfo *module, int role) const { switch (role) { case ModuleNameRole: // Qt::DisplayRole - return m_data.at(row)->name(); + return module->name(); case ModuleIconRole: // Qt::DecorationRole - return moduleIcon(m_data.at(row)); + return moduleIcon(module); case ModulePointerRole: - return qVariantFromValue((void*) m_data.at(row)); + return qVariantFromValue((void*) module); + case ModuleCategoryRole: + return QVariant::fromValue(module->category()); + case ModuleLanguageRole: + return QVariant(); /// \todo Unimplemented + case ModuleHiddenRole: + return module->isHidden(); default: return QVariant(); } } +QVariant BtBookshelfModel::data(const QModelIndex &index, int role) const { + if (!index.isValid() || index.column() != 0 || index.parent().isValid()) { + return QVariant(); + } + int row(index.row()); + if (row >= m_data.size()) return QVariant(); + + return data(m_data.at(row), role); +} + QVariant BtBookshelfModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role == Qt::DisplayRole && orientation == Qt::Horizontal && @@ -58,8 +69,25 @@ QVariant BtBookshelfModel::headerData(int section, Qt::Orientation orientation, return QVariant(); } +bool BtBookshelfModel::setData(const QModelIndex &index, const QVariant &value, + int role) { + int row(index.row()); + if (role == ModuleHiddenRole && row >= 0 && row < m_data.size() + && index.column() == 0) + { + /* + Emitting dataChanged here is actually mandatory, but were not doing it + directly. Since we're connected to the module, changing its hidden + state will emit a signal we catch in moduleHidden(), which in turn is + what will actually emit dataChanged(). + */ + return m_data.at(row)->setHidden(value.toBool()); + } + return false; +} + QIcon BtBookshelfModel::moduleIcon(const CSwordModuleInfo *m) { - typedef util::filesystem::DirectoryUtil DU; + namespace DU = util::directory; /// \todo Make CSwordModuleInfo::isLocked() const and remove const_cast: CSwordModuleInfo *module(const_cast<CSwordModuleInfo*>(m)); @@ -101,7 +129,7 @@ QIcon BtBookshelfModel::moduleIcon(const CSwordModuleInfo *m) { } QIcon BtBookshelfModel::categoryIcon(const CSwordModuleInfo::Category &category) { - typedef util::filesystem::DirectoryUtil DU; + namespace DU = util::directory; switch (category) { case CSwordModuleInfo::Bibles: @@ -174,6 +202,8 @@ void BtBookshelfModel::addModule(CSwordModuleInfo * const module) { const int index(m_data.size()); beginInsertRows(QModelIndex(), index, index); m_data.append(module); + connect(module, SIGNAL(hiddenChanged(bool)), + this, SLOT(moduleHidden(bool))); endInsertRows(); } @@ -193,13 +223,11 @@ void BtBookshelfModel::addModules(const QSet<CSwordModuleInfo *> &modules) { beginInsertRows(QModelIndex(), m_data.size(), m_data.size() + newModules.size() - 1); -#if QT_VERSION >= 0x040500 - m_data.append(newModules); -#else Q_FOREACH(CSwordModuleInfo *module, newModules) { m_data.append(module); + connect(module, SIGNAL(hiddenChanged(bool)), + this, SLOT(moduleHidden(bool))); } -#endif endInsertRows(); } @@ -210,6 +238,8 @@ void BtBookshelfModel::removeModule(CSwordModuleInfo * const module, if (index == -1) return; beginRemoveRows(QModelIndex(), index, index); + disconnect(module, SIGNAL(hiddenChanged(bool)), + this, SLOT(moduleHidden(bool))); m_data.removeAt(index); endRemoveRows(); if (destroy) delete module; @@ -236,3 +266,13 @@ CSwordModuleInfo* BtBookshelfModel::getModule(const QString &name) const { } return 0; } + +void BtBookshelfModel::moduleHidden(bool) { + Q_ASSERT(qobject_cast<CSwordModuleInfo*>(sender()) != 0); + + CSwordModuleInfo *module(static_cast<CSwordModuleInfo*>(sender())); + Q_ASSERT(m_data.count(module) == 1); + + QModelIndex i(index(m_data.indexOf(module), 0)); + emit dataChanged(i, i); +} |