NorMIT-nav  22.09
An IGT application
cxToolConfigureWidget.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 <cxToolConfigureWidget.h>
13 
14 #include <QVBoxLayout>
15 #include <QHBoxLayout>
16 #include <QComboBox>
17 #include <QLabel>
18 #include <QDir>
19 #include <QLineEdit>
20 #include "cxLogger.h"
21 #include "cxStateService.h"
22 #include "cxTrackingService.h"
23 #include "cxSelectionGroupBox.h"
24 #include "cxToolListWidget.h"
25 #include "cxHelperWidgets.h"
26 #include "cxTrackerConfiguration.h"
27 #include "cxEnumConversion.h"
28 
29 namespace cx
30 {
31 
32 ToolConfigureGroupBox::ToolConfigureGroupBox(TrackingServicePtr trackingService, StateServicePtr stateService, QWidget* parent) :
33  QGroupBox(parent),
34  mConfigFilesComboBox(new QComboBox()),
35  mConfigFileLineEdit(new QLineEdit()),
36  mReferenceComboBox(new QComboBox()),
37  mModified(false),
38  mTrackingService(trackingService),
39  mStateService(stateService)
40 {
41  connect(stateService.get(), &StateService::applicationStateChanged, this, &ToolConfigureGroupBox::onApplicationStateChanged);
42 
43  mConfigFilesComboBox->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Expanding);
44  //mConfigFilesComboBox->setMinimumSize(200, 0);
45  //mConfigFilesComboBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
46 
47  mToolListWidget = new ConfigToolListWidget(trackingService, NULL);
48 
49  this->createGUISelectors();
50 
51 // QGroupBox* toolGroupBox = new QGroupBox();
52 // toolGroupBox->setTitle("Tools");
53 // QVBoxLayout* toolLayout = new QVBoxLayout();
54 // toolGroupBox->setLayout(toolLayout);
55 // toolLayout->addWidget(mToolListWidget);
56 
57  QGridLayout* layout = new QGridLayout(this);
58  int row=0;
59  layout->addWidget(new QLabel("Configuration: "), row, 0, 1, 1);
60  layout->addWidget(mConfigFilesComboBox, row, 1, 1, 1);
61  row++;
62  layout->addWidget(new QLabel("File name: "), row, 0, 1, 1);
63  layout->addWidget(mConfigFileLineEdit, row, 1, 1, 1);
64  row++;
65  sscCreateDataWidget(this, mTrackingSystemSelector, layout, row);
66  row++;
67  sscCreateDataWidget(this, mTrackingSystemImplementationSelector, layout, row);
68  row++;
69  sscCreateDataWidget(this, mApplyRefToTools, layout, row);
70  row++;
71  layout->addWidget(mToolListWidget, row, 0, 1, 2);
72  layout->setRowStretch(row, 1);
73  row++;
74 // layout->addWidget(new QLabel("Reference: "), row, 0, 1, 1);
75 // layout->addWidget(mReferenceComboBox, row, 1, 1, 1);
76 
77  QHBoxLayout* refLayout = new QHBoxLayout;
78  refLayout->addWidget(new QLabel("Reference: "));
79  refLayout->addWidget(mReferenceComboBox);
80  refLayout->setMargin(0);
81  layout->addLayout(refLayout, row, 0, 1, 2);
82 
83  //changes due to programming actions
84  connect(mConfigFilesComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(configChangedSlot()));
85  connect(mToolListWidget, SIGNAL(listSizeChanged()), this, SLOT(toolsChangedSlot()));
86  connect(mToolListWidget, SIGNAL(toolSelected(QString)), this, SIGNAL(toolSelected(QString)));
87 
88  //changes due to user actions
89  connect(mConfigFileLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(configEditedSlot()));
90  connect(mToolListWidget, SIGNAL(userChangedList()), this, SLOT(configEditedSlot()));
91  connect(mReferenceComboBox, SIGNAL(activated(int)), this, SLOT(configEditedSlot()));
92 
93  this->onApplicationStateChanged();
94 }
95 
97 {}
98 
99 void ToolConfigureGroupBox::setCurrentlySelectedCofiguration(QString configAbsoluteFilePath)
100 {
101  QString cleanPath = QDir(configAbsoluteFilePath).absolutePath();
102  int currentIndex = mConfigFilesComboBox->findData(cleanPath, Qt::ToolTipRole);
103  if(currentIndex < 0)
104  {
105  currentIndex = 0;
106  if (!configAbsoluteFilePath.isEmpty())
107  reportWarning("Tool configuration doesn't exist: " + cleanPath);
108  }
109  mConfigFilesComboBox->setCurrentIndex(currentIndex);
110 
111  mModified = false;
112 }
113 
115 {
116  QString retval;
117  retval = mConfigFilesComboBox->itemData(mConfigFilesComboBox->currentIndex(), Qt::ToolTipRole).toString();
118  return retval;
119 }
120 
121 void ToolConfigureGroupBox::createGUISelectors()
122 {
123  QString defaultValue = "";
124  // Just use config for first tracking system, as they should support the same systems for now
125  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
126 
127  mTrackingSystemSelector = StringProperty::initialize("trackingsystem", "Tracking System",
128  "Select tracking system to use",
129  defaultValue,
130  config->getSupportedTrackingSystems(),
131  QDomNode());
132  connect(mTrackingSystemSelector.get(), &StringProperty::changed, this, &ToolConfigureGroupBox::filterToolsSlot);
133 
134  mTrackingSystemImplementationSelector = StringProperty::initialize("trackingSystemImplementation", "Tracking System Implementation",
135  "Select tracking system implementation to use",
136  defaultValue,
137  this->getTrackingSystemImplementationList(),
138  QDomNode());
139  connect(mTrackingSystemImplementationSelector.get(), &StringProperty::changed, this, &ToolConfigureGroupBox::trackingSystemImplementationChangedSlot);
140 
141  mApplyRefToTools = BoolProperty::initialize("applyRefToTools", "Apply ref to tools",
142  "Apply ref tool position to other tools for transforms received over OpenIGTLink.",
143  false,
144  QDomNode());
145  connect(mApplyRefToTools.get(), &BoolProperty::changed, this, &ToolConfigureGroupBox::applyRefToToolsChangedSlot);
146 }
147 
148 QStringList ToolConfigureGroupBox::getTrackingSystemImplementationList()
149 {
150  QStringList retval;
151  std::vector<TrackerConfigurationPtr> configs = mTrackingService->getConfigurations();
152 
153  for (unsigned i=0; i<configs.size(); ++i)
154  {
155  retval << configs[i]->getTrackingSystemImplementation();
156  }
157  return retval;
158 }
159 
160 void ToolConfigureGroupBox::trackingSystemImplementationChangedSlot()
161 {
162  QString systemImplementation = mTrackingSystemImplementationSelector->getValue();//TODO: Needs to be set outside of GUI as well?
163  mTrackingService->setCurrentTrackingSystemImplementation(systemImplementation);
164  if (systemImplementation.contains(TRACKING_SYSTEM_IMPLEMENTATION_IGTLINK, Qt::CaseInsensitive))
165  mApplyRefToTools->setEnabled(true);
166  else
167  mApplyRefToTools->setEnabled(false);
168 
169  mModified = true;
170 }
171 
172 void ToolConfigureGroupBox::applyRefToToolsChangedSlot()
173 {
174  mModified = true;
175 }
176 
178 {
179  return mTrackingSystemSelector;
180 }
181 
183 {
184  QString retval;
185 
186  if(!mModified)
187  return retval;
188 
189  // deconfigure toolmanager in order to be able to reread config data
190  mTrackingService->setState(Tool::tsNONE);
191 
193  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
194  config->saveConfiguration(current);
195  mModified = false;
196 
197  this->populateConfigurations();
198 
199  return current.mUid;
200 }
201 
202 void ToolConfigureGroupBox::onApplicationStateChanged()
203 {
204  QString application = mStateService->getApplicationStateName();
205  this->setTitle("Tool configurations for "+ application);
206  this->populateConfigurations();
207 }
208 
209 void ToolConfigureGroupBox::configChangedSlot()
210 {
211  QString uid = mConfigFilesComboBox->itemData(mConfigFilesComboBox->currentIndex(),
212  Qt::ToolTipRole).toString();
213 
214  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
215  TrackerConfiguration::Configuration data = config->getConfiguration(uid);
216  bool isNewConfig = mConfigFilesComboBox->currentText().contains("<new config>");
217 
218  if (isNewConfig)
219  {
220  data.mTrackingSystemName = enum2string(tsPOLARIS);
221  data.mName = "MyConfig";
222  }
223 
224  mConfigFileLineEdit->setText(data.mName);
225  mConfigFileLineEdit->setEnabled(isNewConfig);
226  mConfigFileLineEdit->setToolTip(data.mUid);
227  mModified = true;
228  mTrackingSystemSelector->setValue(data.mTrackingSystemName);
229  mTrackingSystemImplementationSelector->setValue(data.mTrackingSystemImplementation);
230  mApplyRefToTools->setValue(data.mApplyRefToTools);
231  mToolListWidget->configSlot(data.mTools);
232 }
233 
234 void ToolConfigureGroupBox::configEditedSlot()
235 {
236  mModified = true;
237 }
238 
239 void ToolConfigureGroupBox::toolsChangedSlot()
240 {
241  this->populateReference();
242  this->filterToolsSlot();
243 }
244 
245 void ToolConfigureGroupBox::filterToolsSlot()
246 {
247  QString ts = mTrackingSystemSelector->getValue();
248  mToolListWidget->filterSlot(QStringList() << ts);
249  mModified = true;
250 }
251 
252 void ToolConfigureGroupBox::populateConfigurations()
253 {
254  mConfigFilesComboBox->clear();
255 
256  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
257  QStringList configurations = config->getConfigurationsGivenApplication();
258 
259  foreach(QString filename, configurations)
260  {
261  TrackerConfiguration::Configuration configuration = config->getConfiguration(filename);
262  this->addConfigurationToComboBox(configuration.mName, configuration.mUid);
263  }
264  QString newConfig = "<new config>";
265  this->addConfigurationToComboBox(newConfig, newConfig);
266 
267  int currentIndex = mConfigFilesComboBox->findText(newConfig);
268  mConfigFilesComboBox->setCurrentIndex(currentIndex);
269 }
270 
271 int ToolConfigureGroupBox::addConfigurationToComboBox(QString displayName, QString absoluteFilePath)
272 {
273  mConfigFilesComboBox->addItem(displayName);
274  int index = mConfigFilesComboBox->findText(displayName);
275  mConfigFilesComboBox->setItemData(index, absoluteFilePath, Qt::ToolTipRole);
276 
277  return index;
278 }
279 
281 {
283  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
284  QString application = mStateService->getApplicationStateName();
285 
286  QString filename = mConfigFileLineEdit->text();
287  QString filepath = config->getConfigurationApplicationsPath();
288 
289  retval.mUid = QString("%1/%2.xml").arg(filepath).arg(filename);
290  retval.mClinicalApplication = application;
291  retval.mTrackingSystemImplementation = mTrackingService->getCurrentTrackingSystemImplementation();
292  retval.mTrackingSystemName = mTrackingSystemSelector->getValue();
293  retval.mApplyRefToTools = mApplyRefToTools->getValue();
294  retval.mTools = mToolListWidget->getTools();
295  retval.mReferenceTool = mReferenceComboBox->itemData(mReferenceComboBox->currentIndex(), Qt::ToolTipRole).toString();
296 
297  // TODO fix retval.mToolList. See TrackerConfigurationImpl::saveConfiguration()
298  // Will need to keep mOpenIGTLinkImageId and mOpenIGTLinkTransformId when creating return value
299  // For now ConfigurationFileParser::saveConfiguration() refuse to save openigtlink tracking files
300  // to prevent these from being destroyed.
301 
302  return retval;
303 }
304 
305 QString ToolConfigureGroupBox::getCurrentConfigFilePath()
306 {
307  QString configAbsoluteFilePath = mConfigFilesComboBox->itemData(mConfigFilesComboBox->currentIndex(), Qt::ToolTipRole).toString();
308  return configAbsoluteFilePath;
309 }
310 
311 void ToolConfigureGroupBox::populateReference()
312 {
313  mReferenceComboBox->clear();
314 
315  int currentIndex = -1;
316 
317  TrackerConfigurationPtr config = mTrackingService->getConfiguration();//Get last config. No problem here?
318 
319  // populate list
320  QStringList selectedTools = mToolListWidget->getTools();
321  foreach(QString string, selectedTools)
322  {
323  if (config->getTool(string).mIsReference)
324  currentIndex = this->addRefrenceToComboBox(string);
325  }
326 
327  // look for a selected reference
328  QString reference = config->getConfiguration(this->getCurrentConfigFilePath()).mReferenceTool;
329  currentIndex = this->addRefrenceToComboBox(reference);
330 
331  // always select a reference if available:
332  if (currentIndex<0)
333  currentIndex = 0;
334 
335  mReferenceComboBox->setCurrentIndex(currentIndex);
336 }
337 
338 int ToolConfigureGroupBox::addRefrenceToComboBox(QString absoluteRefereneFilePath)
339 {
340  int index;
341 
342  QFile file(absoluteRefereneFilePath);
343  QFileInfo info(file);
344  QString refUid = info.dir().dirName();
345 
346  QStringList selectedTools = mToolListWidget->getTools();
347  if (!selectedTools.count(absoluteRefereneFilePath))
348  return -1;
349 
350  if (refUid.isEmpty())
351  return -1;
352 
353  if (mReferenceComboBox->findText(refUid) < 0)
354  mReferenceComboBox->addItem(refUid);
355  index = mReferenceComboBox->findText(refUid);
356  mReferenceComboBox->setItemData(index, info.absoluteFilePath(), Qt::ToolTipRole);
357 
358  return index;
359 }
360 
361 
362 }//namespace cx
cx::BoolProperty::initialize
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
Definition: cxBoolProperty.cpp:30
cxLogger.h
cx::TrackerConfiguration::Configuration::mApplyRefToTools
bool mApplyRefToTools
Definition: cxTrackerConfiguration.h:45
cx::TrackerConfiguration::Configuration::mReferenceTool
QString mReferenceTool
Definition: cxTrackerConfiguration.h:43
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::ToolConfigureGroupBox::getCurrenctlySelectedConfiguration
QString getCurrenctlySelectedConfiguration() const
Definition: cxToolConfigureWidget.cpp:114
cx::ToolConfigureGroupBox::toolSelected
void toolSelected(QString absoluteFilePath)
cx::ConfigToolListWidget::filterSlot
void filterSlot(QStringList trackingsystemFilter)
filters the tools on tracking system
Definition: cxToolListWidget.cpp:212
cx::Property::changed
void changed()
emit when the underlying data value is changed: The user interface will be updated.
cx::TrackerConfigurationPtr
boost::shared_ptr< class TrackerConfiguration > TrackerConfigurationPtr
Definition: cxTrackerConfiguration.h:24
cx::ToolConfigureGroupBox::ToolConfigureGroupBox
ToolConfigureGroupBox(TrackingServicePtr trackingService, StateServicePtr stateService, QWidget *parent=NULL)
Definition: cxToolConfigureWidget.cpp:32
tsPOLARIS
tsPOLARIS
NDIs Polaris tracker.
Definition: cxDefinitions.h:113
cxHelperWidgets.h
cxToolListWidget.h
cx::ToolConfigureGroupBox::~ToolConfigureGroupBox
virtual ~ToolConfigureGroupBox()
Definition: cxToolConfigureWidget.cpp:96
cx::TRACKING_SYSTEM_IMPLEMENTATION_IGTLINK
const char * TRACKING_SYSTEM_IMPLEMENTATION_IGTLINK
Definition: cxDefinitions.cpp:17
cx::TrackerConfiguration::Configuration::mTrackingSystemName
QString mTrackingSystemName
Definition: cxTrackerConfiguration.h:41
cx::ToolConfigureGroupBox::getTrackingSystemSelector
StringPropertyBasePtr getTrackingSystemSelector()
Definition: cxToolConfigureWidget.cpp:177
cx::TrackerConfiguration::Configuration::mTrackingSystemImplementation
QString mTrackingSystemImplementation
Definition: cxTrackerConfiguration.h:44
cx::ToolConfigureGroupBox::getConfiguration
TrackerConfiguration::Configuration getConfiguration()
Definition: cxToolConfigureWidget.cpp:280
cx::TrackerConfiguration::Configuration::mClinicalApplication
QString mClinicalApplication
Definition: cxTrackerConfiguration.h:40
cx::ToolConfigureGroupBox::setCurrentlySelectedCofiguration
void setCurrentlySelectedCofiguration(QString configAbsoluteFilePath)
Definition: cxToolConfigureWidget.cpp:99
cx::StateService::applicationStateChanged
void applicationStateChanged()
cx::ConfigToolListWidget::getTools
QStringList getTools()
get absolute file path to all tools currently in the list
Definition: cxToolListWidget.cpp:194
cx::TrackingServicePtr
boost::shared_ptr< class TrackingService > TrackingServicePtr
Definition: cxToolFilterWidget.h:27
cx::ToolConfigureGroupBox::requestSaveConfigurationSlot
QString requestSaveConfigurationSlot()
will save the currently selected configuration if its been edited
Definition: cxToolConfigureWidget.cpp:182
cx::Tool::tsNONE
@ tsNONE
not available
Definition: cxTool.h:74
cx::TrackerConfiguration::Configuration::mTools
QStringList mTools
Definition: cxTrackerConfiguration.h:42
cxStateService.h
cx::ConfigToolListWidget::configSlot
void configSlot(QStringList toolsAbsoluteFilePath)
adds all input tools to the list
Definition: cxToolListWidget.cpp:207
cxSelectionGroupBox.h
cx::StringProperty::initialize
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
Definition: cxStringProperty.cpp:29
cx::sscCreateDataWidget
QWidget * sscCreateDataWidget(QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
Definition: cxHelperWidgets.cpp:59
cx::ConfigToolListWidget
Helper class for tool config list display.
Definition: cxToolListWidget.h:103
cx::StringPropertyBasePtr
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr
Definition: cxIGTLinkStreamerService.h:20
cxTrackerConfiguration.h
cx::TrackerConfiguration::Configuration::mUid
QString mUid
absolute path and filename for the new config file
Definition: cxTrackerConfiguration.h:38
enum2string
QString enum2string(const ENUM &val)
cxEnumConversion.h
cx::TrackerConfiguration::Configuration
Definition: cxTrackerConfiguration.h:36
cxToolConfigureWidget.h
cxTrackingService.h
cx::reportWarning
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
cx::StateServicePtr
boost::shared_ptr< class StateService > StateServicePtr
Definition: cxLogicManager.h:27