From 6c18be39f0e10b43163de194fe93d0714df40eef Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sat, 8 Dec 2012 17:53:45 +0100 Subject: [PATCH] added stage class --- apps/opencs/CMakeLists.txt | 4 +-- apps/opencs/model/tools/operation.cpp | 49 ++++++++++++++++++++++++--- apps/opencs/model/tools/operation.hpp | 19 ++++++++++- apps/opencs/model/tools/stage.cpp | 4 +++ apps/opencs/model/tools/stage.hpp | 20 +++++++++++ 5 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 apps/opencs/model/tools/stage.cpp create mode 100644 apps/opencs/model/tools/stage.hpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index d2b87be51..381ce51cf 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -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/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 @@ -23,7 +23,7 @@ set (OPENCS_HDR model/world/idtable.hpp model/world/columns.hpp model/world/idtableproxymodel.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 diff --git a/apps/opencs/model/tools/operation.cpp b/apps/opencs/model/tools/operation.cpp index ba5866168..12c4a8108 100644 --- a/apps/opencs/model/tools/operation.cpp +++ b/apps/opencs/model/tools/operation.cpp @@ -5,11 +5,33 @@ #include "../doc/state.hpp" +#include "stage.hpp" + +void CSMTools::Operation::prepareStages() +{ + mCurrentStage = mStages.begin(); + mCurrentStep = 0; + mCurrentStepTotal = 0; + mTotalSteps = 0; + + for (std::vector >::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() +{ + for (std::vector >::iterator iter (mStages.begin()); iter!=mStages.end(); ++iter) + delete iter->first; +} + void CSMTools::Operation::run() { - mStep = 0; + prepareStages(); QTimer timer; @@ -20,6 +42,11 @@ void CSMTools::Operation::run() exec(); } +void CSMTools::Operation::appendStage (Stage *stage) +{ + mStages.push_back (std::make_pair (stage, 0)); +} + void CSMTools::Operation::abort() { exit(); @@ -27,9 +54,23 @@ void CSMTools::Operation::abort() void CSMTools::Operation::verify() { - ++mStep; - emit progress (mStep, 1000, mType); + while (mCurrentStage!=mStages.end()) + { + 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(); } \ No newline at end of file diff --git a/apps/opencs/model/tools/operation.hpp b/apps/opencs/model/tools/operation.hpp index b75e2898f..72419e00a 100644 --- a/apps/opencs/model/tools/operation.hpp +++ b/apps/opencs/model/tools/operation.hpp @@ -1,23 +1,40 @@ #ifndef CSM_TOOLS_OPERATION_H #define CSM_TOOLS_OPERATION_H +#include + #include namespace CSMTools { + class Stage; + class Operation : public QThread { Q_OBJECT int mType; - int mStep; + std::vector > mStages; // stage, number of steps + std::vector >::iterator mCurrentStage; + int mCurrentStep; + int mCurrentStepTotal; + int mTotalSteps; + + void prepareStages(); public: Operation (int type); + virtual ~Operation(); + 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: void progress (int current, int max, int type); diff --git a/apps/opencs/model/tools/stage.cpp b/apps/opencs/model/tools/stage.cpp new file mode 100644 index 000000000..6f4567e57 --- /dev/null +++ b/apps/opencs/model/tools/stage.cpp @@ -0,0 +1,4 @@ + +#include "stage.hpp" + +CSMTools::Stage::~Stage() {} \ No newline at end of file diff --git a/apps/opencs/model/tools/stage.hpp b/apps/opencs/model/tools/stage.hpp new file mode 100644 index 000000000..912559410 --- /dev/null +++ b/apps/opencs/model/tools/stage.hpp @@ -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 +