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