CustusX  15.8
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 
41 OptionsWidget::OptionsWidget(VisualizationServicePtr visualizationService, PatientModelServicePtr patientModelService, QWidget* parent) :
42  mShowAdvanced(false),
43  mVisualizationService(visualizationService),
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  this->showAdvanced(!mShowAdvanced);
90 }
91 
92 void OptionsWidget::clear()
93 {
94  QLayoutItem *child;
95  while ((child = mStackedLayout->takeAt(0)) != 0)
96  {
97  // delete both the layoutitem AND the widget. Not auto done because layoutitem is no QObject.
98  QWidget* widget = child->widget();
99  delete child;
100  delete widget;
101  }
102 }
103 
104 void OptionsWidget::populate(bool showAdvanced)
105 {
106  // No existing found,
107  // create a new stack element for this uid:
108  QWidget* widget = new QWidget(this);
109  widget->setObjectName(mUid);
110  mStackedLayout->addWidget(widget);
111  QGridLayout* layout = new QGridLayout(widget);
112  layout->setMargin(layout->margin()/2);
113 
114  std::map<QString, QWidget*> groupWidgets;
115  QWidget* otherWidget = NULL;
116  for (unsigned i = 0; i < mOptions.size(); ++i)
117  {
118  if(showAdvanced || (!showAdvanced && !mOptions[i]->getAdvanced()))
119  {
120  QWidget* groupWidget = NULL;
121  QGridLayout* groupLayout = NULL;
122 
123  //make new group if needed
124  QString groupName = mOptions[i]->getGroup();
125  if(groupName.isEmpty())
126  groupName = "other";
127  std::map<QString, QWidget*>::iterator it = groupWidgets.find(groupName);
128  if(it == groupWidgets.end())
129  {
130  groupWidget = new QWidget(widget);
131  groupWidget->setObjectName(groupName);
132  groupLayout = new QGridLayout(groupWidget);
133  groupLayout->setMargin(groupLayout->margin()/2);
134  QWidget* temp = this->createGroupHeaderWidget(groupName);
135  groupLayout->addWidget(temp,0,0,1,2);
136  layout->addWidget(groupWidget);
137  groupWidgets[groupName] = groupWidget;
138  if(groupName == "other")
139  otherWidget = temp;
140  }
141  else
142  {
143  groupWidget = it->second;
144  groupLayout = (QGridLayout*) groupWidget->layout();
145  }
146 
147  //count groupwidgets items to determine row
148  int itemsInGroup = groupLayout->count();
149 
150  //make dataadaptewidget and add to existing group
151  blockSignals(true);
152  createDataWidget(mVisualizationService, mPatientModelService, groupWidget, mOptions[i], groupLayout, ++itemsInGroup);
153  blockSignals(false);
154  }
155  }
156 
157  //hide group header if only one the "other" group exists
158  if((groupWidgets.size() == 1) && (otherWidget != NULL))
159  otherWidget->hide();
160 
161  mStackedLayout->setCurrentWidget(widget);
162 }
163 
164 QWidget* OptionsWidget::createGroupHeaderWidget(QString title)
165 {
166  QWidget* retval = new QWidget(this);
167  QVBoxLayout* layout = new QVBoxLayout(retval);
168  layout->setMargin(0);
169  layout->setSpacing(0);
170 
171  QLabel* label = new QLabel(title);
172  QFont font = label->font();
173  font.setPointSize(8);
174  label->setFont(font);
175  layout->addWidget(label);
176  layout->addWidget(BaseWidget::createHorizontalLine());
177 
178  return retval;
179 }
180 
181 } /* namespace cx */
QWidget * createDataWidget(VisualizationServicePtr visualizationService, PatientModelServicePtr patientModelService, QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
boost::shared_ptr< class VisualizationService > VisualizationServicePtr
Definition: cxRegServices.h:43
static QFrame * createHorizontalLine()
Creates a horizontal line which can be inserted into widgets.
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void showAdvanced(bool show)
void setOptions(QString uid, std::vector< PropertyPtr > options, bool showAdvanced)
OptionsWidget(VisualizationServicePtr visualizationService, PatientModelServicePtr patientModelService, QWidget *parent)