CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 
34 
35 #include <iostream>
36 #include "cxTypeConversions.h"
37 #include "cxHelperWidgets.h"
38 #include "cxLogger.h"
39 #include <QWidgetAction>
40 #include <QCheckBox>
41 #include <QMenu>
42 #include "boost/bind.hpp"
43 #include "cxStringListProperty.h"
44 #include "cxData.h"
45 
46 namespace cx
47 {
48 
50  QGridLayout* gridLayout, int row) :
51  BaseWidget(parent, "StringListSelectWidget", "StringListSelectWidget"),
52  mData(property)
53 {
54  connect(mData.get(), &Property::changed, this, &StringListSelectWidget::setModified);
55 
56  this->setEnabled(mData->getEnabled());
57 
58  mLabel = new QLabel(this);
59  mLabel->setText(mData->getDisplayName());
60 
61  mMenu = new QMenu(this);
62  mButton = new CXSmallToolButton();
63  mButton->setIcon(QIcon(":icons/open_icon_library/go-down-4.png"));
64  mButton->setPopupMode(QToolButton::InstantPopup);
65  mButton->setMenu(mMenu);
66 
67  if (gridLayout) // add to input gridlayout
68  {
69  gridLayout->addLayout(mergeWidgetsIntoHBoxLayout(mLabel, addDummyMargin(this)), row, 0);
70  gridLayout->addWidget(mButton, row, 1);
71  }
72  else // add directly to this
73  {
74  mTopLayout = new QHBoxLayout;
75  mTopLayout->setMargin(0);
76  this->setLayout(mTopLayout);
77 
78  mTopLayout->addWidget(mLabel);
79  mTopLayout->addWidget(mButton, 1);
80  }
81 
82  this->setModified();
83 }
84 
86 {
87  mButton->setIcon(icon);
88 }
89 
90 void StringListSelectWidget::onCheckToggled(QString nodeType, bool value)
91 {
92  QStringList data = mData->getValue();
93 
94  if (value)
95  data.append(nodeType);
96  else
97  data.removeAll(nodeType);
98 
99  mData->setValue(data);
100 }
101 
103 {
104  mLabel->setVisible(on);
105 }
106 
107 void StringListSelectWidget::prePaintEvent()
108 {
109  this->setEnabled(mData->getEnabled());
110  mLabel->setEnabled(mData->getEnabled());
111  mButton->setEnabled(mData->getEnabled());
112 
113  QStringList value = mData->getValue();
114  QStringList range = mData->getValueRange();
115 
116  if (mCachedRange!=range)
117  {
118  mCachedRange = range;
119  mCheckBoxes.clear();
120  mMenu->clear();
121 
122  for (int i=0; i<range.size(); ++i)
123  {
124  QString uid = range[i];
125  QString name = mData->convertInternal2Display(uid);
126  mCheckBoxes.push_back(new QCheckBox(name, mMenu));
127  boost::function<void(bool)> func = boost::bind(&StringListSelectWidget::onCheckToggled, this, uid, _1);
128  connect(mCheckBoxes[i], &QCheckBox::toggled, func);
129  QWidgetAction *checkableAction = new QWidgetAction(mMenu);
130  checkableAction->setDefaultWidget(mCheckBoxes[i]);
131  mMenu->addAction(checkableAction);
132  }
133  }
134 
135  for (int i=0; i<range.size(); ++i)
136  {
137  mCheckBoxes[i]->setChecked(value.contains(range[i]));
138  }
139 
140  mButton->setToolTip(mData->getHelp());
141  mLabel->setToolTip(mData->getHelp());
142 }
143 
144 } // namespace cx
145 
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:108