configmodel in own file

This commit is contained in:
Marco Martin 2013-09-11 14:40:11 +02:00
parent 52e96b41df
commit 6bf3f54783
7 changed files with 449 additions and 370 deletions

View File

@ -2,6 +2,7 @@ project(PlasmaQuick)
set(plasmaquick_LIB_SRC
plasmaquickview.cpp
configmodel.cpp
configview.cpp
containmentconfigview_p.cpp
currentcontainmentactionsmodel_p.cpp
@ -39,6 +40,7 @@ set(plasmaquick_LIB_INCLUDES
${CMAKE_CURRENT_BINARY_DIR}/plasmaquick_export.h
plasmaquickview.h
configview.h
configmodel.h
)
install(FILES ${plasmaquick_LIB_INCLUDES}

View File

@ -0,0 +1,334 @@
/*
* Copyright 2013 Marco Martin <mart@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* 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 Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "configview_p.h"
#include "configview.h"
#include "configmodel.h"
#include "Plasma/Applet"
#include "Plasma/Containment"
//#include "plasmoid/wallpaperinterface.h"
#include "kdeclarative/configpropertymap.h"
#include <QDebug>
#include <QDir>
#include <QQmlComponent>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQuickItem>
#include <KGlobal>
#include <KLocalizedString>
#include <kdeclarative/kdeclarative.h>
#include <Plasma/Corona>
#include <Plasma/PluginLoader>
///////////////////////ConfigCategory
ConfigCategory::ConfigCategory(QObject *parent)
: QObject(parent)
{
}
ConfigCategory::~ConfigCategory()
{}
QString ConfigCategory::name() const
{
return m_name;
}
void ConfigCategory::setName(const QString &name)
{
if (m_name == name) {
return;
}
m_name = name;
emit nameChanged();
}
QString ConfigCategory::icon() const
{
return m_icon;
}
void ConfigCategory::setIcon(const QString &icon)
{
if (m_icon == icon) {
return;
}
m_icon = icon;
emit iconChanged();
}
QString ConfigCategory::source() const
{
return m_source;
}
void ConfigCategory::setSource(const QString &source)
{
if (m_source == source) {
return;
}
m_source = source;
emit sourceChanged();
}
QString ConfigCategory::pluginName() const
{
return m_pluginName;
}
void ConfigCategory::setPluginName(const QString &name)
{
if (m_pluginName == name) {
return;
}
m_pluginName = name;
emit pluginNameChanged();
}
//////////////////////////////ConfigModel
class ConfigModelPrivate
{
public:
ConfigModelPrivate(ConfigModel *model);
~ConfigModelPrivate();
ConfigModel *q;
QList<ConfigCategory*> categories;
QWeakPointer<Plasma::Applet> appletInterface;
void appendCategory(ConfigCategory *c);
void clear();
QVariant get(int row) const;
static ConfigCategory *categories_at(QQmlListProperty<ConfigCategory> *prop, int index);
static void categories_append(QQmlListProperty<ConfigCategory> *prop, ConfigCategory *o);
static int categories_count(QQmlListProperty<ConfigCategory> *prop);
static void categories_clear(QQmlListProperty<ConfigCategory> *prop);
};
ConfigModelPrivate::ConfigModelPrivate(ConfigModel *model)
: q(model)
{
}
ConfigModelPrivate::~ConfigModelPrivate()
{
}
ConfigCategory *ConfigModelPrivate::categories_at(QQmlListProperty<ConfigCategory> *prop, int index)
{
ConfigModel *model = qobject_cast<ConfigModel *>(prop->object);
if (!model || index >= model->d->categories.count() || index < 0) {
return 0;
} else {
return model->d->categories.at(index);
}
}
void ConfigModelPrivate::categories_append(QQmlListProperty<ConfigCategory> *prop, ConfigCategory *o)
{
ConfigModel *model = qobject_cast<ConfigModel *>(prop->object);
if (!o || !model) {
return;
}
if (o->parent() == prop->object) {
o->setParent(0);
}
o->setParent(prop->object);
model->appendCategory(o);
}
int ConfigModelPrivate::categories_count(QQmlListProperty<ConfigCategory> *prop)
{
ConfigModel *model = qobject_cast<ConfigModel *>(prop->object);
if (model) {
return model->d->categories.count();
} else {
return 0;
}
}
void ConfigModelPrivate::categories_clear(QQmlListProperty<ConfigCategory> *prop)
{
ConfigModel *model = qobject_cast<ConfigModel *>(prop->object);
if (!model) {
return;
}
model->clear();
}
void ConfigModelPrivate::clear()
{
q->beginResetModel();
while (!categories.isEmpty()) {
categories.first()->setParent(0);
categories.pop_front();
}
q->endResetModel();
emit q->countChanged();
}
void ConfigModelPrivate::appendCategory(ConfigCategory *c)
{
q->beginInsertRows(QModelIndex(), categories.size(), categories.size());
categories.append(c);
q->endInsertRows();
emit q->countChanged();
}
QVariant ConfigModelPrivate::get(int row) const
{
QVariantMap value;
if (row < 0 || row >= categories.count()) {
return value;
}
value["name"] = categories.at(row)->name();
value["icon"] = categories.at(row)->icon();
value["pluginName"] = categories.at(row)->pluginName();
if (appletInterface) {
value["source"] = QUrl::fromLocalFile(appletInterface.data()->package().filePath("ui", categories.at(row)->source()));
} else {
value["source"] = categories.at(row)->source();
}
return value;
}
ConfigModel::ConfigModel(QObject *parent)
: QAbstractListModel(parent),
d(new ConfigModelPrivate(this))
{
QHash<int, QByteArray> roleNames;
roleNames[NameRole] = "name";
roleNames[IconRole] = "icon";
roleNames[SourceRole] = "source";
roleNames[PluginNameRole] = "pluginName";
setRoleNames(roleNames);
}
ConfigModel::~ConfigModel()
{
delete d;
}
int ConfigModel::rowCount(const QModelIndex &index) const
{
if (index.column() > 0) {
return 0;
}
return d->categories.count();
}
QVariant ConfigModel::data(const QModelIndex& index, int role) const
{
if (index.row() < 0 || index.row() >= d->categories.count()) {
return QVariant();
}
switch (role) {
case NameRole:
return d->categories.at(index.row())->name();
case IconRole:
return d->categories.at(index.row())->icon();
case SourceRole:
if (d->appletInterface) {
return QUrl::fromLocalFile(d->appletInterface.data()->package().filePath("ui", d->categories.at(index.row())->source()));
} else {
return d->categories.at(index.row())->source();
}
case PluginNameRole:
return d->categories.at(index.row())->pluginName();
default:
return QVariant();
}
}
QVariant ConfigModel::get(int row) const
{
return d->get(row);
}
void ConfigModel::appendCategory(ConfigCategory *c)
{
d->appendCategory(c);
}
void ConfigModel::clear()
{
d->clear();
}
void ConfigModel::setApplet(Plasma::Applet *interface)
{
d->appletInterface = interface;
}
Plasma::Applet *ConfigModel::applet() const
{
return d->appletInterface.data();
}
QQmlListProperty<ConfigCategory> ConfigModel::categories()
{
return QQmlListProperty<ConfigCategory>(this, 0, ConfigModel::categories_append,
ConfigModel::categories_count,
ConfigModel::categories_at,
ConfigModel::categories_clear);
}
ConfigCategory *ConfigModel::categories_at(QQmlListProperty<ConfigCategory> *prop, int index)
{
return ConfigModelPrivate::categories_at(prop, index);
}
void ConfigModel::categories_append(QQmlListProperty<ConfigCategory> *prop, ConfigCategory *o)
{
ConfigModelPrivate::categories_append(prop, o);
}
int ConfigModel::categories_count(QQmlListProperty<ConfigCategory> *prop)
{
return ConfigModelPrivate::categories_count(prop);
}
void ConfigModel::categories_clear(QQmlListProperty<ConfigCategory> *prop)
{
ConfigModelPrivate::categories_clear(prop);
}
#include "moc_configmodel.cpp"

View File

@ -0,0 +1,102 @@
/*
* Copyright 2013 Marco Martin <mart@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* 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 Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef CONFIGMODEL_H
#define CONFIGMODEL_H
#include <QQmlListProperty>
#include <QAbstractListModel>
#include <plasmaquick/plasmaquick_export.h>
namespace Plasma {
class Applet;
}
class ConfigPropertyMap;
class ConfigCategoryPrivate;
class ConfigModelPrivate;
class ConfigCategory;
class PLASMAQUICK_EXPORT ConfigModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<ConfigCategory> categories READ categories CONSTANT)
Q_CLASSINFO("DefaultProperty", "categories")
Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
enum Roles {
NameRole = Qt::UserRole+1,
IconRole,
SourceRole,
PluginNameRole
};
ConfigModel(QObject *parent = 0);
~ConfigModel();
/**
* add a new category in the model
* @param ConfigCategory the new category
**/
void appendCategory(ConfigCategory *c);
/**
* clears the model
**/
void clear();
void setApplet(Plasma::Applet *interface);
Plasma::Applet *applet() const;
int count() {return rowCount();}
virtual int rowCount(const QModelIndex &index = QModelIndex()) const;
virtual QVariant data(const QModelIndex&, int) const;
/**
* @param row the row for which the data will be returned
* @raturn the data of the specified row
**/
Q_INVOKABLE QVariant get(int row) const;
/**
* @return the categories of the model
**/
QQmlListProperty<ConfigCategory> categories();
static ConfigCategory *categories_at(QQmlListProperty<ConfigCategory> *prop, int index);
static void categories_append(QQmlListProperty<ConfigCategory> *prop, ConfigCategory *o);
static int categories_count(QQmlListProperty<ConfigCategory> *prop);
static void categories_clear(QQmlListProperty<ConfigCategory> *prop);
Q_SIGNALS:
/**
* emitted when the count is changed
**/
void countChanged();
private:
friend class ConfigModelPrivate;
ConfigModelPrivate *const d;
};
#endif // multiple inclusion guard

View File

@ -19,6 +19,7 @@
#include "configview_p.h"
#include "configview.h"
#include "configmodel.h"
#include "Plasma/Applet"
#include "Plasma/Containment"
//#include "plasmoid/wallpaperinterface.h"
@ -38,297 +39,6 @@
#include <Plasma/Corona>
#include <Plasma/PluginLoader>
///////////////////////ConfigCategory
ConfigCategory::ConfigCategory(QObject *parent)
: QObject(parent)
{
}
ConfigCategory::~ConfigCategory()
{}
QString ConfigCategory::name() const
{
return m_name;
}
void ConfigCategory::setName(const QString &name)
{
if (m_name == name) {
return;
}
m_name = name;
emit nameChanged();
}
QString ConfigCategory::icon() const
{
return m_icon;
}
void ConfigCategory::setIcon(const QString &icon)
{
if (m_icon == icon) {
return;
}
m_icon = icon;
emit iconChanged();
}
QString ConfigCategory::source() const
{
return m_source;
}
void ConfigCategory::setSource(const QString &source)
{
if (m_source == source) {
return;
}
m_source = source;
emit sourceChanged();
}
QString ConfigCategory::pluginName() const
{
return m_pluginName;
}
void ConfigCategory::setPluginName(const QString &name)
{
if (m_pluginName == name) {
return;
}
m_pluginName = name;
emit pluginNameChanged();
}
//////////////////////////////ConfigModel
class ConfigModelPrivate
{
public:
ConfigModelPrivate(ConfigModel *model);
~ConfigModelPrivate();
ConfigModel *q;
QList<ConfigCategory*> categories;
QWeakPointer<Plasma::Applet> appletInterface;
void appendCategory(ConfigCategory *c);
void clear();
QVariant get(int row) const;
static ConfigCategory *categories_at(QQmlListProperty<ConfigCategory> *prop, int index);
static void categories_append(QQmlListProperty<ConfigCategory> *prop, ConfigCategory *o);
static int categories_count(QQmlListProperty<ConfigCategory> *prop);
static void categories_clear(QQmlListProperty<ConfigCategory> *prop);
};
ConfigModelPrivate::ConfigModelPrivate(ConfigModel *model)
: q(model)
{
}
ConfigModelPrivate::~ConfigModelPrivate()
{
}
ConfigCategory *ConfigModelPrivate::categories_at(QQmlListProperty<ConfigCategory> *prop, int index)
{
ConfigModel *model = qobject_cast<ConfigModel *>(prop->object);
if (!model || index >= model->d->categories.count() || index < 0) {
return 0;
} else {
return model->d->categories.at(index);
}
}
void ConfigModelPrivate::categories_append(QQmlListProperty<ConfigCategory> *prop, ConfigCategory *o)
{
ConfigModel *model = qobject_cast<ConfigModel *>(prop->object);
if (!o || !model) {
return;
}
if (o->parent() == prop->object) {
o->setParent(0);
}
o->setParent(prop->object);
model->appendCategory(o);
}
int ConfigModelPrivate::categories_count(QQmlListProperty<ConfigCategory> *prop)
{
ConfigModel *model = qobject_cast<ConfigModel *>(prop->object);
if (model) {
return model->d->categories.count();
} else {
return 0;
}
}
void ConfigModelPrivate::categories_clear(QQmlListProperty<ConfigCategory> *prop)
{
ConfigModel *model = qobject_cast<ConfigModel *>(prop->object);
if (!model) {
return;
}
model->clear();
}
void ConfigModelPrivate::clear()
{
q->beginResetModel();
while (!categories.isEmpty()) {
categories.first()->setParent(0);
categories.pop_front();
}
q->endResetModel();
emit q->countChanged();
}
void ConfigModelPrivate::appendCategory(ConfigCategory *c)
{
q->beginInsertRows(QModelIndex(), categories.size(), categories.size());
categories.append(c);
q->endInsertRows();
emit q->countChanged();
}
QVariant ConfigModelPrivate::get(int row) const
{
QVariantMap value;
if (row < 0 || row >= categories.count()) {
return value;
}
value["name"] = categories.at(row)->name();
value["icon"] = categories.at(row)->icon();
value["pluginName"] = categories.at(row)->pluginName();
if (appletInterface) {
value["source"] = QUrl::fromLocalFile(appletInterface.data()->package().filePath("ui", categories.at(row)->source()));
} else {
value["source"] = categories.at(row)->source();
}
return value;
}
ConfigModel::ConfigModel(QObject *parent)
: QAbstractListModel(parent),
d(new ConfigModelPrivate(this))
{
QHash<int, QByteArray> roleNames;
roleNames[NameRole] = "name";
roleNames[IconRole] = "icon";
roleNames[SourceRole] = "source";
roleNames[PluginNameRole] = "pluginName";
setRoleNames(roleNames);
}
ConfigModel::~ConfigModel()
{
delete d;
}
int ConfigModel::rowCount(const QModelIndex &index) const
{
if (index.column() > 0) {
return 0;
}
return d->categories.count();
}
QVariant ConfigModel::data(const QModelIndex& index, int role) const
{
if (index.row() < 0 || index.row() >= d->categories.count()) {
return QVariant();
}
switch (role) {
case NameRole:
return d->categories.at(index.row())->name();
case IconRole:
return d->categories.at(index.row())->icon();
case SourceRole:
if (d->appletInterface) {
return QUrl::fromLocalFile(d->appletInterface.data()->package().filePath("ui", d->categories.at(index.row())->source()));
} else {
return d->categories.at(index.row())->source();
}
case PluginNameRole:
return d->categories.at(index.row())->pluginName();
default:
return QVariant();
}
}
QVariant ConfigModel::get(int row) const
{
return d->get(row);
}
void ConfigModel::appendCategory(ConfigCategory *c)
{
d->appendCategory(c);
}
void ConfigModel::clear()
{
d->clear();
}
void ConfigModel::setApplet(Plasma::Applet *interface)
{
d->appletInterface = interface;
}
Plasma::Applet *ConfigModel::applet() const
{
return d->appletInterface.data();
}
QQmlListProperty<ConfigCategory> ConfigModel::categories()
{
return QQmlListProperty<ConfigCategory>(this, 0, ConfigModel::categories_append,
ConfigModel::categories_count,
ConfigModel::categories_at,
ConfigModel::categories_clear);
}
ConfigCategory *ConfigModel::categories_at(QQmlListProperty<ConfigCategory> *prop, int index)
{
return ConfigModelPrivate::categories_at(prop, index);
}
void ConfigModel::categories_append(QQmlListProperty<ConfigCategory> *prop, ConfigCategory *o)
{
ConfigModelPrivate::categories_append(prop, o);
}
int ConfigModel::categories_count(QQmlListProperty<ConfigCategory> *prop)
{
return ConfigModelPrivate::categories_count(prop);
}
void ConfigModel::categories_clear(QQmlListProperty<ConfigCategory> *prop)
{
ConfigModelPrivate::categories_clear(prop);
}
//////////////////////////////ConfigView

View File

@ -17,14 +17,11 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef CONFIGUILOADER_H
#define CONFIGUILOADER_H
#ifndef CONFIGVIEW_H
#define CONFIGVIEW_H
#include <QQuickView>
#include <QJSValue>
#include <QQmlListProperty>
#include <QStandardItemModel>
#include <plasmaquick/plasmaquick_export.h>
@ -32,78 +29,10 @@ namespace Plasma {
class Applet;
}
class ConfigPropertyMap;
class ConfigCategoryPrivate;
class ConfigModelPrivate;
class ConfigCategory;
class PLASMAQUICK_EXPORT ConfigModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<ConfigCategory> categories READ categories CONSTANT)
Q_CLASSINFO("DefaultProperty", "categories")
Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
enum Roles {
NameRole = Qt::UserRole+1,
IconRole,
SourceRole,
PluginNameRole
};
ConfigModel(QObject *parent = 0);
~ConfigModel();
/**
* add a new category in the model
* @param ConfigCategory the new category
**/
void appendCategory(ConfigCategory *c);
/**
* clears the model
**/
void clear();
void setApplet(Plasma::Applet *interface);
Plasma::Applet *applet() const;
int count() {return rowCount();}
virtual int rowCount(const QModelIndex &index = QModelIndex()) const;
virtual QVariant data(const QModelIndex&, int) const;
/**
* @param row the row for which the data will be returned
* @raturn the data of the specified row
**/
Q_INVOKABLE QVariant get(int row) const;
/**
* @return the categories of the model
**/
QQmlListProperty<ConfigCategory> categories();
static ConfigCategory *categories_at(QQmlListProperty<ConfigCategory> *prop, int index);
static void categories_append(QQmlListProperty<ConfigCategory> *prop, ConfigCategory *o);
static int categories_count(QQmlListProperty<ConfigCategory> *prop);
static void categories_clear(QQmlListProperty<ConfigCategory> *prop);
Q_SIGNALS:
/**
* emitted when the count is changed
**/
void countChanged();
private:
friend class ConfigModelPrivate;
ConfigModelPrivate *const d;
};
class ConfigViewPrivate;
class ConfigModel;
class PLASMAQUICK_EXPORT ConfigView : public QQuickView
{
Q_OBJECT

View File

@ -20,6 +20,7 @@
#include "currentcontainmentactionsmodel_p.h"
#include "containmentconfigview_p.h"
#include "configview_p.h"
#include "configmodel.h"
#include <kdeclarative/configpropertymap.h>
@ -45,7 +46,7 @@ ContainmentConfigView::ContainmentConfigView(Plasma::Containment *cont, QWindow
m_currentWallpaperConfig(0),
m_ownWallpaperConfig(0)
{
qmlRegisterType<QStandardItemModel>();
qmlRegisterType<QAbstractItemModel>();
engine()->rootContext()->setContextProperty("configDialog", this);
setCurrentWallpaper(cont->containment()->wallpaper());
@ -90,7 +91,7 @@ ConfigModel *ContainmentConfigView::containmentActionConfigModel()
return m_containmentActionConfigModel;
}
QStandardItemModel *ContainmentConfigView::currentContainmentActionsModel()
QAbstractItemModel *ContainmentConfigView::currentContainmentActionsModel()
{
if (!m_currentContainmentActionsModel) {
m_currentContainmentActionsModel = new CurrentContainmentActionsModel(m_containment, this);

View File

@ -28,6 +28,7 @@ namespace Plasma {
class Containment;
}
class QAbstractItemModel;
class ConfigPropertyMap;
class CurrentContainmentActionsModel;
@ -36,7 +37,7 @@ class ContainmentConfigView : public ConfigView
{
Q_OBJECT
Q_PROPERTY(ConfigModel *containmentActionConfigModel READ containmentActionConfigModel CONSTANT)
Q_PROPERTY(QStandardItemModel *currentContainmentActionsModel READ currentContainmentActionsModel CONSTANT)
Q_PROPERTY(QAbstractItemModel *currentContainmentActionsModel READ currentContainmentActionsModel CONSTANT)
Q_PROPERTY(ConfigModel *wallpaperConfigModel READ wallpaperConfigModel CONSTANT)
Q_PROPERTY(ConfigPropertyMap *wallpaperConfiguration READ wallpaperConfiguration NOTIFY wallpaperConfigurationChanged)
Q_PROPERTY(QString currentWallpaper READ currentWallpaper WRITE setCurrentWallpaper NOTIFY currentWallpaperChanged)
@ -48,7 +49,7 @@ public:
virtual void init();
ConfigModel *containmentActionConfigModel();
QStandardItemModel *currentContainmentActionsModel();
QAbstractItemModel *currentContainmentActionsModel();
ConfigModel *wallpaperConfigModel();
QString currentWallpaper() const;
void setCurrentWallpaper(const QString &wallpaper);