From b9082fda48bb026fc4e6148efeba9aabf608373a Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Fri, 10 Aug 2012 14:02:07 +0200 Subject: NotificationProviders UI --- sowatchui/main.cpp | 2 + sowatchui/providersmodel.cc | 105 ++++++++++++++++++++++++++++++++++++++++++++ sowatchui/providersmodel.h | 44 +++++++++++++++++++ sowatchui/qml/WatchPage.qml | 29 ++++++------ sowatchui/sowatchui.pro | 6 ++- 5 files changed, 169 insertions(+), 17 deletions(-) create mode 100644 sowatchui/providersmodel.cc create mode 100644 sowatchui/providersmodel.h (limited to 'sowatchui') diff --git a/sowatchui/main.cpp b/sowatchui/main.cpp index af2a0c0..5e116ac 100644 --- a/sowatchui/main.cpp +++ b/sowatchui/main.cpp @@ -6,6 +6,7 @@ #include "watchesmodel.h" #include "watchscannermodel.h" +#include "providersmodel.h" static sowatch::Registry *registry; static WatchesModel *watches; @@ -24,6 +25,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[]) qmlRegisterType(); qmlRegisterType("com.javispedro.sowatch", 1, 0, "GConfKey"); + qmlRegisterType("com.javispedro.sowatch", 1, 0, "ProvidersModel"); viewer->rootContext()->setContextProperty("watches", watches); viewer->rootContext()->setContextProperty("watchScanner", watchScanner); diff --git a/sowatchui/providersmodel.cc b/sowatchui/providersmodel.cc new file mode 100644 index 0000000..68ce21b --- /dev/null +++ b/sowatchui/providersmodel.cc @@ -0,0 +1,105 @@ +#include + +#include "providersmodel.h" + +using namespace sowatch; + +ProvidersModel::ProvidersModel(QObject *parent) : + QAbstractListModel(parent), + _config(0) +{ + QHash roles = roleNames(); + roles[Qt::DisplayRole] = QByteArray("title"); + roles[NameRole] = QByteArray("name"); + roles[EnabledRole] = QByteArray("enabled"); + setRoleNames(roles); +} + +QString ProvidersModel::configKey() const +{ + if (_config) { + return _config->key(); + } else { + return QString(); + } +} + +void ProvidersModel::setConfigKey(const QString &configKey) +{ + QString oldConfigKey = this->configKey(); + if (_config) { + delete _config; + _config = 0; + } + if (!configKey.isEmpty()) { + _config = new GConfKey(configKey, this); + connect(_config, SIGNAL(changed()), SLOT(reload())); + } + if (this->configKey() != oldConfigKey) { + reload(); + emit configKeyChanged(); + } +} + +int ProvidersModel::rowCount(const QModelIndex &parent) const +{ + return _all_list.count(); +} + +QVariant ProvidersModel::data(const QModelIndex &index, int role) const +{ + switch (role) { + case Qt::DisplayRole: + return QVariant::fromValue(_info_list[index.row()].name); + case NameRole: + return QVariant::fromValue(_all_list[index.row()]); + case EnabledRole: + return QVariant::fromValue(_enabled.contains(_all_list[index.row()])); + } + return QVariant(); +} + +bool ProvidersModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + switch (role) { + case EnabledRole: + setProviderEnabled(_all_list[index.row()], value.toBool()); + return true; + } + return false; +} + +void ProvidersModel::setProviderEnabled(const QString &id, bool enabled) +{ + if (enabled) { + _enabled.insert(id); + } else { + _enabled.remove(id); + } + _config->set(QVariant::fromValue(QStringList(_enabled.toList()))); +} + +void ProvidersModel::reload() +{ + Registry *registry = Registry::registry(); + beginResetModel(); + _all_list.clear(); + _info_list.clear(); + _enabled.clear(); + + _all_list = registry->allNotificationProviders(); + _info_list.reserve(_all_list.size()); + foreach (const QString& s, _all_list) { + NotificationPluginInterface *plugin = registry->getNotificationPlugin(s); + if (plugin) { + _info_list.append(plugin->describeProvider(s)); + } else { + NotificationPluginInterface::NotificationProviderInfo info; + info.name = s; + _info_list.append(info); + } + } + + _enabled = _config->value().toStringList().toSet(); + endResetModel(); +} diff --git a/sowatchui/providersmodel.h b/sowatchui/providersmodel.h new file mode 100644 index 0000000..9cf6ec6 --- /dev/null +++ b/sowatchui/providersmodel.h @@ -0,0 +1,44 @@ +#ifndef PROVIDERSMODEL_H +#define PROVIDERSMODEL_H + +#include + +#include + +class ProvidersModel : public QAbstractListModel +{ + Q_OBJECT + Q_PROPERTY(QString configKey READ configKey WRITE setConfigKey NOTIFY configKeyChanged) + +public: + explicit ProvidersModel(QObject *parent = 0); + + enum DataRoles { + NameRole = Qt::UserRole, + EnabledRole + }; + + QString configKey() const; + void setConfigKey(const QString& configKey); + + int rowCount(const QModelIndex &parent) const; + QVariant data(const QModelIndex &index, int role) const; + bool setData(const QModelIndex &index, const QVariant &value, int role); + +public slots: + void setProviderEnabled(const QString& id, bool enabled); + +signals: + void configKeyChanged(); + +private slots: + void reload(); + +private: + sowatch::ConfigKey *_config; + QStringList _all_list; + QList _info_list; + QSet _enabled; +}; + +#endif // PROVIDERSMODEL_H diff --git a/sowatchui/qml/WatchPage.qml b/sowatchui/qml/WatchPage.qml index 59bb5c4..21c4543 100644 --- a/sowatchui/qml/WatchPage.qml +++ b/sowatchui/qml/WatchPage.qml @@ -73,38 +73,37 @@ Page { GroupHeader { width: parent.width - text: "Watchlets" + text: "Notification sources" visible: configQmlLoader.status === Loader.Ready } ListView { - id: watchletsListView + id: providersListView interactive: false width: parent.width height: UiConstants.ListItemHeightDefault * count - model: ListModel { - ListElement { - title: "Test" - } + model: ProvidersModel { + id: providersModel + configKey: watchPage.configKey + "/providers" } delegate: ListDelegate { - + CheckBox { + anchors.verticalCenter: parent.verticalCenter + anchors.right: parent.right + checked: model.enabled + onCheckedChanged: providersModel.setProviderEnabled(model.name, checked); + } } } - Button { - anchors.horizontalCenter: parent.horizontalCenter - text: qsTr("Add watchlet") - } - GroupHeader { width: parent.width - text: "Notification sources" + text: "Watchlets" visible: configQmlLoader.status === Loader.Ready } ListView { - id: providersListView + id: watchletsListView interactive: false width: parent.width height: UiConstants.ListItemHeightDefault * count @@ -120,7 +119,7 @@ Page { Button { anchors.horizontalCenter: parent.horizontalCenter - text: qsTr("Add notification source") + text: qsTr("Add new watchlet") } } } diff --git a/sowatchui/sowatchui.pro b/sowatchui/sowatchui.pro index c0589fc..fa965d3 100644 --- a/sowatchui/sowatchui.pro +++ b/sowatchui/sowatchui.pro @@ -37,11 +37,13 @@ DEPENDPATH += $$PWD/../libsowatch # Source files SOURCES += main.cpp \ watchesmodel.cpp daemonproxy.cpp \ - watchscannermodel.cpp + watchscannermodel.cpp \ + providersmodel.cc HEADERS += \ watchesmodel.h daemonproxy.h \ - watchscannermodel.h + watchscannermodel.h \ + providersmodel.h OTHER_FILES += qml/main.qml \ qml/MainPage.qml \ -- cgit v1.2.3