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 --- libsowatch/sowatch.h | 1 + libsowatch/watch.h | 2 + libsowatch/watchserver.cpp | 5 +- liveview/liveview.cpp | 69 +++++++++- liveview/liveview.h | 15 +++ liveview/liveview.pro | 6 +- liveview/liveviewpaintengine.cpp | 98 ++++++++++++++ liveview/liveviewpaintengine.h | 32 +++++ liveview/liveviewplugin.cpp | 1 + metawatch/metawatch.cpp | 2 +- metawatch/metawatchpaintengine.cpp | 5 +- metawatch/metawatchpaintengine.h | 5 +- metawatchwatchlets/metawatchwatchletsplugin.cpp | 2 + qmapwatchlet/mapview.cpp | 2 +- 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 -------- 22 files changed, 455 insertions(+), 244 deletions(-) create mode 100644 liveview/liveviewpaintengine.cpp create mode 100644 liveview/liveviewpaintengine.h create mode 100644 sowatchui/configuredwatchletsmodel.cpp create mode 100644 sowatchui/configuredwatchletsmodel.h delete mode 100644 sowatchui/watchletsmodel.cpp delete mode 100644 sowatchui/watchletsmodel.h diff --git a/libsowatch/sowatch.h b/libsowatch/sowatch.h index e0ace02..31ee48a 100644 --- a/libsowatch/sowatch.h +++ b/libsowatch/sowatch.h @@ -9,6 +9,7 @@ #include "watch.h" #include "watchserver.h" #include "watchscanner.h" +#include "watchpaintengine.h" #include "watchplugininterface.h" #include "notification.h" diff --git a/libsowatch/watch.h b/libsowatch/watch.h index 938071b..b0a2a93 100644 --- a/libsowatch/watch.h +++ b/libsowatch/watch.h @@ -85,6 +85,8 @@ signals: void buttonPressed(int button); /** A button has been pressed and then released. */ void buttonReleased(int button); + /** Emitted when e.g. either via the watch menu, or similar, a watchlet is requested. */ + void watchletRequested(const QString& id); }; } diff --git a/libsowatch/watchserver.cpp b/libsowatch/watchserver.cpp index f79b1c1..fa4011f 100644 --- a/libsowatch/watchserver.cpp +++ b/libsowatch/watchserver.cpp @@ -9,8 +9,9 @@ using namespace sowatch; WatchServer::WatchServer(Watch* watch, QObject* parent) : QObject(parent), _watch(watch), - _nextWatchletButton(-1), _idleWatchlet(0), _notificationWatchlet(0), + _nextWatchletButton(-1), _oldNotificationThreshold(300), + _idleWatchlet(0), _notificationWatchlet(0), _notifications(new NotificationsModel(this)), _activeWatchlet(0), _currentWatchlet(0), _currentWatchletIndex(-1), _syncTimeTimer(new QTimer(this)) @@ -161,7 +162,6 @@ const NotificationsModel * WatchServer::notifications() const void WatchServer::postNotification(Notification *notification) { - const Notification::Type type = notification->type(); const Notification::Priority priority = notification->priority(); // Add notification to model @@ -401,7 +401,6 @@ void WatchServer::handleNotificationChanged() QObject *obj = sender(); if (obj) { Notification* n = static_cast(obj); - const Notification::Type type = n->type(); const uint lastCount = _notificationCounts[n]; _notificationCounts[n] = n->count(); diff --git a/liveview/liveview.cpp b/liveview/liveview.cpp index 5e2c5aa..df2b099 100644 --- a/liveview/liveview.cpp +++ b/liveview/liveview.cpp @@ -1,5 +1,6 @@ #include +#include "liveviewpaintengine.h" #include "liveview.h" using namespace sowatch; @@ -10,28 +11,56 @@ QTM_USE_NAMESPACE LiveView::LiveView(ConfigKey* settings, QObject* parent) : BluetoothWatch(QBluetoothAddress(settings->value("address").toString()), parent), _settings(settings->getSubkey(QString(), this)), + _24hMode(settings->value("24h-mode", false).toBool()), + _screenWidth(0), _screenHeight(0), _sendTimer(new QTimer(this)) { _sendTimer->setInterval(DelayBetweenMessages); connect(_sendTimer, SIGNAL(timeout()), SLOT(handleSendTimerTick())); - _24hMode = settings->value("24h-mode", false).toBool(); _buttons << "Select" << "Up" << "Down" << "Left" << "Right"; } LiveView::~LiveView() { - + if (_paintEngine) { + delete _paintEngine; + } } QPaintEngine* LiveView::paintEngine() const { - return 0; // TODO + if (!_paintEngine) { + _paintEngine = new LiveViewPaintEngine; + } + + return _paintEngine; } int LiveView::metric(PaintDeviceMetric metric) const { - return 0; // TODO + switch (metric) { + case PdmWidth: + return _screenWidth; + case PdmHeight: + return _screenHeight; + case PdmWidthMM: + return 24; + case PdmHeightMM: + return 24; + case PdmNumColors: + return 65536; + case PdmDepth: + return 16; + case PdmDpiX: + case PdmPhysicalDpiX: + return 136; + case PdmDpiY: + case PdmPhysicalDpiY: + return 136; + } + + return -1; } QString LiveView::model() const @@ -106,7 +135,28 @@ void LiveView::displayApplication() void LiveView::vibrate(int msecs) { + // TODO +} +QImage* LiveView::image() +{ + return &_image; +} + +void LiveView::renderImage(int x, int y, const QImage &image) +{ + QBuffer buffer; + buffer.open(QIODevice::WriteOnly); + if (image.save(&buffer, "PNG")) { + displayBitmap(x, y, buffer.buffer()); + } else { + qWarning() << "Failed to encode image"; + } +} + +void LiveView::clear() +{ + displayClear(); } void LiveView::setupBluetoothWatch() @@ -359,6 +409,17 @@ void LiveView::handleDateTimeRequest(const Message &msg) void LiveView::handleDisplayProperties(const Message &msg) { + if (msg.data.size() < 2) { + qWarning() << "Invalid DPR response"; + return; + } + + _screenWidth = msg.data[0]; + _screenHeight = msg.data[1]; + + // Recreate the display image + _image = QImage(_screenWidth, _screenHeight, QImage::Format_RGB16); + // For some reason firmware expects us to send this message right // after display properties // Otherwise the watch hangs up the connection diff --git a/liveview/liveview.h b/liveview/liveview.h index e150797..4a2e165 100644 --- a/liveview/liveview.h +++ b/liveview/liveview.h @@ -7,6 +7,8 @@ namespace sowatch { +class LiveViewPaintEngine; + class LiveView : public BluetoothWatch { Q_OBJECT @@ -39,6 +41,13 @@ public: void vibrate(int msecs); + // Only for application mode + QImage* image(); + /** Render a image in a certain position. */ + void renderImage(int x, int y, const QImage& image); + /** Clear the current display to black. */ + void clear(); + protected: static const int DelayBetweenMessages = 5; @@ -134,8 +143,14 @@ private: bool _24hMode : 1; + int _screenWidth; + int _screenHeight; QStringList _buttons; + // Required by QPaintDevice + mutable LiveViewPaintEngine* _paintEngine; + QImage _image; + /** Message outbox queue. */ QQueue _sendingMsgs; QTimer* _sendTimer; diff --git a/liveview/liveview.pro b/liveview/liveview.pro index 045c0eb..0e6c582 100644 --- a/liveview/liveview.pro +++ b/liveview/liveview.pro @@ -13,10 +13,12 @@ MOBILITY += connectivity systeminfo SOURCES += liveviewplugin.cpp \ liveviewscanner.cpp \ - liveview.cpp + liveview.cpp \ + liveviewpaintengine.cpp HEADERS += liveviewplugin.h \ liveviewscanner.h \ - liveview.h + liveview.h \ + liveviewpaintengine.h res_files.files += res/graphics res/fonts diff --git a/liveview/liveviewpaintengine.cpp b/liveview/liveviewpaintengine.cpp new file mode 100644 index 0000000..1785d62 --- /dev/null +++ b/liveview/liveviewpaintengine.cpp @@ -0,0 +1,98 @@ +#include "liveviewpaintengine.h" + +using namespace sowatch; + +LiveViewPaintEngine::LiveViewPaintEngine() : + WatchPaintEngine() +{ + +} + +bool LiveViewPaintEngine::begin(QPaintDevice *pdev) +{ + _watch = static_cast(pdev); + + return WatchPaintEngine::begin(_watch->image()); +} + +bool LiveViewPaintEngine::end() +{ + bool ret = WatchPaintEngine::end(); + if (ret) { + QRect rect = _damaged.boundingRect(); + if (!rect.isEmpty()) { + QImage sub_image = _watch->image()->copy(rect); + _watch->renderImage(rect.x(), rect.y(), sub_image); + } + } + return ret; +} + +void LiveViewPaintEngine::drawRects(const QRectF *rects, int rectCount) +{ + int i; + for (i = 0; i < rectCount; i++) { + const QRectF& r = rects[i]; + if (_hasBrush && fillsEntireImage(r.toRect()) && _isBrushBlack) { + _watch->clear(); + _damaged = QRegion(); + continue; + } + if (_hasBrush) { + damageRect(r); + } + if (_hasPen) { + damagePenStroke(QLineF(r.left(), r.top(), r.right(), r.top())); + damagePenStroke(QLineF(r.right(), r.top(), r.right(), r.bottom())); + damagePenStroke(QLineF(r.left(), r.bottom(), r.right(), r.bottom())); + damagePenStroke(QLineF(r.left(), r.top(), r.left(), r.bottom())); + } + } + _painter.drawRects(rects, rectCount); +} + +void LiveViewPaintEngine::drawRects(const QRect *rects, int rectCount) +{ + int i; + for (i = 0; i < rectCount; i++) { + const QRect& r = rects[i]; + if (_hasBrush && fillsEntireImage(r) && _isBrushBlack) { + _watch->clear(); + _damaged = QRegion(); + continue; + } + if (_hasBrush) { + damageRect(r); + } + if (_hasPen) { + damagePenStroke(QLine(r.left(), r.top(), r.right(), r.top())); + damagePenStroke(QLine(r.right(), r.top(), r.right(), r.bottom())); + damagePenStroke(QLine(r.left(), r.bottom(), r.right(), r.bottom())); + damagePenStroke(QLine(r.left(), r.top(), r.left(), r.bottom())); + } + } + + _painter.drawRects(rects, rectCount); +} + +void LiveViewPaintEngine::updateState(const QPaintEngineState &state) +{ + WatchPaintEngine::updateState(state); + if (state.state() & QPaintEngine::DirtyBrush) { + QBrush brush = state.brush(); + _isBrushBlack = false; + if (brush.style() == Qt::SolidPattern) { + const QColor color = brush.color(); + if (color == Qt::black) { + _isBrushBlack = true; + } + } + } +} + +bool LiveViewPaintEngine::fillsEntireImage(const QRect& rect) +{ + return rect == _area && + (!_clipEnabled || + (_clipRegion.numRects() == 1 && _clipRegion.rects().at(0) == _area)); +} diff --git a/liveview/liveviewpaintengine.h b/liveview/liveviewpaintengine.h new file mode 100644 index 0000000..f94c133 --- /dev/null +++ b/liveview/liveviewpaintengine.h @@ -0,0 +1,32 @@ +#ifndef LIVEVIEWPAINTENGINE_H +#define LIVEVIEWPAINTENGINE_H + +#include +#include "liveview.h" + +namespace sowatch +{ + +class LiveViewPaintEngine : public WatchPaintEngine +{ +public: + LiveViewPaintEngine(); + + bool begin(QPaintDevice *pdev); + bool end(); + + void drawRects(const QRectF *rects, int rectCount); + void drawRects(const QRect *rects, int rectCount); + + void updateState(const QPaintEngineState &state); + +protected: + bool fillsEntireImage(const QRect& rect); + + LiveView* _watch; + bool _isBrushBlack; +}; + +} + +#endif // LIVEVIEWPAINTENGINE_H diff --git a/liveview/liveviewplugin.cpp b/liveview/liveviewplugin.cpp index 0562f06..c7d862e 100644 --- a/liveview/liveviewplugin.cpp +++ b/liveview/liveviewplugin.cpp @@ -9,6 +9,7 @@ QTM_USE_NAMESPACE LiveViewPlugin::LiveViewPlugin() { + } LiveViewPlugin::~LiveViewPlugin() diff --git a/metawatch/metawatch.cpp b/metawatch/metawatch.cpp index 32e8707..c7e0db3 100644 --- a/metawatch/metawatch.cpp +++ b/metawatch/metawatch.cpp @@ -117,7 +117,7 @@ MetaWatch::~MetaWatch() QPaintEngine* MetaWatch::paintEngine() const { if (!_paintEngine) { - _paintEngine = new MetaWatchPaintEngine(const_cast(this)); + _paintEngine = new MetaWatchPaintEngine; } return _paintEngine; diff --git a/metawatch/metawatchpaintengine.cpp b/metawatch/metawatchpaintengine.cpp index 33f9490..cf41783 100644 --- a/metawatch/metawatchpaintengine.cpp +++ b/metawatch/metawatchpaintengine.cpp @@ -3,9 +3,8 @@ using namespace sowatch; -MetaWatchPaintEngine::MetaWatchPaintEngine(MetaWatch* watch) : - WatchPaintEngine(), - _watch(watch) +MetaWatchPaintEngine::MetaWatchPaintEngine() + : WatchPaintEngine() { } diff --git a/metawatch/metawatchpaintengine.h b/metawatch/metawatchpaintengine.h index ec5cf86..06cbdc1 100644 --- a/metawatch/metawatchpaintengine.h +++ b/metawatch/metawatchpaintengine.h @@ -1,9 +1,8 @@ #ifndef METAWATCHPAINTENGINE_H #define METAWATCHPAINTENGINE_H -#include +#include #include "metawatch.h" -#include "watchpaintengine.h" namespace sowatch { @@ -12,7 +11,7 @@ namespace sowatch class MetaWatchPaintEngine : public WatchPaintEngine { public: - explicit MetaWatchPaintEngine(MetaWatch* watch); + MetaWatchPaintEngine(); bool begin(QPaintDevice *pdev); bool end(); diff --git a/metawatchwatchlets/metawatchwatchletsplugin.cpp b/metawatchwatchlets/metawatchwatchletsplugin.cpp index 98c1713..601e659 100644 --- a/metawatchwatchlets/metawatchwatchletsplugin.cpp +++ b/metawatchwatchlets/metawatchwatchletsplugin.cpp @@ -40,6 +40,8 @@ Watchlet* MetaWatchWatchletsPlugin::getWatchlet(const QString& id, ConfigKey *se return new MetaWatchFaceWatchlet(watch); } else if (id == MetaWatchNotificationWatchlet::myId) { return new MetaWatchNotificationWatchlet(watch); + } else { + return 0; } } diff --git a/qmapwatchlet/mapview.cpp b/qmapwatchlet/mapview.cpp index 5972e07..3a0e902 100644 --- a/qmapwatchlet/mapview.cpp +++ b/qmapwatchlet/mapview.cpp @@ -265,7 +265,7 @@ void MapView::handleCurrentLocationNameSearchFinished() void MapView::handleCurrentLocationNameSearchError(QGeoSearchReply::Error error, const QString &errorString) { - qWarning() << "Current location name search error: " << errorString; + qWarning() << "Current location name search error: " << error << errorString; if (_searchReply) { _searchReply->deleteLater(); _searchReply = 0; 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