Fraxinus  16.5.0-fx-rc1
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  this->setToolTip("Edit a space (coordinate system)");
48  CX_ASSERT(dataInterface->getAllowOnlyValuesInRange()==true);
49 
50  this->setEnabled(dataInterface->getEnabled());
51 
52  mData = dataInterface;
53  connect(mData.get(), SIGNAL(changed()), this, SLOT(setModified()));
54 
55  mLabel = new QLabel(this);
56  mLabel->setText(mData->getDisplayName());
57 
58  mIdCombo = new QComboBox(this);
59  connect(mIdCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(comboIndexChanged()));
60 
61  mRefCombo = new QComboBox(this);
62  connect(mRefCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(comboIndexChanged()));
63 
64  if (gridLayout) // add to input gridlayout
65  {
66  gridLayout->addLayout(mergeWidgetsIntoHBoxLayout(mLabel, addDummyMargin(this)), row, 0);
67  gridLayout->addWidget(mIdCombo, row, 1);
68  gridLayout->addWidget(mRefCombo, row, 2);
69  }
70  else // add directly to this
71  {
72  mTopLayout = new QHBoxLayout;
73  mTopLayout->setMargin(0);
74  this->setLayout(mTopLayout);
75 
76  mTopLayout->addWidget(mLabel);
77  mTopLayout->addWidget(mIdCombo, 1);
78  mTopLayout->addWidget(mRefCombo, 2);
79  }
80 
81  this->setModified();
82 }
83 
84 void SpaceEditWidget::attemptSetValue(COORDINATE_SYSTEM id, QString ref)
85 {
86 // std::cout << "setting space " << QString::number(id) << " -- " << ref << std::endl;
87  Space space(id, ref);
88 
89  QStringList refs = this->getAvailableSpaceRefs(space.mId);
90  if (refs.isEmpty())
91  space.mRefObject = "";
92  else if (!refs.contains(space.mRefObject))
93  space.mRefObject = refs[0];
94 
95 
96  std::vector<Space> range = mData->getValueRange();
97  if (!count(range.begin(), range.end(), space))
98  {
99  this->setModified(); // repaint with old data
100  return;
101  }
102 
103 // std::cout << "setting space3 " << space.toString() << std::endl;
104  mData->setValue(space);
105 }
106 
107 void SpaceEditWidget::comboIndexChanged()
108 {
109  this->attemptSetValue(COORDINATE_SYSTEM(mIdCombo->currentData().toInt()), mRefCombo->currentData().toString());
110 }
111 
113 {
114  mLabel->setVisible(on);
115 }
116 
117 
118 void SpaceEditWidget::rebuildIdCombobox()
119 {
120  mIdCombo->blockSignals(true);
121  mIdCombo->clear();
122 
123  Space currentValue = mData->getValue();
124  std::vector<COORDINATE_SYSTEM> ids = this->getAvailableSpaceIds();
125 
126  int currentIndex = -1;
127  for (int i = 0; i < ids.size(); ++i)
128  {
129  COORDINATE_SYSTEM id = ids[i];
130  QString name = enum2string<COORDINATE_SYSTEM>(id);
131 
132  mIdCombo->addItem(name);
133  mIdCombo->setItemData(i, QVariant::fromValue<int>(id));
134  if (id == currentValue.mId)
135  currentIndex = i;
136  }
137  mIdCombo->setCurrentIndex(currentIndex);
138 }
139 
140 std::vector<COORDINATE_SYSTEM> SpaceEditWidget::getAvailableSpaceIds()
141 {
142  std::vector<COORDINATE_SYSTEM> retval;
143  std::vector<Space> range = mData->getValueRange();
144 
145  for (unsigned i=0; i<range.size(); ++i)
146  {
147  if (!count(retval.begin(), retval.end(), range[i].mId))
148  retval.push_back(range[i].mId);
149  }
150 
151  return retval;
152 }
153 
154 void SpaceEditWidget::rebuildRefCombobox()
155 {
156  mRefCombo->blockSignals(true);
157  mRefCombo->clear();
158 
159  Space currentValue = mData->getValue();
160  QStringList refs = this->getAvailableSpaceRefs(currentValue.mId);
161 
162  int currentIndex = -1;
163  for (int i = 0; i < refs.size(); ++i)
164  {
165  QString ref = refs[i];
166  QString name = mData->convertRefObjectInternal2Display(ref);
167  mRefCombo->addItem(name);
168  mRefCombo->setItemData(i, ref);
169  if (ref == currentValue.mRefObject)
170  currentIndex = i;
171  }
172  mRefCombo->setCurrentIndex(currentIndex);
173  mRefCombo->setVisible(!refs.empty());
174 }
175 
176 QStringList SpaceEditWidget::getAvailableSpaceRefs(COORDINATE_SYSTEM id)
177 {
178  QStringList retval;
179  std::vector<Space> range = mData->getValueRange();
180 
181  for (unsigned i=0; i<range.size(); ++i)
182  {
183  if (range[i].mId == id)
184  retval.push_back(range[i].mRefObject);
185  }
186  retval.removeDuplicates();
187  retval.removeAll("");
188 
189  return retval;
190 }
191 
192 void SpaceEditWidget::prePaintEvent()
193 {
194  mRefCombo->blockSignals(true);
195  mIdCombo->blockSignals(true);
196  mIdCombo->clear();
197  mRefCombo->clear();
198 
199  this->setEnabled(mData->getEnabled());
200  mLabel->setEnabled(mData->getEnabled());
201  mIdCombo->setEnabled(mData->getEnabled());
202  mRefCombo->setEnabled(mData->getEnabled());
203 
204  this->rebuildIdCombobox();
205  this->rebuildRefCombobox();
206 
207  mIdCombo->setToolTip(QString("%1\nSet space type").arg(mData->getHelp()));
208  mRefCombo->setToolTip(QString("%1\nSet space identifier").arg(mData->getHelp()));
209  mLabel->setToolTip(mData->getHelp());
210 
211  mRefCombo->blockSignals(false);
212  mIdCombo->blockSignals(false);
213 }
214 
215 } // namespace cx
QWidget * addDummyMargin(QWidget *widget)
#define CX_ASSERT(statement)
Definition: cxLogger.h:131
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.
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)