File indexing completed on 2025-01-19 04:56:38
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org> 0003 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 */ 0005 0006 0007 #include "availablesourcesmodel.h" 0008 0009 #include <QIcon> 0010 0011 #include <KLocalizedString> 0012 0013 0014 #include "presentation/querytreemodel.h" 0015 0016 using namespace Presentation; 0017 0018 AvailableSourcesModel::AvailableSourcesModel(const Domain::DataSourceQueries::Ptr &dataSourceQueries, 0019 const Domain::DataSourceRepository::Ptr &dataSourceRepository, 0020 QObject *parent) 0021 : QObject(parent), 0022 m_sourceListModel(nullptr), 0023 m_dataSourceQueries(dataSourceQueries), 0024 m_dataSourceRepository(dataSourceRepository) 0025 { 0026 } 0027 0028 QAbstractItemModel *AvailableSourcesModel::sourceListModel() 0029 { 0030 if (!m_sourceListModel) 0031 m_sourceListModel = createSourceListModel(); 0032 return m_sourceListModel; 0033 } 0034 0035 void AvailableSourcesModel::showConfigDialog() 0036 { 0037 m_dataSourceRepository->showConfigDialog(); 0038 } 0039 0040 QAbstractItemModel *AvailableSourcesModel::createSourceListModel() 0041 { 0042 auto query = [this] (const Domain::DataSource::Ptr &source) { 0043 if (!source) 0044 return m_dataSourceQueries->findTopLevel(); 0045 else 0046 return m_dataSourceQueries->findChildren(source); 0047 }; 0048 0049 auto flags = [] (const Domain::DataSource::Ptr &source) -> Qt::ItemFlags { 0050 const Qt::ItemFlags defaultFlags = Qt::ItemIsSelectable 0051 | Qt::ItemIsEnabled; 0052 if (source->contentTypes() != Domain::DataSource::NoContent) 0053 return defaultFlags | Qt::ItemIsUserCheckable; 0054 else 0055 return defaultFlags; 0056 }; 0057 0058 auto data = [this] (const Domain::DataSource::Ptr &source, int role, int) -> QVariant { 0059 if (role != Qt::DisplayRole 0060 && role != Qt::EditRole 0061 && role != Qt::DecorationRole 0062 && role != Qt::CheckStateRole 0063 && role != QueryTreeModelBase::IconNameRole 0064 && role != QueryTreeModelBase::IsDefaultRole) { 0065 return QVariant(); 0066 } 0067 0068 if (role == Qt::DisplayRole || role == Qt::EditRole) { 0069 return source->name(); 0070 } else if (role == Qt::DecorationRole || role == QueryTreeModelBase::IconNameRole) { 0071 const QString iconName = source->iconName().isEmpty() ? QStringLiteral("folder") : source->iconName(); 0072 0073 if (role == Qt::DecorationRole) 0074 return QVariant::fromValue(QIcon::fromTheme(iconName)); 0075 else 0076 return iconName; 0077 } else if (role == Qt::CheckStateRole) { 0078 if (source->contentTypes() != Domain::DataSource::NoContent) 0079 return source->isSelected() ? Qt::Checked : Qt::Unchecked; 0080 else 0081 return QVariant(); 0082 } else if (role == QueryTreeModelBase::IsDefaultRole) { 0083 return m_dataSourceQueries->isDefaultSource(source); 0084 } else { 0085 return QVariant(); 0086 } 0087 }; 0088 0089 auto setData = [this] (const Domain::DataSource::Ptr &source, const QVariant &value, int role) { 0090 if (role != Qt::CheckStateRole) 0091 return false; 0092 if (source->contentTypes() == Domain::DataSource::NoContent) 0093 return false; 0094 0095 source->setSelected(value.toInt() == Qt::Checked); 0096 const auto job = m_dataSourceRepository->update(source); 0097 installHandler(job, i18n("Cannot modify source %1", source->name())); 0098 return true; 0099 }; 0100 0101 auto drop = [] (const QMimeData *mimeData, Qt::DropAction, const Domain::DataSource::Ptr &source) { 0102 Q_UNUSED(mimeData) 0103 Q_UNUSED(source) 0104 return false; 0105 }; 0106 0107 auto drag = [](const Domain::DataSource::List &) -> QMimeData* { 0108 return nullptr; 0109 }; 0110 0111 connect(m_dataSourceQueries->notifier(), &Domain::DataSourceQueriesNotifier::defaultSourceChanged, 0112 this, &AvailableSourcesModel::onDefaultSourceChanged); 0113 return new QueryTreeModel<Domain::DataSource::Ptr>(query, flags, data, setData, drop, drag, nullptr, this); 0114 } 0115 0116 void AvailableSourcesModel::setDefaultItem(const QModelIndex &index) 0117 { 0118 auto source = index.data(QueryTreeModelBase::ObjectRole).value<Domain::DataSource::Ptr>(); 0119 Q_ASSERT(source); 0120 m_dataSourceQueries->setDefaultSource(source); 0121 } 0122 0123 void AvailableSourcesModel::onDefaultSourceChanged() 0124 { 0125 emitDefaultSourceChanged(QModelIndex()); 0126 } 0127 0128 void AvailableSourcesModel::emitDefaultSourceChanged(const QModelIndex &root) 0129 { 0130 const auto rowCount = m_sourceListModel->rowCount(root); 0131 for (int row = 0; row < rowCount; row++) { 0132 const auto index = m_sourceListModel->index(row, 0, root); 0133 emit m_sourceListModel->dataChanged(index, index); 0134 emitDefaultSourceChanged(index); 0135 } 0136 } 0137 0138 #include "moc_availablesourcesmodel.cpp"