From bc899047089079dde323e84a57efe46ce6af653d Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Sat, 11 May 2013 16:10:50 +0200 Subject: add the liveview paint engine --- sowatchui/configuredwatchletsmodel.cpp | 168 +++++++++++++++++++++++++++++++++ sowatchui/configuredwatchletsmodel.h | 53 +++++++++++ sowatchui/main.cpp | 4 +- sowatchui/qml/AddWatchletSheet.qml | 2 +- sowatchui/qml/WatchPage.qml | 2 +- sowatchui/sowatchui.pro | 4 +- sowatchui/watchletsmodel.cpp | 168 --------------------------------- sowatchui/watchletsmodel.h | 53 ----------- 8 files changed, 227 insertions(+), 227 deletions(-) create mode 100644 sowatchui/configuredwatchletsmodel.cpp create mode 100644 sowatchui/configuredwatchletsmodel.h delete mode 100644 sowatchui/watchletsmodel.cpp delete mode 100644 sowatchui/watchletsmodel.h (limited to 'sowatchui') diff --git a/sowatchui/configuredwatchletsmodel.cpp b/sowatchui/configuredwatchletsmodel.cpp new file mode 100644 index 0000000..dd301b7 --- /dev/null +++ b/sowatchui/configuredwatchletsmodel.cpp @@ -0,0 +1,168 @@ +#include "configuredwatchletsmodel.h" + +using namespace sowatch; + +static const QString watchletsSubKey("/watchlets"); + +ConfiguredWatchletsModel::ConfiguredWatchletsModel(QObject *parent) : + QAbstractListModel(parent), + _config(0), + _unadded(false) +{ + QHash roles = roleNames(); + roles[Qt::DisplayRole] = QByteArray("title"); + roles[Qt::DecorationRole] = QByteArray("iconSource"); + roles[NameRole] = QByteArray("name"); + roles[ConfigQmlUrlRole] = QByteArray("configQmlUrl"); + setRoleNames(roles); +} + +QString ConfiguredWatchletsModel::configKey() const +{ + if (_config) { + QString key = _config->key(); + return key.left(key.length() - watchletsSubKey.length()); + } else { + return QString(); + } +} + +void ConfiguredWatchletsModel::setConfigKey(const QString &configKey) +{ + QString oldConfigKey = this->configKey(); + if (_config) { + delete _config; + _config = 0; + } + if (!configKey.isEmpty()) { + _config = new GConfKey(configKey + watchletsSubKey, this); + connect(_config, SIGNAL(changed()), SLOT(handleConfigChanged())); + } + if (this->configKey() != oldConfigKey) { + reload(); + emit configKeyChanged(); + } +} + +bool ConfiguredWatchletsModel::displayUnadded() const +{ + return _unadded; +} + +void ConfiguredWatchletsModel::setDisplayUnadded(bool displayUnadded) +{ + qDebug() << "Set dunadded" << displayUnadded; + _unadded = displayUnadded; + if (_config) reload(); + emit displayUnaddedChanged(); +} + +int ConfiguredWatchletsModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return _list.count(); +} + +QVariant ConfiguredWatchletsModel::data(const QModelIndex &index, int role) const +{ + const QString id = _list[index.row()]; + switch (role) { + case Qt::DisplayRole: + return QVariant::fromValue(_info[id].name); + case Qt::DecorationRole: + return QVariant::fromValue(_info[id].icon); + case NameRole: + return QVariant::fromValue(id); + case ConfigQmlUrlRole: + return QVariant::fromValue(_info[id].configQmlUrl); + } + return QVariant(); +} + +void ConfiguredWatchletsModel::addWatchlet(const QString &name) +{ + if (!_config) return; + QStringList enabled = _config->value().toStringList(); + if (_enabled.contains(name)) return; + enabled << name; + _config->set(enabled); +} + +void ConfiguredWatchletsModel::removeWatchlet(const QString &name) +{ + if (!_config) return; + QStringList enabled = _config->value().toStringList(); + enabled.removeAll(name); + _config->set(enabled); +} + +void ConfiguredWatchletsModel::moveWatchletUp(const QString &name) +{ + if (!_config) return; + QStringList enabled = _config->value().toStringList(); + int index = enabled.indexOf(name); + qDebug() << "move up" << enabled << index << enabled[index]; + if (index > 0 && index < enabled.size()) { + enabled.swap(index - 1, index); + } + _config->set(enabled); +} + +void ConfiguredWatchletsModel::moveWatchletDown(const QString &name) +{ + if (!_config) return; + QStringList enabled = _config->value().toStringList(); + int index = enabled.indexOf(name); + qDebug() << "move down" << enabled << index << enabled[index]; + if (index >= 0 && index < enabled.size() - 1) { + enabled.swap(index, index + 1); + } + _config->set(enabled); +} + +void ConfiguredWatchletsModel::reload() +{ + Registry *registry = Registry::registry(); + Q_ASSERT(_config); + beginResetModel(); + _list.clear(); + _info.clear(); + _enabled.clear(); + + qDebug() << "Reloading watchlets"; + + QStringList all = registry->allWatchlets(); + foreach (const QString& s, all) { + WatchletPluginInterface *plugin = registry->getWatchletPlugin(s); + if (plugin) { + _info[s] = plugin->describeWatchlet(s); + } else { + WatchletPluginInterface::WatchletInfo info; + info.name = s; + _info[s] = info; + } + } + + QStringList enabled = _config->value().toStringList(); + _enabled = enabled.toSet(); + + if (_unadded) { + qDebug() << "Listing unadded watchlets from" << all; + foreach (const QString& s, all) { + if (!_info[s].hidden && !_enabled.contains(s)) { + _list.append(s); + } + } + _list.sort(); + } else { + qDebug() << "Listing added watchlets from" << enabled; + _list = enabled; + } + endResetModel(); +} + +void ConfiguredWatchletsModel::handleConfigChanged() +{ + // TODO + reload(); +} diff --git a/sowatchui/configuredwatchletsmodel.h b/sowatchui/configuredwatchletsmodel.h new file mode 100644 index 0000000..c0a114d --- /dev/null +++ b/sowatchui/configuredwatchletsmodel.h @@ -0,0 +1,53 @@ +#ifndef CONFIGUREDWATCHLETSMODEL_H +#define CONFIGUREDWATCHLETSMODEL_H + +#include + +#include + +class ConfiguredWatchletsModel : public QAbstractListModel +{ + Q_OBJECT + Q_PROPERTY(QString configKey READ configKey WRITE setConfigKey NOTIFY configKeyChanged) + Q_PROPERTY(bool displayUnadded READ displayUnadded WRITE setDisplayUnadded NOTIFY displayUnaddedChanged) + +public: + explicit ConfiguredWatchletsModel(QObject *parent = 0); + + enum DataRoles { + NameRole = Qt::UserRole, + ConfigQmlUrlRole + }; + + QString configKey() const; + void setConfigKey(const QString& configKey); + + bool displayUnadded() const; + void setDisplayUnadded(bool displayUnadded); + + int rowCount(const QModelIndex &parent) const; + QVariant data(const QModelIndex &index, int role) const; + +public slots: + void addWatchlet(const QString& name); + void removeWatchlet(const QString& name); + void moveWatchletUp(const QString& name); + void moveWatchletDown(const QString& name); + +signals: + void configKeyChanged(); + void displayUnaddedChanged(); + +private slots: + void reload(); + void handleConfigChanged(); + +private: + sowatch::ConfigKey *_config; + bool _unadded; + QStringList _list; + QMap _info; + QSet _enabled; +}; + +#endif // WATCHLETSMODEL_H diff --git a/sowatchui/main.cpp b/sowatchui/main.cpp index 80a0f6b..f8f77e3 100644 --- a/sowatchui/main.cpp +++ b/sowatchui/main.cpp @@ -7,7 +7,7 @@ #include "watchesmodel.h" #include "watchscannermodel.h" #include "providersmodel.h" -#include "watchletsmodel.h" +#include "configuredwatchletsmodel.h" static sowatch::Registry *registry; static WatchesModel *watches; @@ -27,7 +27,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"); - qmlRegisterType("com.javispedro.sowatch", 1, 0, "WatchletsModel"); + qmlRegisterType("com.javispedro.sowatch", 1, 0, "ConfiguredWatchletsModel"); viewer->rootContext()->setContextProperty("watches", watches); viewer->rootContext()->setContextProperty("watchScanner", watchScanner); diff --git a/sowatchui/qml/AddWatchletSheet.qml b/sowatchui/qml/AddWatchletSheet.qml index e052350..de10bd6 100644 --- a/sowatchui/qml/AddWatchletSheet.qml +++ b/sowatchui/qml/AddWatchletSheet.qml @@ -18,7 +18,7 @@ Sheet { flickableDirection: Flickable.VerticalFlick - model: WatchletsModel { + model: ConfiguredWatchletsModel { id: watchletsModel configKey: sheet.configKey displayUnadded: true diff --git a/sowatchui/qml/WatchPage.qml b/sowatchui/qml/WatchPage.qml index 8d623a6..a47faf8 100644 --- a/sowatchui/qml/WatchPage.qml +++ b/sowatchui/qml/WatchPage.qml @@ -158,7 +158,7 @@ Page { interactive: false width: parent.width height: UiConstants.ListItemHeightDefault * count - model: WatchletsModel { + model: ConfiguredWatchletsModel { id: watchletsModel configKey: watchPage.configKey displayUnadded: false diff --git a/sowatchui/sowatchui.pro b/sowatchui/sowatchui.pro index 4098228..afbedb6 100644 --- a/sowatchui/sowatchui.pro +++ b/sowatchui/sowatchui.pro @@ -39,13 +39,13 @@ SOURCES += main.cpp \ watchesmodel.cpp daemonproxy.cpp \ watchscannermodel.cpp \ providersmodel.cpp \ - watchletsmodel.cpp + configuredwatchletsmodel.cpp HEADERS += \ watchesmodel.h daemonproxy.h \ watchscannermodel.h \ providersmodel.h \ - watchletsmodel.h + configuredwatchletsmodel.h OTHER_FILES += qml/main.qml \ qml/MainPage.qml \ diff --git a/sowatchui/watchletsmodel.cpp b/sowatchui/watchletsmodel.cpp deleted file mode 100644 index 461a38b..0000000 --- a/sowatchui/watchletsmodel.cpp +++ /dev/null @@ -1,168 +0,0 @@ -#include "watchletsmodel.h" - -using namespace sowatch; - -static const QString watchletsSubKey("/watchlets"); - -WatchletsModel::WatchletsModel(QObject *parent) : - QAbstractListModel(parent), - _config(0), - _unadded(false) -{ - QHash roles = roleNames(); - roles[Qt::DisplayRole] = QByteArray("title"); - roles[Qt::DecorationRole] = QByteArray("iconSource"); - roles[NameRole] = QByteArray("name"); - roles[ConfigQmlUrlRole] = QByteArray("configQmlUrl"); - setRoleNames(roles); -} - -QString WatchletsModel::configKey() const -{ - if (_config) { - QString key = _config->key(); - return key.left(key.length() - watchletsSubKey.length()); - } else { - return QString(); - } -} - -void WatchletsModel::setConfigKey(const QString &configKey) -{ - QString oldConfigKey = this->configKey(); - if (_config) { - delete _config; - _config = 0; - } - if (!configKey.isEmpty()) { - _config = new GConfKey(configKey + watchletsSubKey, this); - connect(_config, SIGNAL(changed()), SLOT(handleConfigChanged())); - } - if (this->configKey() != oldConfigKey) { - reload(); - emit configKeyChanged(); - } -} - -bool WatchletsModel::displayUnadded() const -{ - return _unadded; -} - -void WatchletsModel::setDisplayUnadded(bool displayUnadded) -{ - qDebug() << "Set dunadded" << displayUnadded; - _unadded = displayUnadded; - if (_config) reload(); - emit displayUnaddedChanged(); -} - -int WatchletsModel::rowCount(const QModelIndex &parent) const -{ - Q_UNUSED(parent); - return _list.count(); -} - -QVariant WatchletsModel::data(const QModelIndex &index, int role) const -{ - const QString id = _list[index.row()]; - switch (role) { - case Qt::DisplayRole: - return QVariant::fromValue(_info[id].name); - case Qt::DecorationRole: - return QVariant::fromValue(_info[id].icon); - case NameRole: - return QVariant::fromValue(id); - case ConfigQmlUrlRole: - return QVariant::fromValue(_info[id].configQmlUrl); - } - return QVariant(); -} - -void WatchletsModel::addWatchlet(const QString &name) -{ - if (!_config) return; - QStringList enabled = _config->value().toStringList(); - if (_enabled.contains(name)) return; - enabled << name; - _config->set(enabled); -} - -void WatchletsModel::removeWatchlet(const QString &name) -{ - if (!_config) return; - QStringList enabled = _config->value().toStringList(); - enabled.removeAll(name); - _config->set(enabled); -} - -void WatchletsModel::moveWatchletUp(const QString &name) -{ - if (!_config) return; - QStringList enabled = _config->value().toStringList(); - int index = enabled.indexOf(name); - qDebug() << "move up" << enabled << index << enabled[index]; - if (index > 0 && index < enabled.size()) { - enabled.swap(index - 1, index); - } - _config->set(enabled); -} - -void WatchletsModel::moveWatchletDown(const QString &name) -{ - if (!_config) return; - QStringList enabled = _config->value().toStringList(); - int index = enabled.indexOf(name); - qDebug() << "move down" << enabled << index << enabled[index]; - if (index >= 0 && index < enabled.size() - 1) { - enabled.swap(index, index + 1); - } - _config->set(enabled); -} - -void WatchletsModel::reload() -{ - Registry *registry = Registry::registry(); - Q_ASSERT(_config); - beginResetModel(); - _list.clear(); - _info.clear(); - _enabled.clear(); - - qDebug() << "Reloading watchlets"; - - QStringList all = registry->allWatchlets(); - foreach (const QString& s, all) { - WatchletPluginInterface *plugin = registry->getWatchletPlugin(s); - if (plugin) { - _info[s] = plugin->describeWatchlet(s); - } else { - WatchletPluginInterface::WatchletInfo info; - info.name = s; - _info[s] = info; - } - } - - QStringList enabled = _config->value().toStringList(); - _enabled = enabled.toSet(); - - if (_unadded) { - qDebug() << "Listing unadded watchlets from" << all; - foreach (const QString& s, all) { - if (!_info[s].hidden && !_enabled.contains(s)) { - _list.append(s); - } - } - _list.sort(); - } else { - qDebug() << "Listing added watchlets from" << enabled; - _list = enabled; - } - endResetModel(); -} - -void WatchletsModel::handleConfigChanged() -{ - // TODO - reload(); -} diff --git a/sowatchui/watchletsmodel.h b/sowatchui/watchletsmodel.h deleted file mode 100644 index 4f7ae8f..0000000 --- a/sowatchui/watchletsmodel.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef WATCHLETSMODEL_H -#define WATCHLETSMODEL_H - -#include - -#include - -class WatchletsModel : public QAbstractListModel -{ - Q_OBJECT - Q_PROPERTY(QString configKey READ configKey WRITE setConfigKey NOTIFY configKeyChanged) - Q_PROPERTY(bool displayUnadded READ displayUnadded WRITE setDisplayUnadded NOTIFY displayUnaddedChanged) - -public: - explicit WatchletsModel(QObject *parent = 0); - - enum DataRoles { - NameRole = Qt::UserRole, - ConfigQmlUrlRole - }; - - QString configKey() const; - void setConfigKey(const QString& configKey); - - bool displayUnadded() const; - void setDisplayUnadded(bool displayUnadded); - - int rowCount(const QModelIndex &parent) const; - QVariant data(const QModelIndex &index, int role) const; - -public slots: - void addWatchlet(const QString& name); - void removeWatchlet(const QString& name); - void moveWatchletUp(const QString& name); - void moveWatchletDown(const QString& name); - -signals: - void configKeyChanged(); - void displayUnaddedChanged(); - -private slots: - void reload(); - void handleConfigChanged(); - -private: - sowatch::ConfigKey *_config; - bool _unadded; - QStringList _list; - QMap _info; - QSet _enabled; -}; - -#endif // WATCHLETSMODEL_H -- cgit v1.2.3