CustusX  2023.01.05-dev+develop.0da12
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxWorkflowStateMachine.h"
13 #include <QAbstractTransition>
14 #include <QMenu>
15 #include <QToolBar>
16 #include "cxWorkflowState.h"
18 #include "cxPatientModelService.h"
19 #include "cxCoreServices.h"
20 
21 namespace cx
22 {
23 
25 {
26  mStarted = false;
27  connect(this, SIGNAL(started()), this, SLOT(startedSlot()));
28  mActionGroup = new QActionGroup(this);
29 
31 
32  connect(mServices->patient().get(), SIGNAL(clinicalApplicationChanged()), this, SLOT(clinicalApplicationChangedSlot()));
33 }
34 
36 {
37 }
38 
39 void WorkflowStateMachine::clinicalApplicationChangedSlot()
40 {
41  this->setActiveState("PatientDataUid");
42 }
43 
44 void WorkflowStateMachine::startedSlot()
45 {
46  mStarted = true;
47 }
48 
50 {
52  transToState->setTargetState(state);
53  mParentState->addTransition(transToState);
54 
55  connect(state, SIGNAL(entered()), this, SIGNAL(activeStateChanged()));
56 
57  mStates[state->getUid()] = state;
58 
59  connect(state, SIGNAL(aboutToExit()), this, SIGNAL(activeStateAboutToChange()));
60 
61  return state;
62 }
63 
65 {
66 // mActionGroup
67  this->fillActionGroup(mParentState, mActionGroup);
68 
69  QList<QAction *> actions = mActionGroup->actions();
70  for (int i = 1; i <= actions.size(); ++i)
71  {
72  QString shortcut = "Ctrl+" + QString::number(i);
73  actions[i - 1]->setShortcut(shortcut);
74  }
75 
76  return mActionGroup;
77 }
78 
79 void WorkflowStateMachine::fillActionGroup(WorkflowState* current, QActionGroup* group)
80 {
81  std::vector<WorkflowState*> childStates = current->getChildStates();
82 
83  if (childStates.empty())
84  {
85  group->addAction(current->createAction(group));
86  }
87  else // current is a node. create submenu and fill in recursively
88  {
89  for (unsigned i = 0; i < childStates.size(); ++i)
90  this->fillActionGroup(childStates[i], group);
91  }
92 }
93 
95 {
96 
97  if (mStarted)
98  this->postEvent(new RequestEnterStateEvent(uid));
99 }
100 
102 {
103  QSet<QAbstractState *> states = this->configuration();
104  for (QSet<QAbstractState *>::iterator iter = states.begin(); iter != states.end(); ++iter)
105  {
106  WorkflowState* wfs = dynamic_cast<WorkflowState*>(*iter);
107  if (!wfs || !wfs->getChildStates().empty())
108  continue;
109 
110  return wfs->getUid();
111  }
112  return QString();
113 }
114 
115 } //namespace cx
virtual QString getUid() const
Utility class for StateService states.
virtual WorkflowState * newState(WorkflowState *state)
std::vector< WorkflowState * > getChildStates()
WorkflowStateMachine(CoreServicesPtr services)
QAction * createAction(QActionGroup *group)
State in a WorkflowStateMachine.
boost::shared_ptr< class CoreServices > CoreServicesPtr
Definition: cxCameraStyle.h:37
Utility class for StateService states.
Namespace for all CustusX production code.