Fraxinus  16.5.0-fx-rc4
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  mCombo->clear();
95 
96  this->setEnabled(mData->getEnabled());
97  mLabel->setEnabled(mData->getEnabled());
98  mCombo->setEnabled(mData->getEnabled());
99 
100  QString currentValue = mData->getValue();
101  QStringList range = mData->getValueRange();
102  int currentIndex = -1;
103  for (int i = 0; i < range.size(); ++i)
104  {
105  QIcon icon = this->getIcon(range[i]);
106  mCombo->addItem(icon, mData->convertInternal2Display(range[i]));
107  mCombo->setItemData(i, range[i]);
108  if (range[i] == currentValue)
109  currentIndex = i;
110  }
111  mCombo->setCurrentIndex(currentIndex);
112 
113  mCombo->setToolTip(mData->getHelp());
114  mLabel->setToolTip(mData->getHelp());
115  mCombo->blockSignals(false);
116 }
117 
118 QIcon LabeledComboBoxWidget::getIcon(QString uid)
119 {
120  SelectDataStringPropertyBasePtr dataProperty = boost::dynamic_pointer_cast<SelectDataStringPropertyBase>(mData);
121  if(!dataProperty)
122  return QIcon();
123 
124  DataPtr data = dataProperty->getData(uid);
125  if(!data)
126  return QIcon();
127  return data->getIcon();
128 }
129 
130 } // namespace cx
LabeledComboBoxWidget(QWidget *parent, StringPropertyBasePtr, QGridLayout *gridLayout=0, int row=0)
QWidget * addDummyMargin(QWidget *widget)
#define CX_ASSERT(statement)
Definition: cxLogger.h:131
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:108