NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxOptionsWidget.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 #include "cxOptionsWidget.h"
34 
35 #include <QLabel>
36 #include "cxBaseWidget.h"
37 #include "cxHelperWidgets.h"
38 
39 namespace cx {
40 
42  mShowAdvanced(false),
43  mViewService(viewService),
44  mPatientModelService(patientModelService)
45 {
46  this->setSizePolicy(this->sizePolicy().horizontalPolicy(), QSizePolicy::Fixed);
47  mStackedLayout = new QStackedLayout(this);
48  mStackedLayout->setMargin(0);
49 }
50 
51 void OptionsWidget::setOptions(QString uid, std::vector<SelectDataStringPropertyBasePtr> options, bool showAdvanced)
52 {
53  std::vector<PropertyPtr> converted;
54  std::copy(options.begin(), options.end(), std::back_inserter(converted));
55  this->setOptions(uid, converted, showAdvanced);
56 }
57 
58 void OptionsWidget::setOptions(QString uid, std::vector<PropertyPtr> options, bool showAdvanced)
59 {
60  // return if already on uid
61  if (mStackedLayout->currentWidget() && (uid == mStackedLayout->currentWidget()->objectName()))
62  return;
63 
64  mOptions = options;
65  mUid = uid;
66 
67  this->showAdvanced(showAdvanced);
68 }
69 
71 {
72  return mStackedLayout->currentWidget()->objectName();
73 }
74 
76 {
77  mShowAdvanced = show;
78  this->rebuild();
79 }
80 
82 {
83  this->clear();
84  this->populate(mShowAdvanced);
85 }
86 
88 {
89  if(mOptions.size() == 0)
90  return false;
91 
92  return true;
93 }
94 
96 {
97  for(std::vector<PropertyPtr>::const_iterator it = mOptions.begin(); it != mOptions.end(); ++it)
98  {
99  if(it->get()->getAdvanced())
100  return true;
101  }
102 
103  return false;
104 }
105 
107 {
108  this->showAdvanced(!mShowAdvanced);
109 }
110 
111 void OptionsWidget::clear()
112 {
113  QLayoutItem *child;
114  while ((child = mStackedLayout->takeAt(0)) != 0)
115  {
116  // delete both the layoutitem AND the widget. Not auto done because layoutitem is no QObject.
117  QWidget* widget = child->widget();
118  delete child;
119  delete widget;
120  }
121 }
122 
123 void OptionsWidget::populate(bool showAdvanced)
124 {
125  // No existing found,
126  // create a new stack element for this uid:
127  QWidget* widget = new QWidget(this);
128  widget->setObjectName(mUid);
129  mStackedLayout->addWidget(widget);
130  QGridLayout* layout = new QGridLayout(widget);
131  layout->setMargin(layout->margin()/2);
132 
133  std::map<QString, QWidget*> groupWidgets;
134  QWidget* otherWidget = NULL;
135  for (unsigned i = 0; i < mOptions.size(); ++i)
136  {
137  if(showAdvanced || (!showAdvanced && !mOptions[i]->getAdvanced()))
138  {
139  QWidget* groupWidget = NULL;
140  QGridLayout* groupLayout = NULL;
141 
142  //make new group if needed
143  QString groupName = mOptions[i]->getGroup();
144  if(groupName.isEmpty())
145  groupName = "other";
146  std::map<QString, QWidget*>::iterator it = groupWidgets.find(groupName);
147  if(it == groupWidgets.end())
148  {
149  groupWidget = new QWidget(widget);
150  groupWidget->setObjectName(groupName);
151  groupLayout = new QGridLayout(groupWidget);
152  groupLayout->setMargin(groupLayout->margin()/2);
153  QWidget* temp = this->createGroupHeaderWidget(groupName);
154  groupLayout->addWidget(temp,0,0,1,2);
155  layout->addWidget(groupWidget);
156  groupWidgets[groupName] = groupWidget;
157  if(groupName == "other")
158  otherWidget = temp;
159  }
160  else
161  {
162  groupWidget = it->second;
163  groupLayout = dynamic_cast<QGridLayout*>(groupWidget->layout());
164  }
165 
166  //count groupwidgets items to determine row
167  int itemsInGroup = groupLayout->count();
168 
169  //make dataadaptewidget and add to existing group
170  blockSignals(true);
171  createDataWidget(mViewService, mPatientModelService, groupWidget, mOptions[i], groupLayout, ++itemsInGroup);
172  blockSignals(false);
173  }
174  }
175 
176  //hide group header if only one the "other" group exists
177  if((groupWidgets.size() == 1) && (otherWidget != NULL))
178  otherWidget->hide();
179 
180  mStackedLayout->setCurrentWidget(widget);
181 }
182 
183 QWidget* OptionsWidget::createGroupHeaderWidget(QString title)
184 {
185  QWidget* retval = new QWidget(this);
186  QVBoxLayout* layout = new QVBoxLayout(retval);
187  layout->setMargin(0);
188  layout->setSpacing(0);
189 
190  QLabel* label = new QLabel(title);
191  QFont font = label->font();
192  font.setPointSize(8);
193  label->setFont(font);
194  layout->addWidget(label);
195  layout->addWidget(BaseWidget::createHorizontalLine());
196 
197  return retval;
198 }
199 
200 } /* namespace cx */
OptionsWidget(ViewServicePtr viewService, PatientModelServicePtr patientModelService, QWidget *parent)
boost::shared_ptr< class ViewService > ViewServicePtr
QWidget * createDataWidget(ViewServicePtr viewService, PatientModelServicePtr patientModelService, QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
bool hasAdvancedOptions() const
bool hasOptions() const
static QFrame * createHorizontalLine()
Creates a horizontal line which can be inserted into widgets.
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void showAdvanced(bool show)
cxLogicManager_EXPORT ViewServicePtr viewService()
void setOptions(QString uid, std::vector< PropertyPtr > options, bool showAdvanced)