CustusX  2023.01.05-dev+develop.0da12
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) 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 
14 #include <iostream>
15 #include "cxTypeConversions.h"
16 #include "cxHelperWidgets.h"
17 #include "cxLogger.h"
18 
20 #include "cxData.h"
21 
22 namespace cx
23 {
24 
26  QGridLayout* gridLayout, int row) :
27  BaseWidget(parent, "LabeledComboBoxWidget", "LabeledComboBoxWidget")
28 {
29  CX_ASSERT(dataInterface->getAllowOnlyValuesInRange()==true);
30 
31  this->setEnabled(dataInterface->getEnabled());
32 
33  mData = dataInterface;
34  connect(mData.get(), &Property::changed, this, &LabeledComboBoxWidget::setModified);
35 
36  mLabel = new QLabel(this);
37  mLabel->setText(mData->getDisplayName());
38 
39  mCombo = new QComboBox(this);
40  connect(mCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(comboIndexChanged(int)));
41 
42  if (gridLayout) // add to input gridlayout
43  {
44  gridLayout->addLayout(mergeWidgetsIntoHBoxLayout(mLabel, addDummyMargin(this)), row, 0);
45  gridLayout->addWidget(mCombo, row, 1);
46  }
47  else // add directly to this
48  {
49  mTopLayout = new QHBoxLayout;
50  mTopLayout->setMargin(0);
51  this->setLayout(mTopLayout);
52 
53  mTopLayout->addWidget(mLabel);
54  mTopLayout->addWidget(mCombo, 1);
55  }
56 
57  this->setModified();
58 }
59 
60 void LabeledComboBoxWidget::comboIndexChanged(int index)
61 {
62  mData->setValue(mCombo->itemData(index).toString());
63 }
64 
66 {
67  mLabel->setVisible(on);
68 }
69 
70 void LabeledComboBoxWidget::prePaintEvent()
71 {
72  mCombo->blockSignals(true);
73 
74  this->setEnabled(mData->getEnabled());
75  mLabel->setEnabled(mData->getEnabled());
76  mCombo->setEnabled(mData->getEnabled());
77 
78  QString currentValue = mData->getValue();
79  QStringList range = mData->getValueRange();
80  if (range.size()!=mCombo->count())
81  {
82  mCombo->clear();
83  mCombo->addItems(range);
84  }
85  int currentIndex = -1;
86  for (int i = 0; i < range.size(); ++i)
87  {
88  mCombo->setItemIcon(i, this->getIcon(range[i]));
89  mCombo->setItemText(i, mData->convertInternal2Display(range[i]));
90  mCombo->setItemData(i, range[i]);
91  if (range[i] == currentValue)
92  currentIndex = i;
93  }
94  mCombo->setCurrentIndex(currentIndex);
95 
96  mCombo->setToolTip(mData->getHelp());
97  mLabel->setToolTip(mData->getHelp());
98  mCombo->blockSignals(false);
99 }
100 
101 QIcon LabeledComboBoxWidget::getIcon(QString uid)
102 {
103  SelectDataStringPropertyBasePtr dataProperty = boost::dynamic_pointer_cast<SelectDataStringPropertyBase>(mData);
104  if(!dataProperty)
105  return QIcon();
106 
107  DataPtr data = dataProperty->getData(uid);
108  if(!data)
109  return QIcon();
110  return data->getIcon();
111 }
112 
113 } // namespace cx
LabeledComboBoxWidget(QWidget *parent, StringPropertyBasePtr, QGridLayout *gridLayout=0, int row=0)
QWidget * addDummyMargin(QWidget *widget)
#define CX_ASSERT(statement)
Definition: cxLogger.h:116
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:88
Namespace for all CustusX production code.