VPTissue Reference Manual
ParamPage.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "ParamPage.h"
21 
26 
27 #include <boost/property_tree/exceptions.hpp>
28 #include <boost/property_tree/xml_parser.hpp>
29 #include <QComboBox>
30 #include <QFormLayout>
31 #include <QLineEdit>
32 #include <QListView>
33 #include <QListWidget>
34 #include <QMessageBox>
35 #include <QRadioButton>
36 #include <QSettings>
37 #include <QString>
38 #include <QVBoxLayout>
39 
40 #include <cctype>
41 #include <iostream>
42 
43 using namespace boost::property_tree;
44 using namespace boost::property_tree::xml_parser;
45 
46 namespace SimPT_Parex {
47 
48 ParamPage::ParamPage(const std::shared_ptr<Exploration> &exploration)
49  : m_exploration(exploration), m_current_index(-1)
50 {
51  // Set title + text
52  setTitle("Parameter selection");
53  setSubTitle("Please specify the required parameters");
54 
55  // Set main layout
56  QVBoxLayout* layout = new QVBoxLayout;
57 
58  m_parameters = new QComboBox;
59  layout->addWidget(m_parameters);
60 
61  /* -- Original -- */
62  QHBoxLayout* originalLayout = new QHBoxLayout;
63  m_original_select = new QRadioButton("Original");
64  m_original_select->setChecked(true);
65  originalLayout->addWidget(m_original_select);
66  m_original = new QLineEdit();
67  originalLayout->addWidget(m_original);
68  m_original->setReadOnly(true);
69 
70  /* -- List -- */
71  QVBoxLayout* listLayout = new QVBoxLayout;
72  m_list_select = new QRadioButton("List");
73  listLayout->addWidget(m_list_select);
74  m_list = new QLineEdit;
75  listLayout->addWidget(m_list);
76 
77  /* -- Loop -- */
78  QDoubleValidator* doubleValidator = new QDoubleValidator(this);
79 
80  QVBoxLayout* loopLayout = new QVBoxLayout;
81  m_loop_select = new QRadioButton("Loop");
82  loopLayout->addWidget(m_loop_select);
83 
84  QFormLayout* loopFormLayout = new QFormLayout;
85 
86  m_from = new QLineEdit;
87  m_from->setValidator(doubleValidator);
88  loopFormLayout->addRow("From", m_from);
89 
90  m_to = new QLineEdit;
91  m_to->setValidator(doubleValidator);
92  loopFormLayout->addRow("To", m_to);
93 
94  m_step = new QLineEdit;
95  m_step->setValidator(doubleValidator);
96  loopFormLayout->addRow("Step", m_step);
97 
98  loopLayout->addLayout(loopFormLayout);
99 
100  layout->addLayout(originalLayout);
101  layout->addLayout(listLayout);
102  layout->addLayout(loopLayout);
103 
104  setLayout(layout);
105 
106  // Connect all objects
107  connect(m_parameters, SIGNAL(currentIndexChanged(int)), this, SLOT(SelectParameter(int)));
108  connect(m_original_select, SIGNAL(toggled(bool)), this, SLOT(UpdateOriginal(bool)));
109  connect(m_list_select, SIGNAL(toggled(bool)), this, SLOT(UpdateList(bool)));
110  connect(m_loop_select, SIGNAL(toggled(bool)), this, SLOT(UpdateLoop(bool)));
111 
112  UpdateOriginal(true);
113  UpdateList(false);
114  UpdateLoop(false);
115 }
116 
117 
118 void ParamPage::UpdateOriginal(bool checked)
119 {
120  m_original->setEnabled(checked);
121 }
122 
123 
124 void ParamPage::UpdateList(bool checked)
125 {
126  m_list->setEnabled(checked);
127 }
128 
129 
130 void ParamPage::UpdateLoop(bool checked)
131 {
132  m_from->setEnabled(checked);
133  m_to->setEnabled(checked);
134  m_step->setEnabled(checked);
135 }
136 
137 
138 void ParamPage::SelectParameter(int index)
139 {
140  ParameterExploration *parameterExploration = dynamic_cast<ParameterExploration*>(m_exploration.get());
141  assert(parameterExploration && "Exploration wasn't a ParameterExploration");
142 
143  if (index == m_current_index) {
144  return;
145  } else if (!SaveParameter()) {
146  m_parameters->setCurrentIndex(m_current_index);
147  return;
148  }
149 
150  m_current_index = index;
151 
152  /* Clear input boxes */
153  m_original->setText("");
154  m_list->setText("");
155  m_from->setText("");
156  m_to->setText("");
157  m_step->setText("");
158 
159  std::string parameter = m_parameters->currentText().toStdString();
160  const ISweep *sweep = parameterExploration->GetSweep(parameter);
161 
162  if (sweep == nullptr) {
163  m_original_select->setChecked(true);
164  m_original->setText(QString::fromStdString(parameterExploration->GetOriginalValue(parameter)));
165  } else if (const ListSweep *listSweep = dynamic_cast<const ListSweep*>(sweep)) {
166  m_list_select->setChecked(true);
167 
168  QString listString;
169  for (unsigned int i = 0; i < listSweep->GetNumberOfValues(); ++i) {
170  if (i > 0) {
171  listString += ", ";
172  }
173  listString += QString::fromStdString(listSweep->GetValue(i));
174  }
175  m_list->setText(listString);
176  } else if (const RangeSweep *rangeSweep = dynamic_cast<const RangeSweep*>(sweep)) {
177  m_loop_select->setChecked(true);
178 
179  m_from->setText(QString::number(rangeSweep->GetFrom()));
180  m_to->setText(QString::number(rangeSweep->GetTo()));
181  m_step->setText(QString::number(rangeSweep->GetStep()));
182  } else {
183  assert(false && "I wouldn't know what kind of sweep this is");
184  }
185 }
186 
187 
188 void ParamPage::initializePage()
189 {
190  ParameterExploration *parameterExploration = dynamic_cast<ParameterExploration*>(m_exploration.get());
191  assert(parameterExploration && "Exploration wasn't a ParameterExploration");
192 
193  std::vector<std::string> parameters = parameterExploration->GetPossibleParameters();
194 
195  if (parameters.size() > 0)
196  {
197  for (const std::string &parameter : parameters) {
198  m_parameters->addItem(QString::fromStdString(parameter));
199  }
200  } else {
201  m_parameters->setEnabled(false);
202  UpdateOriginal(false);
203  UpdateList(false);
204  UpdateLoop(false);
205  }
206 }
207 
208 
209 bool ParamPage::validatePage()
210 {
211  return SaveParameter();
212 }
213 
214 
215 bool ParamPage::SaveParameter()
216 {
217  ParameterExploration *parameterExploration = dynamic_cast<ParameterExploration*>(m_exploration.get());
218  assert(parameterExploration && "Exploration wasn't a ParameterExploration");
219 
220  if (m_current_index == -1)
221  return true;
222 
223  std::string parameter = m_parameters->itemText(m_current_index).toStdString();
224 
225  if (m_original_select->isChecked()) {
226  parameterExploration->RemoveSweep(parameter);
227  } else if (m_list_select->isChecked()) {
228  QStringList qlist = m_list->text().split(QRegExp(" *, *"), QString::SkipEmptyParts);
229  if (qlist.empty()) {
230  QMessageBox::warning(this, "Cannot save parameters", "The list does not contain any strings");
231  return false;
232  }
233 
234  if (!m_list->isModified()) {
235  return true;
236  }
237 
238  std::vector<std::string> list;
239  for (const auto &e : qlist) {
240  list.push_back(e.toStdString());
241  }
242  parameterExploration->SetSweep(parameter, new ListSweep(list));
243 
244  m_list->setModified(false);
245  } else if (m_loop_select->isChecked()) {
246  bool ok;
247  double from = m_from->text().toDouble(&ok);
248  if (!m_from->hasAcceptableInput() || !ok) {
249  QMessageBox::warning(this, "Cannot save parameters", "'From' does not contain a valid number");
250  return false;
251  }
252 
253  double to = m_to->text().toDouble(&ok);
254  if (!m_to->hasAcceptableInput() || !ok) {
255  QMessageBox::warning(this, "Cannot save parameters", "'To' does not contain a valid number");
256  return false;
257  }
258 
259  double step = m_step->text().toDouble(&ok);
260  if (!m_step->hasAcceptableInput() || !ok) {
261  QMessageBox::warning(this, "Cannot save parameters", "'Step' does not contain a valid number");
262  return false;
263  }
264 
265  if (from > to) {
266  QMessageBox::warning(this, "Cannot save parameters", "This is not a valid range: 'from' is greater than 'to'");
267  return false;
268  } else if (step <= 0) {
269  QMessageBox::warning(this, "Cannot save parameters", "This is not a valid range: 'step' is smaller or equal to zero");
270  return false;
271  }
272 
273  if (!m_from->isModified() && !m_to->isModified() && !m_step->isModified()) {
274  return true;
275  }
276 
277  parameterExploration->SetSweep(parameter, new RangeSweep(from, to, step));
278 
279  m_from->setModified(false);
280  m_to->setModified(false);
281  m_step->setModified(false);
282  }
283 
284  return true;
285 }
286 
287 
288 } // namespace
Interface for ParamPage.
Interface for RangeSweep.
Interface for ListSweep.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
Interface for ParameterExploration.
Interface for FileExploration.