[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
This commit is contained in:
Kai Uwe Broulik 2017-08-31 14:29:05 +02:00
parent d574dec0f8
commit 11e416bea6
2 changed files with 47 additions and 0 deletions

View File

@ -57,6 +57,8 @@ public:
QHash<QString, KQuickAddons::ConfigModule *> kcms; QHash<QString, KQuickAddons::ConfigModule *> kcms;
void appendCategory(ConfigCategory *c); void appendCategory(ConfigCategory *c);
void removeCategory(ConfigCategory *c);
void removeCategoryAt(int index);
void clear(); void clear();
QVariant get(int row) const; QVariant get(int row) const;
@ -154,6 +156,31 @@ void ConfigModelPrivate::appendCategory(ConfigCategory *c)
emit q->countChanged(); 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 QVariant ConfigModelPrivate::get(int row) const
{ {
QVariantMap value; QVariantMap value;
@ -287,6 +314,21 @@ void ConfigModel::appendCategory(const QString &iconName, const QString &name,
d->appendCategory(cat); 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() void ConfigModel::clear()
{ {
d->clear(); d->clear();

View File

@ -85,6 +85,11 @@ public:
Q_INVOKABLE void appendCategory(const QString &iconName, const QString &name, Q_INVOKABLE void appendCategory(const QString &iconName, const QString &name,
const QString &path, const QString &pluginName, bool visible); 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 * clears the model