From c7a5d0490fa5f4e8579e6a8e0fbdc7baf7c34145 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Mon, 20 May 2013 16:43:33 -0300 Subject: Skeleton code for a non-blocking UI thread for downloading dives from the DC This is the skeleton code for a non-blocking ui-thread It already creates the first-thread ( 'do not block the ui' ) and the second thread ('download from the dive computer') We can in the future merge both in the same place - I didn't want to do that now because the download function is written in the libdivecomputer.c code, and I cant just transform that to a QThread and use signals, so I used two threads for that. Signed-off-by: Tomaz Canabrava --- qt-ui/downloadfromdivecomputer.cpp | 73 ++++++++++++++++++++++++ qt-ui/downloadfromdivecomputer.h | 46 ++++++++++++++++ qt-ui/downloadfromdivecomputer.ui | 110 +++++++++++++++++++++++++++++++++++++ qt-ui/mainwindow.cpp | 4 +- 4 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 qt-ui/downloadfromdivecomputer.cpp create mode 100644 qt-ui/downloadfromdivecomputer.h create mode 100644 qt-ui/downloadfromdivecomputer.ui (limited to 'qt-ui') diff --git a/qt-ui/downloadfromdivecomputer.cpp b/qt-ui/downloadfromdivecomputer.cpp new file mode 100644 index 000000000..8be4e6cbb --- /dev/null +++ b/qt-ui/downloadfromdivecomputer.cpp @@ -0,0 +1,73 @@ +#include "downloadfromdivecomputer.h" +#include "ui_downloadfromdivecomputer.h" + +#include "../libdivecomputer.h" + +#include +#include + +namespace DownloadFromDcGlobal{ + const char *err_string; +}; + +extern const char *progress_bar_text; +extern double progress_bar_fraction; + +DownloadFromDCWidget::DownloadFromDCWidget(QWidget* parent, Qt::WindowFlags f) : + QDialog(parent, f), ui(new Ui::DownloadFromDiveComputer), thread(0) +{ + ui->setupUi(this); + ui->progressBar->hide(); + ui->progressBar->setMinimum(0); + ui->progressBar->setMaximum(100); +} + +void DownloadFromDCWidget::on_cancel_clicked() +{ + close(); +} + +void DownloadFromDCWidget::on_ok_clicked() +{ + + ui->progressBar->setValue(0); + ui->progressBar->show(); + + if(thread){ + thread->deleteLater(); + } + + device_data_t data; + // still need to fill the data info here. + thread = new InterfaceThread(this, &data); + connect(thread, SIGNAL(updateInterface(int)), ui->progressBar, SLOT(setValue(int)), Qt::QueuedConnection); // Qt::QueuedConnection == threadsafe. + connect(thread, SIGNAL(updateInterface(int)), this, SLOT(setValue(int)), Qt::QueuedConnection); // Qt::QueuedConnection == threadsafe. + thread->start(); +} + +DownloadThread::DownloadThread(device_data_t* data): data(data) +{ +} + +void DownloadThread::run() +{ + do_libdivecomputer_import(data); + qDebug() << "Download thread started"; +} + +InterfaceThread::InterfaceThread(QObject* parent, device_data_t* data): QThread(parent), data(data) +{ + +} + +void InterfaceThread::run() +{ + DownloadThread *download = new DownloadThread(data); + + download->start(); + while(download->isRunning()){ + msleep(200); + updateInterface(progress_bar_fraction *100); + } + updateInterface(100); +} diff --git a/qt-ui/downloadfromdivecomputer.h b/qt-ui/downloadfromdivecomputer.h new file mode 100644 index 000000000..433d43779 --- /dev/null +++ b/qt-ui/downloadfromdivecomputer.h @@ -0,0 +1,46 @@ +#ifndef DOWNLOADFROMDIVECOMPUTER_H +#define DOWNLOADFROMDIVECOMPUTER_H +#include +#include + +namespace Ui{ + class DownloadFromDiveComputer; +} +struct device_data_t; + +class DownloadThread : public QThread{ + Q_OBJECT +public: + explicit DownloadThread(device_data_t* data); + virtual void run(); +private: + device_data_t *data; +}; + +class InterfaceThread : public QThread{ + Q_OBJECT +public: + InterfaceThread(QObject *parent, device_data_t *data) ; + virtual void run(); + +Q_SIGNALS: + void updateInterface(int value); +private: + device_data_t *data; +}; + +class DownloadFromDCWidget : public QDialog{ + Q_OBJECT +public: + explicit DownloadFromDCWidget(QWidget* parent = 0, Qt::WindowFlags f = 0); + +public slots: + void on_ok_clicked(); + void on_cancel_clicked(); +private: + Ui::DownloadFromDiveComputer *ui; + InterfaceThread *thread; +}; + + +#endif \ No newline at end of file diff --git a/qt-ui/downloadfromdivecomputer.ui b/qt-ui/downloadfromdivecomputer.ui new file mode 100644 index 000000000..924b886aa --- /dev/null +++ b/qt-ui/downloadfromdivecomputer.ui @@ -0,0 +1,110 @@ + + + DownloadFromDiveComputer + + + + 0 + 0 + 331 + 199 + + + + Form + + + + + + Vendor + + + + + + + Dive Computer + + + + + + + + + + + + + Device or Mount Point + + + + + + + + + + ... + + + + + + + Force download of all dives + + + + + + + Always prefer downloaded dives + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + OK + + + + + + + Cancel + + + + + + + + + 24 + + + + + + + + diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index d7320ceeb..30629ce95 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -25,6 +25,7 @@ #include "../pref.h" #include "modeldelegates.h" #include "models.h" +#include "downloadfromdivecomputer.h" static MainWindow* instance = 0; @@ -160,7 +161,8 @@ void MainWindow::on_actionQuit_triggered() void MainWindow::on_actionDownloadDC_triggered() { - qDebug("actionDownloadDC"); + DownloadFromDCWidget* downloadWidget = new DownloadFromDCWidget(); + downloadWidget->show(); } void MainWindow::on_actionDownloadWeb_triggered() -- cgit v1.2.3-70-g09d2