CustusX  15.3.3-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 "cxSpaceEditWidget.h"
34 #include <iostream>
35 #include "cxTypeConversions.h"
36 #include "cxHelperWidgets.h"
37 #include "cxLogger.h"
38 #include "cxDefinitionStrings.h"
39 
40 namespace cx
41 {
42 
44  QGridLayout* gridLayout, int row) :
45  BaseWidget(parent, "SpaceEditWidget", "SpaceEditWidget")
46 {
47  CX_ASSERT(dataInterface->getAllowOnlyValuesInRange()==true);
48 
49  this->setEnabled(dataInterface->getEnabled());
50 
51  mData = dataInterface;
52  connect(mData.get(), SIGNAL(changed()), this, SLOT(setModified()));
53 
54  mLabel = new QLabel(this);
55  mLabel->setText(mData->getDisplayName());
56 
57  mIdCombo = new QComboBox(this);
58  connect(mIdCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(comboIndexChanged()));
59 
60  mRefCombo = new QComboBox(this);
61  connect(mRefCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(comboIndexChanged()));
62 
63  if (gridLayout) // add to input gridlayout
64  {
65  gridLayout->addLayout(mergeWidgetsIntoHBoxLayout(mLabel, addDummyMargin(this)), row, 0);
66  gridLayout->addWidget(mIdCombo, row, 1);
67  gridLayout->addWidget(mRefCombo, row, 2);
68  }
69  else // add directly to this
70  {
71  mTopLayout = new QHBoxLayout;
72  mTopLayout->setMargin(0);
73  this->setLayout(mTopLayout);
74 
75  mTopLayout->addWidget(mLabel);
76  mTopLayout->addWidget(mIdCombo, 1);
77  mTopLayout->addWidget(mRefCombo, 2);
78  }
79 
80  this->setModified();
81 }
82 
84 {
85  return "<html></html>";
86 }
87 
88 void SpaceEditWidget::attemptSetValue(COORDINATE_SYSTEM id, QString ref)
89 {
90 // std::cout << "setting space " << QString::number(id) << " -- " << ref << std::endl;
91  Space space(id, ref);
92 
93  QStringList refs = this->getAvailableSpaceRefs(space.mId);
94  if (refs.isEmpty())
95  space.mRefObject = "";
96  else if (!refs.contains(space.mRefObject))
97  space.mRefObject = refs[0];
98 
99 
100  std::vector<Space> range = mData->getValueRange();
101  if (!count(range.begin(), range.end(), space))
102  {
103  this->setModified(); // repaint with old data
104  return;
105  }
106 
107 // std::cout << "setting space3 " << space.toString() << std::endl;
108  mData->setValue(space);
109 }
110 
111 void SpaceEditWidget::comboIndexChanged()
112 {
113  this->attemptSetValue(COORDINATE_SYSTEM(mIdCombo->currentData().toInt()), mRefCombo->currentData().toString());
114 }
115 
117 {
118  mLabel->setVisible(on);
119 }
120 
121 
122 void SpaceEditWidget::rebuildIdCombobox()
123 {
124  mIdCombo->blockSignals(true);
125  mIdCombo->clear();
126 
127  Space currentValue = mData->getValue();
128  std::vector<COORDINATE_SYSTEM> ids = this->getAvailableSpaceIds();
129 
130  int currentIndex = -1;
131  for (int i = 0; i < ids.size(); ++i)
132  {
133  COORDINATE_SYSTEM id = ids[i];
134  QString name = enum2string<COORDINATE_SYSTEM>(id);
135 
136  mIdCombo->addItem(name);
137  mIdCombo->setItemData(i, QVariant::fromValue<int>(id));
138  if (id == currentValue.mId)
139  currentIndex = i;
140  }
141  mIdCombo->setCurrentIndex(currentIndex);
142 }
143 
144 std::vector<COORDINATE_SYSTEM> SpaceEditWidget::getAvailableSpaceIds()
145 {
146  std::vector<COORDINATE_SYSTEM> retval;
147  std::vector<Space> range = mData->getValueRange();
148 
149  for (unsigned i=0; i<range.size(); ++i)
150  {
151  if (!count(retval.begin(), retval.end(), range[i].mId))
152  retval.push_back(range[i].mId);
153  }
154 
155  return retval;
156 }
157 
158 void SpaceEditWidget::rebuildRefCombobox()
159 {
160  mRefCombo->blockSignals(true);
161  mRefCombo->clear();
162 
163  Space currentValue = mData->getValue();
164  QStringList refs = this->getAvailableSpaceRefs(currentValue.mId);
165 
166  int currentIndex = -1;
167  for (int i = 0; i < refs.size(); ++i)
168  {
169  QString ref = refs[i];
170  QString name = mData->convertRefObjectInternal2Display(ref);
171  mRefCombo->addItem(name);
172  mRefCombo->setItemData(i, ref);
173  if (ref == currentValue.mRefObject)
174  currentIndex = i;
175  }
176  mRefCombo->setCurrentIndex(currentIndex);
177  mRefCombo->setVisible(!refs.empty());
178 }
179 
180 QStringList SpaceEditWidget::getAvailableSpaceRefs(COORDINATE_SYSTEM id)
181 {
182  QStringList retval;
183  std::vector<Space> range = mData->getValueRange();
184 
185  for (unsigned i=0; i<range.size(); ++i)
186  {
187  if (range[i].mId == id)
188  retval.push_back(range[i].mRefObject);
189  }
190  retval.removeDuplicates();
191  retval.removeAll("");
192 
193  return retval;
194 }
195 
196 void SpaceEditWidget::prePaintEvent()
197 {
198  mRefCombo->blockSignals(true);
199  mIdCombo->blockSignals(true);
200  mIdCombo->clear();
201  mRefCombo->clear();
202 
203  this->setEnabled(mData->getEnabled());
204  mLabel->setEnabled(mData->getEnabled());
205  mIdCombo->setEnabled(mData->getEnabled());
206  mRefCombo->setEnabled(mData->getEnabled());
207 
208  this->rebuildIdCombobox();
209  this->rebuildRefCombobox();
210 
211  mIdCombo->setToolTip(QString("%1\nSet space type").arg(mData->getHelp()));
212  mRefCombo->setToolTip(QString("%1\nSet space identifier").arg(mData->getHelp()));
213  mLabel->setToolTip(mData->getHelp());
214 
215  mRefCombo->blockSignals(false);
216  mIdCombo->blockSignals(false);
217 }
218 
219 } // namespace cx
QWidget * addDummyMargin(QWidget *widget)
#define CX_ASSERT(statement)
Definition: cxLogger.h:128
boost::shared_ptr< SpacePropertyBase > SpacePropertyBasePtr
QHBoxLayout * mergeWidgetsIntoHBoxLayout(QWidget *first, QWidget *second)
COORDINATE_SYSTEM mId
the type of coordinate system
CoordinateSystem Space
Identification of a Coordinate system.
virtual QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
QHBoxLayout * mTopLayout
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
SpaceEditWidget(QWidget *parent, SpacePropertyBasePtr, QGridLayout *gridLayout=0, int row=0)