a simple model to parse config categories

ConfigModel will be used in the plasmoid config qml that defines a model for the categories of the config dialog
This commit is contained in:
Marco Martin 2013-02-27 17:39:16 +01:00
parent 2168bdec75
commit 872839ecf9
5 changed files with 335 additions and 1 deletions

View File

@ -29,10 +29,184 @@
#include <Plasma/Corona>
///////////////////////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();
}
//////////////////////////////ConfigModel
ConfigModel::ConfigModel(QObject *parent)
: QAbstractListModel(parent)
{
QHash<int, QByteArray> roleNames;
roleNames[NameRole] = "name";
roleNames[IconRole] = "icon";
roleNames[SourceRole] = "source";
setRoleNames(roleNames);
}
ConfigModel::~ConfigModel()
{}
int ConfigModel::rowCount(const QModelIndex &index) const
{
if (index.column() > 0) {
return 0;
}
return m_categories.count();
}
QVariant ConfigModel::data(const QModelIndex& index, int role) const
{
if (index.row() < 0 || index.row() >= m_categories.count()) {
return QVariant();
}
switch (role) {
case NameRole:
return m_categories.at(index.row())->name();
case IconRole:
return m_categories.at(index.row())->icon();
case SourceRole:
return m_categories.at(index.row())->source();
default:
return QVariant();
}
}
void ConfigModel::appendCategory(ConfigCategory *c)
{
beginInsertRows(QModelIndex(), m_categories.size(), m_categories.size());
m_categories.append(c);
endInsertRows();
}
void ConfigModel::clear()
{
beginResetModel();
while (!m_categories.isEmpty()) {
m_categories.first()->setParent(0);
m_categories.pop_front();
}
endResetModel();
}
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)
{
ConfigModel *model = qobject_cast<ConfigModel *>(prop->object);
if (!model || index >= model->m_categories.count() || index < 0)
return 0;
else
return model->m_categories.at(index);
}
void ConfigModel::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 ConfigModel::categories_count(QQmlListProperty<ConfigCategory> *prop)
{
ConfigModel *model = qobject_cast<ConfigModel *>(prop->object);
if (model) {
return model->m_categories.count();
} else {
return 0;
}
}
void ConfigModel::categories_clear(QQmlListProperty<ConfigCategory> *prop)
{
ConfigModel *model = qobject_cast<ConfigModel *>(prop->object);
if (!model) {
return;
}
model->clear();
}
//////////////////////////////ConfigView
ConfigView::ConfigView(AppletInterface *interface, QWindow *parent)
: QQuickView(parent),
m_appletInterface(interface)
{
qmlRegisterType<ConfigModel>("org.kde.plasma.configuration", 0, 1, "ConfigModel");
qmlRegisterType<ConfigCategory>("org.kde.plasma.configuration", 0, 1, "ConfigCategory");
//FIXME: problem on nvidia, all windows should be transparent or won't show
setColor(Qt::transparent);
setTitle(i18n("%1 Settings", m_appletInterface->applet()->title()));
@ -78,6 +252,9 @@ void ConfigView::hideEvent(QHideEvent *ev)
void ConfigView::resizeEvent(QResizeEvent *re)
{
if (!rootObject()) {
return;
}
rootObject()->setWidth(re->size().width());
rootObject()->setHeight(re->size().height());
QQuickWindow::resizeEvent(re);

View File

@ -24,9 +24,73 @@
#include <QQuickView>
#include <QJSValue>
#include <QQmlListProperty>
#include <QStandardItemModel>
class AppletInterface;
class ConfigCategory : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString icon READ icon WRITE setIcon NOTIFY iconChanged)
Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)
public:
ConfigCategory(QObject *parent = 0);
~ConfigCategory();
QString name() const;
void setName(const QString &name);
QString icon() const;
void setIcon(const QString &icon);
QString source() const;
void setSource(const QString &source);
Q_SIGNALS:
void nameChanged();
void iconChanged();
void sourceChanged();
private:
QString m_name;
QString m_icon;
QString m_source;
};
class ConfigModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<ConfigCategory> categories READ categories CONSTANT)
Q_CLASSINFO("DefaultProperty", "categories")
public:
enum Roles {
NameRole = Qt::UserRole+1,
IconRole,
SourceRole
};
ConfigModel(QObject *parent = 0);
~ConfigModel();
void appendCategory(ConfigCategory *c);
void clear();
virtual int rowCount(const QModelIndex &index) const;
virtual QVariant data(const QModelIndex&, int) const;
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);
private:
QList<ConfigCategory*>m_categories;
};
class ConfigView : public QQuickView
{
Q_OBJECT

View File

@ -21,6 +21,7 @@ import QtQuick 2.0
import org.kde.plasma.core 0.1 as PlasmaCore
import org.kde.plasma.components 0.1 as PlasmaComponents
QtObject {
property list<QtObject> modules: [
@ -36,7 +37,17 @@ QtObject {
implicitHeight: pageColumn.implicitHeight
property alias cfg_Test: testConfigField.text
/*ListView {
model: ConfigModel {
ConfigCategory {
//property int title: Math.max(3, 9)
title: "AAAAA"
icon: "plasma"
source: "kkkk"
}
}
delegate: Text {text: title}
}*/
Column {
id: pageColumn
anchors.fill: parent

View File

@ -0,0 +1,80 @@
/*
* 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 General Public License as published by
* the Free Software Foundation; either version 2 of the License, 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 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.
*/
import QtQuick 2.0
import org.kde.plasma.components 0.1 as PlasmaComponents
import org.kde.plasma.extras 0.1 as PlasmaExtras
import org.kde.plasma.core 0.1 as PlasmaCore
MouseArea {
id: delegate
anchors {
left: parent.left
right: parent.right
}
//BEGIN properties
width: childrenRect.width
height: childrenRect.height
property bool current: main.sourceComponent == dataSource[modelData].component
//END properties
//BEGIN model
property list<QtObject> dataSource
//END model
//BEGIN connections
onClicked: {
if (delegate.current) {
return
} else {
main.sourceComponent = dataSource[modelData].component
root.restoreConfig()
}
}
onCurrentChanged: {
if (current) {
categoriesView.currentItem = delegate
}
}
//END connections
//BEGIN UI components
Column {
anchors {
left: parent.left
right: parent.right
}
PlasmaCore.IconItem {
anchors.horizontalCenter: parent.horizontalCenter
width: theme.IconSizeHuge
height: width
source: dataSource[modelData].icon
}
PlasmaComponents.Label {
anchors {
left: parent.left
right: parent.right
}
text: dataSource[modelData].name
wrapMode: Text.Wrap
horizontalAlignment: Text.AlignHCenter
}
}
//END UI components
}

View File

@ -20,6 +20,8 @@ import QtQuick 2.0
import org.kde.plasma.components 0.1 as PlasmaComponents
import org.kde.plasma.extras 0.1 as PlasmaExtras
import org.kde.plasma.core 0.1 as PlasmaCore
import org.kde.plasma.configuration 0.1
//TODO: all of this will be done with desktop components
Rectangle {