COPASI API  4.16.103
ScanWidget.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2003 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 //*** In this file I have put "//+++" in all places where something has to be added
16 //*** if a new scan item is introduced.
17 
18 #include <QtGui/QLabel>
19 #include <QtGui/QPushButton>
20 #include <QtGui/QLayout>
21 #include <QtGui/QToolTip>
22 #include <QtGui/QWhatsThis>
23 #include <QtGui/QComboBox>
24 #include <QtGui/QLineEdit>
25 #include <QtGui/QGridLayout>
26 #include <QtGui/QHBoxLayout>
27 
28 #include <QtGui/QInputDialog>
29 
30 #include "copasi.h"
31 
32 #include "ScanWidget.h"
33 #include "scan/CScanTask.h"
34 #include "scan/CScanProblem.h"
35 #include "scan/CScanMethod.h"
38 
39 #include "CQTaskHeaderWidget.h"
40 #include "CQTaskBtnWidget.h"
41 #include "CQSimpleSelectionTree.h"
42 #include "CCopasiSelectionDialog.h"
43 
44 #include "report/CKeyFactory.h"
45 #include "qtUtilities.h"
46 #include "CScanContainerWidget.h"
47 #include "utilities/CopasiTime.h"
48 
49 //+++
50 //#include "CScanWidgetBreak.h"
51 #include "CScanWidgetRandom.h"
52 #include "CScanWidgetRepeat.h"
53 #include "CScanWidgetScan.h"
54 #include "CScanWidgetTask.h"
55 
56 ScanWidget::ScanWidget(QWidget* parent, const char* name, Qt::WFlags f)
57  : TaskWidget(parent, name, f)
58 {
59  if (!name)
60  setObjectName("ScanWidget");
61 
62  setWindowTitle(trUtf8("ScanWidget"));
63  ScanWidgetLayout = new QGridLayout(this);
64  ScanWidgetLayout->setMargin(11);
65  ScanWidgetLayout->setSpacing(6);
66  ScanWidgetLayout->setObjectName("ScanWidgetLayout");
67 
68  mpHeaderWidget->setTaskName("Parameter Scan");
69 
70  ScanWidgetLayout->addWidget(mpHeaderWidget, 0, 0);
71 
72  mpBtnWidget->verticalLayout->removeItem(mpBtnWidget->verticalSpacer);
73  ScanWidgetLayout->addWidget(mpBtnWidget, 3, 0);
74 
75  //*****************
76 
77  QHBoxLayout* tmpLayout = new QHBoxLayout();
78 
79  QSpacerItem* tmpSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
80  tmpLayout->addItem(tmpSpacer);
81 
82  QLabel* tmpLabel = new QLabel(this);
83  tmpLabel->setText("New scan item: ");
84  tmpLayout->addWidget(tmpLabel);
85 
86  comboType = new QComboBox(this);
87  //+++
88  comboType->addItem("Scan");
89  comboType->addItem("Repeat");
90  comboType->addItem("Random distribution");
91  tmpLayout->addWidget(comboType);
92 
93  QSpacerItem *mpSpacer = new QSpacerItem(20, 20, QSizePolicy::Maximum, QSizePolicy::Minimum);
94  tmpLayout->addItem(mpSpacer);
95 
96  buttonNewItem = new QPushButton(this);
97  buttonNewItem->setObjectName("buttonNewItem");
98  buttonNewItem->setText("Create");
99  //ScanWidgetLayout->addWidget(buttonNewItem, 1, 2);
100  tmpLayout->addWidget(buttonNewItem);
101 
102  ScanWidgetLayout->addLayout(tmpLayout, 1, 0);
103 
104  //*****************************
105 
106  scrollview = new CScanContainerWidget(this);
107  ScanWidgetLayout->addWidget(scrollview, 2, 0);
108 
109  // tab order
110  /*setTabOrder(taskName, sExecutable);
111  setTabOrder(sExecutable, steadyState);*/
112 
113  connect(buttonNewItem, SIGNAL(clicked()), this, SLOT(slotAddItem()));
114 }
115 
117 {}
118 
120 {
121  if (!commonBeforeRunTask()) return false;
122 
123  bool success = true;
124 
125  if (!commonRunTask()) success = false;
126 
127  return success;
128 }
129 
131 {
132  loadCommon();
133 
134  CScanTask* scanTask =
135  dynamic_cast< CScanTask * >(CCopasiRootContainer::getKeyFactory()->get(mKey));
136 
137  if (!scanTask) return false;
138 
139  CScanProblem *scanProblem = dynamic_cast<CScanProblem *>(scanTask->getProblem());
140 
141  if (!scanProblem) return false;
142 
144 
145  //std::vector<QWidget*> & widgetList = scrollview->getWidgetList();
146 
147  //+++
148  CScanWidgetScan* tmp1;
149  CScanWidgetRepeat* tmp2;
150  CScanWidgetRandom* tmp3;
151  //CScanWidgetBreak* tmp4;
152 
153  // the scan items
154  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
155  //CCopasiDataModel* pDataModel = (*CCopasiRootContainer::getDatamodelList())[0];
156  //assert(pDataModel != NULL);
157  size_t i, imax = scanProblem->getNumberOfScanItems();
158 
159  for (i = 0; i < imax; ++i)
160  {
161  void * pTmp;
162 
163  if (!(pTmp = scanProblem->getScanItem(i)->getValue("Type").pVOID)) return false;
164 
165  CScanProblem::Type type = *(CScanProblem::Type*)pTmp;
166 
167  switch (type)
168  {
169  //+++
171  tmp1 = new CScanWidgetScan(scrollview);
172  tmp1->load(scanProblem->getScanItem(i));
173  scrollview->addWidget(tmp1);
174  break;
175 
177  tmp2 = new CScanWidgetRepeat(scrollview);
178  tmp2->load(scanProblem->getScanItem(i));
179  scrollview->addWidget(tmp2);
180  break;
181 
183  tmp3 = new CScanWidgetRandom(scrollview);
184  tmp3->load(scanProblem->getScanItem(i));
185  scrollview->addWidget(tmp3);
186  break;
187 
188  default:
189  break;
190  }
191  }
192 
193  // the widget for the subtask
195  tmpT->load(scanProblem);
196  scrollview->addWidget(tmpT, false); //false: no control buttons (up/down/del)
197 
198  mChanged = false;
199 
200  return true;
201 }
202 
204 {
205  int totalRows = -1;
206  //+++
207  CScanWidgetScan* tmp1;
208  CScanWidgetRepeat* tmp2;
209  CScanWidgetRandom* tmp3;
210  //CScanWidgetBreak* tmp4;
211 
212  int intType = comboType->currentIndex();
213  CScanProblem::Type type;
214 
215  switch (intType)
216  {
217  case 0:
219  break;
220 
221  case 1:
223  break;
224 
225  case 2:
227  break;
228 
229  default:
231  break;
232  }
233 
234  switch (type)
235  {
237  {
238  tmp2 = new CScanWidgetRepeat(scrollview);
239 
240  //create item to get the default values
242  tmp2->load(pItem);
243  pdelete(pItem);
244 
245  scrollview->insertWidget(tmp2);
246  totalRows = scrollview->rowCount();
247  tmp2->lineEditNumber->setFocus();
248  }
249  break;
250 
252  {
255 
256  std::vector< const CCopasiObject * > Selection = CCopasiSelectionDialog::getObjectVector(this, Classes);
257 
258  // create scan widgets as many as the number of selected objects
259  std::vector< const CCopasiObject * >::iterator it = Selection.begin();
260  std::vector< const CCopasiObject * >::iterator end = Selection.end();
261 
262  for (; it != end; ++it)
263  {
264  tmp1 = new CScanWidgetScan(scrollview);
265  tmp1->initFromObject(*it);
266  scrollview->insertWidget(tmp1);
267  totalRows = scrollview->rowCount();
268  tmp1->lineEditMin->setFocus();
269  }
270 
271  break;
272  }
273 
275  {
278 
279  std::vector< const CCopasiObject * > Selection = CCopasiSelectionDialog::getObjectVector(this, Classes);
280 
281  // create scan widgets as many as the number of selected objects
282  std::vector< const CCopasiObject * >::iterator it = Selection.begin();
283  std::vector< const CCopasiObject * >::iterator end = Selection.end();
284 
285  for (; it != end; ++it)
286  {
287  tmp3 = new CScanWidgetRandom(scrollview);
288  tmp3->initFromObject(*it);
289  scrollview->insertWidget(tmp3);
290  totalRows = scrollview->rowCount();
291  tmp3->lineEditMin->setFocus();
292  }
293 
294  break;
295  }
296 
297  default:
298  break;
299  }
300 
301  return true;
302 }
303 
305 {
306  saveCommon();
307 
308  CScanTask* scanTask =
309  dynamic_cast< CScanTask * >(CCopasiRootContainer::getKeyFactory()->get(mKey));
310 
311  if (!scanTask) return false;
312 
313  CScanProblem *scanProblem = dynamic_cast<CScanProblem *>(scanTask->getProblem());
314 
315  if (!scanProblem) return false;
316 
317  const std::vector< QWidget * > & widgetList = scrollview->getWidgetList();
318 
319  size_t newSize = widgetList.size() - 1; // The last widget is reserved for the subtask.
320  size_t oldSize = scanProblem->getNumberOfScanItems();
321 
322  size_t i, imax = std::min(newSize, oldSize);
323 
324  mChanged = false;
325 
326  for (i = 0; i < imax; ++i)
327  {
328  QWidget * pWidget = widgetList[i];
329 
330  if (pWidget->objectName() == "CScanWidgetScan")
331  {
332  mChanged |= static_cast< CScanWidgetScan * >(pWidget)->save(scanProblem->getScanItem(i));
333  }
334  else if (pWidget->objectName() == "CScanWidgetRandom")
335  {
336  mChanged |= static_cast< CScanWidgetRandom * >(pWidget)->save(scanProblem->getScanItem(i));
337  }
338  else if (pWidget->objectName() == "CScanWidgetRepeat")
339  {
340  mChanged |= static_cast< CScanWidgetRepeat * >(pWidget)->save(scanProblem->getScanItem(i));
341  }
342  }
343 
344  for (; i < newSize; ++i)
345  {
346  mChanged = true;
347  QWidget * pWidget = widgetList[i];
349 
350  if (pWidget->objectName() == "CScanWidgetScan")
351  {
352  static_cast< CScanWidgetScan * >(pWidget)->save(pItem);
353  }
354  else if (pWidget->objectName() == "CScanWidgetRandom")
355  {
356  static_cast< CScanWidgetRandom * >(pWidget)->save(pItem);
357  }
358  else if (pWidget->objectName() == "CScanWidgetRepeat")
359  {
360  static_cast< CScanWidgetRepeat * >(pWidget)->save(pItem);
361  }
362  }
363 
364  for (; i < oldSize; ++i)
365  {
366  mChanged = true;
367  scanProblem->removeScanItem(newSize);
368  }
369 
370  // the subtask
371  const CScanWidgetTask * tmpT = dynamic_cast<CScanWidgetTask*>(widgetList[newSize]);
372 
373  if (tmpT != NULL)
374  {
375  mChanged |= tmpT->save(scanProblem);
376  }
377 
378  // :TODO Bug 322: This should only be called when actual changes have been saved.
379  // However we do not check whether the scan item are mChanged we delete all
380  // and create them new.
381  if (mChanged)
382  {
383  if (mpDataModel != NULL)
384  {
385  mpDataModel->changed();
386  }
387 
388  mChanged = false;
389  }
390 
391  return true;
392 }
#define pdelete(p)
Definition: copasi.h:215
void initFromObject(const CCopasiObject *obj)
CCopasiProblem * getProblem()
virtual bool loadTask()
Definition: ScanWidget.cpp:130
bool loadCommon()
Definition: TaskWidget.cpp:247
CScanContainerWidget * scrollview
Definition: ScanWidget.h:46
QGridLayout * ScanWidgetLayout
Definition: ScanWidget.h:53
CCopasiObject * get(const std::string &key)
virtual void load(const CCopasiParameterGroup *pItem)
static std::vector< const CCopasiObject * > getObjectVector(QWidget *pParent, const CQSimpleSelectionTree::ObjectClasses &classes, const std::vector< const CCopasiObject * > *pCurrentSelection=NULL)
bool saveCommon()
Definition: TaskWidget.cpp:258
ScanWidget(QWidget *parent=0, const char *name=0, Qt::WFlags fl=0)
Definition: ScanWidget.cpp:56
virtual void load(const CScanProblem *pg)
virtual void load(const CCopasiParameterGroup *pItem)
size_t getNumberOfScanItems() const
void changed(const bool &changed=true)
bool slotAddItem()
Definition: ScanWidget.cpp:203
bool setTaskName(const std::string &name)
bool mChanged
Definition: TaskWidget.h:133
CCopasiParameterGroup * addScanItem(CScanProblem::Type type, size_t steps=5, const CCopasiObject *obj=NULL)
virtual bool saveTask()
Definition: ScanWidget.cpp:304
QComboBox * comboType
Definition: ScanWidget.h:51
std::vector< QWidget * > getWidgetList() const
QPushButton * buttonNewItem
Definition: ScanWidget.h:50
static CCopasiParameterGroup * createScanItem(CScanProblem::Type type, size_t steps=5, const CCopasiObject *obj=NULL)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
CCopasiDataModel * mpDataModel
Definition: copasiWidget.h:65
static CKeyFactory * getKeyFactory()
bool commonRunTask()
Definition: TaskWidget.cpp:395
CQTaskBtnWidget * mpBtnWidget
Definition: TaskWidget.h:124
CQTaskHeaderWidget * mpHeaderWidget
Definition: TaskWidget.h:122
virtual void load(const CCopasiParameterGroup *pItem)
const CCopasiParameter::Value & getValue(const std::string &name) const
const CCopasiParameterGroup * getScanItem(size_t index) const
bool commonBeforeRunTask()
Definition: TaskWidget.cpp:317
void addWidget(QWidget *widget, bool controls=true)
std::string mKey
Definition: copasiWidget.h:63
#define min(a, b)
Definition: f2c.h:175
virtual bool save(CScanProblem *pg) const
bool removeScanItem(const size_t &index)
void initFromObject(const CCopasiObject *obj)
virtual bool runTask()
Definition: ScanWidget.cpp:119
void insertWidget(QWidget *widget, int row=-1)