diff --git a/src/presentation/availablesourcesmodel.cpp b/src/presentation/availablesourcesmodel.cpp index 6c0fceb0..96e07316 100644 --- a/src/presentation/availablesourcesmodel.cpp +++ b/src/presentation/availablesourcesmodel.cpp @@ -1,157 +1,156 @@ /* This file is part of Zanshin Copyright 2014 Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "availablesourcesmodel.h" #include #include #include "domain/datasourcequeries.h" #include "domain/datasourcerepository.h" #include "presentation/querytreemodel.h" using namespace Presentation; AvailableSourcesModel::AvailableSourcesModel(const Domain::DataSourceQueries::Ptr &dataSourceQueries, const Domain::DataSourceRepository::Ptr &dataSourceRepository, QObject *parent) : QObject(parent), m_sourceListModel(Q_NULLPTR), m_dataSourceQueries(dataSourceQueries), m_dataSourceRepository(dataSourceRepository) { } QAbstractItemModel *AvailableSourcesModel::sourceListModel() { if (!m_sourceListModel) m_sourceListModel = createSourceListModel(); return m_sourceListModel; } void AvailableSourcesModel::showConfigDialog() { m_dataSourceRepository->showConfigDialog(); } QAbstractItemModel *AvailableSourcesModel::createSourceListModel() { auto query = [this] (const Domain::DataSource::Ptr &source) { if (!source) return m_dataSourceQueries->findTopLevel(); else return m_dataSourceQueries->findChildren(source); }; auto flags = [] (const Domain::DataSource::Ptr &source) -> Qt::ItemFlags { const Qt::ItemFlags defaultFlags = Qt::ItemIsSelectable | Qt::ItemIsEnabled; if (source->contentTypes() != Domain::DataSource::NoContent) return defaultFlags | Qt::ItemIsUserCheckable; else return defaultFlags; }; auto data = [this] (const Domain::DataSource::Ptr &source, int role) -> QVariant { if (role != Qt::DisplayRole && role != Qt::EditRole && role != Qt::DecorationRole && role != Qt::CheckStateRole && role != QueryTreeModelBase::IconNameRole && role != QueryTreeModelBase::IsDefaultRole) { return QVariant(); } if (role == Qt::DisplayRole || role == Qt::EditRole) { return source->name(); } else if (role == Qt::DecorationRole || role == QueryTreeModelBase::IconNameRole) { const QString iconName = source->iconName().isEmpty() ? QStringLiteral("folder") : source->iconName(); if (role == Qt::DecorationRole) return QVariant::fromValue(QIcon::fromTheme(iconName)); else return iconName; } else if (role == Qt::CheckStateRole) { if (source->contentTypes() != Domain::DataSource::NoContent) return source->isSelected() ? Qt::Checked : Qt::Unchecked; else return QVariant(); } else if (role == QueryTreeModelBase::IsDefaultRole) { return m_dataSourceQueries->isDefaultSource(source); } else { return QVariant(); } }; auto setData = [this] (const Domain::DataSource::Ptr &source, const QVariant &value, int role) { if (role != Qt::CheckStateRole) return false; if (source->contentTypes() == Domain::DataSource::NoContent) return false; source->setSelected(value.toInt() == Qt::Checked); const auto job = m_dataSourceRepository->update(source); installHandler(job, i18n("Cannot modify source %1", source->name())); return true; }; auto drop = [] (const QMimeData *mimeData, Qt::DropAction, const Domain::DataSource::Ptr &source) { Q_UNUSED(mimeData) Q_UNUSED(source) return false; }; auto drag = [](const Domain::DataSource::List &) -> QMimeData* { return Q_NULLPTR; }; connect(m_dataSourceQueries->notifier(), &Domain::DataSourceQueriesNotifier::defaultSourceChanged, this, &AvailableSourcesModel::onDefaultSourceChanged); return new QueryTreeModel(query, flags, data, setData, drop, drag, this); } void AvailableSourcesModel::setDefaultItem(const QModelIndex &index) { auto source = index.data(QueryTreeModelBase::ObjectRole).value(); Q_ASSERT(source); m_dataSourceQueries->setDefaultSource(source); } void AvailableSourcesModel::onDefaultSourceChanged() { emitDefaultSourceChanged(QModelIndex()); } void AvailableSourcesModel::emitDefaultSourceChanged(const QModelIndex &root) { const auto rowCount = m_sourceListModel->rowCount(root); for (int row = 0; row < rowCount; row++) { const auto index = m_sourceListModel->index(row, 0, root); - // TODO Qt5: Remove static_cast - emit static_cast(m_sourceListModel)->dataChanged(index, index); + emit m_sourceListModel->dataChanged(index, index); emitDefaultSourceChanged(index); } } diff --git a/src/presentation/querytreemodelbase.h b/src/presentation/querytreemodelbase.h index 95b3694e..eb90f35e 100644 --- a/src/presentation/querytreemodelbase.h +++ b/src/presentation/querytreemodelbase.h @@ -1,115 +1,112 @@ /* This file is part of Zanshin Copyright 2014 Mario Bensi Copyright 2014 Kevin Ottens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef PRESENTATION_QUERYTREEMODELBASE_H #define PRESENTATION_QUERYTREEMODELBASE_H #include namespace Presentation { class QueryTreeModelBase; class QueryTreeNodeBase { public: QueryTreeNodeBase(QueryTreeNodeBase *parent, QueryTreeModelBase *model); virtual ~QueryTreeNodeBase(); virtual Qt::ItemFlags flags() const = 0; virtual QVariant data(int role) const = 0; virtual bool setData(const QVariant &value, int role) = 0; virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action) = 0; int row(); QueryTreeNodeBase *parent() const; QueryTreeNodeBase *child(int row) const; void insertChild(int row, QueryTreeNodeBase *node); void appendChild(QueryTreeNodeBase *node); void removeChildAt(int row); int childCount() const; protected: QModelIndex index(int row, int column, const QModelIndex &parent) const; QModelIndex createIndex(int row, int column, void *data) const; void beginInsertRows(const QModelIndex &parent, int first, int last); void endInsertRows(); void beginRemoveRows(const QModelIndex &parent, int first, int last); void endRemoveRows(); void emitDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); private: QueryTreeNodeBase *m_parent; QList m_childNode; QueryTreeModelBase *m_model; }; class QueryTreeModelBase : public QAbstractItemModel { Q_OBJECT public: enum { ObjectRole = Qt::UserRole + 1, IconNameRole, IsDefaultRole, UserRole }; ~QueryTreeModelBase(); Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE; int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE; bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) Q_DECL_OVERRIDE; QMimeData *mimeData(const QModelIndexList &indexes) const Q_DECL_OVERRIDE; QStringList mimeTypes() const Q_DECL_OVERRIDE; Qt::DropActions supportedDragActions() const Q_DECL_OVERRIDE; Qt::DropActions supportedDropActions() const Q_DECL_OVERRIDE; - // TODO Qt5: Remove but needed in Qt4, so that we can trigger it from the outside - using QAbstractItemModel::dataChanged; - protected: explicit QueryTreeModelBase(QueryTreeNodeBase *rootNode, QObject *parent = Q_NULLPTR); virtual QMimeData *createMimeData(const QModelIndexList &indexes) const = 0; QueryTreeNodeBase *nodeFromIndex(const QModelIndex &index) const; void setRootIndexFlag(Qt::ItemFlags flags); private: friend class QueryTreeNodeBase; bool isModelIndexValid(const QModelIndex &index) const; Qt::ItemFlags m_rootIndexFlag; QueryTreeNodeBase *m_rootNode; }; } #endif // PRESENTATION_QUERYTREEMODELBASE_H