Fraxinus  18.10
An IGT application
cxSpaceEditWidget.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 "cxSpaceEditWidget.h"
13 #include <iostream>
14 #include "cxTypeConversions.h"
15 #include "cxHelperWidgets.h"
16 #include "cxLogger.h"
17 #include "cxDefinitionStrings.h"
18 
19 namespace cx
20 {
21 
23  QGridLayout* gridLayout, int row) :
24  BaseWidget(parent, "SpaceEditWidget", "SpaceEditWidget")
25 {
26  this->setToolTip("Edit a space (coordinate system)");
27  CX_ASSERT(dataInterface->getAllowOnlyValuesInRange()==true);
28 
29  this->setEnabled(dataInterface->getEnabled());
30 
31  mData = dataInterface;
32  connect(mData.get(), SIGNAL(changed()), this, SLOT(setModified()));
33 
34  mLabel = new QLabel(this);
35  mLabel->setText(mData->getDisplayName());
36 
37  mIdCombo = new QComboBox(this);
38  connect(mIdCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(comboIndexChanged()));
39 
40  mRefCombo = new QComboBox(this);
41  connect(mRefCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(comboIndexChanged()));
42 
43  if (gridLayout) // add to input gridlayout
44  {
45  gridLayout->addLayout(mergeWidgetsIntoHBoxLayout(mLabel, addDummyMargin(this)), row, 0);
46  gridLayout->addWidget(mIdCombo, row, 1);
47  gridLayout->addWidget(mRefCombo, row, 2);
48  }
49  else // add directly to this
50  {
51  mTopLayout = new QHBoxLayout;
52  mTopLayout->setMargin(0);
53  this->setLayout(mTopLayout);
54 
55  mTopLayout->addWidget(mLabel);
56  mTopLayout->addWidget(mIdCombo, 1);
57  mTopLayout->addWidget(mRefCombo, 2);
58  }
59 
60  this->setModified();
61 }
62 
63 void SpaceEditWidget::attemptSetValue(COORDINATE_SYSTEM id, QString ref)
64 {
65  Space space(id, ref);
66 
67  QStringList refs = this->getAvailableSpaceRefs(space.mId);
68  if (refs.isEmpty())
69  space.mRefObject = "";
70  else if (!refs.contains(space.mRefObject))
71  space.mRefObject = refs[0];
72 
73 
74  std::vector<Space> range = mData->getValueRange();
75  if (!count(range.begin(), range.end(), space))
76  {
77  this->setModified(); // repaint with old data
78  return;
79  }
80 
81  mData->setValue(space);
82 }
83 
84 void SpaceEditWidget::comboIndexChanged()
85 {
86  this->attemptSetValue(COORDINATE_SYSTEM(mIdCombo->currentData().toInt()), mRefCombo->currentData().toString());
87 }
88 
90 {
91  mLabel->setVisible(on);
92 }
93 
94 
95 void SpaceEditWidget::rebuildIdCombobox()
96 {
97  mIdCombo->clear();
98 
99  Space currentValue = mData->getValue();
100  std::vector<COORDINATE_SYSTEM> ids = this->getAvailableSpaceIds();
101 
102  int currentIndex = -1;
103  for (int i = 0; i < ids.size(); ++i)
104  {
105  COORDINATE_SYSTEM id = ids[i];
106  QString name = enum2string<COORDINATE_SYSTEM>(id);
107 
108  mIdCombo->addItem(name);
109  mIdCombo->setItemData(i, QVariant::fromValue<int>(id));
110  if (id == currentValue.mId)
111  currentIndex = i;
112  }
113  mIdCombo->setCurrentIndex(currentIndex);
114 }
115 
116 std::vector<COORDINATE_SYSTEM> SpaceEditWidget::getAvailableSpaceIds()
117 {
118  std::vector<COORDINATE_SYSTEM> retval;
119  std::vector<Space> range = mData->getValueRange();
120 
121  for (unsigned i=0; i<range.size(); ++i)
122  {
123  if (!count(retval.begin(), retval.end(), range[i].mId))
124  retval.push_back(range[i].mId);
125  }
126 
127  return retval;
128 }
129 
130 void SpaceEditWidget::rebuildRefCombobox()
131 {
132  mRefCombo->clear();
133 
134  Space currentValue = mData->getValue();
135  QStringList refs = this->getAvailableSpaceRefs(currentValue.mId);
136 
137  int currentIndex = -1;
138  for (int i = 0; i < refs.size(); ++i)
139  {
140  QString ref = refs[i];
141  QString name = mData->convertRefObjectInternal2Display(ref);
142  mRefCombo->addItem(name);
143  mRefCombo->setItemData(i, ref);
144  if (ref == currentValue.mRefObject)
145  currentIndex = i;
146  }
147  mRefCombo->setCurrentIndex(currentIndex);
148 
149  // Cannot set visibility inside a paint event.
150  // Instead do it by placing a call in the queue.
151  this->setRefComboVisibilityQueued();
152 }
153 
154 void SpaceEditWidget::setRefComboVisibilityQueued()
155 {
156  bool show = mRefCombo->count();
157 
158  if (mRefCombo->isVisible()==show)
159  return;
160 
161  if (show)
162  QMetaObject::invokeMethod(mRefCombo, "show", Qt::QueuedConnection);
163  else
164  QMetaObject::invokeMethod(mRefCombo, "hide", Qt::QueuedConnection);
165 }
166 
167 QStringList SpaceEditWidget::getAvailableSpaceRefs(COORDINATE_SYSTEM id)
168 {
169  QStringList retval;
170  std::vector<Space> range = mData->getValueRange();
171 
172  for (unsigned i=0; i<range.size(); ++i)
173  {
174  if (range[i].mId == id)
175  retval.push_back(range[i].mRefObject);
176  }
177  retval.removeDuplicates();
178  retval.removeAll("");
179 
180  return retval;
181 }
182 
183 void SpaceEditWidget::prePaintEvent()
184 {
185  mRefCombo->blockSignals(true);
186  mIdCombo->blockSignals(true);
187  mIdCombo->clear();
188  mRefCombo->clear();
189 
190  this->setEnabled(mData->getEnabled());
191  mLabel->setEnabled(mData->getEnabled());
192  mIdCombo->setEnabled(mData->getEnabled());
193  mRefCombo->setEnabled(mData->getEnabled());
194 
195  this->rebuildIdCombobox();
196  this->rebuildRefCombobox();
197 
198  mIdCombo->setToolTip(QString("%1\nSet space type").arg(mData->getHelp()));
199  mRefCombo->setToolTip(QString("%1\nSet space identifier").arg(mData->getHelp()));
200  mLabel->setToolTip(mData->getHelp());
201 
202  mRefCombo->blockSignals(false);
203  mIdCombo->blockSignals(false);
204 }
205 
206 } // namespace cx
QWidget * addDummyMargin(QWidget *widget)
#define CX_ASSERT(statement)
Definition: cxLogger.h:116
boost::shared_ptr< SpacePropertyBase > SpacePropertyBasePtr
QHBoxLayout * mergeWidgetsIntoHBoxLayout(QWidget *first, QWidget *second)
COORDINATE_SYSTEM mId
the type of coordinate system
Identification of a Coordinate system.
QString mRefObject
for tool, sensor and data we need a object uid to define the coordinate system
QHBoxLayout * mTopLayout
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
SpaceEditWidget(QWidget *parent, SpacePropertyBasePtr, QGridLayout *gridLayout=0, int row=0)
Namespace for all CustusX production code.