CustusX  20.03-rc1
An IGT application
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) 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 
12 #include "cxOptionsWidget.h"
13 
14 #include <QLabel>
15 #include "cxBaseWidget.h"
16 #include "cxHelperWidgets.h"
17 
18 namespace cx {
19 
20 OptionsWidget::OptionsWidget(ViewServicePtr viewService, PatientModelServicePtr patientModelService, QWidget* parent) :
21  mShowAdvanced(false),
22  mViewService(viewService),
23  mPatientModelService(patientModelService)
24 {
25  this->setSizePolicy(this->sizePolicy().horizontalPolicy(), QSizePolicy::Fixed);
26  mStackedLayout = new QStackedLayout(this);
27  mStackedLayout->setMargin(0);
28 }
29 
30 void OptionsWidget::setOptions(QString uid, std::vector<SelectDataStringPropertyBasePtr> options, bool showAdvanced)
31 {
32  std::vector<PropertyPtr> converted;
33  std::copy(options.begin(), options.end(), std::back_inserter(converted));
34  this->setOptions(uid, converted, showAdvanced);
35 }
36 
37 void OptionsWidget::setOptions(QString uid, std::vector<PropertyPtr> options, bool showAdvanced)
38 {
39  // return if already on uid
40  if (mStackedLayout->currentWidget() && (uid == mStackedLayout->currentWidget()->objectName()))
41  return;
42 
43  mOptions = options;
44  mUid = uid;
45 
46  this->showAdvanced(showAdvanced);
47 }
48 
50 {
51  return mStackedLayout->currentWidget()->objectName();
52 }
53 
55 {
56  mShowAdvanced = show;
57  this->rebuild();
58 }
59 
61 {
62  this->clear();
63  this->populate(mShowAdvanced);
64 }
65 
67 {
68  if(mOptions.size() == 0)
69  return false;
70 
71  return true;
72 }
73 
75 {
76  for(std::vector<PropertyPtr>::const_iterator it = mOptions.begin(); it != mOptions.end(); ++it)
77  {
78  if(it->get()->getAdvanced())
79  return true;
80  }
81 
82  return false;
83 }
84 
86 {
87  this->showAdvanced(!mShowAdvanced);
88 }
89 
90 void OptionsWidget::clear()
91 {
92  QLayoutItem *child;
93  while ((child = mStackedLayout->takeAt(0)) != 0)
94  {
95  // delete both the layoutitem AND the widget. Not auto done because layoutitem is no QObject.
96  QWidget* widget = child->widget();
97  delete child;
98  delete widget;
99  }
100 }
101 
102 void OptionsWidget::populate(bool showAdvanced)
103 {
104  // No existing found,
105  // create a new stack element for this uid:
106  QWidget* widget = new QWidget(this);
107  widget->setObjectName(mUid);
108  mStackedLayout->addWidget(widget);
109  QGridLayout* layout = new QGridLayout(widget);
110  layout->setMargin(layout->margin()/2);
111 
112  std::map<QString, QWidget*> groupWidgets;
113  QWidget* otherWidget = NULL;
114  for (unsigned i = 0; i < mOptions.size(); ++i)
115  {
116  if(showAdvanced || (!showAdvanced && !mOptions[i]->getAdvanced()))
117  {
118  QWidget* groupWidget = NULL;
119  QGridLayout* groupLayout = NULL;
120 
121  //make new group if needed
122  QString groupName = mOptions[i]->getGroup();
123  if(groupName.isEmpty())
124  groupName = "other";
125  std::map<QString, QWidget*>::iterator it = groupWidgets.find(groupName);
126  if(it == groupWidgets.end())
127  {
128  groupWidget = new QWidget(widget);
129  groupWidget->setObjectName(groupName);
130  groupLayout = new QGridLayout(groupWidget);
131  groupLayout->setMargin(groupLayout->margin()/2);
132  QWidget* temp = this->createGroupHeaderWidget(groupName);
133  groupLayout->addWidget(temp,0,0,1,2);
134  layout->addWidget(groupWidget);
135  groupWidgets[groupName] = groupWidget;
136  if(groupName == "other")
137  otherWidget = temp;
138  }
139  else
140  {
141  groupWidget = it->second;
142  groupLayout = dynamic_cast<QGridLayout*>(groupWidget->layout());
143  }
144 
145  //count groupwidgets items to determine row
146  int itemsInGroup = groupLayout->count();
147 
148  //make dataadaptewidget and add to existing group
149  blockSignals(true);
150  createDataWidget(mViewService, mPatientModelService, groupWidget, mOptions[i], groupLayout, ++itemsInGroup);
151  blockSignals(false);
152  }
153  }
154 
155  //hide group header if only one the "other" group exists
156  if((groupWidgets.size() == 1) && (otherWidget != NULL))
157  otherWidget->hide();
158 
159  mStackedLayout->setCurrentWidget(widget);
160 }
161 
162 QWidget* OptionsWidget::createGroupHeaderWidget(QString title)
163 {
164  QWidget* retval = new QWidget(this);
165  QVBoxLayout* layout = new QVBoxLayout(retval);
166  layout->setMargin(0);
167  layout->setSpacing(0);
168 
169  QLabel* label = new QLabel(title);
170  QFont font = label->font();
171  font.setPointSize(8);
172  label->setFont(font);
173  layout->addWidget(label);
174  layout->addWidget(BaseWidget::createHorizontalLine());
175 
176  return retval;
177 }
178 
179 } /* 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)
void setOptions(QString uid, std::vector< PropertyPtr > options, bool showAdvanced)
Namespace for all CustusX production code.