NorMIT-nav  22.09
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
cxLogger.h
cx::addDummyMargin
QWidget * addDummyMargin(QWidget *widget)
Definition: cxHelperWidgets.cpp:123
CX_ASSERT
#define CX_ASSERT(statement)
Definition: cxLogger.h:116
cx::OptimizedUpdateWidget::setModified
virtual void setModified()
Definition: cxOptimizedUpdateWidget.cpp:36
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::BaseWidget
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
cx::Property::changed
void changed()
emit when the underlying data value is changed: The user interface will be updated.
cxSelectDataStringPropertyBase.h
cx::mergeWidgetsIntoHBoxLayout
QHBoxLayout * mergeWidgetsIntoHBoxLayout(QWidget *first, QWidget *second)
Definition: cxHelperWidgets.cpp:131
cxLabeledComboBoxWidget.h
cxHelperWidgets.h
cxData.h
cxTypeConversions.h
cx::LabeledComboBoxWidget::showLabel
void showLabel(bool on)
Definition: cxLabeledComboBoxWidget.cpp:65
cx::DataPtr
boost::shared_ptr< class Data > DataPtr
Definition: cxRegistrationApplicator.h:22
cx::LabeledComboBoxWidget::LabeledComboBoxWidget
LabeledComboBoxWidget(QWidget *parent, StringPropertyBasePtr, QGridLayout *gridLayout=0, int row=0)
Definition: cxLabeledComboBoxWidget.cpp:25
cx::LabeledComboBoxWidget::mTopLayout
QHBoxLayout * mTopLayout
Definition: cxLabeledComboBoxWidget.h:46
cx::StringPropertyBasePtr
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr
Definition: cxIGTLinkStreamerService.h:20
cx::SelectDataStringPropertyBasePtr
boost::shared_ptr< class SelectDataStringPropertyBase > SelectDataStringPropertyBasePtr
Definition: cxMeshGlyphsWidget.h:26