Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkFileReaderWriterOptionsWidget.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 #include <QFormLayout>
20 
21 #include <limits>
22 
23 static QString GetAnyWidgetLabel(const std::string &name)
24 {
25  QString label;
26  // get the last segment from the option name and use it as the label
27  std::string::size_type pos = name.find_last_of('.');
28  if (pos == std::string::npos)
29  {
30  label = QString::fromStdString(name);
31  }
32  else if (pos < name.length() - 1)
33  {
34  label = QString::fromStdString(name.substr(pos + 1));
35  }
36  return label;
37 }
38 
39 static QWidget *GetAnyWidget(const std::string &name, const us::Any &any, const std::string &defaultValue)
40 {
41  const std::type_info &anyType = any.Type();
42 
43  if (anyType == typeid(std::string))
44  {
45  return new QmitkAnyStringWidget(name, any);
46  }
47  else if (anyType == typeid(std::vector<std::string>))
48  {
49  return new QmitkAnyVectorWidget(name, any, QString::fromStdString(defaultValue));
50  }
51  // else if (anyType == typeid(std::list<std::string>))
52  // {
53  // const std::list<std::string>& list = ref_any_cast<std::list<std::string> >(m_Any);
54  // for (std::list<std::string>::const_iterator it = list.begin();
55  // it != list.end(); ++it)
56  // {
57  // }
58  // }
59  else if (anyType == typeid(bool))
60  {
61  return new QmitkAnyBoolWidget(name, any);
62  }
63  else if (anyType == typeid(short))
64  {
65  return new QmitkAnyShortWidget(name, any);
66  }
67  else if (anyType == typeid(int))
68  {
69  return new QmitkAnyIntWidget(name, any);
70  }
71  else if (anyType == typeid(unsigned short))
72  {
73  return new QmitkAnyUShortWidget(name, any);
74  }
75  else if (anyType == typeid(unsigned int))
76  {
77  return new QmitkAnyUIntWidget(name, any);
78  }
79  else if (anyType == typeid(float))
80  {
81  return new QmitkAnyFloatWidget(name, any);
82  }
83  else if (anyType == typeid(double))
84  {
85  return new QmitkAnyDoubleWidget(name, any);
86  }
87  else
88  {
89  return new QmitkInvalidAnyWidget(name, any);
90  }
91 }
92 
94  : QWidget(parent)
95 {
96  Options filteredOptions = options;
97  std::map<std::string, std::string> optionToDefaultValue;
98  for (const auto &option : options)
99  {
100  const std::string &name = option.first;
101  if (name.size() > 4 && name.substr(name.size() - 4) == "enum")
102  {
103  filteredOptions.erase(name);
104 
105  std::string nameWithoutEnum = name.substr(0, name.size() - 5);
106  us::Any value = filteredOptions[nameWithoutEnum];
107  if (!value.Empty())
108  {
109  optionToDefaultValue[nameWithoutEnum] = value.ToString();
110  }
111  filteredOptions[nameWithoutEnum] = option.second;
112  }
113  }
114 
115  auto formLayout = new QFormLayout();
116  for (Options::const_iterator iter = filteredOptions.begin(), iterEnd = filteredOptions.end(); iter != iterEnd; ++iter)
117  {
118  formLayout->addRow(GetAnyWidgetLabel(iter->first),
119  GetAnyWidget(iter->first, iter->second, optionToDefaultValue[iter->first]));
120  }
121 
122  this->setLayout(formLayout);
123 }
124 
126 {
127  Options options;
128 
129  QFormLayout *layout = qobject_cast<QFormLayout *>(this->layout());
130  const int rows = layout->rowCount();
131  for (int i = 0; i < rows; ++i)
132  {
133  QmitkAnyAdapter *anyAdapter = dynamic_cast<QmitkAnyAdapter *>(layout->itemAt(i, QFormLayout::FieldRole)->widget());
134  if (anyAdapter)
135  {
136  options.insert(std::make_pair(anyAdapter->GetName(), anyAdapter->GetAny()));
137  }
138  }
139  return options;
140 }
141 
142 QmitkAnyStringWidget::QmitkAnyStringWidget(const std::string &name, const us::Any &any, QWidget *parent)
143  : QLineEdit(parent), QmitkAnyAdapter(name)
144 {
145  this->setText(QString::fromStdString(any.ToString()));
146 }
147 
149 {
150  return us::Any(this->text().toStdString());
151 }
152 
153 QmitkAnyBoolWidget::QmitkAnyBoolWidget(const std::string &name, const us::Any &any, QWidget *parent)
154  : QCheckBox(parent), QmitkAnyAdapter(name)
155 {
156  this->setChecked(us::any_cast<bool>(any));
157 }
158 
160 {
161  return us::Any(this->isChecked());
162 }
163 
164 QmitkAnyShortWidget::QmitkAnyShortWidget(const std::string &name, const us::Any &any, QWidget *parent)
165  : QSpinBox(parent), QmitkAnyAdapter(name)
166 {
168  this->setValue(us::any_cast<short>(any));
169 }
170 
172 {
173  return us::Any(static_cast<short>(this->value()));
174 }
175 
176 QmitkAnyUShortWidget::QmitkAnyUShortWidget(const std::string &name, const us::Any &any, QWidget *parent)
177  : QSpinBox(parent), QmitkAnyAdapter(name)
178 {
180  this->setValue(us::any_cast<unsigned short>(any));
181 }
182 
184 {
185  return us::Any(static_cast<unsigned short>(this->value()));
186 }
187 
188 QmitkAnyIntWidget::QmitkAnyIntWidget(const std::string &name, const us::Any &any, QWidget *parent)
189  : QSpinBox(parent), QmitkAnyAdapter(name)
190 {
192  this->setValue(us::any_cast<int>(any));
193 }
194 
196 {
197  return us::Any(this->value());
198 }
199 
200 QmitkAnyUIntWidget::QmitkAnyUIntWidget(const std::string &name, const us::Any &any, QWidget *parent)
201  : QSpinBox(parent), QmitkAnyAdapter(name)
202 {
204  this->setValue(us::any_cast<unsigned int>(any));
205 }
206 
208 {
209  return us::Any(static_cast<unsigned int>(this->value()));
210 }
211 
212 QmitkAnyFloatWidget::QmitkAnyFloatWidget(const std::string &name, const us::Any &any, QWidget *parent)
213  : QDoubleSpinBox(parent), QmitkAnyAdapter(name)
214 {
216  this->setValue(static_cast<double>(us::any_cast<float>(any)));
217 }
218 
220 {
221  return us::Any(static_cast<float>(this->value()));
222 }
223 
224 QmitkAnyDoubleWidget::QmitkAnyDoubleWidget(const std::string &name, const us::Any &any, QWidget *parent)
225  : QDoubleSpinBox(parent), QmitkAnyAdapter(name)
226 {
228  this->setValue(us::any_cast<double>(any));
229 }
230 
232 {
233  return us::Any(this->value());
234 }
235 
236 QmitkInvalidAnyWidget::QmitkInvalidAnyWidget(const std::string &name, const us::Any &any, QWidget *parent)
237  : QLabel(parent), QmitkAnyAdapter(name)
238 {
239  this->setText(QString("Unknown option type '%1'").arg(any.Type().name()));
240 }
241 
243 {
244  return us::Any();
245 }
246 
248  const us::Any &any,
249  const QString &defaultValue,
250  QWidget *parent)
251  : QComboBox(parent), QmitkAnyAdapter(name)
252 {
253  const std::vector<std::string> &entries = us::ref_any_cast<std::vector<std::string>>(any);
254  int index = 0;
255  int defaultIndex = 0;
256  for (auto iter = entries.begin(), iterEnd = entries.end(); iter != iterEnd; ++iter, ++index)
257  {
258  QString item = QString::fromStdString(*iter);
259  this->addItem(item);
260  if (item == defaultValue)
261  {
262  defaultIndex = index;
263  }
264  }
265  this->setCurrentIndex(defaultIndex);
266 }
267 
269 {
270  return us::Any(std::string(this->currentText().toStdString()));
271 }
const std::type_info & Type() const
Definition: usAny.h:278
QmitkAnyUIntWidget(const std::string &name, const us::Any &any, QWidget *parent=nullptr)
virtual us::Any GetAny() const =0
QmitkAnyFloatWidget(const std::string &name, const us::Any &any, QWidget *parent=nullptr)
static QString GetAnyWidgetLabel(const std::string &name)
QmitkAnyUShortWidget(const std::string &name, const us::Any &any, QWidget *parent=nullptr)
QmitkFileReaderWriterOptionsWidget(const Options &options, QWidget *parent=nullptr)
Definition: usAny.h:163
QmitkAnyVectorWidget(const std::string &name, const us::Any &any, const QString &defaultValue, QWidget *parent=nullptr)
QmitkAnyShortWidget(const std::string &name, const us::Any &any, QWidget *parent=nullptr)
static T max(T x, T y)
Definition: svm.cpp:70
std::string ToString() const
Definition: usAny.h:257
const ValueType & ref_any_cast(const Any &operand)
Definition: usAny.h:449
static T min(T x, T y)
Definition: svm.cpp:67
QmitkAnyIntWidget(const std::string &name, const us::Any &any, QWidget *parent=nullptr)
QmitkAnyStringWidget(const std::string &name, const us::Any &any, QWidget *parent=nullptr)
static QWidget * GetAnyWidget(const std::string &name, const us::Any &any, const std::string &defaultValue)
QmitkAnyBoolWidget(const std::string &name, const us::Any &any, QWidget *parent=nullptr)
QmitkInvalidAnyWidget(const std::string &name, const us::Any &any, QWidget *parent=nullptr)
QmitkAnyDoubleWidget(const std::string &name, const us::Any &any, QWidget *parent=nullptr)
bool Empty() const
Definition: usAny.h:246