Procházet zdrojové kódy

Call registerWorkflowStep when appropriate and add comments

Jean-Christophe Fillion-Robin před 13 roky
rodič
revize
bd742afbd0

+ 11 - 2
Libs/Core/ctkWorkflow.cpp

@@ -19,6 +19,7 @@
 =========================================================================*/
 
 // Qt includes
+#include <QDebug>
 #include <QStateMachine>
 #include <QState>
 
@@ -541,7 +542,9 @@ ctkWorkflow::~ctkWorkflow()
 
   // Clean registered step
   while (!d->registeredSteps.isEmpty())
-      delete d->registeredSteps.takeFirst();
+    {
+    delete d->registeredSteps.takeFirst();
+    }
 }
 
 // --------------------------------------------------------------------------
@@ -624,10 +627,16 @@ bool ctkWorkflow::addTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destin
   return true;
 }
 
+// --------------------------------------------------------------------------
 void ctkWorkflow::registerWorkflowStep(ctkWorkflowStep* step)
 {
   Q_D(ctkWorkflow);
-  d->registeredSteps.append(step);
+  if (!step)
+    {
+    qCritical() << "ctkWorkflow::registerWorkflowStep - Failed to register Null step !";
+    return;
+    }
+  d->registeredSteps << step;
 }
 
 // --------------------------------------------------------------------------

+ 8 - 2
Libs/Core/ctkWorkflow.h

@@ -110,8 +110,14 @@ public:
   Q_INVOKABLE ctkWorkflowStep* initialStep()const;
   Q_INVOKABLE virtual void setInitialStep(ctkWorkflowStep* step);
 
-  /// \brief Register standard steps for cleaning purpose
-  Q_INVOKABLE virtual void registerWorkflowStep(ctkWorkflowStep* step);
+  /// \brief Register ctkWorkflowStep so that ctkWorkflow keeps track of the associated steps
+  /// and clean the memory when appropriate.
+  /// \note This function is declared public for convenience and shouldn't be directly used.
+  /// The step will register itself when instantiated.
+  /// \note Since ctkWorkflowStep are neither QObject nor QWidget, they will be registered. On
+  /// on the othen hand, ctkWorkflowWidgetStep will be managed by their parent QWidget and
+  /// won't be registered.
+  void registerWorkflowStep(ctkWorkflowStep* step);
 
   /// Get the current step of the state machine
   Q_INVOKABLE ctkWorkflowStep* currentStep()const;

+ 4 - 2
Libs/Core/ctkWorkflowStep.cpp

@@ -117,6 +117,8 @@ void ctkWorkflowStepPrivate::invokeOnExitCommandInternal(const ctkWorkflowStep*
 // --------------------------------------------------------------------------
 ctkWorkflowStep::ctkWorkflowStep(): d_ptr(new ctkWorkflowStepPrivate(*this))
 {
+  Q_D(ctkWorkflowStep);
+  d->Workflow->registerWorkflowStep(this);
 }
 
 // --------------------------------------------------------------------------
@@ -127,7 +129,8 @@ ctkWorkflowStep::ctkWorkflowStep(ctkWorkflow* newWorkflow, const QString& newId)
 
   d->Id = newId;
   d->Workflow = newWorkflow;
-  newWorkflow->registerWorkflowStep(this);
+
+  d->Workflow->registerWorkflowStep(this);
 }
 
 // --------------------------------------------------------------------------
@@ -142,7 +145,6 @@ ctkWorkflowStep::ctkWorkflowStep(ctkWorkflowStepPrivate * pimpl,
   Q_D(ctkWorkflowStep);
   d->Id = newId;
   d->Workflow = newWorkflow;
-  newWorkflow->registerWorkflowStep(this);
 }
 
 // --------------------------------------------------------------------------