From e1436f3961f256ac0b075d42bb04566e69d0a6bb Mon Sep 17 00:00:00 2001 From: Teuniz Date: Sun, 9 Oct 2016 16:59:15 +0200 Subject: [PATCH] Move the code for downloading waveform data into a separate thread in order to keep the gui responsive. --- dsremote.pro | 2 ++ global.h | 2 +- mainwindow.h | 2 ++ save_data.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/dsremote.pro b/dsremote.pro index 76a5582..7607a01 100644 --- a/dsremote.pro +++ b/dsremote.pro @@ -55,6 +55,7 @@ HEADERS += settings_dialog.h HEADERS += screen_thread.h HEADERS += lan_connect_thread.h HEADERS += read_settings_thread.h +HEADERS += save_data_thread.h HEADERS += third_party/kiss_fft/kiss_fft.h HEADERS += third_party/kiss_fft/_kiss_fft_guts.h @@ -78,6 +79,7 @@ SOURCES += settings_dialog.cpp SOURCES += screen_thread.cpp SOURCES += lan_connect_thread.cpp SOURCES += read_settings_thread.cpp +SOURCES += save_data_thread.cpp SOURCES += third_party/kiss_fft/kiss_fft.c SOURCES += third_party/kiss_fft/kiss_fftr.c diff --git a/global.h b/global.h index e0a92d4..c090994 100644 --- a/global.h +++ b/global.h @@ -35,7 +35,7 @@ #define PROGRAM_NAME "DSRemote" -#define PROGRAM_VERSION "0.32_1610022021" +#define PROGRAM_VERSION "0.32_1610091654" #define MAX_PATHLEN 4096 diff --git a/mainwindow.h b/mainwindow.h index 309adb3..57b276b 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -64,6 +64,7 @@ #include #include #include +#include #include #include @@ -86,6 +87,7 @@ #include "screen_thread.h" #include "lan_connect_thread.h" #include "read_settings_thread.h" +#include "save_data_thread.h" #include "third_party/kiss_fft/kiss_fftr.h" diff --git a/save_data.cpp b/save_data.cpp index 11f6f43..9a438c7 100644 --- a/save_data.cpp +++ b/save_data.cpp @@ -55,14 +55,27 @@ void UI_Mainwindow::save_screenshot() tmc_write(":DISP:DATA?"); - QApplication::setOverrideCursor(Qt::WaitCursor); + save_data_thread get_data_thrd(0); - qApp->processEvents(); + QMessageBox w_msg_box; + w_msg_box.setIcon(QMessageBox::NoIcon); + w_msg_box.setText("Downloading data..."); + w_msg_box.setStandardButtons(QMessageBox::Abort); - n = tmc_read(); + connect(&get_data_thrd, SIGNAL(finished()), &w_msg_box, SLOT(accept())); - QApplication::restoreOverrideCursor(); + get_data_thrd.start(); + if(w_msg_box.exec() != QDialog::Accepted) + { + disconnect(&get_data_thrd, 0, 0, 0); + strcpy(str, "Aborted by user."); + goto OUT_ERROR; + } + + disconnect(&get_data_thrd, 0, 0, 0); + + n = get_data_thrd.get_num_bytes_rcvd(); if(n < 0) { strcpy(str, "Can not read from device."); @@ -150,6 +163,13 @@ void UI_Mainwindow::save_screenshot() OUT_ERROR: + if(get_data_thrd.isFinished() != true) + { + connect(&get_data_thrd, SIGNAL(finished()), &w_msg_box, SLOT(accept())); + w_msg_box.setText("Waiting for thread to finish, please wait..."); + w_msg_box.exec(); + } + scrn_timer->start(devparms.screentimerival); QMessageBox msgBox; @@ -183,6 +203,10 @@ void UI_Mainwindow::save_memory_waveform() double yinc[MAX_CHNS]; + QEventLoop ev_loop; + + save_data_thread get_data_thrd(0); + if(device == NULL) { return; @@ -203,7 +227,9 @@ void UI_Mainwindow::save_memory_waveform() QProgressDialog progress("Downloading data...", "Abort", 0, mempnts, this); progress.setWindowModality(Qt::WindowModal); - progress.setMinimumDuration(100); + progress.setMinimumDuration(0); + + connect(&get_data_thrd, SIGNAL(finished()), &ev_loop, SLOT(quit())); statusLabel->setText("Downloading data..."); @@ -335,8 +361,6 @@ void UI_Mainwindow::save_memory_waveform() { progress.setValue(bytes_rcvd); - qApp->processEvents(); - if(progress.wasCanceled()) { strcpy(str, "Canceled"); @@ -366,8 +390,11 @@ void UI_Mainwindow::save_memory_waveform() tmc_write(":WAV:DATA?"); - n = tmc_read(); + get_data_thrd.start(); + ev_loop.exec(); + + n = get_data_thrd.get_num_bytes_rcvd(); if(n < 0) { sprintf(str, "Can not read from device. line %i file %s", __LINE__, __FILE__); @@ -551,6 +578,8 @@ void UI_Mainwindow::save_memory_waveform() OUT_NORMAL: + disconnect(&get_data_thrd, 0, 0, 0); + if(hdl >= 0) { edfclose_file(hdl); @@ -567,6 +596,8 @@ OUT_NORMAL: OUT_ERROR: + disconnect(&get_data_thrd, 0, 0, 0); + progress.reset(); statusLabel->setText("Downloading aborted"); @@ -576,6 +607,18 @@ OUT_ERROR: edfclose_file(hdl); } + if(get_data_thrd.isRunning() == true) + { + QMessageBox w_msg_box; + w_msg_box.setIcon(QMessageBox::NoIcon); + w_msg_box.setText("Waiting for thread to finish, please wait..."); + w_msg_box.setStandardButtons(QMessageBox::Abort); + + connect(&get_data_thrd, SIGNAL(finished()), &w_msg_box, SLOT(accept())); + + w_msg_box.exec(); + } + if(progress.wasCanceled() == false) { QMessageBox msgBox; @@ -688,6 +731,13 @@ void UI_Mainwindow::save_screen_waveform() wavbuf[2] = NULL; wavbuf[3] = NULL; + save_data_thread get_data_thrd(0); + + QMessageBox w_msg_box; + w_msg_box.setIcon(QMessageBox::NoIcon); + w_msg_box.setText("Downloading data..."); + w_msg_box.setStandardButtons(QMessageBox::Abort); + scrn_timer->stop(); scrn_thread->wait(); @@ -803,14 +853,20 @@ void UI_Mainwindow::save_screen_waveform() tmc_write(":WAV:DATA?"); - QApplication::setOverrideCursor(Qt::WaitCursor); + connect(&get_data_thrd, SIGNAL(finished()), &w_msg_box, SLOT(accept())); - qApp->processEvents(); + get_data_thrd.start(); - n = tmc_read(); + if(w_msg_box.exec() != QDialog::Accepted) + { + disconnect(&get_data_thrd, 0, 0, 0); + strcpy(str, "Aborted by user."); + goto OUT_ERROR; + } - QApplication::restoreOverrideCursor(); + disconnect(&get_data_thrd, 0, 0, 0); + n = get_data_thrd.get_num_bytes_rcvd(); if(n < 0) { strcpy(str, "Can not read from device."); @@ -929,6 +985,13 @@ OUT_NORMAL: OUT_ERROR: + if(get_data_thrd.isFinished() != true) + { + connect(&get_data_thrd, SIGNAL(finished()), &w_msg_box, SLOT(accept())); + w_msg_box.setText("Waiting for thread to finish, please wait..."); + w_msg_box.exec(); + } + QMessageBox msgBox; msgBox.setIcon(QMessageBox::Critical); msgBox.setText(str);