NorMIT-nav  16.5
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  Space space(id, ref);
87 
88  QStringList refs = this->getAvailableSpaceRefs(space.mId);
89  if (refs.isEmpty())
90  space.mRefObject = "";
91  else if (!refs.contains(space.mRefObject))
92  space.mRefObject = refs[0];
93 
94 
95  std::vector<Space> range = mData->getValueRange();
96  if (!count(range.begin(), range.end(), space))
97  {
98  this->setModified(); // repaint with old data
99  return;
100  }
101 
102  mData->setValue(space);
103 }
104 
105 void SpaceEditWidget::comboIndexChanged()
106 {
107  this->attemptSetValue(COORDINATE_SYSTEM(mIdCombo->currentData().toInt()), mRefCombo->currentData().toString());
108 }
109 
111 {
112  mLabel->setVisible(on);
113 }
114 
115 
116 void SpaceEditWidget::rebuildIdCombobox()
117 {
118  mIdCombo->clear();
119 
120  Space currentValue = mData->getValue();
121  std::vector<COORDINATE_SYSTEM> ids = this->getAvailableSpaceIds();
122 
123  int currentIndex = -1;
124  for (int i = 0; i < ids.size(); ++i)
125  {
126  COORDINATE_SYSTEM id = ids[i];
127  QString name = enum2string<COORDINATE_SYSTEM>(id);
128 
129  mIdCombo->addItem(name);
130  mIdCombo->setItemData(i, QVariant::fromValue<int>(id));
131  if (id == currentValue.mId)
132  currentIndex = i;
133  }
134  mIdCombo->setCurrentIndex(currentIndex);
135 }
136 
137 std::vector<COORDINATE_SYSTEM> SpaceEditWidget::getAvailableSpaceIds()
138 {
139  std::vector<COORDINATE_SYSTEM> retval;
140  std::vector<Space> range = mData->getValueRange();
141 
142  for (unsigned i=0; i<range.size(); ++i)
143  {
144  if (!count(retval.begin(), retval.end(), range[i].mId))
145  retval.push_back(range[i].mId);
146  }
147 
148  return retval;
149 }
150 
151 void SpaceEditWidget::rebuildRefCombobox()
152 {
153  mRefCombo->clear();
154 
155  Space currentValue = mData->getValue();
156  QStringList refs = this->getAvailableSpaceRefs(currentValue.mId);
157 
158  int currentIndex = -1;
159  for (int i = 0; i < refs.size(); ++i)
160  {
161  QString ref = refs[i];
162  QString name = mData->convertRefObjectInternal2Display(ref);
163  mRefCombo->addItem(name);
164  mRefCombo->setItemData(i, ref);
165  if (ref == currentValue.mRefObject)
166  currentIndex = i;
167  }
168  mRefCombo->setCurrentIndex(currentIndex);
169 
170  // Cannot set visibility inside a paint event.
171  // Instead do it by placing a call in the queue.
172  this->setRefComboVisibilityQueued();
173 }
174 
175 void SpaceEditWidget::setRefComboVisibilityQueued()
176 {
177  bool show = mRefCombo->count();
178 
179  if (mRefCombo->isVisible()==show)
180  return;
181 
182  if (show)
183  QMetaObject::invokeMethod(mRefCombo, "show", Qt::QueuedConnection);
184  else
185  QMetaObject::invokeMethod(mRefCombo, "hide", Qt::QueuedConnection);
186 }
187 
188 QStringList SpaceEditWidget::getAvailableSpaceRefs(COORDINATE_SYSTEM id)
189 {
190  QStringList retval;
191  std::vector<Space> range = mData->getValueRange();
192 
193  for (unsigned i=0; i<range.size(); ++i)
194  {
195  if (range[i].mId == id)
196  retval.push_back(range[i].mRefObject);
197  }
198  retval.removeDuplicates();
199  retval.removeAll("");
200 
201  return retval;
202 }
203 
204 void SpaceEditWidget::prePaintEvent()
205 {
206  mRefCombo->blockSignals(true);
207  mIdCombo->blockSignals(true);
208  mIdCombo->clear();
209  mRefCombo->clear();
210 
211  this->setEnabled(mData->getEnabled());
212  mLabel->setEnabled(mData->getEnabled());
213  mIdCombo->setEnabled(mData->getEnabled());
214  mRefCombo->setEnabled(mData->getEnabled());
215 
216  this->rebuildIdCombobox();
217  this->rebuildRefCombobox();
218 
219  mIdCombo->setToolTip(QString("%1\nSet space type").arg(mData->getHelp()));
220  mRefCombo->setToolTip(QString("%1\nSet space identifier").arg(mData->getHelp()));
221  mLabel->setToolTip(mData->getHelp());
222 
223  mRefCombo->blockSignals(false);
224  mIdCombo->blockSignals(false);
225 }
226 
227 } // 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)