Fraxinus  22.09
An IGT application
cxStringListSelectWidget.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 
13 
14 #include <iostream>
15 #include "cxTypeConversions.h"
16 #include "cxHelperWidgets.h"
17 #include "cxLogger.h"
18 #include <QWidgetAction>
19 #include <QCheckBox>
20 #include <QMenu>
21 #include "boost/bind.hpp"
22 #include "cxStringListProperty.h"
23 #include "cxData.h"
24 
25 namespace cx
26 {
27 
29  QGridLayout* gridLayout, int row) :
30  BaseWidget(parent, "StringListSelectWidget", "StringListSelectWidget"),
31  mData(property)
32 {
33  connect(mData.get(), &Property::changed, this, &StringListSelectWidget::setModified);
34 
35  this->setEnabled(mData->getEnabled());
36 
37  mLabel = new QLabel(this);
38  mLabel->setText(mData->getDisplayName());
39 
40  mMenu = new QMenu(this);
41  mButton = new CXSmallToolButton();
42  mButton->setIcon(QIcon(":icons/open_icon_library/go-down-4.png"));
43  mButton->setPopupMode(QToolButton::InstantPopup);
44  mButton->setMenu(mMenu);
45 
46  if (gridLayout) // add to input gridlayout
47  {
48  gridLayout->addLayout(mergeWidgetsIntoHBoxLayout(mLabel, addDummyMargin(this)), row, 0);
49  gridLayout->addWidget(mButton, row, 1);
50  }
51  else // add directly to this
52  {
53  mTopLayout = new QHBoxLayout;
54  mTopLayout->setMargin(0);
55  this->setLayout(mTopLayout);
56 
57  mTopLayout->addWidget(mLabel);
58  mTopLayout->addWidget(mButton, 1);
59  }
60 
61  this->setModified();
62 }
63 
65 {
66  mButton->setIcon(icon);
67 }
68 
69 void StringListSelectWidget::onCheckToggled(QString nodeType, bool value)
70 {
71  QStringList data = mData->getValue();
72 
73  if (value)
74  data.append(nodeType);
75  else
76  data.removeAll(nodeType);
77 
78  mData->setValue(data);
79 }
80 
82 {
83  mLabel->setVisible(on);
84 }
85 
86 void StringListSelectWidget::prePaintEvent()
87 {
88  this->setEnabled(mData->getEnabled());
89  mLabel->setEnabled(mData->getEnabled());
90  mButton->setEnabled(mData->getEnabled());
91 
92  QStringList value = mData->getValue();
93  QStringList range = mData->getValueRange();
94 
95  if (mCachedRange!=range)
96  {
97  mCachedRange = range;
98  mCheckBoxes.clear();
99  mMenu->clear();
100 
101  for (int i=0; i<range.size(); ++i)
102  {
103  QString uid = range[i];
104  QString name = mData->convertInternal2Display(uid);
105  mCheckBoxes.push_back(new QCheckBox(name, mMenu));
106  boost::function<void(bool)> func = boost::bind(&StringListSelectWidget::onCheckToggled, this, uid, _1);
107  connect(mCheckBoxes[i], &QCheckBox::toggled, func);
108  QWidgetAction *checkableAction = new QWidgetAction(mMenu);
109  checkableAction->setDefaultWidget(mCheckBoxes[i]);
110  mMenu->addAction(checkableAction);
111  }
112  }
113 
114  for (int i=0; i<range.size(); ++i)
115  {
116  mCheckBoxes[i]->setChecked(value.contains(range[i]));
117  }
118 
119  mButton->setToolTip(mData->getHelp());
120  mLabel->setToolTip(mData->getHelp());
121 }
122 
123 } // namespace cx
124 
StringListSelectWidget(QWidget *parent, StringListPropertyPtr property, QGridLayout *gridLayout=0, int row=0)
QWidget * addDummyMargin(QWidget *widget)
QHBoxLayout * mergeWidgetsIntoHBoxLayout(QWidget *first, QWidget *second)
void changed()
emit when the underlying data value is changed: The user interface will be updated.
boost::shared_ptr< class StringListProperty > StringListPropertyPtr
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
Namespace for all CustusX production code.