Fraxinus  2023.01.05-dev+develop.0da12
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 //Create option widgets from propoerties (=mOptions)
103 void OptionsWidget::populate(bool showAdvanced)
104 {
105  // No existing found,
106  // create a new stack element for this uid:
107  QWidget* widget = new QWidget(this);
108  widget->setObjectName(mUid);
109  mStackedLayout->addWidget(widget);
110  QGridLayout* layout = new QGridLayout(widget);
111  layout->setMargin(layout->margin()/2);
112 
113  std::map<QString, QWidget*> groupWidgets;
114  QWidget* otherWidget = NULL;
115  for (unsigned i = 0; i < mOptions.size(); ++i)
116  {
117  if(showAdvanced || (!showAdvanced && !mOptions[i]->getAdvanced()))
118  {
119  QWidget* groupWidget = NULL;
120  QGridLayout* groupLayout = NULL;
121 
122  //make new group if needed
123  QString groupName = mOptions[i]->getGroup();
124  if(groupName.isEmpty())
125  groupName = "other";
126  std::map<QString, QWidget*>::iterator it = groupWidgets.find(groupName);
127  if(it == groupWidgets.end())
128  {
129  groupWidget = new QWidget(widget);
130  groupWidget->setObjectName(groupName);
131  groupLayout = new QGridLayout(groupWidget);
132  groupLayout->setMargin(groupLayout->margin()/2);
133  QWidget* temp = this->createGroupHeaderWidget(groupName);
134  groupLayout->addWidget(temp,0,0,1,2);
135  layout->addWidget(groupWidget);
136  groupWidgets[groupName] = groupWidget;
137  if(groupName == "other")
138  otherWidget = temp;
139  }
140  else
141  {
142  groupWidget = it->second;
143  groupLayout = dynamic_cast<QGridLayout*>(groupWidget->layout());
144  }
145 
146  //count groupwidgets items to determine row
147  int itemsInGroup = groupLayout->count();
148 
149  //make dataadaptewidget and add to existing group
150  blockSignals(true);
151  createDataWidget(mViewService, mPatientModelService, groupWidget, mOptions[i], groupLayout, ++itemsInGroup);
152  blockSignals(false);
153  }
154  }
155 
156  //hide group header if only one the "other" group exists
157  if((groupWidgets.size() == 1) && (otherWidget != NULL))
158  otherWidget->hide();
159 
160  mStackedLayout->setCurrentWidget(widget);
161 }
162 
163 QWidget* OptionsWidget::createGroupHeaderWidget(QString title)
164 {
165  QWidget* retval = new QWidget(this);
166  QVBoxLayout* layout = new QVBoxLayout(retval);
167  layout->setMargin(0);
168  layout->setSpacing(0);
169 
170  QLabel* label = new QLabel(title);
171  QFont font = label->font();
172  font.setPointSize(8);
173  label->setFont(font);
174  layout->addWidget(label);
175  layout->addWidget(BaseWidget::createHorizontalLine());
176 
177  return retval;
178 }
179 
180 } /* 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.