From 2077dd06f2e28c32719ef1127f310de50e6d9ccc Mon Sep 17 00:00:00 2001 From: lordmulder Date: Fri, 3 Feb 2012 15:28:00 +0100 Subject: [PATCH] Added support for running multiple jobs in parallel. --- src/win_main.cpp | 65 ++++++++++++++++++++++++++--------------- src/win_main.h | 4 +-- src/win_preferences.cpp | 2 +- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/win_main.cpp b/src/win_main.cpp index 2bd6037..df5493b 100644 --- a/src/win_main.cpp +++ b/src/win_main.cpp @@ -154,7 +154,7 @@ void MainWindow::addButtonPressed(const QString &filePath, bool *ok) if(ok) *ok = false; AddJobDialog *addDialog = new AddJobDialog(this, m_options, m_x64supported); - addDialog->setRunImmediately(!havePendingJobs()); + addDialog->setRunImmediately(countPendingJobs() < m_preferences.maxRunningJobCount); if(!filePath.isEmpty()) addDialog->setSourceFile(filePath); int result = addDialog->exec(); @@ -266,7 +266,7 @@ void MainWindow::jobChangedData(const QModelIndex &topLeft, const QModelIndex & { int selected = jobsView->currentIndex().row(); - if(topLeft.column() <= 1 && bottomRight.column() >= 1) + if(topLeft.column() <= 1 && bottomRight.column() >= 1) /*STATUS*/ { for(int i = topLeft.row(); i <= bottomRight.row(); i++) { @@ -276,13 +276,13 @@ void MainWindow::jobChangedData(const QModelIndex &topLeft, const QModelIndex & qDebug("Current job changed status!"); updateButtons(status); } - if((status == EncodeThread::JobStatus_Completed) && m_preferences.autoRunNextJob) + if((status == EncodeThread::JobStatus_Completed || status == EncodeThread::JobStatus_Failed) && m_preferences.autoRunNextJob) { QTimer::singleShot(0, this, SLOT(launchNextJob())); } } } - else if(topLeft.column() <= 2 && bottomRight.column() >= 2) + else if(topLeft.column() <= 2 && bottomRight.column() >= 2) /*PROGRESS*/ { for(int i = topLeft.row(); i <= bottomRight.row(); i++) { @@ -293,7 +293,7 @@ void MainWindow::jobChangedData(const QModelIndex &topLeft, const QModelIndex & } } } - else if(topLeft.column() <= 3 && bottomRight.column() >= 3) + else if(topLeft.column() <= 3 && bottomRight.column() >= 3) /*DETAILS*/ { for(int i = topLeft.row(); i <= bottomRight.row(); i++) { @@ -368,27 +368,44 @@ void MainWindow::showPreferences(void) void MainWindow::launchNextJob(void) { const int rows = m_jobList->rowCount(QModelIndex()); + unsigned int running = countRunningJobs(); - if(haveRunningJobs()) + if(running >= m_preferences.maxRunningJobCount) { - qWarning("Still have a job running, won't launch next yet!"); + qWarning("Still have too many jobs running, won't launch next one yet!"); return; } - for(int i = 0; i < rows; i++) + bool first = true; + while(running < m_preferences.maxRunningJobCount) { - EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex())); - if(status == EncodeThread::JobStatus_Enqueued) + bool ok = false; + for(int i = 0; i < rows; i++) { - if(m_jobList->startJob(m_jobList->index(i, 0, QModelIndex()))) + EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex())); + if(status == EncodeThread::JobStatus_Enqueued) { - jobsView->selectRow(i); + if(m_jobList->startJob(m_jobList->index(i, 0, QModelIndex()))) + { + if(first) + { + first = false; + jobsView->selectRow(i); + } + running++; + ok = true; + qApp->processEvents(QEventLoop::ExcludeUserInputEvents); + break; + } } - return; + } + + if(!ok) + { + qWarning("No enqueued jobs left!"); + break; } } - - qWarning("No enqueued jobs left!"); } void MainWindow::init(void) @@ -502,14 +519,14 @@ void MainWindow::showEvent(QShowEvent *e) void MainWindow::closeEvent(QCloseEvent *e) { - if(haveRunningJobs()) + if(countRunningJobs() > 0) { e->ignore(); QMessageBox::warning(this, tr("Jobs Are Running"), tr("Sorry, can not exit while there still are running jobs!")); return; } - if(havePendingJobs()) + if(countPendingJobs() > 0) { int ret = QMessageBox::question(this, tr("Jobs Are Pending"), tr("Do you really want to quit and discard the pending jobs?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No); if(ret != QMessageBox::Yes) @@ -598,8 +615,9 @@ void MainWindow::dropEvent(QDropEvent *event) /////////////////////////////////////////////////////////////////////////////// /*Jobs that are not completed (or failed, or aborted) yet*/ -bool MainWindow::havePendingJobs(void) +unsigned int MainWindow::countPendingJobs(void) { + unsigned int count = 0; const int rows = m_jobList->rowCount(QModelIndex()); for(int i = 0; i < rows; i++) @@ -607,16 +625,17 @@ bool MainWindow::havePendingJobs(void) EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex())); if(status != EncodeThread::JobStatus_Completed && status != EncodeThread::JobStatus_Aborted && status != EncodeThread::JobStatus_Failed) { - return true; + count++; } } - return false; + return count; } /*Jobs that are still active, i.e. not terminated or enqueued*/ -bool MainWindow::haveRunningJobs(void) +unsigned int MainWindow::countRunningJobs(void) { + unsigned int count = 0; const int rows = m_jobList->rowCount(QModelIndex()); for(int i = 0; i < rows; i++) @@ -624,11 +643,11 @@ bool MainWindow::haveRunningJobs(void) EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex())); if(status != EncodeThread::JobStatus_Completed && status != EncodeThread::JobStatus_Aborted && status != EncodeThread::JobStatus_Failed && status != EncodeThread::JobStatus_Enqueued) { - return true; + count++; } } - return false; + return count; } void MainWindow::updateButtons(EncodeThread::JobStatus status) diff --git a/src/win_main.h b/src/win_main.h index d541757..8fc5103 100644 --- a/src/win_main.h +++ b/src/win_main.h @@ -59,8 +59,8 @@ private: const QString m_appDir; void updateButtons(EncodeThread::JobStatus status); - bool havePendingJobs(void); - bool haveRunningJobs(void); + unsigned int countPendingJobs(void); + unsigned int countRunningJobs(void); private slots: void addButtonPressed(const QString &filePath = QString(), bool *ok = NULL); diff --git a/src/win_preferences.cpp b/src/win_preferences.cpp index 4a79706..f09bf9c 100644 --- a/src/win_preferences.cpp +++ b/src/win_preferences.cpp @@ -70,7 +70,7 @@ void PreferencesDialog::loadPreferences(Preferences *preferences) settings.beginGroup("preferences"); preferences->autoRunNextJob = settings.value("auto_run_next_job", QVariant(true)).toBool(); - preferences->maxRunningJobCount = settings.value("max_running_job_count", QVariant(1U)).toUInt(); + preferences->maxRunningJobCount = qBound(1U, settings.value("max_running_job_count", QVariant(1U)).toUInt(), 16U); } void PreferencesDialog::savePreferences(Preferences *preferences)