Fraxinus  17.12
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) 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 <cxToolConfigureWidget.h>
34 
35 #include <QVBoxLayout>
36 #include <QHBoxLayout>
37 #include <QComboBox>
38 #include <QLabel>
39 #include <QDir>
40 #include <QLineEdit>
41 #include "cxEnumConverter.h"
42 #include "cxLogger.h"
43 #include "cxStateService.h"
44 #include "cxTrackingService.h"
45 #include "cxSelectionGroupBox.h"
46 #include "cxToolListWidget.h"
47 #include "cxHelperWidgets.h"
48 #include "cxTrackerConfiguration.h"
49 
50 namespace cx
51 {
52 
53 ToolConfigureGroupBox::ToolConfigureGroupBox(TrackingServicePtr trackingService, StateServicePtr stateService, QWidget* parent) :
54  QGroupBox(parent),
55  mConfigFilesComboBox(new QComboBox()),
56  mConfigFileLineEdit(new QLineEdit()),
57  mReferenceComboBox(new QComboBox()),
58  mModified(false),
59  mTrackingService(trackingService),
60  mStateService(stateService)
61 {
62  connect(stateService.get(), &StateService::applicationStateChanged, this, &ToolConfigureGroupBox::onApplicationStateChanged);
63 
64  mConfigFilesComboBox->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Expanding);
65  //mConfigFilesComboBox->setMinimumSize(200, 0);
66  //mConfigFilesComboBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
67 
68  mToolListWidget = new ConfigToolListWidget(trackingService, NULL);
69 
70  this->createTrackingSystemSelector();
71 
72 // QGroupBox* toolGroupBox = new QGroupBox();
73 // toolGroupBox->setTitle("Tools");
74 // QVBoxLayout* toolLayout = new QVBoxLayout();
75 // toolGroupBox->setLayout(toolLayout);
76 // toolLayout->addWidget(mToolListWidget);
77 
78  QGridLayout* layout = new QGridLayout(this);
79  int row=0;
80  layout->addWidget(new QLabel("Configuration: "), row, 0, 1, 1);
81  layout->addWidget(mConfigFilesComboBox, row, 1, 1, 1);
82  row++;
83  layout->addWidget(new QLabel("File name: "), row, 0, 1, 1);
84  layout->addWidget(mConfigFileLineEdit, row, 1, 1, 1);
85  row++;
86  sscCreateDataWidget(this, mTrackingSystemSelector, layout, row);
87  row++;
88  layout->addWidget(mToolListWidget, row, 0, 1, 2);
89  layout->setRowStretch(row, 1);
90  row++;
91 // layout->addWidget(new QLabel("Reference: "), row, 0, 1, 1);
92 // layout->addWidget(mReferenceComboBox, row, 1, 1, 1);
93 
94  QHBoxLayout* refLayout = new QHBoxLayout;
95  refLayout->addWidget(new QLabel("Reference: "));
96  refLayout->addWidget(mReferenceComboBox);
97  refLayout->setMargin(0);
98  layout->addLayout(refLayout, row, 0, 1, 2);
99 
100  //changes due to programming actions
101  connect(mConfigFilesComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(configChangedSlot()));
102  connect(mToolListWidget, SIGNAL(listSizeChanged()), this, SLOT(toolsChangedSlot()));
103  connect(mToolListWidget, SIGNAL(toolSelected(QString)), this, SIGNAL(toolSelected(QString)));
104 
105  //changes due to user actions
106  connect(mConfigFileLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(configEditedSlot()));
107  connect(mToolListWidget, SIGNAL(userChangedList()), this, SLOT(configEditedSlot()));
108  connect(mReferenceComboBox, SIGNAL(activated(int)), this, SLOT(configEditedSlot()));
109 
110  this->onApplicationStateChanged();
111 }
112 
114 {}
115 
117 {
118  QString cleanPath = QDir(configAbsoluteFilePath).absolutePath();
119  int currentIndex = mConfigFilesComboBox->findData(cleanPath, Qt::ToolTipRole);
120  if(currentIndex < 0)
121  {
122  currentIndex = 0;
123  if (!configAbsoluteFilePath.isEmpty())
124  reportWarning("Tool configuration doesn't exist: " + cleanPath);
125  }
126  mConfigFilesComboBox->setCurrentIndex(currentIndex);
127 
128  mModified = false;
129 }
130 
132 {
133  QString retval;
134  retval = mConfigFilesComboBox->itemData(mConfigFilesComboBox->currentIndex(), Qt::ToolTipRole).toString();
135  return retval;
136 }
137 
138 void ToolConfigureGroupBox::createTrackingSystemSelector()
139 {
140  QString defaultValue = "";
141  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
142  mTrackingSystemSelector = StringProperty::initialize("trackingsystem", "Tracking System",
143  "Select tracking system to use",
144  defaultValue,
145  config->getSupportedTrackingSystems(),
146  QDomNode());
147  connect(mTrackingSystemSelector.get(), SIGNAL(changed()), this, SLOT(filterToolsSlot()));
148 }
149 
151 {
152  return mTrackingSystemSelector;
153 }
154 
156 {
157  QString retval;
158 
159  if(!mModified)
160  return retval;
161 
162  // deconfigure toolmanager in order to be able to reread config data
163  mTrackingService->setState(Tool::tsNONE);
164 
165  TrackerConfiguration::Configuration current = this->getCurrentConfiguration();
166  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
167  config->saveConfiguration(current);
168  mModified = false;
169 
170  this->populateConfigurations();
171 
172  return current.mUid;
173 }
174 
175 void ToolConfigureGroupBox::onApplicationStateChanged()
176 {
177  QString application = mStateService->getApplicationStateName();
178  this->setTitle("Tool configurations for "+ application);
179  this->populateConfigurations();
180 }
181 
182 void ToolConfigureGroupBox::configChangedSlot()
183 {
184  QString uid = mConfigFilesComboBox->itemData(mConfigFilesComboBox->currentIndex(),
185  Qt::ToolTipRole).toString();
186 
187  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
188  TrackerConfiguration::Configuration data = config->getConfiguration(uid);
189  bool isNewConfig = mConfigFilesComboBox->currentText().contains("<new config>");
190 
191  if (isNewConfig)
192  {
194  data.mName = "MyConfig";
195  }
196 
197  mConfigFileLineEdit->setText(data.mName);
198  mConfigFileLineEdit->setEnabled(isNewConfig);
199  mConfigFileLineEdit->setToolTip(data.mUid);
200  mModified = true;
201  mTrackingSystemSelector->setValue(data.mTrackingSystem);
202  mToolListWidget->configSlot(data.mTools);
203 }
204 
205 void ToolConfigureGroupBox::configEditedSlot()
206 {
207  mModified = true;
208 }
209 
210 void ToolConfigureGroupBox::toolsChangedSlot()
211 {
212  this->populateReference();
213  this->filterToolsSlot();
214 }
215 
216 void ToolConfigureGroupBox::filterToolsSlot()
217 {
218  QString ts = mTrackingSystemSelector->getValue();
219  mToolListWidget->filterSlot(QStringList() << ts);
220  mModified = true;
221 }
222 
223 void ToolConfigureGroupBox::populateConfigurations()
224 {
225  mConfigFilesComboBox->clear();
226 
227  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
228  QStringList configurations = config->getConfigurationsGivenApplication();
229 
230  foreach(QString filename, configurations)
231  {
232  TrackerConfiguration::Configuration configuration = config->getConfiguration(filename);
233  this->addConfigurationToComboBox(configuration.mName, configuration.mUid);
234  }
235  QString newConfig = "<new config>";
236  this->addConfigurationToComboBox(newConfig, newConfig);
237 
238  int currentIndex = mConfigFilesComboBox->findText(newConfig);
239  mConfigFilesComboBox->setCurrentIndex(currentIndex);
240 }
241 
242 int ToolConfigureGroupBox::addConfigurationToComboBox(QString displayName, QString absoluteFilePath)
243 {
244  mConfigFilesComboBox->addItem(displayName);
245  int index = mConfigFilesComboBox->findText(displayName);
246  mConfigFilesComboBox->setItemData(index, absoluteFilePath, Qt::ToolTipRole);
247 
248  return index;
249 }
250 
251 TrackerConfiguration::Configuration ToolConfigureGroupBox::getCurrentConfiguration()
252 {
254  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
255  QString application = mStateService->getApplicationStateName();
256 
257  QString filename = mConfigFileLineEdit->text();
258  QString filepath = config->getConfigurationApplicationsPath();
259 
260  retval.mUid = QString("%1/%2.xml").arg(filepath).arg(filename);
261  retval.mClinicalApplication = application;
262  retval.mTrackingSystem = mTrackingSystemSelector->getValue();
263  retval.mTools = mToolListWidget->getTools();
264  retval.mReferenceTool = mReferenceComboBox->itemData(mReferenceComboBox->currentIndex(), Qt::ToolTipRole).toString();
265 
266  return retval;
267 }
268 
269 void ToolConfigureGroupBox::populateReference()
270 {
271  mReferenceComboBox->clear();
272 
273  int currentIndex = -1;
274 
275  TrackerConfigurationPtr config = mTrackingService->getConfiguration();
276 
277  // populate list
278  QStringList selectedTools = mToolListWidget->getTools();
279  foreach(QString string, selectedTools)
280  {
281  if (config->getTool(string).mIsReference)
282  currentIndex = this->addRefrenceToComboBox(string);
283  }
284 
285  // look for a selected reference
286  QString configAbsoluteFilePath = mConfigFilesComboBox->itemData(mConfigFilesComboBox->currentIndex(), Qt::ToolTipRole).toString();
287  QString reference = config->getConfiguration(configAbsoluteFilePath).mReferenceTool;
288  currentIndex = this->addRefrenceToComboBox(reference);
289 
290  // always select a reference if available:
291  if (currentIndex<0)
292  currentIndex = 0;
293 
294  mReferenceComboBox->setCurrentIndex(currentIndex);
295 }
296 
297 int ToolConfigureGroupBox::addRefrenceToComboBox(QString absoluteRefereneFilePath)
298 {
299  int index;
300 
301  QFile file(absoluteRefereneFilePath);
302  QFileInfo info(file);
303  QString refUid = info.dir().dirName();
304 
305  QStringList selectedTools = mToolListWidget->getTools();
306  if (!selectedTools.count(absoluteRefereneFilePath))
307  return -1;
308 
309  if (refUid.isEmpty())
310  return -1;
311 
312  if (mReferenceComboBox->findText(refUid) < 0)
313  mReferenceComboBox->addItem(refUid);
314  index = mReferenceComboBox->findText(refUid);
315  mReferenceComboBox->setItemData(index, info.absoluteFilePath(), Qt::ToolTipRole);
316 
317  return index;
318 }
319 
320 
321 }//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:91
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:95
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.