From: Kai Uwe Broulik Date: Thu, 16 Dec 2021 18:37:39 +0000 (+0100) Subject: Remove KStandardItem and KStandardItemModel X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/af2baf8047c0f4ca248b22f2c36074ad86a2020e?ds=sidebyside Remove KStandardItem and KStandardItemModel They were used by the custom places panel and are now unused. --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a3a15aaf3..016edb8d3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -74,11 +74,9 @@ target_sources(dolphinprivate PRIVATE kitemviews/kitemlistwidget.cpp kitemviews/kitemmodelbase.cpp kitemviews/kitemset.cpp - kitemviews/kstandarditem.cpp kitemviews/kstandarditemlistgroupheader.cpp kitemviews/kstandarditemlistwidget.cpp kitemviews/kstandarditemlistview.cpp - kitemviews/kstandarditemmodel.cpp kitemviews/private/kdirectorycontentscounter.cpp kitemviews/private/kdirectorycontentscounterworker.cpp kitemviews/private/kfileitemclipboard.cpp diff --git a/src/kitemviews/kstandarditem.cpp b/src/kitemviews/kstandarditem.cpp deleted file mode 100644 index fcaa50b9d..000000000 --- a/src/kitemviews/kstandarditem.cpp +++ /dev/null @@ -1,129 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2012 Peter Penz - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#include "kstandarditem.h" -#include "kstandarditemmodel.h" - -KStandardItem::KStandardItem(KStandardItem* parent) : - QObject(parent), - m_model(nullptr), - m_data() -{ -} - -KStandardItem::KStandardItem(const QString& text, KStandardItem* parent) : - QObject(parent), - m_model(nullptr), - m_data() -{ - setText(text); -} - -KStandardItem::KStandardItem(const QString& icon, const QString& text, KStandardItem* parent) : - QObject(parent), - m_model(nullptr), - m_data() -{ - setIcon(icon); - setText(text); -} - -KStandardItem::~KStandardItem() -{ -} - -void KStandardItem::setText(const QString& text) -{ - setDataValue("text", text); -} - -QString KStandardItem::text() const -{ - return m_data["text"].toString(); -} - -void KStandardItem::setIcon(const QString& icon) -{ - setDataValue("iconName", icon); -} - -QString KStandardItem::icon() const -{ - return m_data["iconName"].toString(); -} - -void KStandardItem::setIconOverlays(const QStringList& overlays) -{ - setDataValue("iconOverlays", overlays); -} - -QStringList KStandardItem::iconOverlays() const -{ - return m_data["iconOverlays"].toStringList(); -} - -void KStandardItem::setGroup(const QString& group) -{ - setDataValue("group", group); -} - -QString KStandardItem::group() const -{ - return m_data["group"].toString(); -} - -void KStandardItem::setDataValue(const QByteArray& role, const QVariant& value) -{ - const QVariant previous = m_data.value(role); - if (previous == value) { - return; - } - - m_data.insert(role, value); - onDataValueChanged(role, value, previous); - - if (m_model) { - const int index = m_model->index(this); - QSet changedRoles; - changedRoles.insert(role); - m_model->onItemChanged(index, changedRoles); - Q_EMIT m_model->itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles); - } -} - -QVariant KStandardItem::dataValue(const QByteArray& role) const -{ - return m_data[role]; -} - -void KStandardItem::setData(const QHash& values) -{ - const QHash previous = m_data; - m_data = values; - onDataChanged(values, previous); -} - -QHash KStandardItem::data() const -{ - return m_data; -} - -void KStandardItem::onDataValueChanged(const QByteArray& role, - const QVariant& current, - const QVariant& previous) -{ - Q_UNUSED(role) - Q_UNUSED(current) - Q_UNUSED(previous) -} - -void KStandardItem::onDataChanged(const QHash& current, - const QHash& previous) -{ - Q_UNUSED(current) - Q_UNUSED(previous) -} - diff --git a/src/kitemviews/kstandarditem.h b/src/kitemviews/kstandarditem.h deleted file mode 100644 index fb64b334d..000000000 --- a/src/kitemviews/kstandarditem.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2012 Peter Penz - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#ifndef KSTANDARDITEM_H -#define KSTANDARDITEM_H - -#include "dolphin_export.h" - -#include -#include -#include -#include -#include - -class KStandardItemModel; - -/** - * @brief Represents and item of KStandardItemModel. - * - * Provides setter- and getter-methods for the most commonly - * used roles. It is possible to assign values for custom - * roles by using setDataValue(). - */ -class DOLPHIN_EXPORT KStandardItem : public QObject -{ - Q_OBJECT -public: - explicit KStandardItem(KStandardItem* parent = nullptr); - explicit KStandardItem(const QString& text, KStandardItem* parent = nullptr); - KStandardItem(const QString& icon, const QString& text, KStandardItem* parent = nullptr); - ~KStandardItem() override; - - /** - * Sets the text for the "text"-role. - */ - void setText(const QString& text); - QString text() const; - - /** - * Sets the icon for the "iconName"-role. - */ - void setIcon(const QString& icon); - QString icon() const; - - void setIconOverlays(const QStringList& overlays); - QStringList iconOverlays() const; - - /** - * Sets the group for the "group"-role. - */ - void setGroup(const QString& group); - QString group() const; - - void setDataValue(const QByteArray& role, const QVariant& value); - QVariant dataValue(const QByteArray& role) const; - - void setData(const QHash& values); - QHash data() const; - -protected: - virtual void onDataValueChanged(const QByteArray& role, - const QVariant& current, - const QVariant& previous); - - virtual void onDataChanged(const QHash& current, - const QHash& previous); - -private: - KStandardItemModel* m_model; - - QHash m_data; - - friend class KStandardItemModel; -}; - -#endif - - diff --git a/src/kitemviews/kstandarditemmodel.cpp b/src/kitemviews/kstandarditemmodel.cpp deleted file mode 100644 index 128841ca8..000000000 --- a/src/kitemviews/kstandarditemmodel.cpp +++ /dev/null @@ -1,225 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2012 Peter Penz - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#include "kstandarditemmodel.h" - -#include "kstandarditem.h" - -KStandardItemModel::KStandardItemModel(QObject* parent) : - KItemModelBase(parent), - m_items(), - m_indexesForItems() -{ -} - -KStandardItemModel::~KStandardItemModel() -{ - qDeleteAll(m_items); - m_items.clear(); - m_indexesForItems.clear(); -} - -void KStandardItemModel::insertItem(int index, KStandardItem* item) -{ - if (index < 0 || index > count() || !item) { - delete item; - return; - } - - if (!m_indexesForItems.contains(item)) { - item->m_model = this; - m_items.insert(index, item); - m_indexesForItems.insert(item, index); - - // Inserting an item requires to update the indexes - // afterwards from m_indexesForItems. - for (int i = index + 1; i < m_items.count(); ++i) { - m_indexesForItems.insert(m_items[i], i); - } - - // TODO: no hierarchical items are handled yet - - onItemInserted(index); - Q_EMIT itemsInserted(KItemRangeList() << KItemRange(index, 1)); - } -} - -void KStandardItemModel::changeItem(int index, KStandardItem* item) -{ - if (index < 0 || index >= count() || !item) { - delete item; - return; - } - - item->m_model = this; - - QSet changedRoles; - - KStandardItem* oldItem = m_items[index]; - const QHash oldData = oldItem->data(); - const QHash newData = item->data(); - - // Determine which roles have been changed - QHashIterator it(oldData); - while (it.hasNext()) { - it.next(); - const QByteArray role = it.key(); - const QVariant oldValue = it.value(); - if (newData.contains(role) && newData.value(role) != oldValue) { - changedRoles.insert(role); - } - } - - m_indexesForItems.remove(oldItem); - delete oldItem; - oldItem = nullptr; - - m_items[index] = item; - m_indexesForItems.insert(item, index); - - onItemChanged(index, changedRoles); - Q_EMIT itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles); -} - -void KStandardItemModel::removeItem(int index) -{ - if (index >= 0 && index < count()) { - KStandardItem* item = m_items[index]; - m_indexesForItems.remove(item); - m_items.removeAt(index); - - // Removing an item requires to update the indexes - // afterwards from m_indexesForItems. - for (int i = index; i < m_items.count(); ++i) { - m_indexesForItems.insert(m_items[i], i); - } - - onItemRemoved(index, item); - - item->deleteLater(); - item = nullptr; - - Q_EMIT itemsRemoved(KItemRangeList() << KItemRange(index, 1)); - - // TODO: no hierarchical items are handled yet - } -} - -void KStandardItemModel::clear() -{ - int size = m_items.size(); - m_items.clear(); - m_indexesForItems.clear(); - - Q_EMIT itemsRemoved(KItemRangeList() << KItemRange(0, size)); -} - -KStandardItem* KStandardItemModel::item(int index) const -{ - if (index < 0 || index >= m_items.count()) { - return nullptr; - } - return m_items[index]; -} - -int KStandardItemModel::index(const KStandardItem* item) const -{ - return m_indexesForItems.value(item, -1); -} - -void KStandardItemModel::appendItem(KStandardItem *item) -{ - insertItem(m_items.count(), item); -} - -int KStandardItemModel::count() const -{ - return m_items.count(); -} - -QHash KStandardItemModel::data(int index) const -{ - if (index >= 0 && index < count()) { - const KStandardItem* item = m_items[index]; - if (item) { - return item->data(); - } - } - return QHash(); -} - -bool KStandardItemModel::setData(int index, const QHash& values) -{ - Q_UNUSED(values) - if (index < 0 || index >= count()) { - return false; - } - - return true; -} - -QMimeData* KStandardItemModel::createMimeData(const KItemSet& indexes) const -{ - Q_UNUSED(indexes) - return nullptr; -} - -int KStandardItemModel::indexForKeyboardSearch(const QString& text, int startFromIndex) const -{ - Q_UNUSED(text) - Q_UNUSED(startFromIndex) - return -1; -} - -bool KStandardItemModel::supportsDropping(int index) const -{ - Q_UNUSED(index) - return false; -} - -QString KStandardItemModel::roleDescription(const QByteArray& role) const -{ - Q_UNUSED(role) - return QString(); -} - -QList > KStandardItemModel::groups() const -{ - QList > groups; - - const QByteArray role = sortRole().isEmpty() ? "group" : sortRole(); - bool isFirstGroupValue = true; - QString groupValue; - const int maxIndex = count() - 1; - for (int i = 0; i <= maxIndex; ++i) { - const QString newGroupValue = m_items.at(i)->dataValue(role).toString(); - if (newGroupValue != groupValue || isFirstGroupValue) { - groupValue = newGroupValue; - groups.append(QPair(i, newGroupValue)); - isFirstGroupValue = false; - } - } - - return groups; -} - -void KStandardItemModel::onItemInserted(int index) -{ - Q_UNUSED(index) -} - -void KStandardItemModel::onItemChanged(int index, const QSet& changedRoles) -{ - Q_UNUSED(index) - Q_UNUSED(changedRoles) -} - -void KStandardItemModel::onItemRemoved(int index, KStandardItem* removedItem) -{ - Q_UNUSED(index) - Q_UNUSED(removedItem) -} - diff --git a/src/kitemviews/kstandarditemmodel.h b/src/kitemviews/kstandarditemmodel.h deleted file mode 100644 index d92ec5d46..000000000 --- a/src/kitemviews/kstandarditemmodel.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2012 Peter Penz - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#ifndef KSTANDARDITEMMODEL_H -#define KSTANDARDITEMMODEL_H - -#include "dolphin_export.h" -#include "kitemviews/kitemmodelbase.h" - -#include -#include - -class KStandardItem; - -/** - * @brief Model counterpart for KStandardItemListView. - * - * Allows to add items to the model in an easy way by the - * class KStandardItem. - * - * @see KStandardItem - */ -class DOLPHIN_EXPORT KStandardItemModel : public KItemModelBase -{ - Q_OBJECT - -public: - explicit KStandardItemModel(QObject* parent = nullptr); - ~KStandardItemModel() override; - - /** - * Inserts the item \a item at the index \a index. If the index - * is equal to the number of items of the model, the item - * gets appended as last element. KStandardItemModel takes - * the ownership of the item. If the index is invalid, the item - * gets deleted. - */ - void insertItem(int index, KStandardItem* item); - - /** - * Changes the item on the index \a index to \a item. - * KStandardItemModel takes the ownership of the item. The - * old item gets deleted. If the index is invalid, the item - * gets deleted. - */ - void changeItem(int index, KStandardItem* item); - - void removeItem(int index); - KStandardItem* item(int index) const; - int index(const KStandardItem* item) const; - - /** - * Convenience method for insertItem(count(), item). - */ - void appendItem(KStandardItem* item); - - int count() const override; - QHash data(int index) const override; - bool setData(int index, const QHash& values) override; - QMimeData* createMimeData(const KItemSet& indexes) const override; - int indexForKeyboardSearch(const QString& text, int startFromIndex = 0) const override; - bool supportsDropping(int index) const override; - QString roleDescription(const QByteArray& role) const override; - QList > groups() const override; - - virtual void clear(); -protected: - /** - * Is invoked after an item has been inserted and before the signal - * itemsInserted() gets emitted. - */ - virtual void onItemInserted(int index); - - /** - * Is invoked after an item or one of its roles has been changed and - * before the signal itemsChanged() gets emitted. - */ - virtual void onItemChanged(int index, const QSet& changedRoles); - - /** - * Is invoked after an item has been removed and before the signal - * itemsRemoved() gets emitted. The item \a removedItem has already - * been removed from the model and will get deleted after the - * execution of onItemRemoved(). - */ - virtual void onItemRemoved(int index, KStandardItem* removedItem); - -private: - QList m_items; - QHash m_indexesForItems; - - friend class KStandardItem; - friend class KStandardItemModelTest; // For unit testing -}; - -#endif - - diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 95ba01861..909bc9edf 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -51,11 +51,6 @@ if (KF5Baloo_FOUND) LINK_LIBRARIES dolphinprivate dolphinstatic Qt5::Test) endif() -# KStandardItemModelTest -ecm_add_test(kstandarditemmodeltest.cpp -TEST_NAME kstandarditemmodeltest -LINK_LIBRARIES dolphinprivate dolphinstatic Qt5::Test) - # ViewPropertiesTest ecm_add_test(viewpropertiestest.cpp testdir.cpp TEST_NAME viewpropertiestest diff --git a/src/tests/kstandarditemmodeltest.cpp b/src/tests/kstandarditemmodeltest.cpp deleted file mode 100644 index 943a85214..000000000 --- a/src/tests/kstandarditemmodeltest.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2011 Peter Penz - * SPDX-FileCopyrightText: 2011 Frank Reininghaus - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#include "dolphindebug.h" - -#include "kitemviews/kstandarditem.h" -#include "kitemviews/kstandarditemmodel.h" - -#include -#include - -class KStandardItemModelTest : public QObject -{ - Q_OBJECT - -private Q_SLOTS: - void initTestCase(); - void init(); - void cleanup(); - - void testNewItems(); - void testRemoveItems(); - -private: - bool isModelConsistent() const; - -private: - KStandardItemModel* m_model; -}; - -void KStandardItemModelTest::initTestCase() -{ - QStandardPaths::setTestModeEnabled(true); -} - -void KStandardItemModelTest::init() -{ - m_model = new KStandardItemModel(); -} - -void KStandardItemModelTest::cleanup() -{ - delete m_model; - m_model = nullptr; -} - -void KStandardItemModelTest::testNewItems() -{ - m_model->insertItem(0, new KStandardItem("item 1")); - m_model->insertItem(0, new KStandardItem("item 2")); - m_model->insertItem(2, new KStandardItem("item 3")); - m_model->appendItem(new KStandardItem("item 4")); - m_model->insertItem(-1, new KStandardItem("invalid 1")); - m_model->insertItem(5, new KStandardItem("invalid 2")); - QCOMPARE(m_model->count(), 4); - QCOMPARE(m_model->item(0)->text(), QString("item 2")); - QCOMPARE(m_model->item(1)->text(), QString("item 1")); - QCOMPARE(m_model->item(2)->text(), QString("item 3")); - QCOMPARE(m_model->item(3)->text(), QString("item 4")); - - QVERIFY(isModelConsistent()); -} - -void KStandardItemModelTest::testRemoveItems() -{ - for (int i = 1; i <= 10; ++i) { - m_model->appendItem(new KStandardItem("item " + QString::number(i))); - } - - m_model->removeItem(0); - m_model->removeItem(3); - m_model->removeItem(5); - m_model->removeItem(-1); - QCOMPARE(m_model->count(), 7); - QCOMPARE(m_model->item(0)->text(), QString("item 2")); - QCOMPARE(m_model->item(1)->text(), QString("item 3")); - QCOMPARE(m_model->item(2)->text(), QString("item 4")); - QCOMPARE(m_model->item(3)->text(), QString("item 6")); - QCOMPARE(m_model->item(4)->text(), QString("item 7")); - QCOMPARE(m_model->item(5)->text(), QString("item 9")); - QCOMPARE(m_model->item(6)->text(), QString("item 10")); -} - -bool KStandardItemModelTest::isModelConsistent() const -{ - if (m_model->m_items.count() != m_model->m_indexesForItems.count()) { - return false; - } - - for (int i = 0; i < m_model->count(); ++i) { - const KStandardItem* item = m_model->item(i); - if (!item) { - qCWarning(DolphinDebug) << "Item" << i << "is null"; - return false; - } - - const int itemIndex = m_model->index(item); - if (itemIndex != i) { - qCWarning(DolphinDebug) << "Item" << i << "has a wrong index:" << itemIndex; - return false; - } - } - - return true; -} - -QTEST_GUILESS_MAIN(KStandardItemModelTest) - -#include "kstandarditemmodeltest.moc"