Fraxinus  17.12-rc3
An IGT application
cxLabeledComboBoxWidget.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 
35 #include <iostream>
36 #include "cxTypeConversions.h"
37 #include "cxHelperWidgets.h"
38 #include "cxLogger.h"
39 
41 #include "cxData.h"
42 
43 namespace cx
44 {
45 
47  QGridLayout* gridLayout, int row) :
48  BaseWidget(parent, "LabeledComboBoxWidget", "LabeledComboBoxWidget")
49 {
50  CX_ASSERT(dataInterface->getAllowOnlyValuesInRange()==true);
51 
52  this->setEnabled(dataInterface->getEnabled());
53 
54  mData = dataInterface;
55  connect(mData.get(), &Property::changed, this, &LabeledComboBoxWidget::setModified);
56 
57  mLabel = new QLabel(this);
58  mLabel->setText(mData->getDisplayName());
59 
60  mCombo = new QComboBox(this);
61  connect(mCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(comboIndexChanged(int)));
62 
63  if (gridLayout) // add to input gridlayout
64  {
65  gridLayout->addLayout(mergeWidgetsIntoHBoxLayout(mLabel, addDummyMargin(this)), row, 0);
66  gridLayout->addWidget(mCombo, row, 1);
67  }
68  else // add directly to this
69  {
70  mTopLayout = new QHBoxLayout;
71  mTopLayout->setMargin(0);
72  this->setLayout(mTopLayout);
73 
74  mTopLayout->addWidget(mLabel);
75  mTopLayout->addWidget(mCombo, 1);
76  }
77 
78  this->setModified();
79 }
80 
81 void LabeledComboBoxWidget::comboIndexChanged(int index)
82 {
83  mData->setValue(mCombo->itemData(index).toString());
84 }
85 
87 {
88  mLabel->setVisible(on);
89 }
90 
91 void LabeledComboBoxWidget::prePaintEvent()
92 {
93  mCombo->blockSignals(true);
94 
95  this->setEnabled(mData->getEnabled());
96  mLabel->setEnabled(mData->getEnabled());
97  mCombo->setEnabled(mData->getEnabled());
98 
99  QString currentValue = mData->getValue();
100  QStringList range = mData->getValueRange();
101  if (range.size()!=mCombo->count())
102  {
103  mCombo->clear();
104  mCombo->addItems(range);
105  }
106  int currentIndex = -1;
107  for (int i = 0; i < range.size(); ++i)
108  {
109  mCombo->setItemIcon(i, this->getIcon(range[i]));
110  mCombo->setItemText(i, mData->convertInternal2Display(range[i]));
111  mCombo->setItemData(i, range[i]);
112  if (range[i] == currentValue)
113  currentIndex = i;
114  }
115  mCombo->setCurrentIndex(currentIndex);
116 
117  mCombo->setToolTip(mData->getHelp());
118  mLabel->setToolTip(mData->getHelp());
119  mCombo->blockSignals(false);
120 }
121 
122 QIcon LabeledComboBoxWidget::getIcon(QString uid)
123 {
124  SelectDataStringPropertyBasePtr dataProperty = boost::dynamic_pointer_cast<SelectDataStringPropertyBase>(mData);
125  if(!dataProperty)
126  return QIcon();
127 
128  DataPtr data = dataProperty->getData(uid);
129  if(!data)
130  return QIcon();
131  return data->getIcon();
132 }
133 
134 } // namespace cx
LabeledComboBoxWidget(QWidget *parent, StringPropertyBasePtr, QGridLayout *gridLayout=0, int row=0)
QWidget * addDummyMargin(QWidget *widget)
#define CX_ASSERT(statement)
Definition: cxLogger.h:137
QHBoxLayout * mergeWidgetsIntoHBoxLayout(QWidget *first, QWidget *second)
boost::shared_ptr< class Data > DataPtr
boost::shared_ptr< class SelectDataStringPropertyBase > SelectDataStringPropertyBasePtr
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr
void changed()
emit when the underlying data value is changed: The user interface will be updated.
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:109
Namespace for all CustusX production code.