Add abort button to Import process

This commit is contained in:
Mark Watkins 2018-05-07 11:57:58 +10:00
parent c4ac37d4ae
commit 0f639eecbc
7 changed files with 52 additions and 3 deletions

View File

@ -513,6 +513,7 @@ int PRS1Loader::OpenMachine(const QString & path)
if (!dir.exists() || (!dir.isReadable())) {
return 0;
}
m_abort = false;
emit updateMessage(QObject::tr("Getting Ready..."));
QCoreApplication::processEvents();
@ -648,6 +649,7 @@ int PRS1Loader::OpenMachine(const QString & path)
// Scan for individual session files
for (int i = 0; i < flist.size(); i++) {
if (isAborted()) break;
QFileInfo fi = flist.at(i);
QString ext_s = fi.fileName().section(".", -1);
@ -706,6 +708,8 @@ int PRS1Loader::OpenMachine(const QString & path)
QList<PRS1DataChunk *> Chunks = ParseFile(fi.canonicalFilePath());
for (int i=0; i < Chunks.size(); ++i) {
if (isAborted()) break;
PRS1DataChunk * chunk = Chunks.at(i);
if (ext <= 1) {
@ -752,6 +756,7 @@ int PRS1Loader::OpenMachine(const QString & path)
}
}
}
if (isAborted()) break;
}

View File

@ -1454,6 +1454,8 @@ int ResmedLoader::scanFiles(Machine * mach, const QString & datalog_path)
qDebug() << "Starting EDF duration scan pass";
for (int i=0; i < totalfiles; ++i) {
if (isAborted()) return 0;
const QFileInfo & fi = EDFfiles.at(i);
// Update progress bar
@ -2304,6 +2306,8 @@ int ResmedLoader::Open(const QString & dirpath)
return -1;
}
m_abort = false;
///////////////////////////////////////////////////////////////////////////////////
// Parse Identification.tgt file (containing serial number and machine information)
///////////////////////////////////////////////////////////////////////////////////
@ -2595,7 +2599,10 @@ int ResmedLoader::Open(const QString & dirpath)
emit updateMessage(QObject::tr("Searching for EDF Files..."));
QApplication::processEvents();
if (isAborted()) return 0;
scanFiles(mach, newpath);
if (isAborted()) return 0;
// Now at this point we have resdayList populated with processable summary and EDF files data
// that can be processed in threads..
@ -2604,6 +2611,8 @@ int ResmedLoader::Open(const QString & dirpath)
QApplication::processEvents();
for (auto rdi=resdayList.begin(), rend=resdayList.end(); rdi != rend; rdi++) {
if (isAborted()) return 0;
QDate date = rdi.key();
ResMedDay & resday = rdi.value();

View File

@ -215,7 +215,7 @@ void MachineLoader::runTasks(bool threaded)
threaded=AppSetting->multithreading();
if (!threaded) {
while (!m_tasklist.isEmpty()) {
while (!m_tasklist.isEmpty() && !m_abort) {
ImportTask * task = m_tasklist.takeFirst();
task->run();
@ -223,13 +223,14 @@ void MachineLoader::runTasks(bool threaded)
m_currenttask++;
qprogress->setValue(m_currenttask);
QApplication::processEvents();
delete task;
}
} else {
ImportTask * task = m_tasklist[0];
QThreadPool * threadpool = QThreadPool::globalInstance();
while (true) {
while (!m_abort) {
if (threadpool->tryStart(task)) {
m_tasklist.pop_front();
@ -251,6 +252,13 @@ void MachineLoader::runTasks(bool threaded)
}
QThreadPool::globalInstance()->waitForDone(-1);
}
if (m_abort) {
// delete remaining tasks and clear task list
for (auto & task : m_tasklist) {
delete task;
}
m_tasklist.clear();
}
}

View File

@ -106,6 +106,8 @@ class MachineLoader: public QObject
}
return genericPixmapPath;
}
public slots:
void abortImport() { abort(); }
signals:
void updateProgress(int cnt, int total);

View File

@ -24,10 +24,14 @@ ProgressDialog::ProgressDialog(QWidget * parent):
hlayout->addWidget(waitmsg,1,Qt::AlignCenter);
vlayout->addWidget(progress,1);
progress->setMaximum(100);
abortButton = nullptr;
}
ProgressDialog::~ProgressDialog()
{
if (abortButton) {
disconnect(abortButton, SIGNAL(released()), this, SLOT(onAbortClicked()));
}
}
void ProgressDialog::doUpdateProgress(int cnt, int total)
@ -40,3 +44,15 @@ void ProgressDialog::doUpdateProgress(int cnt, int total)
void ProgressDialog::setMessage(QString msg) {
waitmsg->setText(msg); update();
}
void ProgressDialog::addAbortButton()
{
abortButton = new QPushButton(tr("Abort"),this);
connect(abortButton, SIGNAL(released()), this, SLOT(onAbortClicked()));
hlayout->addWidget(abortButton);
}
void ProgressDialog::onAbortClicked()
{
emit abortClicked();
}

View File

@ -14,6 +14,7 @@
#include <QVBoxLayout>
#include <QLabel>
#include <QProgressBar>
#include <QPushButton>
class ProgressDialog:public QDialog {
Q_OBJECT
@ -21,17 +22,22 @@ public:
explicit ProgressDialog(QWidget * parent);
virtual ~ProgressDialog();
void addAbortButton();
void setPixmap(QPixmap &pixmap) { imglabel->setPixmap(pixmap); }
QProgressBar * progress;
public slots:
void setMessage(QString msg);
void doUpdateProgress(int cnt, int total);
void onAbortClicked();
signals:
void abortClicked();
protected:
QLabel * waitmsg;
QHBoxLayout *hlayout;
QLabel * imglabel;
QVBoxLayout * vlayout;
QPushButton * abortButton;
};

View File

@ -566,12 +566,14 @@ int MainWindow::importCPAP(ImportPath import, const QString &message)
QProgressBar *saveQprogress = qprogress;
qprogress = progdlg->progress;
progdlg->addAbortButton();
progdlg->setWindowModality(Qt::ApplicationModal);
progdlg->open();
progdlg->setMessage(message);
connect(import.loader, SIGNAL(updateMessage(QString)), progdlg, SLOT(setMessage(QString)));
connect(progdlg, SIGNAL(abortClicked()), import.loader, SLOT(abortImport()));
int c = import.loader->Open(import.path);
@ -582,6 +584,7 @@ int MainWindow::importCPAP(ImportPath import, const QString &message)
} else {
Notify(tr("Couldn't find any valid Machine Data at\n\n%1").arg(import.path),tr("Import Problem"));
}
disconnect(progdlg, SIGNAL(abortClicked()), import.loader, SLOT(abortImport()));
disconnect(import.loader, SIGNAL(updateMessage(QString)), progdlg, SLOT(setMessage(QString)));
progdlg->hide();