File indexing completed on 2024-11-24 04:54:53

0001 /*
0002  *   SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "DummySourcesBackend.h"
0008 #include "resources/DiscoverAction.h"
0009 #include <QDebug>
0010 
0011 DummySourcesBackend::DummySourcesBackend(AbstractResourcesBackend *parent)
0012     : AbstractSourcesBackend(parent)
0013     , m_sources(new QStandardItemModel(this))
0014     , m_testAction(new DiscoverAction(QStringLiteral("kalgebra"), QStringLiteral("DummyAction"), this))
0015 {
0016     for (int i = 0; i < 10; ++i)
0017         addSource(QStringLiteral("DummySource%1").arg(i));
0018 
0019     connect(m_testAction, &DiscoverAction::triggered, []() {
0020         qDebug() << "action triggered!";
0021     });
0022     connect(m_sources, &QStandardItemModel::itemChanged, this, [](QStandardItem *item) {
0023         qDebug() << "DummySource changed" << item << item->checkState();
0024     });
0025 }
0026 
0027 QAbstractItemModel *DummySourcesBackend::sources()
0028 {
0029     return m_sources;
0030 }
0031 
0032 bool DummySourcesBackend::addSource(const QString &id)
0033 {
0034     if (id.isEmpty())
0035         return false;
0036 
0037     QStandardItem *it = new QStandardItem(id);
0038     it->setData(id, AbstractSourcesBackend::IdRole);
0039     it->setData(QVariant(id + QLatin1Char(' ') + id), Qt::ToolTipRole);
0040     it->setCheckable(true);
0041     it->setCheckState(Qt::Checked);
0042     m_sources->appendRow(it);
0043     return true;
0044 }
0045 
0046 QStandardItem *DummySourcesBackend::sourceForId(const QString &id) const
0047 {
0048     for (int i = 0, c = m_sources->rowCount(); i < c; ++i) {
0049         const auto it = m_sources->item(i, 0);
0050         if (it->text() == id)
0051             return it;
0052     }
0053     return nullptr;
0054 }
0055 
0056 bool DummySourcesBackend::removeSource(const QString &id)
0057 {
0058     const auto it = sourceForId(id);
0059     if (!it) {
0060         Q_EMIT passiveMessage(QStringLiteral("Could not find %1").arg(id));
0061         return false;
0062     }
0063     return m_sources->removeRow(it->row());
0064 }
0065 
0066 QVariantList DummySourcesBackend::actions() const
0067 {
0068     return QVariantList() << QVariant::fromValue<QObject *>(m_testAction);
0069 }
0070 
0071 bool DummySourcesBackend::moveSource(const QString &sourceId, int delta)
0072 {
0073     int row = sourceForId(sourceId)->row();
0074     auto prevRow = m_sources->takeRow(row);
0075     Q_ASSERT(!prevRow.isEmpty());
0076 
0077     const auto destRow = row + delta;
0078     m_sources->insertRow(destRow, prevRow);
0079     if (destRow == 0 || row == 0)
0080         Q_EMIT firstSourceIdChanged();
0081     if (destRow == m_sources->rowCount() - 1 || row == m_sources->rowCount() - 1)
0082         Q_EMIT lastSourceIdChanged();
0083     return true;
0084 }