Fraxinus  17.12
An IGT application
cxTrainingWidget.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 "cxTrainingWidget.h"
34 #include <QtWidgets>
35 #include <QPushButton>
36 #include "boost/bind.hpp"
37 #include "boost/function.hpp"
38 #include "cxHelpEngine.h"
39 #include "cxHelpBrowser.h"
40 #include "cxLogger.h"
41 #include "cxApplication.h"
42 #include "cxPatientModelService.h"
43 #include "cxImage.h"
44 #include "cxMesh.h"
45 #include "cxRegServices.h"
46 
47 namespace cx {
48 
49 TrainingWidget::TrainingWidget(RegServicesPtr services, QString objectName, QString windowTitle, QWidget* parent) :
50  BaseWidget(parent, objectName, windowTitle),
51  mServices(services)
52 {
53  mEngine.reset(new HelpEngine);
54  mBrowser = new HelpBrowser(this, mEngine);
55 
56  this->createActions();
57 
58  QVBoxLayout* topLayout = new QVBoxLayout(this);
59  QHBoxLayout* buttonLayout = new QHBoxLayout;
60 
61  topLayout->addWidget(mBrowser);
62  topLayout->addLayout(buttonLayout);
63 
64  buttonLayout->addStretch(1);
65  this->addToolButtonFor(buttonLayout, mImportAction);
66  this->addToolButtonFor(buttonLayout, mPreviousAction);
67  CXToolButton* button = this->addToolButtonFor(buttonLayout, mCurrentAction);
68  button->setToolButtonStyle(Qt::ToolButtonIconOnly);
69 
70  this->addToolButtonFor(buttonLayout, mNextAction);
71 
72  connect(mImportAction, &QAction::triggered, this, &TrainingWidget::onImportSimulatedPatient);
73  connect(mPreviousAction, &QAction::triggered, boost::function<void()>(boost::bind(&TrainingWidget::onStep, this, -1)));
74  connect(mCurrentAction, &QAction::triggered, boost::function<void()>(boost::bind(&TrainingWidget::onStep, this, 0)));
75  connect(mNextAction, &QAction::triggered, boost::function<void()>(boost::bind(&TrainingWidget::onStep, this, +1)));
76 
77  //must always be the initial step
78  func_t welcome = boost::bind(&TrainingWidget::toWelcomeStep, this);
80 }
81 
83 {
84 }
85 
87 {
88  mCurrentStep = -1;
89  this->stepTo(0);
90 }
91 
93 {
94  mTransitions.push_back(transition);
95  int numberOfSteps = mTransitions.size();
96  this->createSteps(numberOfSteps);
97 }
98 
99 void TrainingWidget::createActions()
100 {
101  mImportAction = this->createAction2(this,
102  QIcon(":/icons/open_icon_library/document-open-7.png"),
103  "Import new training dataset", "Clear current data and import new training patient folder",
104  //SLOT(onImport()),
105  NULL);
106 
107  mPreviousAction = this->createAction2(this,
108  QIcon(":/icons/open_icon_library/arrow-left-3.png"),
109  "Previous", "Go to previous training step",
110  // SLOT(onPrevious()),
111  NULL);
112 
113  mCurrentAction = this->createAction2(this,
114  QIcon(":/icons/open_icon_library/button-green.png"),
115  "Reload", "Reload the current training step",
116  NULL);
117 
118  mNextAction = this->createAction2(this,
119  QIcon(":/icons/open_icon_library/arrow-right-3.png"),
120  "Next", "Go to next training step",
121  // SLOT(onNext()),
122  NULL);
123 }
124 
125 void TrainingWidget::createSteps(unsigned numberOfSteps)
126 {
127 // CX_LOG_DEBUG() << "Creating " << numberOfSteps << " steps!";
128  mSessionIDs.clear();
129 
130  for (unsigned i=1; i<=numberOfSteps; ++i)
131  mSessionIDs << QString("org_custusx_training_sessionA_step%1").arg(i);
132 
133  this->resetSteps();
134 }
135 
136 CXToolButton* TrainingWidget::addToolButtonFor(QHBoxLayout* layout, QAction* action)
137 {
138  CXToolButton* button = new CXToolButton();
139  button->setDefaultAction(action);
140  button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
141  // button->setToolTip(action->toolTip());
142  layout->addWidget(button);
143  return button;
144 }
145 
146 void TrainingWidget::toWelcomeStep()
147 {
148 // std::cout << "toWelcomeStep" << std::endl;
149 }
150 
151 void TrainingWidget::onImportSimulatedPatient()
152 {
155 
156  //Transition to first step after welcome
157  this->stepTo(1);
158 }
159 
160 void TrainingWidget::onStep(int delta)
161 {
162  this->stepTo(mCurrentStep+delta);
163 }
164 
165 void TrainingWidget::stepTo(int step)
166 {
167 // CX_LOG_DEBUG() << "stepTo " << step;
168  step = std::min<int>(step, mSessionIDs.size()-1);
169  step = std::max<int>(step, 0);
170  mCurrentStep = step;
171 // CX_LOG_DEBUG() << "Current step is now " << mCurrentStep;
172 // CX_LOG_DEBUG() << "mSessionIDs.size(): " << mSessionIDs.size();
173 
174  mBrowser->showHelpForKeyword(mSessionIDs[mCurrentStep]);
175 
176  this->transitionToStep(step);
177 }
178 
179 void TrainingWidget::transitionToStep(int step)
180 {
181 // CX_LOG_DEBUG() << "Want to transition to step " << step;
182  int transitionNumber = step;
183  if(transitionNumber >= 0)
184  {
185 // CX_LOG_DEBUG() << "Going to execute transition number " << transitionNumber;
186  func_t transition = mTransitions.at(transitionNumber);
187  if(transition)
188  {
189 // CX_LOG_DEBUG() << "Transitioning";
190  transition();
191  }
192  }
193 
194 }
195 
197 {
198  std::map<QString, DataPtr> datas = mServices->patient()->getDatas();
199  std::map<QString, DataPtr>::iterator iter = datas.begin();
200 
201  for(; iter != datas.end(); ++iter)
202  {
203  DataPtr data = iter->second;
204  ImagePtr image = boost::dynamic_pointer_cast<Image>(data);
205 
206  if (image && image->getModality().contains("US"))
207  return image->getUid();
208  }
209  return QString();
210 }
211 
213 {
214  std::map<QString, MeshPtr> datas = mServices->patient()->getDataOfType<Mesh>();
215  std::map<QString, MeshPtr>::iterator iter = datas.begin();
216  for(; iter != datas.end(); ++iter)
217  {
218  MeshPtr mesh = iter->second;
219  if(mesh && mesh->getUid().contains(uidPart))
220  return mesh;
221  }
222  return MeshPtr();
223 }
224 
225 void TrainingWidget::makeUnavailable(QString uidPart, bool makeModalityUnavailable)
226 {
227  std::map<QString, DataPtr> datas = mServices->patient()->getDatas();
228  this->setAvailability(datas, false, uidPart, makeModalityUnavailable);
229 }
230 
231 
232 void TrainingWidget::makeAvailable(QString uidPart, bool makeModalityUnavailable)
233 {
234  std::map<QString, DataPtr> datas = mServices->patient()->getDatas(PatientModelService::AllData);
235  this->setAvailability(datas, true, uidPart, makeModalityUnavailable);
236 }
237 
238 void TrainingWidget::setAvailability(std::map<QString, DataPtr> datas, bool available, QString uidPart, bool makeModalityUnavailable)
239 {
240  std::map<QString, DataPtr>::iterator iter = datas.begin();
241 
242  for(; iter != datas.end(); ++iter)
243  {
244  DataPtr data = iter->second;
245  ImagePtr image = boost::dynamic_pointer_cast<Image>(data);
246 
247  if (makeModalityUnavailable && image && image->getModality().contains(uidPart))
248  mServices->patient()->makeAvailable(image->getUid(), available);
249  else if (data && data->getUid().contains(uidPart))
250  mServices->patient()->makeAvailable(data->getUid(), available);
251  }
252 }
253 
254 } /* namespace cx */
A mesh data set.
Definition: cxMesh.h:66
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
void registrateTransition(func_t transition)
void triggerMainWindowActionWithObjectName(QString actionName)
virtual QString getUid() const
Definition: cxData.cpp:85
void makeAvailable(QString uidPart, bool makeModalityUnavailable)
MeshPtr getMesh(QString uidPart)
boost::shared_ptr< class Data > DataPtr
A volumetric data set.
Definition: cxImage.h:66
void showHelpForKeyword(const QString &id)
boost::shared_ptr< class RegServices > RegServicesPtr
Definition: cxRegServices.h:41
RegServicesPtr mServices
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:109
void makeUnavailable(QString uidPart, bool makeModalityUnavailable=false)
TrainingWidget(RegServicesPtr services, QString objectName, QString windowTitle, QWidget *parent=NULL)
boost::function< void(void)> func_t
boost::shared_ptr< class Mesh > MeshPtr
QAction * createAction2(QObject *parent, QIcon iconName, QString text, QString tip, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:129
Namespace for all CustusX production code.