added stage class

This commit is contained in:
Marc Zinnschlag 2012-12-08 17:53:45 +01:00
parent 8b7f342641
commit 6c18be39f0
5 changed files with 89 additions and 7 deletions

View file

@ -7,7 +7,7 @@ set (OPENCS_SRC
model/world/universalid.cpp model/world/idcollection.cpp model/world/data.cpp model/world/idtable.cpp model/world/universalid.cpp model/world/idcollection.cpp model/world/data.cpp model/world/idtable.cpp
model/world/commands.cpp model/world/idtableproxymodel.cpp model/world/record.cpp model/world/commands.cpp model/world/idtableproxymodel.cpp model/world/record.cpp
model/tools/tools.cpp model/tools/operation.cpp model/tools/verifier.cpp model/tools/tools.cpp model/tools/operation.cpp model/tools/stage.cpp model/tools/verifier.cpp
view/doc/viewmanager.cpp view/doc/view.cpp view/doc/operations.cpp view/doc/operation.cpp view/doc/viewmanager.cpp view/doc/view.cpp view/doc/operations.cpp view/doc/operation.cpp
@ -23,7 +23,7 @@ set (OPENCS_HDR
model/world/idtable.hpp model/world/columns.hpp model/world/idtableproxymodel.hpp model/world/idtable.hpp model/world/columns.hpp model/world/idtableproxymodel.hpp
model/world/commands.hpp model/world/commands.hpp
model/tools/tools.hpp model/tools/operation.hpp model/tools/verifier.hpp model/tools/tools.hpp model/tools/operation.hpp model/tools/stage.hpp model/tools/verifier.hpp
view/doc/viewmanager.hpp view/doc/view.hpp view/doc/operations.hpp view/doc/operation.hpp view/doc/viewmanager.hpp view/doc/view.hpp view/doc/operations.hpp view/doc/operation.hpp

View file

@ -5,11 +5,33 @@
#include "../doc/state.hpp" #include "../doc/state.hpp"
#include "stage.hpp"
void CSMTools::Operation::prepareStages()
{
mCurrentStage = mStages.begin();
mCurrentStep = 0;
mCurrentStepTotal = 0;
mTotalSteps = 0;
for (std::vector<std::pair<Stage *, int> >::iterator iter (mStages.begin()); iter!=mStages.end(); ++iter)
{
iter->second = mTotalSteps;
mTotalSteps += iter->first->setup();
}
}
CSMTools::Operation::Operation (int type) : mType (type) {} CSMTools::Operation::Operation (int type) : mType (type) {}
CSMTools::Operation::~Operation()
{
for (std::vector<std::pair<Stage *, int> >::iterator iter (mStages.begin()); iter!=mStages.end(); ++iter)
delete iter->first;
}
void CSMTools::Operation::run() void CSMTools::Operation::run()
{ {
mStep = 0; prepareStages();
QTimer timer; QTimer timer;
@ -20,6 +42,11 @@ void CSMTools::Operation::run()
exec(); exec();
} }
void CSMTools::Operation::appendStage (Stage *stage)
{
mStages.push_back (std::make_pair (stage, 0));
}
void CSMTools::Operation::abort() void CSMTools::Operation::abort()
{ {
exit(); exit();
@ -27,9 +54,23 @@ void CSMTools::Operation::abort()
void CSMTools::Operation::verify() void CSMTools::Operation::verify()
{ {
++mStep; while (mCurrentStage!=mStages.end())
emit progress (mStep, 1000, mType); {
if (mCurrentStep>=mCurrentStage->second)
{
mCurrentStep = 0;
++mCurrentStage;
}
else
{
mCurrentStage->first->perform (mCurrentStep++);
++mCurrentStepTotal;
break;
}
}
if (mStep>=1000) emit progress (mCurrentStepTotal, mTotalSteps ? mTotalSteps : 1, mType);
if (mCurrentStage==mStages.end())
exit(); exit();
} }

View file

@ -1,23 +1,40 @@
#ifndef CSM_TOOLS_OPERATION_H #ifndef CSM_TOOLS_OPERATION_H
#define CSM_TOOLS_OPERATION_H #define CSM_TOOLS_OPERATION_H
#include <vector>
#include <QThread> #include <QThread>
namespace CSMTools namespace CSMTools
{ {
class Stage;
class Operation : public QThread class Operation : public QThread
{ {
Q_OBJECT Q_OBJECT
int mType; int mType;
int mStep; std::vector<std::pair<Stage *, int> > mStages; // stage, number of steps
std::vector<std::pair<Stage *, int> >::iterator mCurrentStage;
int mCurrentStep;
int mCurrentStepTotal;
int mTotalSteps;
void prepareStages();
public: public:
Operation (int type); Operation (int type);
virtual ~Operation();
virtual void run(); virtual void run();
void appendStage (Stage *stage);
///< The ownership of \a stage is transferred to *this.
///
/// \attention Do no call this function while this Operation is running.
signals: signals:
void progress (int current, int max, int type); void progress (int current, int max, int type);

View file

@ -0,0 +1,4 @@
#include "stage.hpp"
CSMTools::Stage::~Stage() {}

View file

@ -0,0 +1,20 @@
#ifndef CSM_TOOLS_STAGE_H
#define CSM_TOOLS_STAGE_H
namespace CSMTools
{
class Stage
{
public:
virtual ~Stage();
virtual int setup() = 0;
///< \return number of steps
virtual void perform (int stage) = 0;
};
}
#endif