From b9b1829dbc50534190c8b81f91ee477af6971834 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Sun, 14 Sep 2014 14:11:26 +0200 Subject: addign watch properties, starting notification work --- src/controller.cpp | 152 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 133 insertions(+), 19 deletions(-) (limited to 'src/controller.cpp') diff --git a/src/controller.cpp b/src/controller.cpp index 9d1961c..a5ff22d 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -4,9 +4,12 @@ #include "controller.h" static const QLatin1String setting_address("address"); -static const QLatin1String setting_num_pages("num-pages"); -static const QLatin1String setting_page("page%1"); -static const QLatin1String setting_page_widget("widget%1"); +static const QLatin1String setting_cur_page("cur-page"); +static const QLatin1String setting_24h_mode("24h-mode"); +static const QLatin1String setting_ddmm_mode("ddmm-mode"); +static const QLatin1String setting_seconds("show-seconds"); +static const QLatin1String setting_separation_lines("show-separation-lines"); +static const QLatin1String setting_auto_backlight("auto-backlight"); Controller::Controller(const QString &settingsPrefix, QQuickView *view, QObject *parent) : QObject(parent), @@ -14,22 +17,39 @@ Controller::Controller(const QString &settingsPrefix, QQuickView *view, QObject _metawatch(0), _reconnect(new ReconnectTimer(this)), _widgets(new WidgetInfoModel(settingsPrefix, this)), + _curMode(MetaWatch::WatchModeIdle), _curPage(0), _batteryCharge(0), _batteryCharging(false) { _settings->setPath(settingsPrefix); - _metawatch = new MetaWatch(_settings->value(setting_address).toString(), this); + _curPage = _settings->value(setting_cur_page).toInt(); connect(_settings, &MDConfGroup::valueChanged, this, &Controller::handleSettingChanged); - connect(_metawatch, &MetaWatch::connected, _reconnect, &ReconnectTimer::stop); - connect(_metawatch, &MetaWatch::connected, this, &Controller::handleMetaWatchConnected); - connect(_metawatch, &MetaWatch::disconnected, _reconnect, &ReconnectTimer::scheduleNextAttempt); - connect(_metawatch, &MetaWatch::batteryStatus, this, &Controller::handleMetaWatchBatteryStatus); - connect(_reconnect, &ReconnectTimer::tryReconnect, _metawatch, &MetaWatch::connectDevice); - connect(_recc) + connect(_widgets, &QAbstractItemModel::dataChanged, this, &Controller::handleWidgetChanged); - _reconnect->scheduleNextAttempt(); + connectToAddress(_settings->value(setting_address).toString()); +} + +Controller::~Controller() +{ +} + +MetaWatch::WatchMode Controller::mode() const +{ + return _curMode; +} + +int Controller::page() const +{ + return _curPage; +} - reloadPages(); +void Controller::setPage(int page) +{ + if (page != _curPage) { + _curPage = page; + _metawatch->updateLcdDisplayPage(_curPage); + emit pageChanged(); + } } int Controller::batteryCharge() const @@ -42,22 +62,79 @@ bool Controller::batteryCharging() const return _batteryCharging; } -void Controller::reloadPages() +void Controller::connectToAddress(const QString &address) { + if (_metawatch) { + if (address == _address) { + // Connecting to the same address and there's already a watch object created + return; + } + + _reconnect->stop(); + disconnect(_metawatch, 0, this, 0); + disconnect(_metawatch, 0, _reconnect, 0); + delete _metawatch; + _metawatch = 0; + } + + _address = address; + + if (_address.isEmpty()) { + qDebug() << "Empty address, doing nothing except wasting memory"; + return; + } + + _metawatch = new MetaWatch(_address, this); + connect(_metawatch, &MetaWatch::connected, _reconnect, &ReconnectTimer::stop); + connect(_metawatch, &MetaWatch::connected, this, &Controller::handleMetaWatchConnected); + connect(_metawatch, &MetaWatch::disconnected, _reconnect, &ReconnectTimer::scheduleNextAttempt); + connect(_metawatch, &MetaWatch::modeChange, this, &Controller::handleMetaWatchModeChange); + connect(_metawatch, &MetaWatch::batteryStatus, this, &Controller::handleMetaWatchBatteryStatus); + connect(_reconnect, &ReconnectTimer::tryReconnect, _metawatch, &MetaWatch::connectDevice); + _reconnect->scheduleNextAttempt(); } -void Controller::switchToPage(int page) +void Controller::updateProperties() { + MetaWatch::WatchProperties props; + + if (_settings->value(setting_24h_mode).toBool()) { + props |= MetaWatch::WatchPropertyHourFormat24h; + } + + if (_settings->value(setting_ddmm_mode).toBool()) { + props |= MetaWatch::WatchPropertyDateFormatDDMM; + } + + if (_settings->value(setting_seconds).toBool()) { + props |= MetaWatch::WatchPropertyShowSeconds; + } + + if (_settings->value(setting_separation_lines).toBool()) { + props |= MetaWatch::WatchPropertyShowSeparationLines; + } + + if (_settings->value(setting_auto_backlight).toBool()) { + props |= MetaWatch::WatchPropertyAutoBacklight; + } + _metawatch->configure(props); } void Controller::handleSettingChanged(const QString &key) { qDebug() << "Setting" << key << "changed"; - if (key == setting_num_pages) { - _settings->value(setting_num_pages, 0); - switchToPage(0); + if (key == setting_address) { + connectToAddress(_settings->value(setting_address).toString()); + } else if (key == setting_cur_page) { + int page = _settings->value(setting_cur_page).toInt(); + if (_curPage != page) { + setPage(page); + } + } else if (key == setting_24h_mode || key == setting_ddmm_mode || key == setting_seconds + || key == setting_separation_lines || key == setting_auto_backlight) { + updateProperties(); } } @@ -68,9 +145,33 @@ void Controller::handleMetaWatchConnected() _metawatch->updateDeviceType(); _metawatch->updateBatteryStatus(); _metawatch->setDateTime(QDateTime::currentDateTime()); - _metawatch->updateLcdDisplay(); + _metawatch->updateWidgetList(_widgets->toList()); + _metawatch->updateLcdDisplayPage(_curPage); + + _curMode = MetaWatch::WatchModeIdle; + + updateProperties(); +} - // TODO _metawatch->updateWidgetList(_view->widgets()); +void Controller::handleMetaWatchModeChange(MetaWatch::WatchMode mode, int page) +{ + switch (mode) { + case MetaWatch::WatchModeIdle: + if (_curMode != mode) { + _curMode = mode; + emit modeChanged(); + } + if (page != _curPage) { + _curPage = page; + _settings->setValue(setting_cur_page, QVariant::fromValue(_curPage)); + emit pageChanged(); + } + + break; + default: + qWarning() << "Unhandled metawatch mode:" << mode; + break; + } } void Controller::handleMetaWatchBatteryStatus(bool charging, int charge) @@ -84,3 +185,16 @@ void Controller::handleMetaWatchBatteryStatus(bool charging, int charge) emit batteryChargeChanged(); } } + +void Controller::handleWidgetChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) +{ + if (roles.indexOf(WidgetInfoModel::UrlRole) >= 0) { + // Can't send partial updates to watch, so entire list must be + // resubmitted everytime something important changes + _metawatch->updateWidgetList(_widgets->toList()); + + if (_widgets->data(topLeft, WidgetInfoModel::PageRole).toInt() == _curPage) { + _metawatch->updateLcdDisplayPage(_curPage); + } + } +} -- cgit v1.2.3