CustusX  16.5
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 "cxPatientModelService.h"
40 #include "cxCoreServices.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 
52 
53  connect(mServices->patient().get(), SIGNAL(clinicalApplicationChanged()), this, SLOT(clinicalApplicationChangedSlot()));
54 }
55 
57 {
58 }
59 
60 void WorkflowStateMachine::clinicalApplicationChangedSlot()
61 {
62  this->setActiveState("PatientDataUid");
63 }
64 
65 void WorkflowStateMachine::startedSlot()
66 {
67  mStarted = true;
68 }
69 
71 {
73  transToState->setTargetState(state);
74  mParentState->addTransition(transToState);
75 
76  connect(state, SIGNAL(entered()), this, SIGNAL(activeStateChanged()));
77 
78  mStates[state->getUid()] = state;
79 
80  connect(state, SIGNAL(aboutToExit()), this, SIGNAL(activeStateAboutToChange()));
81 
82  return state;
83 }
84 
86 {
87 // mActionGroup
88  this->fillActionGroup(mParentState, mActionGroup);
89 
90  QList<QAction *> actions = mActionGroup->actions();
91  for (int i = 1; i <= actions.size(); ++i)
92  {
93  QString shortcut = "Ctrl+" + QString::number(i);
94  actions[i - 1]->setShortcut(shortcut);
95  }
96 
97  return mActionGroup;
98 }
99 
100 void WorkflowStateMachine::fillActionGroup(WorkflowState* current, QActionGroup* group)
101 {
102  std::vector<WorkflowState*> childStates = current->getChildStates();
103 
104  if (childStates.empty())
105  {
106  group->addAction(current->createAction(group));
107  }
108  else // current is a node. create submenu and fill in recursively
109  {
110  for (unsigned i = 0; i < childStates.size(); ++i)
111  this->fillActionGroup(childStates[i], group);
112  }
113 }
114 
116 {
117 
118  if (mStarted)
119  this->postEvent(new RequestEnterStateEvent(uid));
120 }
121 
123 {
124  QSet<QAbstractState *> states = this->configuration();
125  for (QSet<QAbstractState *>::iterator iter = states.begin(); iter != states.end(); ++iter)
126  {
127  WorkflowState* wfs = dynamic_cast<WorkflowState*>(*iter);
128  if (!wfs || !wfs->getChildStates().empty())
129  continue;
130 
131  return wfs->getUid();
132  }
133  return QString();
134 }
135 
136 } //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:59
Utility class for StateService states.