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