CustusX  15.4.0-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  QStringList configurations = config->getConfigurationsGivenApplication();
227 
228  foreach(QString filename, configurations)
229  {
230  TrackerConfiguration::Configuration configuration = config->getConfiguration(filename);
231  this->addConfigurationToComboBox(configuration.mName, configuration.mUid);
232  }
233  QString newConfig = "<new config>";
234  this->addConfigurationToComboBox(newConfig, newConfig);
235 
236  int currentIndex = mConfigFilesComboBox->findText(newConfig);
237  mConfigFilesComboBox->setCurrentIndex(currentIndex);
238 }
239 
240 int ToolConfigureGroupBox::addConfigurationToComboBox(QString displayName, QString absoluteFilePath)
241 {
242  mConfigFilesComboBox->addItem(displayName);
243  int index = mConfigFilesComboBox->findText(displayName);
244  mConfigFilesComboBox->setItemData(index, absoluteFilePath, Qt::ToolTipRole);
245 
246  return index;
247 }
248 
249 TrackerConfiguration::Configuration ToolConfigureGroupBox::getCurrentConfiguration()
250 {
251  TrackerConfiguration::Configuration retval;
252  TrackerConfigurationPtr config = trackingService()->getConfiguration();
253  QString application = stateService()->getApplicationStateName();
254 
255  QString filename = mConfigFileLineEdit->text();
256  QString filepath = config->getConfigurationApplicationsPath();
257 
258  retval.mUid = QString("%1/%2.xml").arg(filepath).arg(filename);
259  retval.mClinicalApplication = application;
260  retval.mTrackingSystem = mTrackingSystemSelector->getValue();
261  retval.mTools = mToolListWidget->getTools();
262  retval.mReferenceTool = mReferenceComboBox->itemData(mReferenceComboBox->currentIndex(), Qt::ToolTipRole).toString();
263 
264  return retval;
265 }
266 
267 void ToolConfigureGroupBox::populateReference()
268 {
269  mReferenceComboBox->clear();
270 
271  int currentIndex = -1;
272 
273  TrackerConfigurationPtr config = trackingService()->getConfiguration();
274 
275  // populate list
276  QStringList selectedTools = mToolListWidget->getTools();
277  foreach(QString string, selectedTools)
278  {
279  if (config->getTool(string).mIsReference)
280  currentIndex = this->addRefrenceToComboBox(string);
281  }
282 
283  // look for a selected reference
284  QString configAbsoluteFilePath = mConfigFilesComboBox->itemData(mConfigFilesComboBox->currentIndex(), Qt::ToolTipRole).toString();
285  QString reference = config->getConfiguration(configAbsoluteFilePath).mReferenceTool;
286  currentIndex = this->addRefrenceToComboBox(reference);
287 
288  // always select a reference if available:
289  if (currentIndex<0)
290  currentIndex = 0;
291 
292  mReferenceComboBox->setCurrentIndex(currentIndex);
293 }
294 
295 int ToolConfigureGroupBox::addRefrenceToComboBox(QString absoluteRefereneFilePath)
296 {
297  int index;
298 
299  QFile file(absoluteRefereneFilePath);
300  QFileInfo info(file);
301  QString refUid = info.dir().dirName();
302 
303  QStringList selectedTools = mToolListWidget->getTools();
304  if (!selectedTools.count(absoluteRefereneFilePath))
305  return -1;
306 
307  if (refUid.isEmpty())
308  return -1;
309 
310  if (mReferenceComboBox->findText(refUid) < 0)
311  mReferenceComboBox->addItem(refUid);
312  index = mReferenceComboBox->findText(refUid);
313  mReferenceComboBox->setItemData(index, info.absoluteFilePath(), Qt::ToolTipRole);
314 
315  return index;
316 }
317 
318 
319 }//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