diff --git a/libdiscover/backends/DummyBackend/DummySourcesBackend.cpp b/libdiscover/backends/DummyBackend/DummySourcesBackend.cpp index fe052a63..3c81be81 100644 --- a/libdiscover/backends/DummyBackend/DummySourcesBackend.cpp +++ b/libdiscover/backends/DummyBackend/DummySourcesBackend.cpp @@ -1,70 +1,80 @@ /*************************************************************************** * Copyright © 2014 Aleix Pol Gonzalez * * * * 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, see . * ***************************************************************************/ #include "DummySourcesBackend.h" #include #include DummySourcesBackend::DummySourcesBackend(AbstractResourcesBackend * parent) : AbstractSourcesBackend(parent) , m_sources(new QStandardItemModel(this)) , m_testAction(new QAction(QIcon::fromTheme(QStringLiteral("kalgebra")), QStringLiteral("DummyAction"), this)) { for (int i = 0; i<10; ++i) addSource(QStringLiteral("DummySource%1").arg(i)); connect(m_testAction, &QAction::triggered, [](){ qDebug() << "action triggered!"; }); connect(m_sources, &QStandardItemModel::itemChanged, this, [](QStandardItem* item) { qDebug() << "DummySource changed" << item << item->checkState(); }); } QAbstractItemModel* DummySourcesBackend::sources() { return m_sources; } bool DummySourcesBackend::addSource(const QString& id) { if (id.isEmpty()) return false; QStandardItem* it = new QStandardItem(id); + it->setData(id, AbstractSourcesBackend::IdRole); it->setData(QVariant(id + QLatin1Char(' ') + id), Qt::ToolTipRole); it->setCheckable(true); it->setCheckState(Qt::Checked); m_sources->appendRow(it); return true; } +QStandardItem * DummySourcesBackend::sourceForId(const QString& id) const +{ + for (int i=0, c=m_sources->rowCount(); iitem(i, 0); + if (it->text() == id) + return it; + } + return nullptr; +} + bool DummySourcesBackend::removeSource(const QString& id) { - QList items = m_sources->findItems(id); - if (items.count()==1) { - m_sources->removeRow(items.first()->row()); - } else { - qWarning() << "couldn't find " << id << items; + const auto it = sourceForId(id); + if (!it) { + qWarning() << "couldn't find " << id; + return false; } - return items.count()==1; + return m_sources->removeRow(it->row()); } QList DummySourcesBackend::actions() const { return QList() << m_testAction; } diff --git a/libdiscover/backends/DummyBackend/DummySourcesBackend.h b/libdiscover/backends/DummyBackend/DummySourcesBackend.h index 3033f65b..fb7671a9 100644 --- a/libdiscover/backends/DummyBackend/DummySourcesBackend.h +++ b/libdiscover/backends/DummyBackend/DummySourcesBackend.h @@ -1,44 +1,46 @@ /*************************************************************************** * Copyright © 2014 Aleix Pol Gonzalez * * * * 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, see . * ***************************************************************************/ #ifndef DUMMYSOURCESBACKEND_H #define DUMMYSOURCESBACKEND_H #include #include class DummySourcesBackend : public AbstractSourcesBackend { public: explicit DummySourcesBackend(AbstractResourcesBackend * parent); QAbstractItemModel* sources() override; bool addSource(const QString& id) override; bool removeSource(const QString& id) override; QString idDescription() override { return QStringLiteral("Random weird text"); } QList actions() const override; bool supportsAdding() const override { return true; } private: + QStandardItem* sourceForId(const QString& id) const; + QStandardItemModel* m_sources; QAction* m_testAction; }; #endif // DUMMYSOURCESBACKEND_H