From 7ed83b6c6666eb8b6b104c211ae7e52907350372 Mon Sep 17 00:00:00 2001 From: craig Date: Sun, 1 Jan 2012 11:40:09 +0000 Subject: Branch 1.3.5 tree to 1.4.x tree, goodbye 1.3.x git-svn-id: svn://scribus.net/branches/Version14x/Scribus@17163 11d20701-8431-0410-a711-e3c959e3b870 --- scribus/deferredtask.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 scribus/deferredtask.cpp (limited to 'scribus/deferredtask.cpp') diff --git a/scribus/deferredtask.cpp b/scribus/deferredtask.cpp new file mode 100644 index 0000000..0d5a6fa --- /dev/null +++ b/scribus/deferredtask.cpp @@ -0,0 +1,99 @@ +/* +For general Scribus (>=1.3.2) copyright and licensing information please refer +to the COPYING file provided with the program. Following this notice may exist +a copyright and/or license notice that predates the release of Scribus 1.3.2 +for which a new license (GPL+exception) is in place. +*/ + +#include +#include "deferredtask.h" + +enum FileSearchStatus +{ + Status_NotStarted, + Status_Running, + Status_Cancelled, + Status_Failed, + Status_Finished, +}; + +DeferredTask::DeferredTask(QObject* parent) : QObject(parent) +{ + init(); +} + +DeferredTask::~DeferredTask() +{ + cleanup(); +} + +void DeferredTask::init() +{ + m_status = Status_NotStarted, + m_lastError = QString::null; + // Build the timer we'll use to access the event loop + m_timer = new QTimer(this); + Q_CHECK_PTR(m_timer); + // and hook up to it. + connect(m_timer, SIGNAL(timeout()), SLOT(next()) ); +} + +void DeferredTask::cleanup() +{ + // We're being destroyed while the search is running. + if (m_status == Status_Running) + { + m_status = Status_Failed; + // Report a failure. This error generally indicates a program bug, + // non-translatable because it should never be user visible. + m_lastError = "DeferredTask unexpectedly deleted"; + emit aborted(false); + } + // Our timer, if we have one, is automatically destroyed when + // we are so don't worry about it. +} + +bool DeferredTask::isFinished() const +{ + return m_status == Status_Finished; +} + +const QString& DeferredTask::lastError() const +{ + Q_ASSERT(m_status == Status_Cancelled || m_status == Status_Failed || m_status == Status_Finished); + Q_ASSERT(!m_lastError.isNull()); + return m_lastError; +} + +void DeferredTask::start() +{ + m_status = Status_Running; + // Start the timer to do our search in the event loop's idle time + m_timer->setSingleShot(false); + m_timer->start(0); +} + +void DeferredTask::cancel() +{ + Q_ASSERT(m_status == Status_Running); + m_status = Status_Cancelled; + m_timer->stop(); + // Cancelled by request + m_lastError = tr("Cancelled by user"); + emit aborted(true); +} + +void DeferredTask::done() +{ + Q_ASSERT(m_status == Status_Running); + m_status = Status_Finished; + m_timer->stop(); + emit finished(); +} + +void DeferredTask::runUntilFinished() +{ + Q_ASSERT(m_status == Status_Running); + while (m_status == Status_Running) + next(); +} -- cgit