From 4fc16b16749a73b8c06d41cb7cb22b78c77ab29e Mon Sep 17 00:00:00 2001 From: "Joseph W. Joshua" Date: Tue, 10 Jun 2014 15:03:26 +0300 Subject: Move divecomputer configuration code to different files This splits the code in configuredivecomputer.cpp into multiple files. The read and write threads are moved to configuredivecomputerthreads.h/cpp, and the device details class is moved to devicedetails.h/.cpp Signed-off-by: Joseph W. Joshua Signed-off-by: Thiago Macieira --- qt-ui/configuredivecomputer.cpp | 141 ++------------------------------- qt-ui/configuredivecomputer.h | 40 ++-------- qt-ui/configuredivecomputerdialog.cpp | 36 +++------ qt-ui/configuredivecomputerdialog.h | 13 ++- qt-ui/configuredivecomputerdialog.ui | 127 +++++++++++++++++++++++------ qt-ui/configuredivecomputerthreads.cpp | 81 +++++++++++++++++++ qt-ui/configuredivecomputerthreads.h | 42 ++++++++++ qt-ui/devicedetails.cpp | 88 ++++++++++++++++++++ qt-ui/devicedetails.h | 54 +++++++++++++ subsurface.pro | 6 +- 10 files changed, 403 insertions(+), 225 deletions(-) create mode 100644 qt-ui/configuredivecomputerthreads.cpp create mode 100644 qt-ui/configuredivecomputerthreads.h create mode 100644 qt-ui/devicedetails.cpp create mode 100644 qt-ui/devicedetails.h diff --git a/qt-ui/configuredivecomputer.cpp b/qt-ui/configuredivecomputer.cpp index 8cc3a6953..d24c8905d 100644 --- a/qt-ui/configuredivecomputer.cpp +++ b/qt-ui/configuredivecomputer.cpp @@ -4,19 +4,21 @@ ConfigureDiveComputer::ConfigureDiveComputer(QObject *parent) : QObject(parent), readThread(0), - writeThread(0) + writeThread(0), + m_deviceDetails(0) { setState(INITIAL); } -void ConfigureDiveComputer::readSettings(device_data_t *data) +void ConfigureDiveComputer::readSettings(DeviceDetails *deviceDetails, device_data_t *data) { setState(READING); + m_deviceDetails = deviceDetails; if (readThread) readThread->deleteLater(); - readThread = new ReadSettingsThread(this, data); + readThread = new ReadSettingsThread(this, deviceDetails, data); connect (readThread, SIGNAL(finished()), this, SLOT(readThreadFinished()), Qt::QueuedConnection); connect (readThread, SIGNAL(error(QString)), this, SLOT(setError(QString))); @@ -24,19 +26,9 @@ void ConfigureDiveComputer::readSettings(device_data_t *data) readThread->start(); } -void ConfigureDiveComputer::setDeviceName(device_data_t *data, QString newName) +void ConfigureDiveComputer::saveDeviceDetails() { - writeSettingToDevice(data, "Name", newName); -} - -void ConfigureDiveComputer::setDeviceDateAndTime(device_data_t *data, QDateTime dateAndTime) -{ - writeSettingToDevice(data, "DateAndTime", dateAndTime); -} -void ConfigureDiveComputer::setDeviceBrightness(device_data_t *data, int brighnessLevel) -{ - writeSettingToDevice(data, "Brightness", brighnessLevel); } void ConfigureDiveComputer::setState(ConfigureDiveComputer::states newState) @@ -45,20 +37,6 @@ void ConfigureDiveComputer::setState(ConfigureDiveComputer::states newState) emit stateChanged(currentState); } -void ConfigureDiveComputer::writeSettingToDevice(device_data_t *data, QString settingName, QVariant settingValue) -{ - setState(READING); - - if (writeThread) - writeThread->deleteLater(); - - writeThread = new WriteSettingsThread(this, data, settingName, settingValue); - connect (writeThread, SIGNAL(error(QString)), this, SLOT(setError(QString))); - connect (writeThread, SIGNAL(finished()), this, SLOT(writeThreadFinished())); - - writeThread->start(); -} - void ConfigureDiveComputer::setError(QString err) { lastError = err; @@ -68,7 +46,7 @@ void ConfigureDiveComputer::setError(QString err) void ConfigureDiveComputer::readThreadFinished() { setState(DONE); - emit deviceSettings(readThread->result); + emit readFinished(); } void ConfigureDiveComputer::writeThreadFinished() @@ -79,108 +57,3 @@ void ConfigureDiveComputer::writeThreadFinished() emit message(tr("Setting successfully written to device")); } } - -ReadSettingsThread::ReadSettingsThread(QObject *parent, device_data_t *data) - : QThread(parent), data(data) -{ - -} - -void ReadSettingsThread::run() -{ - dc_status_t rc; - rc = dc_device_open(&data->device, data->context, data->descriptor, data->devname); - if (rc == DC_STATUS_SUCCESS) { - if (dc_device_get_type(data->device) == DC_FAMILY_HW_OSTC3) { - unsigned char hw_data[10]; - hw_ostc3_device_version(data->device, hw_data, 10); - QTextStream (&result) << "Device Version: " << hw_data; //just a test. I will work on decoding this - } else { - lastError = tr("This feature is not yet available for the selected dive computer."); - emit error(lastError); - } - dc_device_close(data->device); - } else { - lastError = tr("Could not a establish connection to the dive computer."); - emit error(lastError); - } -} - -WriteSettingsThread::WriteSettingsThread(QObject *parent, device_data_t *data, QString settingName, QVariant settingValue) - : QThread(parent), data(data), m_settingName(settingName), m_settingValue(settingValue) -{ - -} - -void WriteSettingsThread::run() -{ - bool supported = false; - dc_status_t rc; - rc = dc_device_open(&data->device, data->context, data->descriptor, data->devname); - if (rc == DC_STATUS_SUCCESS) { - dc_status_t result; - if (m_settingName == "Name") { - switch (dc_device_get_type(data->device)) { - case DC_FAMILY_HW_OSTC3: - supported = true; - result = hw_ostc3_device_customtext(data->device, m_settingValue.toByteArray().data()); - break; - case DC_FAMILY_HW_FROG: - supported = true; - result = hw_frog_device_customtext(data->device, m_settingValue.toByteArray().data()); - break; - } - } - if (m_settingName == "DateAndTime") { - QDateTime timeToSet = m_settingValue.toDateTime(); - dc_datetime_t time; - time.year = timeToSet.date().year(); - time.month = timeToSet.date().month(); - time.day = timeToSet.date().day(); - time.hour = timeToSet.time().hour(); - time.minute = timeToSet.time().minute(); - time.second = timeToSet.time().second(); - - switch (dc_device_get_type(data->device)) { - case DC_FAMILY_HW_OSTC3: - supported = true; - result = hw_ostc3_device_clock(data->device, &time); - break; - case DC_FAMILY_HW_OSTC: - supported = true; - result = hw_ostc_device_clock(data->device, &time); - break; - case DC_FAMILY_HW_FROG: - supported = true; - result = hw_frog_device_clock(data->device, &time); - break; - } - } - if (m_settingName == "Brightness") { - switch (dc_device_get_type(data->device)) { - case DC_FAMILY_HW_OSTC3: - qDebug() << "Brightness"; - supported = true; - unsigned char packet[1] = { m_settingValue.toInt() }; - result = hw_ostc3_device_config_write(data->device, 0x2D, packet, sizeof (packet)); - break; - } - } - qDebug() << result; - if (result != DC_STATUS_SUCCESS) { - qDebug() << result; - lastError = tr("An error occurred while sending data to the dive computer."); - //Todo Update this message to change depending on actual result. - - emit error(lastError); - } - dc_device_close(data->device); - } else { - lastError = tr("Could not a establish connection to the dive computer."); - emit error(lastError); - } - if (!supported) { - lastError = tr("This feature is not yet available for the selected dive computer."); - emit error(lastError); - } -} diff --git a/qt-ui/configuredivecomputer.h b/qt-ui/configuredivecomputer.h index 08c4e190f..65ceaa780 100644 --- a/qt-ui/configuredivecomputer.h +++ b/qt-ui/configuredivecomputer.h @@ -5,41 +5,15 @@ #include #include #include "libdivecomputer.h" +#include "configuredivecomputerthreads.h" #include -class ReadSettingsThread : public QThread { - Q_OBJECT -public: - ReadSettingsThread(QObject *parent, device_data_t *data); - virtual void run(); - QString result; - QString lastError; -signals: - void error(QString err); -private: - device_data_t *data; -}; - -class WriteSettingsThread : public QThread { - Q_OBJECT -public: - WriteSettingsThread(QObject *parent, device_data_t *data, QString settingName, QVariant settingValue); - virtual void run(); - QString result; - QString lastError; -signals: - void error(QString err); -private: - device_data_t *data; - QString m_settingName; - QVariant m_settingValue; -}; class ConfigureDiveComputer : public QObject { Q_OBJECT public: explicit ConfigureDiveComputer(QObject *parent = 0); - void readSettings(device_data_t *data); + void readSettings(DeviceDetails *deviceDetails, device_data_t *data); enum states { INITIAL, @@ -53,23 +27,23 @@ public: QString lastError; states currentState; + DeviceDetails *m_deviceDetails; + device_data_t *m_data; + void saveDeviceDetails(); + void fetchDeviceDetails(); - void setDeviceName(device_data_t *data, QString newName); - void setDeviceDateAndTime(device_data_t *data, QDateTime dateAndTime); - void setDeviceBrightness(device_data_t *data, int brighnessLevel); signals: - void deviceSettings(QString settings); void message(QString msg); void error(QString err); void readFinished(); void writeFinished(); void stateChanged(states newState); + private: ReadSettingsThread *readThread; WriteSettingsThread *writeThread; void setState(states newState); - void writeSettingToDevice(device_data_t *data, QString settingName, QVariant settingValue); private slots: void readThreadFinished(); void writeThreadFinished(); diff --git a/qt-ui/configuredivecomputerdialog.cpp b/qt-ui/configuredivecomputerdialog.cpp index 1d5b1d7f8..8ae250468 100644 --- a/qt-ui/configuredivecomputerdialog.cpp +++ b/qt-ui/configuredivecomputerdialog.cpp @@ -33,14 +33,16 @@ ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(QWidget *parent) : ui(new Ui::ConfigureDiveComputerDialog), config(0), vendorModel(0), - productModel(0) + productModel(0), + deviceDetails(0) { ui->setupUi(this); + deviceDetails = new DeviceDetails(this); config = new ConfigureDiveComputer(this); connect (config, SIGNAL(error(QString)), this, SLOT(configError(QString))); connect (config, SIGNAL(message(QString)), this, SLOT(configMessage(QString))); - connect (config, SIGNAL(deviceSettings(QString)), ui->availableDetails, SLOT(setText(QString))); + connect (config, SIGNAL(readFinished()), this, SLOT(deviceReadFinished())); fill_computer_list(); @@ -157,7 +159,7 @@ void ConfigureDiveComputerDialog::readSettings() ui->errorLabel->clear(); getDeviceData(); - config->readSettings(&device_data); + config->readSettings(deviceDetails, &device_data); } void ConfigureDiveComputerDialog::configMessage(QString msg) @@ -181,6 +183,8 @@ void ConfigureDiveComputerDialog::getDeviceData() set_default_dive_computer(device_data.vendor, device_data.product); set_default_dive_computer_device(device_data.devname); + + //deviceDetails->setData(&device_data); } void ConfigureDiveComputerDialog::on_cancel_clicked() @@ -188,31 +192,13 @@ void ConfigureDiveComputerDialog::on_cancel_clicked() this->close(); } -void ConfigureDiveComputerDialog::on_setDeviceName_clicked() +void ConfigureDiveComputerDialog::deviceReadFinished() { - ui->statusLabel->clear(); - ui->errorLabel->clear(); - ui->availableDetails->clear(); - - QString newDeviceName = QInputDialog::getText(this, tr("Set device name"), tr("Enter the new name for this device:")); - if (newDeviceName.length() > 0) { - getDeviceData(); - config->setDeviceName(&device_data, newDeviceName); - } + ui->brightnessComboBox->setCurrentIndex(config->m_deviceDetails->brightness()); } -void ConfigureDiveComputerDialog::on_setDateAndTime_clicked() +void ConfigureDiveComputerDialog::on_saveSettingsPushButton_clicked() { - ui->statusLabel->clear(); - ui->errorLabel->clear(); - ui->availableDetails->clear(); - - getDeviceData(); - config->setDeviceDateAndTime(&device_data, QDateTime::currentDateTime()); + config->saveDeviceDetails(); } -void ConfigureDiveComputerDialog::on_setBrightnessButton_clicked() -{ - getDeviceData(); - config->setDeviceBrightness(&device_data, ui->brightnessComboBox->currentIndex()); -} diff --git a/qt-ui/configuredivecomputerdialog.h b/qt-ui/configuredivecomputerdialog.h index 7c4243ab8..5a8bf63e1 100644 --- a/qt-ui/configuredivecomputerdialog.h +++ b/qt-ui/configuredivecomputerdialog.h @@ -4,8 +4,7 @@ #include #include #include "../libdivecomputer.h" - -class ConfigureDiveComputer; +#include "configuredivecomputer.h" namespace Ui { class ConfigureDiveComputerDialog; @@ -28,12 +27,8 @@ private slots: void configMessage(QString msg); void configError(QString err); void on_cancel_clicked(); - void on_setDeviceName_clicked(); - - void on_setDateAndTime_clicked(); - - void on_setBrightnessButton_clicked(); - + void deviceReadFinished(); + void on_saveSettingsPushButton_clicked(); private: Ui::ConfigureDiveComputerDialog *ui; @@ -49,6 +44,8 @@ private: QStringListModel *productModel; void fill_computer_list(); void fill_device_list(int dc_type); + + DeviceDetails *deviceDetails; }; #endif // CONFIGUREDIVECOMPUTERDIALOG_H diff --git a/qt-ui/configuredivecomputerdialog.ui b/qt-ui/configuredivecomputerdialog.ui index cb4dacb23..d7c584e0c 100644 --- a/qt-ui/configuredivecomputerdialog.ui +++ b/qt-ui/configuredivecomputerdialog.ui @@ -6,8 +6,8 @@ 0 0 - 362 - 375 + 343 + 365 @@ -91,33 +91,16 @@ - - - - - - - Set Device Name - - - - - - - Set Date && Time - - - - + Brightness: - + @@ -136,15 +119,90 @@ - - + + + + true + + + + + + + + English + + + + + German + + + + + French + + + + + Italian + + + + + + + + + + + Language: + + + + + + + Serial No. + + + + + + + Custom Text: + + + + + - ... + Firmware Version: + + + + + + + true + + + + Qt::Vertical + + + + 20 + 40 + + + + @@ -164,6 +222,13 @@ + + + + Save Chages to Device + + + @@ -188,6 +253,20 @@ + + vendor + product + device + search + retrieveDetails + serialNoLineEdit + firmwareVersionLineEdit + customTextLlineEdit + brightnessComboBox + languageComboBox + saveSettingsPushButton + cancel + diff --git a/qt-ui/configuredivecomputerthreads.cpp b/qt-ui/configuredivecomputerthreads.cpp new file mode 100644 index 000000000..f1ac3b5d9 --- /dev/null +++ b/qt-ui/configuredivecomputerthreads.cpp @@ -0,0 +1,81 @@ +#include "configuredivecomputerthreads.h" +#include "libdivecomputer/hw.h" +#include + +ReadSettingsThread::ReadSettingsThread(QObject *parent, DeviceDetails *deviceDetails, device_data_t *data) + : QThread(parent), m_deviceDetails(deviceDetails), m_data(data) +{ + +} + +void ReadSettingsThread::run() +{ + bool supported = false; + dc_status_t rc; + rc = rc = dc_device_open(&m_data->device, m_data->context, m_data->descriptor, m_data->devname); + if (rc == DC_STATUS_SUCCESS) { + switch (dc_device_get_type(m_data->device)) { + case DC_FAMILY_HW_OSTC3: + supported = true; + m_deviceDetails->setBrightness(0); + m_deviceDetails->setCustomText(""); + m_deviceDetails->setDateFormat(0); + m_deviceDetails->setDiveModeColor(0); + m_deviceDetails->setFirmwareVersion(""); + m_deviceDetails->setLanguage(0); + m_deviceDetails->setLastDeco(0); + m_deviceDetails->setSerialNo(""); + unsigned char uData[1]; + rc = hw_ostc3_device_config_read(m_data->device, 0x2D, uData, sizeof(uData)); + if (rc == DC_STATUS_SUCCESS) + m_deviceDetails->setBrightness(uData[0]); + rc = hw_ostc3_device_config_read(m_data->device, 0x32, uData, sizeof(uData)); + if (rc == DC_STATUS_SUCCESS) + m_deviceDetails->setLanguage(uData[0]); + rc = hw_ostc3_device_config_read(m_data->device, 0x33, uData, sizeof(uData)); + if (rc == DC_STATUS_SUCCESS) + m_deviceDetails->setDateFormat(uData[0]); + break; + + } + dc_device_close(m_data->device); + + if (!supported) { + lastError = tr("This feature is not yet available for the selected dive computer."); + emit error(lastError); + } + } + else { + lastError = tr("Could not a establish connection to the dive computer."); + emit error(lastError); + } +} + +WriteSettingsThread::WriteSettingsThread(QObject *parent, DeviceDetails *deviceDetails, QString settingName, QVariant settingValue) + : QThread(parent), m_deviceDetails(deviceDetails), m_settingName(settingName), m_settingValue(settingValue) +{ + +} + +void WriteSettingsThread::run() +{ + bool supported = false; + dc_status_t rc; + + switch (dc_device_get_type(data->device)) { + case DC_FAMILY_HW_OSTC3: + rc = dc_device_open(&data->device, data->context, data->descriptor, data->devname); + if (rc == DC_STATUS_SUCCESS) { + + } else { + lastError = tr("Could not a establish connection to the dive computer."); + emit error(lastError); + } + break; + + if (!supported) { + lastError = tr("This feature is not yet available for the selected dive computer."); + emit error(lastError); + } + } +} diff --git a/qt-ui/configuredivecomputerthreads.h b/qt-ui/configuredivecomputerthreads.h new file mode 100644 index 000000000..c684cddaa --- /dev/null +++ b/qt-ui/configuredivecomputerthreads.h @@ -0,0 +1,42 @@ +#ifndef CONFIGUREDIVECOMPUTERTHREADS_H +#define CONFIGUREDIVECOMPUTERTHREADS_H + +#include +#include +#include +#include "libdivecomputer.h" +#include +#include "devicedetails.h" + +class ReadSettingsThread : public QThread { + Q_OBJECT +public: + ReadSettingsThread(QObject *parent, DeviceDetails *deviceDetails, device_data_t *data); + virtual void run(); + QString result; + QString lastError; +signals: + void error(QString err); +private: + DeviceDetails *m_deviceDetails; + device_data_t *m_data; +}; + +class WriteSettingsThread : public QThread { + Q_OBJECT +public: + WriteSettingsThread(QObject *parent, DeviceDetails *deviceDetails, QString settingName, QVariant settingValue); + virtual void run(); + QString result; + QString lastError; +signals: + void error(QString err); +private: + device_data_t *data; + QString m_settingName; + QVariant m_settingValue; + + DeviceDetails *m_deviceDetails; +}; + +#endif // CONFIGUREDIVECOMPUTERTHREADS_H diff --git a/qt-ui/devicedetails.cpp b/qt-ui/devicedetails.cpp new file mode 100644 index 000000000..e5a90b9da --- /dev/null +++ b/qt-ui/devicedetails.cpp @@ -0,0 +1,88 @@ +#include "devicedetails.h" + +DeviceDetails::DeviceDetails(QObject *parent) : + QObject(parent) +{ + +} +device_data_t *DeviceDetails::data() const +{ + return m_data; +} + +void DeviceDetails::setData(device_data_t *data) +{ + m_data = data; +} +QString DeviceDetails::serialNo() const +{ + return m_serialNo; +} + +void DeviceDetails::setSerialNo(const QString &serialNo) +{ + m_serialNo = serialNo; +} +QString DeviceDetails::firmwareVersion() const +{ + return m_firmwareVersion; +} + +void DeviceDetails::setFirmwareVersion(const QString &firmwareVersion) +{ + m_firmwareVersion = firmwareVersion; +} +QString DeviceDetails::customText() const +{ + return m_customText; +} + +void DeviceDetails::setCustomText(const QString &customText) +{ + m_customText = customText; +} +int DeviceDetails::brightness() const +{ + return m_brightness; +} + +void DeviceDetails::setBrightness(int brightness) +{ + m_brightness = brightness; +} +int DeviceDetails::diveModeColor() const +{ + return m_diveModeColor; +} + +void DeviceDetails::setDiveModeColor(int diveModeColor) +{ + m_diveModeColor = diveModeColor; +} +int DeviceDetails::language() const +{ + return m_language; +} + +void DeviceDetails::setLanguage(int language) +{ + m_language = language; +} +int DeviceDetails::dateFormat() const +{ + return m_dateFormat; +} + +void DeviceDetails::setDateFormat(int dateFormat) +{ + m_dateFormat = dateFormat; +} +int DeviceDetails::lastDeco() const +{ + return m_lastDeco; +} + +void DeviceDetails::setLastDeco(int lastDeco) +{ + m_lastDeco = lastDeco; +} diff --git a/qt-ui/devicedetails.h b/qt-ui/devicedetails.h new file mode 100644 index 000000000..9f5e7c1dd --- /dev/null +++ b/qt-ui/devicedetails.h @@ -0,0 +1,54 @@ +#ifndef DEVICEDETAILS_H +#define DEVICEDETAILS_H + +#include +#include +#include "libdivecomputer.h" + +class DeviceDetails : public QObject +{ + Q_OBJECT +public: + explicit DeviceDetails(QObject *parent = 0); + + device_data_t *data() const; + void setData(device_data_t *data); + + QString serialNo() const; + void setSerialNo(const QString &serialNo); + + QString firmwareVersion() const; + void setFirmwareVersion(const QString &firmwareVersion); + + QString customText() const; + void setCustomText(const QString &customText); + + int brightness() const; + void setBrightness(int brightness); + + int diveModeColor() const; + void setDiveModeColor(int diveModeColor); + + int language() const; + void setLanguage(int language); + + int dateFormat() const; + void setDateFormat(int dateFormat); + + int lastDeco() const; + void setLastDeco(int lastDeco); + +private: + device_data_t *m_data; + QString m_serialNo; + QString m_firmwareVersion; + QString m_customText; + int m_brightness; + int m_diveModeColor; + int m_language; + int m_dateFormat; + int m_lastDeco; +}; + + +#endif // DEVICEDETAILS_H diff --git a/subsurface.pro b/subsurface.pro index b532e3c99..8711ab505 100644 --- a/subsurface.pro +++ b/subsurface.pro @@ -89,6 +89,8 @@ HEADERS = \ subsurfacesysinfo.h \ qt-ui/configuredivecomputerdialog.h \ qt-ui/configuredivecomputer.h \ + qt-ui/configuredivecomputerthreads.h \ + qt-ui/devicedetails.h android: HEADERS -= \ qt-ui/usermanual.h \ @@ -169,7 +171,9 @@ SOURCES = \ qt-ui/usersurvey.cpp \ subsurfacesysinfo.cpp \ qt-ui/configuredivecomputerdialog.cpp \ - qt-ui/configuredivecomputer.cpp + qt-ui/configuredivecomputer.cpp \ + qt-ui/configuredivecomputerthreads.cpp \ + qt-ui/devicedetails.cpp android: SOURCES += android.cpp else: linux*: SOURCES += linux.c -- cgit v1.2.3-70-g09d2