CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxWorkflowStateMachine.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxWorkflowStateMachine.h"
34 #include <QAbstractTransition>
35 #include <QMenu>
36 #include <QToolBar>
37 #include "cxWorkflowState.h"
39 #include "cxStateServiceBackend.h"
40 #include "cxPatientModelService.h"
41 
42 namespace cx
43 {
44 
46 {
47  mStarted = false;
48  connect(this, SIGNAL(started()), this, SLOT(startedSlot()));
49  mActionGroup = new QActionGroup(this);
50 
51  mParentState = new ParentWorkflowState(this, mBackend);
52 
53  WorkflowState* patientData = this->newState(new PatientDataWorkflowState(mParentState, mBackend));
54  this->newState(new RegistrationWorkflowState(mParentState, mBackend));
55  this->newState(new PreOpPlanningWorkflowState(mParentState, mBackend));
56  this->newState(new NavigationWorkflowState(mParentState, mBackend));
57  this->newState(new IntraOpImagingWorkflowState(mParentState, mBackend));
58  this->newState(new PostOpControllWorkflowState(mParentState, mBackend));
59 
60  //set initial state on all levels
61  this->setInitialState(mParentState);
62  mParentState->setInitialState(patientData);
63 
64  connect(mBackend->getPatientService().get(), SIGNAL(clinicalApplicationChanged()), this, SLOT(clinicalApplicationChangedSlot()));
65 }
66 
67 void WorkflowStateMachine::clinicalApplicationChangedSlot()
68 {
69  this->setActiveState("PatientDataUid");
70 }
71 
72 void WorkflowStateMachine::startedSlot()
73 {
74  mStarted = true;
75 }
76 
77 WorkflowState* WorkflowStateMachine::newState(WorkflowState* state)
78 {
79  RequestEnterStateTransition* transToState = new RequestEnterStateTransition(state->getUid());
80  transToState->setTargetState(state);
81  mParentState->addTransition(transToState);
82 
83  connect(state, SIGNAL(entered()), this, SIGNAL(activeStateChanged()));
84 
85  mStates[state->getUid()] = state;
86 
87  connect(state, SIGNAL(aboutToExit()), this, SIGNAL(activeStateAboutToChange()));
88 
89  return state;
90 }
91 
93 {
94 }
95 
97 {
98 // mActionGroup
99  this->fillActionGroup(mParentState, mActionGroup);
100 
101  QList<QAction *> actions = mActionGroup->actions();
102  for (int i = 1; i <= actions.size(); ++i)
103  {
104  QString shortcut = "Ctrl+" + QString::number(i);
105  actions[i - 1]->setShortcut(shortcut);
106  }
107 
108  return mActionGroup;
109 }
110 
111 void WorkflowStateMachine::fillActionGroup(WorkflowState* current, QActionGroup* group)
112 {
113  std::vector<WorkflowState*> childStates = current->getChildStates();
114 
115  if (childStates.empty())
116  {
117  group->addAction(current->createAction(group));
118  }
119  else // current is a node. create submenu and fill in recursively
120  {
121  for (unsigned i = 0; i < childStates.size(); ++i)
122  this->fillActionGroup(childStates[i], group);
123  }
124 }
125 
127 {
128 
129  if (mStarted)
130  this->postEvent(new RequestEnterStateEvent(uid));
131 }
132 
134 {
135  QSet<QAbstractState *> states = this->configuration();
136  for (QSet<QAbstractState *>::iterator iter = states.begin(); iter != states.end(); ++iter)
137  {
138  WorkflowState* wfs = dynamic_cast<WorkflowState*>(*iter);
139  if (!wfs || !wfs->getChildStates().empty())
140  continue;
141 
142  return wfs->getUid();
143  }
144  return QString();
145 }
146 
147 } //namespace cx
virtual QString getUid() const
Utility class for StateService states.
boost::shared_ptr< class StateServiceBackend > StateServiceBackendPtr
std::vector< WorkflowState * > getChildStates()
QAction * createAction(QActionGroup *group)
State in a WorkflowStateMachine.
WorkflowStateMachine(StateServiceBackendPtr backend)