Fraxinus  18.10
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 "cxEnumConverter.h"
21 #include "cxLogger.h"
22 #include "cxStateService.h"
23 #include "cxTrackingService.h"
24 #include "cxSelectionGroupBox.h"
25 #include "cxToolListWidget.h"
26 #include "cxHelperWidgets.h"
27 #include "cxTrackerConfiguration.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->createTrackingSystemSelector();
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  layout->addWidget(mToolListWidget, row, 0, 1, 2);
68  layout->setRowStretch(row, 1);
69  row++;
70 // layout->addWidget(new QLabel("Reference: "), row, 0, 1, 1);
71 // layout->addWidget(mReferenceComboBox, row, 1, 1, 1);
72 
73  QHBoxLayout* refLayout = new QHBoxLayout;
74  refLayout->addWidget(new QLabel("Reference: "));
75  refLayout->addWidget(mReferenceComboBox);
76  refLayout->setMargin(0);
77  layout->addLayout(refLayout, row, 0, 1, 2);
78 
79  //changes due to programming actions
80  connect(mConfigFilesComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(configChangedSlot()));
81  connect(mToolListWidget, SIGNAL(listSizeChanged()), this, SLOT(toolsChangedSlot()));
82  connect(mToolListWidget, SIGNAL(toolSelected(QString)), this, SIGNAL(toolSelected(QString)));
83 
84  //changes due to user actions
85  connect(mConfigFileLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(configEditedSlot()));
86  connect(mToolListWidget, SIGNAL(userChangedList()), this, SLOT(configEditedSlot()));
87  connect(mReferenceComboBox, SIGNAL(activated(int)), this, SLOT(configEditedSlot()));
88 
89  this->onApplicationStateChanged();
90 }
91 
93 {}
94 
95 void ToolConfigureGroupBox::setCurrentlySelectedCofiguration(QString configAbsoluteFilePath)
96 {
97  QString cleanPath = QDir(configAbsoluteFilePath).absolutePath();
98  int currentIndex = mConfigFilesComboBox->findData(cleanPath, Qt::ToolTipRole);
99  if(currentIndex < 0)
100  {
101  currentIndex = 0;
102  if (!configAbsoluteFilePath.isEmpty())
103  reportWarning("Tool configuration doesn't exist: " + cleanPath);
104  }
105  mConfigFilesComboBox->setCurrentIndex(currentIndex);
106 
107  mModified = false;
108 }
109 
111 {
112  QString retval;
113  retval = mConfigFilesComboBox->itemData(mConfigFilesComboBox->currentIndex(), Qt::ToolTipRole).toString();
114  return retval;
115 }
116 
117 void ToolConfigureGroupBox::createTrackingSystemSelector()
118 {
119  QString defaultValue = "";
120  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
121  mTrackingSystemSelector = StringProperty::initialize("trackingsystem", "Tracking System",
122  "Select tracking system to use",
123  defaultValue,
124  config->getSupportedTrackingSystems(),
125  QDomNode());
126  connect(mTrackingSystemSelector.get(), SIGNAL(changed()), this, SLOT(filterToolsSlot()));
127 }
128 
130 {
131  return mTrackingSystemSelector;
132 }
133 
135 {
136  QString retval;
137 
138  if(!mModified)
139  return retval;
140 
141  // deconfigure toolmanager in order to be able to reread config data
142  mTrackingService->setState(Tool::tsNONE);
143 
144  TrackerConfiguration::Configuration current = this->getCurrentConfiguration();
145  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
146  config->saveConfiguration(current);
147  mModified = false;
148 
149  this->populateConfigurations();
150 
151  return current.mUid;
152 }
153 
154 void ToolConfigureGroupBox::onApplicationStateChanged()
155 {
156  QString application = mStateService->getApplicationStateName();
157  this->setTitle("Tool configurations for "+ application);
158  this->populateConfigurations();
159 }
160 
161 void ToolConfigureGroupBox::configChangedSlot()
162 {
163  QString uid = mConfigFilesComboBox->itemData(mConfigFilesComboBox->currentIndex(),
164  Qt::ToolTipRole).toString();
165 
166  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
167  TrackerConfiguration::Configuration data = config->getConfiguration(uid);
168  bool isNewConfig = mConfigFilesComboBox->currentText().contains("<new config>");
169 
170  if (isNewConfig)
171  {
173  data.mName = "MyConfig";
174  }
175 
176  mConfigFileLineEdit->setText(data.mName);
177  mConfigFileLineEdit->setEnabled(isNewConfig);
178  mConfigFileLineEdit->setToolTip(data.mUid);
179  mModified = true;
180  mTrackingSystemSelector->setValue(data.mTrackingSystemName);
181  mToolListWidget->configSlot(data.mTools);
182  this->mTrackingSystemImplementation = data.mTrackingSystemImplementation;
183 }
184 
185 void ToolConfigureGroupBox::configEditedSlot()
186 {
187  mModified = true;
188 }
189 
190 void ToolConfigureGroupBox::toolsChangedSlot()
191 {
192  this->populateReference();
193  this->filterToolsSlot();
194 }
195 
196 void ToolConfigureGroupBox::filterToolsSlot()
197 {
198  QString ts = mTrackingSystemSelector->getValue();
199  mToolListWidget->filterSlot(QStringList() << ts);
200  mModified = true;
201 }
202 
203 void ToolConfigureGroupBox::populateConfigurations()
204 {
205  mConfigFilesComboBox->clear();
206 
207  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
208  QStringList configurations = config->getConfigurationsGivenApplication();
209 
210  foreach(QString filename, configurations)
211  {
212  TrackerConfiguration::Configuration configuration = config->getConfiguration(filename);
213  this->addConfigurationToComboBox(configuration.mName, configuration.mUid);
214  }
215  QString newConfig = "<new config>";
216  this->addConfigurationToComboBox(newConfig, newConfig);
217 
218  int currentIndex = mConfigFilesComboBox->findText(newConfig);
219  mConfigFilesComboBox->setCurrentIndex(currentIndex);
220 }
221 
222 int ToolConfigureGroupBox::addConfigurationToComboBox(QString displayName, QString absoluteFilePath)
223 {
224  mConfigFilesComboBox->addItem(displayName);
225  int index = mConfigFilesComboBox->findText(displayName);
226  mConfigFilesComboBox->setItemData(index, absoluteFilePath, Qt::ToolTipRole);
227 
228  return index;
229 }
230 
231 TrackerConfiguration::Configuration ToolConfigureGroupBox::getCurrentConfiguration()
232 {
234  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
235  QString application = mStateService->getApplicationStateName();
236 
237  QString filename = mConfigFileLineEdit->text();
238  QString filepath = config->getConfigurationApplicationsPath();
239 
240  retval.mUid = QString("%1/%2.xml").arg(filepath).arg(filename);
241  retval.mClinicalApplication = application;
242  retval.mTrackingSystemImplementation = this->mTrackingSystemImplementation;
243  retval.mTrackingSystemName = mTrackingSystemSelector->getValue();
244  retval.mTools = mToolListWidget->getTools();
245  retval.mReferenceTool = mReferenceComboBox->itemData(mReferenceComboBox->currentIndex(), Qt::ToolTipRole).toString();
246 
247  // TODO fix retval.mToolList. See TrackerConfigurationImpl::saveConfiguration()
248  // Will need to keep mOpenIGTLinkImageId and mOpenIGTLinkTransformId when creating return value
249  // For now ConfigurationFileParser::saveConfiguration() refuse to save openigtlink tracking files
250  // to prevent these from being destroyed.
251 
252  return retval;
253 }
254 
255 QString ToolConfigureGroupBox::getCurrentConfigFilePath()
256 {
257  QString configAbsoluteFilePath = mConfigFilesComboBox->itemData(mConfigFilesComboBox->currentIndex(), Qt::ToolTipRole).toString();
258  return configAbsoluteFilePath;
259 }
260 
261 void ToolConfigureGroupBox::populateReference()
262 {
263  mReferenceComboBox->clear();
264 
265  int currentIndex = -1;
266 
267  TrackerConfigurationPtr config = mTrackingService->getConfiguration();//Get last config. No problem here?
268 
269  // populate list
270  QStringList selectedTools = mToolListWidget->getTools();
271  foreach(QString string, selectedTools)
272  {
273  if (config->getTool(string).mIsReference)
274  currentIndex = this->addRefrenceToComboBox(string);
275  }
276 
277  // look for a selected reference
278  QString reference = config->getConfiguration(this->getCurrentConfigFilePath()).mReferenceTool;
279  currentIndex = this->addRefrenceToComboBox(reference);
280 
281  // always select a reference if available:
282  if (currentIndex<0)
283  currentIndex = 0;
284 
285  mReferenceComboBox->setCurrentIndex(currentIndex);
286 }
287 
288 int ToolConfigureGroupBox::addRefrenceToComboBox(QString absoluteRefereneFilePath)
289 {
290  int index;
291 
292  QFile file(absoluteRefereneFilePath);
293  QFileInfo info(file);
294  QString refUid = info.dir().dirName();
295 
296  QStringList selectedTools = mToolListWidget->getTools();
297  if (!selectedTools.count(absoluteRefereneFilePath))
298  return -1;
299 
300  if (refUid.isEmpty())
301  return -1;
302 
303  if (mReferenceComboBox->findText(refUid) < 0)
304  mReferenceComboBox->addItem(refUid);
305  index = mReferenceComboBox->findText(refUid);
306  mReferenceComboBox->setItemData(index, info.absoluteFilePath(), Qt::ToolTipRole);
307 
308  return index;
309 }
310 
311 
312 }//namespace cx
tsPOLARIS
NDIs Polaris tracker.
void filterSlot(QStringList trackingsystemFilter)
filters the tools on tracking system
void applicationStateChanged()
boost::shared_ptr< class StateService > StateServicePtr
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:755
void setCurrentlySelectedCofiguration(QString configAbsoluteFilePath)
boost::shared_ptr< class TrackingService > TrackingServicePtr
Helper class for tool config list display.
boost::shared_ptr< class TrackerConfiguration > TrackerConfigurationPtr
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr
void configSlot(QStringList toolsAbsoluteFilePath)
adds all input tools to the list
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
QString requestSaveConfigurationSlot()
will save the currently selected configuration if its been edited
QStringList getTools()
get absolute file path to all tools currently in the list
not available
Definition: cxTool.h:74
QString mUid
absolute path and filename for the new config file
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
QWidget * sscCreateDataWidget(QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
QString getCurrenctlySelectedConfiguration() const
StringPropertyBasePtr getTrackingSystemSelector()
ToolConfigureGroupBox(TrackingServicePtr trackingService, StateServicePtr stateService, QWidget *parent=NULL)
void toolSelected(QString absoluteFilePath)
QString enum2string(const ENUM &val)
Namespace for all CustusX production code.