From 356a80a448559df80cfc13d7aeae9eeb3e147afa Mon Sep 17 00:00:00 2001 From: Andy Bryson Date: Fri, 28 Dec 2012 11:05:48 +0000 Subject: [PATCH] Remove Boost Dependency - Devices All devices no longer need boost (use signals and slots instead), other than SRM. --- src/Device.cpp | 8 +++++++- src/Device.h | 31 ++++++++++++++++--------------- src/DownloadRideDialog.cpp | 17 ++++++++--------- src/DownloadRideDialog.h | 7 +++++-- src/JouleDevice.cpp | 28 +++++++++++++--------------- src/JouleDevice.h | 9 ++++----- src/MacroDevice.cpp | 22 ++++++++++------------ src/MacroDevice.h | 8 +++----- src/ManualRideDialog.cpp | 1 - src/PowerTapDevice.cpp | 26 ++++++++++++-------------- src/PowerTapDevice.h | 8 +++----- 11 files changed, 81 insertions(+), 84 deletions(-) diff --git a/src/Device.cpp b/src/Device.cpp index d06ab3625..2491d078f 100644 --- a/src/Device.cpp +++ b/src/Device.cpp @@ -17,6 +17,7 @@ */ #include "Device.h" +#include "assert.h" typedef QMap DevicesMap; @@ -39,7 +40,6 @@ Device::~Device() bool Device::preview( QString &err ) { - (void) statusCallback; (void) err; return true; @@ -60,6 +60,12 @@ Device::cleanup( QString &err ) return false; } +void +Device::cancelled() +{ + m_Cancelled = true; +} + QList Devices::typeNames() diff --git a/src/Device.h b/src/Device.h index 04a765ddd..8745a3e3d 100644 --- a/src/Device.h +++ b/src/Device.h @@ -21,7 +21,7 @@ #include "GoldenCheetah.h" #include "CommPort.h" -#include +#include #include struct DeviceDownloadFile @@ -46,20 +46,16 @@ struct DeviceRideItem }; typedef QSharedPointer DeviceRideItemPtr; -struct Device; +class Device; typedef QSharedPointer DevicePtr; -struct Device +class Device : public QObject { - Q_DECLARE_TR_FUNCTIONS(Device) + Q_OBJECT - public: - typedef boost::function CancelCallback; - typedef boost::function StatusCallback; - typedef boost::function ProgressCallback; - - Device( CommPortPtr dev, StatusCallback cb ) - : dev( dev ), statusCallback( cb ) +public: + Device( CommPortPtr dev ) + : dev( dev ), m_Cancelled( false ) {}; virtual ~Device(); @@ -68,16 +64,21 @@ struct Device virtual bool download( const QDir &tmpdir, QList &files, - CancelCallback cancelCallback, - ProgressCallback progressCallback, QString &err) = 0; virtual bool cleanup( QString &err ); +signals: + void updateStatus( QString statusText ); + void updateProgress( QString progressText ); + +public slots: + virtual void cancelled(); + protected: QList rideList; CommPortPtr dev; - StatusCallback statusCallback; + bool m_Cancelled; }; struct Devices; @@ -86,7 +87,7 @@ typedef QSharedPointer DevicesPtr; struct Devices { virtual ~Devices() {} - virtual DevicePtr newDevice( CommPortPtr, Device::StatusCallback ) = 0; + virtual DevicePtr newDevice( CommPortPtr ) = 0; // port *might* be supported by this device type implementation: virtual bool supportsPort( CommPortPtr dev ) { (void)dev; return true; }; diff --git a/src/DownloadRideDialog.cpp b/src/DownloadRideDialog.cpp index 13d6bc8f5..407cf0837 100644 --- a/src/DownloadRideDialog.cpp +++ b/src/DownloadRideDialog.cpp @@ -25,7 +25,6 @@ #include #include #include -#include DownloadRideDialog::DownloadRideDialog(MainWindow *mainWindow, const QDir &home) : @@ -283,8 +282,11 @@ DownloadRideDialog::downloadClicked() QList files; DevicesPtr devtype = Devices::getType(deviceCombo->currentText()); - DevicePtr device = devtype->newDevice( dev, - boost::bind(&DownloadRideDialog::updateStatus, this, _1) ); + DevicePtr device = devtype->newDevice( dev ); + + connect( device.data(), SIGNAL(updateStatus(QString)), this, SLOT(updateStatus(QString))); + connect( this, SIGNAL(cancel()), device.data(), SLOT(cancelled()) ); + connect( device.data(), SIGNAL(updateProgress(QString)), this, SLOT(updateProgress(QString))); updateStatus(tr("getting summary ...")); if( ! device->preview( err ) ){ @@ -303,10 +305,7 @@ DownloadRideDialog::downloadClicked() } updateStatus(tr("getting data ...")); - if (!device->download( home, files, - boost::bind(&DownloadRideDialog::isCancelled, this), - boost::bind(&DownloadRideDialog::updateProgress, this, _1), - err)) + if (!device->download( home, files, err)) { if (cancelled) { QMessageBox::information(this, tr("Download canceled"), @@ -415,8 +414,7 @@ DownloadRideDialog::eraseClicked() } assert(dev); DevicesPtr devtype = Devices::getType(deviceCombo->currentText()); - DevicePtr device = devtype->newDevice( dev, - boost::bind(&DownloadRideDialog::updateStatus, this, _1) ); + DevicePtr device = devtype->newDevice( dev ); QString err; if( device->cleanup( err) ) @@ -438,6 +436,7 @@ DownloadRideDialog::cancelClicked() default: cancelled = true; + emit cancel(); break; } } diff --git a/src/DownloadRideDialog.h b/src/DownloadRideDialog.h index 37026c550..645b43b99 100644 --- a/src/DownloadRideDialog.h +++ b/src/DownloadRideDialog.h @@ -35,8 +35,9 @@ class DownloadRideDialog : public QDialog DownloadRideDialog(MainWindow *mainWindow, const QDir &home); bool isCancelled(); - void updateStatus(const QString &statusText); - void updateProgress(const QString &progressText); + + signals: + void cancel(); private slots: void downloadClicked(); @@ -46,6 +47,8 @@ class DownloadRideDialog : public QDialog void setReadyInstruct(); void scanCommPorts(); void deviceChanged(QString); + void updateStatus(const QString &statusText); + void updateProgress(const QString &progressText); private: diff --git a/src/JouleDevice.cpp b/src/JouleDevice.cpp index d74de6ee4..f11cea296 100644 --- a/src/JouleDevice.cpp +++ b/src/JouleDevice.cpp @@ -49,9 +49,9 @@ JouleDevices::downloadInstructions() const } DevicePtr -JouleDevices::newDevice( CommPortPtr dev, Device::StatusCallback cb ) +JouleDevices::newDevice( CommPortPtr dev ) { - return DevicePtr( new JouleDevice( dev, cb )); + return DevicePtr( new JouleDevice( dev )); } static QString @@ -113,8 +113,6 @@ readOneByOne(CommPortPtr dev, void *buf, size_t nbyte, QString &err) bool JouleDevice::download( const QDir &tmpdir, QList &files, - CancelCallback cancelCallback, - ProgressCallback progressCallback, QString &err) { if (JOULE_DEBUG) printf("download Joule 1.0 or GPS"); @@ -137,14 +135,14 @@ JouleDevice::download( const QDir &tmpdir, } bool isJouleGPS = getJouleGPS(versionResponse); - statusCallback(QString("Joule %1 indentified").arg(isJouleGPS?"GPS":"1.0")); + emit updateStatus(QString("Joule %1 indentified").arg(isJouleGPS?"GPS":"1.0")); QList trainings; if (!getDownloadableRides(trainings, isJouleGPS, err)) return false; for (int i=0; i255?trainings.at(i).id-255:trainings.at(i).id); int id2 = (trainings.at(i).id>255?trainings.at(i).id%255:0); @@ -156,7 +154,7 @@ JouleDevice::download( const QDir &tmpdir, if (!request.write(dev, err)) return false; - if (cancelCallback()) + if(m_Cancelled) { err = "download cancelled"; return false; @@ -276,7 +274,7 @@ JouleDevice::download( const QDir &tmpdir, bool JouleDevice::getUnitVersion(JoulePacket &response, QString &err) { - statusCallback("Get Unit Software Version..."); + emit updateStatus("Get Unit Software Version..."); if (JOULE_DEBUG) printf("Get Unit Software Version\n"); JoulePacket request(READ_UNIT_VERSION); @@ -295,7 +293,7 @@ JouleDevice::getUnitVersion(JoulePacket &response, QString &err) data_version = qByteArray2Int(response.payload.right(2)); QString version = QString(minor_version<100?"%1.0%2 (%3)":"%1.%2 (%3)").arg(major_version).arg(minor_version).arg(data_version); - statusCallback("Version"+version); + emit updateStatus("Version"+version); return true; } } @@ -305,7 +303,7 @@ JouleDevice::getUnitVersion(JoulePacket &response, QString &err) bool JouleDevice::getSystemInfo(JoulePacket &response, QString &err) { - statusCallback("Get System info..."); + emit updateStatus("Get System info..."); if (JOULE_DEBUG) printf("Get System info\n"); JoulePacket request(READ_SYSTEM_INFO); @@ -329,7 +327,7 @@ JouleDevice::getSystemInfo(JoulePacket &response, QString &err) bool JouleDevice::getUnitFreeSpace(QString &memory, QString &err) { - statusCallback("Get Unit Free Space..."); + emit updateStatus("Get Unit Free Space..."); if (JOULE_DEBUG) printf("Get Unit Free Space\n"); JoulePacket request1(GET_FREE_SPACE); @@ -354,7 +352,7 @@ JouleDevice::getUnitFreeSpace(QString &memory, QString &err) bool JouleDevice::getDownloadableRides(QList &rides, bool isJouleGPS, QString &err) { - statusCallback("Read ride summary..."); + emit updateStatus("Read ride summary..."); if (JOULE_DEBUG) printf("Read ride summary\n"); JoulePacket request(READ_RIDE_SUMMARY); @@ -386,7 +384,7 @@ JouleDevice::getDownloadableRides(QList &rides, bool isJou rides.append(ride); } } - statusCallback(QString("%1 detailled rides").arg(rides.count())); + emit updateStatus(QString("%1 detailled rides").arg(rides.count())); return true; } return false; @@ -394,7 +392,7 @@ JouleDevice::getDownloadableRides(QList &rides, bool isJou bool JouleDevice::cleanup( QString &err ) { - statusCallback("Erase all records on computer"); + emit updateStatus("Erase all records on computer"); if (JOULE_DEBUG) printf("Erase all records on computer\n"); if (!dev->open(err)) { @@ -412,7 +410,7 @@ JouleDevice::cleanup( QString &err ) { return false; for (int i=0; i255?trainings.at(i).id-255:trainings.at(i).id); int id2 = (trainings.at(i).id>255?trainings.at(i).id%255:0); diff --git a/src/JouleDevice.h b/src/JouleDevice.h index 6faea5614..cf59fa72f 100644 --- a/src/JouleDevice.h +++ b/src/JouleDevice.h @@ -3,6 +3,7 @@ #include "CommPort.h" #include "Device.h" +#include "stdint.h" class DeviceFileInfo; class JoulePacket; @@ -12,7 +13,7 @@ struct JouleDevices : public Devices Q_DECLARE_TR_FUNCTIONS(JouleDevices) public: - virtual DevicePtr newDevice( CommPortPtr dev, Device::StatusCallback cb ); + virtual DevicePtr newDevice( CommPortPtr dev ); virtual QString downloadInstructions() const; virtual bool canCleanup( void ) {return true; }; }; @@ -22,13 +23,11 @@ struct JouleDevice : public Device Q_DECLARE_TR_FUNCTIONS(JouleDevice) public: - JouleDevice( CommPortPtr dev, StatusCallback cb ) : - Device( dev, cb ) {}; + JouleDevice( CommPortPtr dev ) : + Device( dev ) {}; virtual bool download( const QDir &tmpdir, QList &files, - CancelCallback cancelCallback, - ProgressCallback progressCallback, QString &err); virtual bool cleanup( QString &err ); diff --git a/src/MacroDevice.cpp b/src/MacroDevice.cpp index 41f0d855c..99cc18434 100644 --- a/src/MacroDevice.cpp +++ b/src/MacroDevice.cpp @@ -51,9 +51,9 @@ MacroDevices::downloadInstructions() const } DevicePtr -MacroDevices::newDevice( CommPortPtr dev, Device::StatusCallback cb ) +MacroDevices::newDevice( CommPortPtr dev ) { - return DevicePtr( new MacroDevice( dev, cb )); + return DevicePtr( new MacroDevice( dev )); } static QString @@ -93,8 +93,6 @@ hexHex2Int(char c, char c2) bool MacroDevice::download( const QDir &tmpdir, QList &files, - CancelCallback cancelCallback, - ProgressCallback progressCallback, QString &err) { if (MACRO_DEBUG) printf("download O-Synce Macro"); @@ -104,7 +102,7 @@ MacroDevice::download( const QDir &tmpdir, return false; } - statusCallback("Request number of training..."); + emit updateStatus("Request number of training..."); if (MACRO_DEBUG) printf("Request number of training\n"); MacroPacket cmd(NUMBER_OF_TRAINING_REQUESTS); @@ -112,7 +110,7 @@ MacroDevice::download( const QDir &tmpdir, if (!cmd.write(dev, err)) return false; - if (cancelCallback()) + if(m_Cancelled) { err = "download cancelled"; return false; @@ -143,7 +141,7 @@ MacroDevice::download( const QDir &tmpdir, return false; } - if (cancelCallback()) + if(m_Cancelled) { err = "download cancelled"; return false; @@ -187,10 +185,10 @@ MacroDevice::download( const QDir &tmpdir, for (int i = 0; i < count; i++) { if (MACRO_DEBUG) printf("Request training %d\n",i); - statusCallback( QString("Request datas of training %1 / %2...") + emit updateStatus( QString("Request datas of training %1 / %2...") .arg(i+1).arg((int)count) ); - if (cancelCallback()) + if(m_Cancelled) { err = "download cancelled"; return false; @@ -200,7 +198,7 @@ MacroDevice::download( const QDir &tmpdir, cmd.addToPayload(i); if (!cmd.write(dev, err)) return false; - if (cancelCallback()) + if(m_Cancelled) { err = "download cancelled"; return false; @@ -222,11 +220,11 @@ MacroDevice::download( const QDir &tmpdir, //int training_flag = hex2Int(response2.payload.at(43)); tmp.write(response2.dataArray()); - progressCallback( QString("training %1/%2... (%3 Bytes)") + emit updateProgress( QString("training %1/%2... (%3 Bytes)") .arg(i+1) .arg((int)count) .arg(tmp.size()) ); - if (cancelCallback()) + if(m_Cancelled) { err = "download cancelled"; return false; diff --git a/src/MacroDevice.h b/src/MacroDevice.h index 463bcf57f..6ab865a1b 100644 --- a/src/MacroDevice.h +++ b/src/MacroDevice.h @@ -11,7 +11,7 @@ struct MacroDevices : public Devices Q_DECLARE_TR_FUNCTIONS(MacroDevices) public: - virtual DevicePtr newDevice( CommPortPtr dev, Device::StatusCallback cb ); + virtual DevicePtr newDevice( CommPortPtr dev ); virtual QString downloadInstructions() const; virtual bool canCleanup( void ) {return true; }; }; @@ -21,13 +21,11 @@ struct MacroDevice : public Device Q_DECLARE_TR_FUNCTIONS(MacroDevice) public: - MacroDevice( CommPortPtr dev, StatusCallback cb ) : - Device( dev, cb ) {}; + MacroDevice( CommPortPtr dev ) : + Device( dev ) {}; virtual bool download( const QDir &tmpdir, QList &files, - CancelCallback cancelCallback, - ProgressCallback progressCallback, QString &err); virtual bool cleanup( QString &err ); diff --git a/src/ManualRideDialog.cpp b/src/ManualRideDialog.cpp index c9523f8f9..c90c20769 100644 --- a/src/ManualRideDialog.cpp +++ b/src/ManualRideDialog.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include "Units.h" #include "MetricAggregator.h" diff --git a/src/PowerTapDevice.cpp b/src/PowerTapDevice.cpp index b017457d9..123916f4a 100644 --- a/src/PowerTapDevice.cpp +++ b/src/PowerTapDevice.cpp @@ -19,6 +19,7 @@ #include "PowerTapDevice.h" #include "PowerTapUtil.h" #include +#include "assert.h" #define PT_DEBUG false @@ -26,9 +27,9 @@ static bool powerTapRegistered = Devices::addType("PowerTap", DevicesPtr(new PowerTapDevices()) ); DevicePtr -PowerTapDevices::newDevice( CommPortPtr dev, Device::StatusCallback cb) +PowerTapDevices::newDevice( CommPortPtr dev ) { - return DevicePtr( new PowerTapDevice( dev, cb )); + return DevicePtr( new PowerTapDevice( dev )); } QString @@ -122,8 +123,6 @@ readUntilNewline(CommPortPtr dev, char *buf, int len, QString &err) bool PowerTapDevice::download( const QDir &tmpdir, QList &files, - CancelCallback cancelCallback, - ProgressCallback progressCallback, QString &err) { if (!dev->open(err)) { @@ -141,9 +140,9 @@ PowerTapDevice::download( const QDir &tmpdir, if (!doWrite(dev, 0x56, false, err)) // 'V' return false; - statusCallback( "Reading version..." ); - if (cancelCallback()) { - err = "download cancelled"; + emit updateStatus( "Reading version..." ); + if(m_Cancelled) { + err = "download cancelled"; return false; } @@ -173,8 +172,8 @@ PowerTapDevice::download( const QDir &tmpdir, bool hwecho = version.indexOf('V') < veridx; if (PT_DEBUG) printf("hwecho=%s\n", hwecho ? "true" : "false"); - statusCallback( "Reading header..." ); - if (cancelCallback()) { + emit updateStatus( "Reading header..." ); + if(m_Cancelled) { err = "download cancelled"; return false; } @@ -199,8 +198,8 @@ PowerTapDevice::download( const QDir &tmpdir, for (size_t i = 0; i < sizeof(header); ++i) records.append(header[i]); - statusCallback( "Reading ride data..." ); - if (cancelCallback()) { + emit updateStatus( "Reading ride data..." ); + if(m_Cancelled) { err = "download cancelled"; return false; } @@ -265,11 +264,11 @@ PowerTapDevice::download( const QDir &tmpdir, } if (recIntSecs != 0.0) { int min = (int) round(records.size() / 6 * recIntSecs); - progressCallback( QString("progress: %1:%2") + emit updateProgress( QString("progress: %1:%2") .arg(min / 60) .arg(min % 60, 2, 10, QLatin1Char('0'))); } - if (cancelCallback()) { + if(m_Cancelled){ err = "download cancelled"; return false; } @@ -328,4 +327,3 @@ PowerTapDevice::download( const QDir &tmpdir, files << file; return true; } - diff --git a/src/PowerTapDevice.h b/src/PowerTapDevice.h index f8cb48733..a76d794ea 100644 --- a/src/PowerTapDevice.h +++ b/src/PowerTapDevice.h @@ -27,7 +27,7 @@ struct PowerTapDevices : public Devices Q_DECLARE_TR_FUNCTIONS(PowerTapDevices) public: - virtual DevicePtr newDevice( CommPortPtr dev, Device::StatusCallback cb ); + virtual DevicePtr newDevice( CommPortPtr dev ); virtual QString downloadInstructions() const; }; @@ -36,13 +36,11 @@ struct PowerTapDevice : public Device Q_DECLARE_TR_FUNCTIONS(PowerTapDevices) public: - PowerTapDevice( CommPortPtr dev, StatusCallback cb ) : - Device( dev, cb ) {}; + PowerTapDevice( CommPortPtr dev ) : + Device( dev ) {}; virtual bool download( const QDir &tmpdir, QList &files, - CancelCallback cancelCallback, - ProgressCallback progressCallback, QString &err); };