From 11e416bea6cdfc0556522502852379a1d52483b4 Mon Sep 17 00:00:00 2001 From: Kai Uwe Broulik Date: Thu, 31 Aug 2017 14:29:05 +0200 Subject: [PATCH] [ConfigModel] Allow programmatically adding and removing ConfigCategory The appendCategory(ConfigCategory*) allows to add a category created by e.g. an Instantiator that can use bindings rather than fixed arguments. The removeCategory methods allow to remove a category when e.g. the model in an Instantiator resets. CCBUG: 372090 Differential Revision: https://phabricator.kde.org/D7602 --- src/plasmaquick/configmodel.cpp | 42 +++++++++++++++++++++++++++++++++ src/plasmaquick/configmodel.h | 5 ++++ 2 files changed, 47 insertions(+) diff --git a/src/plasmaquick/configmodel.cpp b/src/plasmaquick/configmodel.cpp index 9a4b38ca4..6975bd979 100644 --- a/src/plasmaquick/configmodel.cpp +++ b/src/plasmaquick/configmodel.cpp @@ -57,6 +57,8 @@ public: QHash kcms; void appendCategory(ConfigCategory *c); + void removeCategory(ConfigCategory *c); + void removeCategoryAt(int index); void clear(); QVariant get(int row) const; @@ -154,6 +156,31 @@ void ConfigModelPrivate::appendCategory(ConfigCategory *c) emit q->countChanged(); } +void ConfigModelPrivate::removeCategory(ConfigCategory *c) +{ + const int index = categories.indexOf(c); + if (index > -1) { + removeCategoryAt(index); + } +} + +void ConfigModelPrivate::removeCategoryAt(int index) +{ + if (index < 0 || index >= categories.count()) { + return; + } + + q->beginRemoveRows(QModelIndex(), index, index); + + ConfigCategory *c = categories.takeAt(index); + if (c->parent() == q) { + c->deleteLater(); + } + + q->endRemoveRows(); + emit q->countChanged(); +} + QVariant ConfigModelPrivate::get(int row) const { QVariantMap value; @@ -287,6 +314,21 @@ void ConfigModel::appendCategory(const QString &iconName, const QString &name, d->appendCategory(cat); } +void ConfigModel::appendCategory(ConfigCategory *category) +{ + d->appendCategory(category); +} + +void ConfigModel::removeCategory(ConfigCategory *category) +{ + d->removeCategory(category); +} + +void ConfigModel::removeCategoryAt(int index) +{ + d->removeCategoryAt(index); +} + void ConfigModel::clear() { d->clear(); diff --git a/src/plasmaquick/configmodel.h b/src/plasmaquick/configmodel.h index 318f2fdca..ba2bb44db 100644 --- a/src/plasmaquick/configmodel.h +++ b/src/plasmaquick/configmodel.h @@ -85,6 +85,11 @@ public: Q_INVOKABLE void appendCategory(const QString &iconName, const QString &name, const QString &path, const QString &pluginName, bool visible); + // QML Engine isn't particulary smart resolving namespaces, hence fully qualified signature + Q_INVOKABLE void appendCategory(PlasmaQuick::ConfigCategory *category); + + Q_INVOKABLE void removeCategory(PlasmaQuick::ConfigCategory *category); + Q_INVOKABLE void removeCategoryAt(int index); /** * clears the model