From bf083973efd101e05d75882b63aad9bdfa37dfbc Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Sat, 11 May 2013 19:17:07 +0200 Subject: store watchlets in model --- libsowatch/declarativewatchlet.cpp | 1 + libsowatch/libsowatch.pro | 6 +- libsowatch/sowatch.h | 2 + libsowatch/watch.cpp | 10 ++++ libsowatch/watch.h | 6 ++ libsowatch/watchlet.cpp | 5 ++ libsowatch/watchlet.h | 6 ++ libsowatch/watchletsmodel.cpp | 116 +++++++++++++++++++++++++++++++++++++ libsowatch/watchletsmodel.h | 53 +++++++++++++++++ libsowatch/watchserver.cpp | 36 ++++++++---- libsowatch/watchserver.h | 10 +++- 11 files changed, 236 insertions(+), 15 deletions(-) create mode 100644 libsowatch/watchletsmodel.cpp create mode 100644 libsowatch/watchletsmodel.h (limited to 'libsowatch') diff --git a/libsowatch/declarativewatchlet.cpp b/libsowatch/declarativewatchlet.cpp index 6508ce5..c223b8d 100644 --- a/libsowatch/declarativewatchlet.cpp +++ b/libsowatch/declarativewatchlet.cpp @@ -2,6 +2,7 @@ #include #include "watchserver.h" #include "watch.h" +#include "notificationsmodel.h" #include "gconfkey.h" #include "declarativewatchwrapper.h" #include "declarativewatchlet.h" diff --git a/libsowatch/libsowatch.pro b/libsowatch/libsowatch.pro index ce0468e..e042e7a 100644 --- a/libsowatch/libsowatch.pro +++ b/libsowatch/libsowatch.pro @@ -34,7 +34,8 @@ SOURCES += \ allwatchscanner.cpp \ configkey.cpp \ gconfkey.cpp \ - notificationsmodel.cpp + notificationsmodel.cpp \ + watchletsmodel.cpp HEADERS += \ watchserver.h \ @@ -57,7 +58,8 @@ HEADERS += \ allwatchscanner.h \ configkey.h \ gconfkey.h \ - notificationsmodel.h + notificationsmodel.h \ + watchletsmodel.h TRANSLATIONS += libsowatch_en.ts libsowatch_es.ts diff --git a/libsowatch/sowatch.h b/libsowatch/sowatch.h index 31ee48a..4acea75 100644 --- a/libsowatch/sowatch.h +++ b/libsowatch/sowatch.h @@ -16,11 +16,13 @@ #include "weathernotification.h" #include "notificationprovider.h" #include "notificationplugininterface.h" +#include "notificationsmodel.h" #include "watchlet.h" #include "graphicswatchlet.h" #include "declarativewatchlet.h" #include "watchletplugininterface.h" +#include "watchletsmodel.h" #include "registry.h" #include "allwatchscanner.h" diff --git a/libsowatch/watch.cpp b/libsowatch/watch.cpp index e37cdf8..caf46c4 100644 --- a/libsowatch/watch.cpp +++ b/libsowatch/watch.cpp @@ -19,3 +19,13 @@ void Watch::vibrate(int msecs) /* The default implementation does nothing. */ Q_UNUSED(msecs); } + +void Watch::setWatchletsModel(WatchletsModel *model) +{ + Q_UNUSED(model); +} + +void Watch::setNotificationsModel(NotificationsModel *model) +{ + Q_UNUSED(model); +} diff --git a/libsowatch/watch.h b/libsowatch/watch.h index b0a2a93..27e3d04 100644 --- a/libsowatch/watch.h +++ b/libsowatch/watch.h @@ -13,6 +13,9 @@ namespace sowatch { +class WatchletsModel; +class NotificationsModel; + class SOWATCH_EXPORT Watch : public QObject, public QPaintDevice { Q_OBJECT @@ -57,6 +60,9 @@ public: virtual void queryCharging() = 0; virtual bool charging() const = 0; + virtual void setWatchletsModel(WatchletsModel *model); + virtual void setNotificationsModel(NotificationsModel *model); + public slots: /** Go back to the idle screen. */ virtual void displayIdleScreen() = 0; diff --git a/libsowatch/watchlet.cpp b/libsowatch/watchlet.cpp index 03ede41..0bcfa1d 100644 --- a/libsowatch/watchlet.cpp +++ b/libsowatch/watchlet.cpp @@ -48,6 +48,11 @@ void Watchlet::deactivate() emit deactivated(); } +void Watchlet::setWatchletsModel(WatchletsModel *model) +{ + Q_UNUSED(model); +} + void Watchlet::setNotificationsModel(NotificationsModel *model) { Q_UNUSED(model); diff --git a/libsowatch/watchlet.h b/libsowatch/watchlet.h index f32d495..f994447 100644 --- a/libsowatch/watchlet.h +++ b/libsowatch/watchlet.h @@ -2,6 +2,7 @@ #define SOWATCH_WATCHLET_H #include +#include #include "sowatch_global.h" namespace sowatch @@ -9,6 +10,7 @@ namespace sowatch class Watch; class WatchServer; +class WatchletsModel; class Notification; class NotificationsModel; @@ -19,6 +21,7 @@ class SOWATCH_EXPORT Watchlet : public QObject Q_PROPERTY(bool active READ isActive NOTIFY activeChanged) public: + Watchlet(); Watchlet(Watch *watch, const QString& id); ~Watchlet(); @@ -33,6 +36,7 @@ public: virtual void deactivate(); // Some properties + virtual void setWatchletsModel(WatchletsModel *model); virtual void setNotificationsModel(NotificationsModel *model); virtual bool handlesNotification(Notification* notification) const; @@ -53,4 +57,6 @@ private: } +QML_DECLARE_TYPE(sowatch::Watchlet) + #endif // SOWATCH_WATCHLET_H diff --git a/libsowatch/watchletsmodel.cpp b/libsowatch/watchletsmodel.cpp new file mode 100644 index 0000000..9e06928 --- /dev/null +++ b/libsowatch/watchletsmodel.cpp @@ -0,0 +1,116 @@ +#include + +#include "registry.h" +#include "watchletplugininterface.h" +#include "watchletsmodel.h" + +using namespace sowatch; + +WatchletsModel::WatchletsModel(QObject *parent) : + QAbstractListModel(parent) +{ + QHash roles = roleNames(); + roles[TitleRole] = QByteArray("title"); + roles[ObjectRole] = QByteArray("object"); + setRoleNames(roles); +} + +int WatchletsModel::rowCount(const QModelIndex &parent) const +{ + return parent.isValid() ? 0 : _list.count(); +} + +QVariant WatchletsModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) return QVariant(); + const int row = index.row(); + if (row >= _list.size()) { + qWarning() << "Could not seek to" << row; + return QVariant(); + } + + const Watchlet *watchlet = _list.at(row); + const WatchletInfo& info = _info.at(row); + + switch (role) { + case Qt::DisplayRole: + return QVariant::fromValue(info.name); + case ObjectRole: + return QVariant::fromValue(const_cast(watchlet)); + } + return QVariant(); +} + +int WatchletsModel::size() const +{ + return _list.size(); +} + +Watchlet * WatchletsModel::at(int position) const +{ + Q_ASSERT(position < _list.size()); + return const_cast(_list.at(position)); +} + +void WatchletsModel::add(Watchlet *w) +{ + insert(_list.size(), w); +} + +void WatchletsModel::insert(int position, Watchlet *w) +{ + Q_ASSERT(position >= 0 && position <= _list.size()); + + beginInsertRows(QModelIndex(), position, position); + _list.insert(position, w); + _info.insert(position, getInfoForWatchlet(w)); + endInsertRows(); + + emit modelChanged(); +} + +void WatchletsModel::move(const Watchlet *w, int to) +{ + move(_list.indexOf(const_cast(w)), to); +} + +void WatchletsModel::move(int position, int to) +{ + Q_ASSERT(position >= 0 && position < _list.size()); + Q_ASSERT(to >= 0 && to < _list.size()); + + beginMoveRows(QModelIndex(), position, position, QModelIndex(), to); + _list.move(position, to); + _info.move(position, to); + endMoveRows(); + + emit modelChanged(); +} + +void WatchletsModel::remove(const Watchlet *w) +{ + remove(_list.indexOf(const_cast(w))); +} + +void WatchletsModel::remove(int position) +{ + Q_ASSERT(position >= 0 && position < _list.size()); + + beginRemoveRows(QModelIndex(), position, position); + _list.removeAt(position); + _info.removeAt(position); + endRemoveRows(); + + emit modelChanged(); +} + +WatchletsModel::WatchletInfo WatchletsModel::getInfoForWatchlet(const Watchlet *w) +{ + QString id = w->id(); + WatchletPluginInterface *plugin = Registry::registry()->getWatchletPlugin(id); + if (plugin) { + return plugin->describeWatchlet(id); + } else { + return WatchletInfo(); + } +} diff --git a/libsowatch/watchletsmodel.h b/libsowatch/watchletsmodel.h new file mode 100644 index 0000000..cdb6be4 --- /dev/null +++ b/libsowatch/watchletsmodel.h @@ -0,0 +1,53 @@ +#ifndef SOWATCH_WATCHLETSMODEL_H +#define SOWATCH_WATCHLETSMODEL_H + +#include + +#include "watchlet.h" +#include "watchletplugininterface.h" + +namespace sowatch +{ + +class WatchletsModel : public QAbstractListModel +{ + Q_OBJECT + +public: + explicit WatchletsModel(QObject *parent = 0); + + enum DataRoles { + ObjectRole = Qt::UserRole, + TitleRole = Qt::DisplayRole + }; + + int rowCount(const QModelIndex &parent) const; + QVariant data(const QModelIndex &index, int role) const; + + int size() const; + Watchlet * at(int position) const; + + void add(Watchlet *w); + void insert(int position, Watchlet *w); + void move(const Watchlet *w, int to); + void move(int position, int to); + void remove(const Watchlet *w); + void remove(int position); + +signals: + void modelChanged(); + +protected: + typedef WatchletPluginInterface::WatchletInfo WatchletInfo; + + static WatchletInfo getInfoForWatchlet(const Watchlet *w); + +private: + QList _list; + QList _info; + +}; + +} + +#endif // WATCHLETSMODEL_H diff --git a/libsowatch/watchserver.cpp b/libsowatch/watchserver.cpp index fa4011f..c82c3ee 100644 --- a/libsowatch/watchserver.cpp +++ b/libsowatch/watchserver.cpp @@ -1,8 +1,10 @@ #include -#include "notificationprovider.h" #include "watch.h" #include "watchlet.h" +#include "watchletsmodel.h" +#include "notificationprovider.h" +#include "notificationsmodel.h" #include "watchserver.h" using namespace sowatch; @@ -12,6 +14,7 @@ WatchServer::WatchServer(Watch* watch, QObject* parent) : _nextWatchletButton(-1), _oldNotificationThreshold(300), _idleWatchlet(0), _notificationWatchlet(0), + _watchlets(new WatchletsModel(this)), _notifications(new NotificationsModel(this)), _activeWatchlet(0), _currentWatchlet(0), _currentWatchletIndex(-1), _syncTimeTimer(new QTimer(this)) @@ -20,10 +23,14 @@ WatchServer::WatchServer(Watch* watch, QObject* parent) : connect(_watch, SIGNAL(disconnected()), SLOT(handleWatchDisconnected())); connect(_watch, SIGNAL(idling()), SLOT(handleWatchIdling())); connect(_watch, SIGNAL(buttonPressed(int)), SLOT(handleWatchButtonPress(int))); + connect(_watch, SIGNAL(watchletRequested(QString)), + SLOT(handleWatchletRequested(QString))); connect(_syncTimeTimer, SIGNAL(timeout()), SLOT(syncTime())); _syncTimeTimer->setSingleShot(true); _syncTimeTimer->setInterval(24 * 3600 * 1000); // Once a day + + _watch->setWatchletsModel(_watchlets); } Watch* WatchServer::watch() @@ -94,10 +101,14 @@ void WatchServer::setNotificationWatchlet(Watchlet *watchlet) } } +const WatchletsModel * WatchServer::watchlets() const +{ + return _watchlets; +} + void WatchServer::addWatchlet(Watchlet *watchlet) { - Q_ASSERT(watchlet); - insertWatchlet(_watchlets.size(), watchlet); + insertWatchlet(_watchlets->size(), watchlet); } void WatchServer::insertWatchlet(int position, Watchlet *watchlet) @@ -110,20 +121,18 @@ void WatchServer::insertWatchlet(int position, Watchlet *watchlet) setWatchletProperties(watchlet); - _watchlets.insert(position, watchlet); + _watchlets->insert(position, watchlet); _watchletIds[id] = watchlet; } void WatchServer::moveWatchlet(const Watchlet *watchlet, int to) { const QString id = watchlet->id(); - int index = _watchlets.indexOf(const_cast(watchlet)); Q_ASSERT(watchlet->watch() == _watch); Q_ASSERT(_watchletIds.contains(id)); - Q_ASSERT(index >= 0); - _watchlets.move(index, to); + _watchlets->move(watchlet, to); } void WatchServer::removeWatchlet(const Watchlet *watchlet) @@ -139,7 +148,7 @@ void WatchServer::removeWatchlet(const Watchlet *watchlet) unsetWatchletProperties(const_cast(watchlet)); - _watchlets.removeAll(const_cast(watchlet)); + _watchlets->remove(watchlet); _watchletIds.remove(id); } @@ -261,12 +270,14 @@ void WatchServer::closeWatchlet() void WatchServer::setWatchletProperties(Watchlet *watchlet) { Q_ASSERT(watchlet->watch() == _watch); + watchlet->setWatchletsModel(_watchlets); watchlet->setNotificationsModel(_notifications); } void WatchServer::unsetWatchletProperties(Watchlet *watchlet) { Q_ASSERT(watchlet->watch() == _watch); + watchlet->setWatchletsModel(0); watchlet->setNotificationsModel(0); } @@ -301,11 +312,11 @@ void WatchServer::nextWatchlet() { qDebug() << "current watchlet index" << _currentWatchletIndex; _currentWatchletIndex++; - if (_currentWatchletIndex >= _watchlets.size() || _currentWatchletIndex < 0) { + if (_currentWatchletIndex >= _watchlets->size() || _currentWatchletIndex < 0) { _currentWatchletIndex = -1; closeWatchlet(); } else { - Watchlet* watchlet = _watchlets.at(_currentWatchletIndex); + Watchlet* watchlet = _watchlets->at(_currentWatchletIndex); openWatchlet(watchlet); } } @@ -396,6 +407,11 @@ void WatchServer::handleWatchButtonPress(int button) } } +void WatchServer::handleWatchletRequested(const QString &id) +{ + openWatchlet(id); +} + void WatchServer::handleNotificationChanged() { QObject *obj = sender(); diff --git a/libsowatch/watchserver.h b/libsowatch/watchserver.h index 43bb7d4..a1cda45 100644 --- a/libsowatch/watchserver.h +++ b/libsowatch/watchserver.h @@ -8,15 +8,16 @@ #include #include "sowatch_global.h" -#include "notificationsmodel.h" +#include "notification.h" namespace sowatch { class Watch; class Watchlet; +class WatchletsModel; class NotificationProvider; -class WeatherNotification; +class NotificationsModel; class SOWATCH_EXPORT WatchServer : public QObject { @@ -41,6 +42,8 @@ public: Watchlet *notificationWatchlet(); void setNotificationWatchlet(Watchlet *watchlet); + const WatchletsModel * watchlets() const; + void addWatchlet(Watchlet *watchlet); void insertWatchlet(int position, Watchlet *watchlet); void moveWatchlet(const Watchlet *watchlet, int to); @@ -87,7 +90,7 @@ private: Watchlet *_notificationWatchlet; /** A list of watchlets, in order. */ - QList _watchlets; + WatchletsModel *_watchlets; /** Stores all the watchlets with a given watchled id. */ QMap _watchletIds; @@ -123,6 +126,7 @@ private slots: void handleWatchDisconnected(); void handleWatchIdling(); void handleWatchButtonPress(int button); + void handleWatchletRequested(const QString& id); void handleNotificationChanged(); void handleNotificationDismissed(); -- cgit v1.2.3