diff --git a/src/plasmaview/configview.cpp b/src/plasmaview/configview.cpp index fea5a7350..075ac04f7 100644 --- a/src/plasmaview/configview.cpp +++ b/src/plasmaview/configview.cpp @@ -39,8 +39,28 @@ ///////////////////////ConfigCategory +class ConfigCategoryPrivate +{ +public: + ConfigCategoryPrivate(); + ~ConfigCategoryPrivate(); + QString name; + QString icon; + QString source; + QString pluginName; +}; + +ConfigCategoryPrivate::ConfigCategoryPrivate() +{ +} + +ConfigCategoryPrivate::~ConfigCategoryPrivate() +{ +} + ConfigCategory::ConfigCategory(QObject *parent) - : QObject(parent) + : QObject(parent), + d(new ConfigCategoryPrivate()) { } @@ -49,182 +69,110 @@ ConfigCategory::~ConfigCategory() QString ConfigCategory::name() const { - return m_name; + return d->name; } void ConfigCategory::setName(const QString &name) { - if (m_name == name) { + if (d->name == name) { return; } - m_name = name; + d->name = name; emit nameChanged(); } QString ConfigCategory::icon() const { - return m_icon; + return d->icon; } void ConfigCategory::setIcon(const QString &icon) { - if (m_icon == icon) { + if (d->icon == icon) { return; } - m_icon = icon; + d->icon = icon; emit iconChanged(); } QString ConfigCategory::source() const { - return m_source; + return d->source; } void ConfigCategory::setSource(const QString &source) { - if (m_source == source) { + if (d->source == source) { return; } - m_source = source; + d->source = source; emit sourceChanged(); } QString ConfigCategory::pluginName() const { - return m_pluginName; + return d->pluginName; } void ConfigCategory::setPluginName(const QString &name) { - if (m_pluginName == name) { + if (d->pluginName == name) { return; } - m_pluginName = name; + d->pluginName = name; emit pluginNameChanged(); } //////////////////////////////ConfigModel -ConfigModel::ConfigModel(QObject *parent) - : QAbstractListModel(parent) -{ - QHash roleNames; - roleNames[NameRole] = "name"; - roleNames[IconRole] = "icon"; - roleNames[SourceRole] = "source"; - roleNames[PluginNameRole] = "pluginName"; - setRoleNames(roleNames); - qDebug() << " XXX loaded QALM"; +class ConfigModelPrivate +{ +public: + ConfigModelPrivate(ConfigModel *model); + ~ConfigModelPrivate(); + + ConfigModel *q; + QList categories; + QWeakPointer appletInterface; + + void appendCategory(ConfigCategory *c); + void clear(); + QVariant get(int row) const; + + static ConfigCategory *categories_at(QQmlListProperty *prop, int index); + static void categories_append(QQmlListProperty *prop, ConfigCategory *o); + static int categories_count(QQmlListProperty *prop); + static void categories_clear(QQmlListProperty *prop); +}; + +ConfigModelPrivate::ConfigModelPrivate(ConfigModel *model) + : q(model) +{ } -ConfigModel::~ConfigModel() -{} - -int ConfigModel::rowCount(const QModelIndex &index) const +ConfigModelPrivate::~ConfigModelPrivate() { - 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: - if (m_appletInterface) { - return QUrl::fromLocalFile(m_appletInterface.data()->package().filePath("ui", m_categories.at(index.row())->source())); - } else { - return m_categories.at(index.row())->source(); - } - case PluginNameRole: - return m_categories.at(index.row())->pluginName(); - default: - return QVariant(); - } -} - -QVariant ConfigModel::get(int row) const -{ - QVariantMap value; - if (row < 0 || row >= m_categories.count()) { - return value; - } - - value["name"] = m_categories.at(row)->name(); - value["icon"] = m_categories.at(row)->icon(); - value["pluginName"] = m_categories.at(row)->pluginName(); - if (m_appletInterface) { - value["source"] = QUrl::fromLocalFile(m_appletInterface.data()->package().filePath("ui", m_categories.at(row)->source())); - } else { - value["source"] = m_categories.at(row)->source(); - } - return value; -} - -void ConfigModel::appendCategory(ConfigCategory *c) -{ - beginInsertRows(QModelIndex(), m_categories.size(), m_categories.size()); - m_categories.append(c); - endInsertRows(); - emit countChanged(); -} - -void ConfigModel::clear() -{ - beginResetModel(); - while (!m_categories.isEmpty()) { - m_categories.first()->setParent(0); - m_categories.pop_front(); - } - endResetModel(); - emit countChanged(); -} - -void ConfigModel::setApplet(Plasma::Applet *interface) -{ - m_appletInterface = interface; -} - -Plasma::Applet *ConfigModel::applet() const -{ - return m_appletInterface.data(); -} - -QQmlListProperty ConfigModel::categories() -{ - return QQmlListProperty(this, 0, ConfigModel::categories_append, - ConfigModel::categories_count, - ConfigModel::categories_at, - ConfigModel::categories_clear); - -} - -ConfigCategory *ConfigModel::categories_at(QQmlListProperty *prop, int index) +ConfigCategory *ConfigModelPrivate::categories_at(QQmlListProperty *prop, int index) { ConfigModel *model = qobject_cast(prop->object); - if (!model || index >= model->m_categories.count() || index < 0) + if (!model || index >= model->d->categories.count() || index < 0) { return 0; - else - return model->m_categories.at(index); + } else { + return model->d->categories.at(index); + } } -void ConfigModel::categories_append(QQmlListProperty *prop, ConfigCategory *o) +void ConfigModelPrivate::categories_append(QQmlListProperty *prop, ConfigCategory *o) { ConfigModel *model = qobject_cast(prop->object); if (!o || !model) { @@ -239,17 +187,17 @@ void ConfigModel::categories_append(QQmlListProperty *prop, Conf model->appendCategory(o); } -int ConfigModel::categories_count(QQmlListProperty *prop) +int ConfigModelPrivate::categories_count(QQmlListProperty *prop) { ConfigModel *model = qobject_cast(prop->object); if (model) { - return model->m_categories.count(); + return model->d->categories.count(); } else { return 0; } } -void ConfigModel::categories_clear(QQmlListProperty *prop) +void ConfigModelPrivate::categories_clear(QQmlListProperty *prop) { ConfigModel *model = qobject_cast(prop->object); if (!model) { @@ -259,66 +207,234 @@ void ConfigModel::categories_clear(QQmlListProperty *prop) 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 roleNames; + roleNames[NameRole] = "name"; + roleNames[IconRole] = "icon"; + roleNames[SourceRole] = "source"; + roleNames[PluginNameRole] = "pluginName"; + + setRoleNames(roleNames); + qDebug() << " XXX loaded QALM"; +} + +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 ConfigModel::categories() +{ + return QQmlListProperty(this, 0, ConfigModel::categories_append, + ConfigModel::categories_count, + ConfigModel::categories_at, + ConfigModel::categories_clear); + +} + +ConfigCategory *ConfigModel::categories_at(QQmlListProperty *prop, int index) +{ + return ConfigModelPrivate::categories_at(prop, index); +} + +void ConfigModel::categories_append(QQmlListProperty *prop, ConfigCategory *o) +{ + ConfigModelPrivate::categories_append(prop, o); +} + +int ConfigModel::categories_count(QQmlListProperty *prop) +{ + return ConfigModelPrivate::categories_count(prop); +} + +void ConfigModel::categories_clear(QQmlListProperty *prop) +{ + ConfigModelPrivate::categories_clear(prop); +} + //////////////////////////////ConfigView -ConfigView::ConfigView(Plasma::Applet *applet, QWindow *parent) - : QQuickView(parent), - m_applet(applet) + +class ConfigViewPrivate +{ +public: + ConfigViewPrivate(Plasma::Applet *appl, ConfigView *view); + ~ConfigViewPrivate(); + + void init(); + ConfigView *q; + Plasma::Applet *applet; + ConfigModel *configModel; +}; + +ConfigViewPrivate::ConfigViewPrivate(Plasma::Applet *appl, ConfigView *view) + : q(view), + applet(appl) +{ +} + +void ConfigViewPrivate::init() { applet->setUserConfiguring(true); + KDeclarative kdeclarative; - kdeclarative.setDeclarativeEngine(engine()); + kdeclarative.setDeclarativeEngine(q->engine()); kdeclarative.setupBindings(); qmlRegisterType("org.kde.plasma.configuration", 2, 0, "ConfigModel"); qmlRegisterType("org.kde.plasma.configuration", 2, 0, "ConfigCategory"); //FIXME: problem on nvidia, all windows should be transparent or won't show - setColor(Qt::transparent); - setTitle(i18n("%1 Settings", m_applet->title())); + q->setColor(Qt::transparent); + q->setTitle(i18n("%1 Settings", applet->title())); - if (!m_applet->containment()->corona()->package().isValid()) { + if (!applet->containment()->corona()->package().isValid()) { qWarning() << "Invalid home screen package"; } - setResizeMode(QQuickView::SizeViewToRootObject); + q->setResizeMode(QQuickView::SizeViewToRootObject); //config model local of the applet - QQmlComponent *component = new QQmlComponent(engine(), QUrl::fromLocalFile(m_applet->package().filePath("configmodel")), this); - QObject *object = component->beginCreate(engine()->rootContext()); - m_configModel = qobject_cast(object); - if (m_configModel) { - m_configModel->setApplet(m_applet); + QQmlComponent *component = new QQmlComponent(q->engine(), QUrl::fromLocalFile(applet->package().filePath("configmodel")), q); + QObject *object = component->beginCreate(q->engine()->rootContext()); + configModel = qobject_cast(object); + if (configModel) { + configModel->setApplet(applet); } else { delete object; } - Plasma::Containment *cont = qobject_cast(m_applet); - - engine()->rootContext()->setContextProperty("plasmoid", applet->property("graphicObject").value()); - engine()->rootContext()->setContextProperty("configDialog", this); + q->engine()->rootContext()->setContextProperty("plasmoid", applet->property("graphicObject").value()); + q->engine()->rootContext()->setContextProperty("configDialog", q); component->completeCreate(); delete component; } + + +ConfigView::ConfigView(Plasma::Applet *applet, QWindow *parent) + : QQuickView(parent), + d(new ConfigViewPrivate(applet, this)) +{ + d->init(); + qmlRegisterType("org.kde.plasma.configuration", 2, 0, "ConfigModel"); + qmlRegisterType("org.kde.plasma.configuration", 2, 0, "ConfigCategory"); +} + ConfigView::~ConfigView() { - m_applet->setUserConfiguring(false); + d->applet->setUserConfiguring(false); } void ConfigView::init() { - setSource(QUrl::fromLocalFile(m_applet->containment()->corona()->package().filePath("appletconfigurationui"))); + setSource(QUrl::fromLocalFile(d->applet->containment()->corona()->package().filePath("appletconfigurationui"))); } ConfigModel *ConfigView::configModel() const { - return m_configModel; + return d->configModel; } - //To emulate Qt::WA_DeleteOnClose that QWindow doesn't have void ConfigView::hideEvent(QHideEvent *ev) { diff --git a/src/plasmaview/configview.h b/src/plasmaview/configview.h index 2e8f68ff8..7f3c1f5b7 100644 --- a/src/plasmaview/configview.h +++ b/src/plasmaview/configview.h @@ -26,14 +26,17 @@ #include #include +#include + namespace Plasma { class Applet; } class ConfigPropertyMap; +class ConfigCategoryPrivate; -class ConfigCategory : public QObject +class PLASMAVIEW_EXPORT ConfigCategory : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) @@ -64,13 +67,12 @@ Q_SIGNALS: void pluginNameChanged(); private: - QString m_name; - QString m_icon; - QString m_source; - QString m_pluginName; + ConfigCategoryPrivate *const d; }; -class ConfigModel : public QAbstractListModel +class ConfigModelPrivate; + +class PLASMAVIEW_EXPORT ConfigModel : public QAbstractListModel { Q_OBJECT Q_PROPERTY(QQmlListProperty categories READ categories CONSTANT) @@ -99,7 +101,7 @@ public: Q_INVOKABLE QVariant get(int row) const; QQmlListProperty categories(); - + static ConfigCategory *categories_at(QQmlListProperty *prop, int index); static void categories_append(QQmlListProperty *prop, ConfigCategory *o); static int categories_count(QQmlListProperty *prop); @@ -109,12 +111,16 @@ Q_SIGNALS: void countChanged(); private: - QListm_categories; - QWeakPointer m_appletInterface; + friend class ConfigModelPrivate; + ConfigModelPrivate *const d; }; -class ConfigView : public QQuickView +class ConfigViewPrivate; + +//TODO: the config view for the containment should be a subclass +//TODO: is it possible to move this in the shell? +class PLASMAVIEW_EXPORT ConfigView : public QQuickView { Q_OBJECT Q_PROPERTY(ConfigModel *configModel READ configModel CONSTANT) @@ -132,8 +138,7 @@ protected: void resizeEvent(QResizeEvent *re); private: - Plasma::Applet *m_applet; - ConfigModel *m_configModel; + ConfigViewPrivate *const d; }; #endif // multiple inclusion guard