moved loading to a separate thread
parent
e324450118
commit
dbb192f084
@ -0,0 +1,66 @@
|
||||
|
||||
#include "loader.hpp"
|
||||
|
||||
#include <QTimer>
|
||||
|
||||
#include "document.hpp"
|
||||
|
||||
CSMDoc::Loader::Loader()
|
||||
{
|
||||
QTimer *timer = new QTimer (this);
|
||||
|
||||
connect (timer, SIGNAL (timeout()), this, SLOT (load()));
|
||||
timer->start (1000);
|
||||
}
|
||||
|
||||
QWaitCondition& CSMDoc::Loader::hasThingsToDo()
|
||||
{
|
||||
return mThingsToDo;
|
||||
}
|
||||
|
||||
void CSMDoc::Loader::load()
|
||||
{
|
||||
if (mDocuments.empty())
|
||||
{
|
||||
mMutex.lock();
|
||||
mThingsToDo.wait (&mMutex);
|
||||
mMutex.unlock();
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<std::pair<Document *, bool> >::iterator iter = mDocuments.begin();
|
||||
|
||||
Document *document = iter->first;
|
||||
bool new_ = iter->second;
|
||||
|
||||
mDocuments.erase (iter);
|
||||
|
||||
try
|
||||
{
|
||||
document->setupData (new_);
|
||||
emit documentLoaded (document);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
emit documentNotLoaded (document, e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void CSMDoc::Loader::loadDocument (Document *document, bool new_)
|
||||
{
|
||||
mDocuments.push_back (std::make_pair (document, new_));
|
||||
}
|
||||
|
||||
void CSMDoc::Loader::abortLoading (Document *document)
|
||||
{
|
||||
for (std::vector<std::pair<Document *, bool> >::iterator iter = mDocuments.begin();
|
||||
iter!=mDocuments.end(); ++iter)
|
||||
{
|
||||
if (iter->first==document)
|
||||
{
|
||||
mDocuments.erase (iter);
|
||||
emit documentNotLoaded (document, "");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
#ifndef CSM_DOC_LOADER_H
|
||||
#define CSM_DOC_LOADER_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <QObject>
|
||||
#include <QMutex>
|
||||
#include <QWaitCondition>
|
||||
|
||||
namespace CSMDoc
|
||||
{
|
||||
class Document;
|
||||
|
||||
class Loader : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
QMutex mMutex;
|
||||
QWaitCondition mThingsToDo;
|
||||
std::vector<std::pair<Document *, bool> > mDocuments;
|
||||
|
||||
public:
|
||||
|
||||
Loader();
|
||||
|
||||
QWaitCondition& hasThingsToDo();
|
||||
|
||||
private slots:
|
||||
|
||||
void load();
|
||||
|
||||
public slots:
|
||||
|
||||
void loadDocument (Document *document, bool new_);
|
||||
///< The ownership of \a document is not transferred.
|
||||
/// \param new_ Do not load the last content file in the files list specified in
|
||||
/// \a document and instead create it in an appropriate way.
|
||||
|
||||
void abortLoading (Document *document);
|
||||
///< Abort loading \a docuemnt (ignored if \a document has already finished being
|
||||
/// loaded). Will result in a documentNotLoaded signal, once the Loader has finished
|
||||
/// cleaning up.
|
||||
|
||||
signals:
|
||||
|
||||
void documentLoaded (Document *document);
|
||||
///< The ownership of \a document is not transferred.
|
||||
|
||||
void documentNotLoaded (Document *document, const std::string& error);
|
||||
///< Document load has been interrupted either because of a call to abortLoading
|
||||
/// or a problem during loading). In the former case error will be an empty string.
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue