COPASI API  4.16.103
CQParameterOverviewWidget.cpp
Go to the documentation of this file.
1 // Copyright (C) 2012 - 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 #include <QtGui/QSortFilterProxyModel>
7 
10 #include "CQMessageBox.h"
11 #include "CopasiFileDialog.h"
12 #include "qtUtilities.h"
13 #include "CQNameSelectionDialog.h"
14 #include "CQPushButtonDelegate.h"
15 #include "CQComboDelegate.h"
16 
20 #include "model/CModel.h"
23 
24 CQParameterOverviewWidget::CQParameterOverviewWidget(QWidget* parent, const char* name):
25  CopasiWidget(parent, name),
26  mpParameterSet(NULL),
27  mpParameterSetCopy(NULL),
28  mpParameterSetDM(NULL),
29  mpParameterSetSortDM(NULL),
30  mGlobalQuantities()
31 {
32  setupUi(this);
33 
34  // create a new QListview to be displayed on the screen..and set its property
36  mpParameterSetSortDM = new QSortFilterProxyModel(this);
37 
38  mpParameterSetSortDM->setSourceModel(mpParameterSetDM);
39  mpParameterSetSortDM->setSortRole(Qt::EditRole);
40  mpParameterSetSortDM->setSortCaseSensitivity(Qt::CaseInsensitive);
41  // pSortModel->sort(0, Qt::AscendingOrder);
42 
43  mpTreeView->setModel(mpParameterSetSortDM);
44  mpTreeView->sortByColumn(0, Qt::AscendingOrder);
45 
47  mpTreeView->setItemDelegateForColumn(1, pPushButtonDelegate);
48  connect(pPushButtonDelegate, SIGNAL(clicked(const QModelIndex &)), this, SLOT(slotResolve(const QModelIndex &)));
49 
50  CQComboDelegate * pComboDelegate = new CQComboDelegate(&mGlobalQuantities, this);
51  mpTreeView->setItemDelegateForColumn(5, pComboDelegate);
52 
53  connect(mpParameterSetDM, SIGNAL(signalOpenEditor(const QModelIndex &)), this, SLOT(slotOpenEditor(const QModelIndex &)));
54  connect(mpParameterSetDM, SIGNAL(signalCloseEditor(const QModelIndex &)), this, SLOT(slotCloseEditor(const QModelIndex &)));
55 }
56 
58 {
59  // TODO Auto-generated destructor stub
60 }
61 
62 // virtual
64 {
65  if (mIgnoreUpdates)
66  {
67  return true;
68  }
69 
70  switch (objectType)
71  {
72  case ListViews::STATE:
74  break;
75 
76  case ListViews::MODEL:
77 
78  if (action == ListViews::ADD ||
79  action == ListViews::DELETE)
80  {
81  mKey = "";
82  mpObject = NULL;
83 
85  }
86 
87  break;
88 
91 
92  if (mKey == key)
93  {
94  switch (action)
95  {
96  case ListViews::DELETE:
97  mKey = "";
98  mpObject = NULL;
99 
100  enterProtected();
101  break;
102 
103  case ListViews::CHANGE:
104  enterProtected();
105  break;
106 
107  default:
108  break;
109  }
110  }
111 
112  break;
113 
114  default:
115  break;
116  }
117 
118  return true;
119 }
120 
121 // virtual
123 {
124  if (mpParameterSet == NULL)
125  {
126  return true;
127  }
128 
130  static_cast< CModelParameter::Framework >(mFramework),
131  false) != CModelParameter::Identical)
132  {
134 
135  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
136  (*CCopasiRootContainer::getDatamodelList())[0]->changed();
137 
138  if (mpParameterSet->isActive())
139  {
142  }
143  else
144  {
146  }
147  }
148 
149  return true;
150 }
151 
152 // virtual
154 {
155  CopasiWidget::setFramework(framework);
156 
157  if (mpParameterSet == NULL)
158  {
159  return;
160  }
161 
162  if (!mpParameterSet->isActive())
163  {
164  mpParameterSetCopy->compareWithModel(static_cast< CModelParameter::Framework >(mFramework));
165  }
166 
167  mpParameterSetDM->setFramework(framework);
168  mpTreeView->expandAll();
169  mpTreeView->resizeColumnToContents(3);
170 }
171 
172 // virtual
174 {
175  mpParameterSet = dynamic_cast< CModelParameterSet * >(mpObject);
176 
177  if (!mpParameterSet)
178  {
181 
182  return false;
183  }
184 
185  // We need to make sure the original is fully compiled.
187 
188  CModelParameterSet * pOldParameterSet = mpParameterSetCopy;
190 
191  mGlobalQuantities.clear();
192 
193  if (mpParameterSet->isActive())
194  {
195  mpTreeView->header()->hideSection(1);
196  }
197  else
198  {
199  mpParameterSetCopy->compareWithModel(static_cast< CModelParameter::Framework >(mFramework));
200  mpHeaderWidget->hide();
201  mpBtnWidget->hide();
202  }
203 
205 
207  pdelete(pOldParameterSet);
208 
209  mpTreeView->expandAll();
210 
211  for (int i = 0; i < 6; i++)
212  {
213  mpTreeView->resizeColumnToContents(i);
214  }
215 
216  return true;
217 }
218 
220 {
221  // We build the selection for the global parameters for kinetic constants.
222  const CModelParameterGroup *pGlobalQuantities =
223  static_cast< CModelParameterGroup * >(mpParameterSetCopy->getModelParameter(CCopasiStaticString("Initial Global Quantities").getCN()));
224 
225  CModelParameterGroup::const_iterator it = pGlobalQuantities->begin();
226  CModelParameterGroup::const_iterator end = pGlobalQuantities->end();
227 
228  mGlobalQuantities.clear();
229  mGlobalQuantities.append("");
230 
231  for (; it != end; ++it)
232  {
233  if ((*it)->getCompareResult() != CModelParameter::Missing)
234  {
235  mGlobalQuantities.append(FROM_UTF8((*it)->getName()));
236  }
237  }
238 }
239 
240 // virtual
242 {
243  mpBtnRevert->setFocus();
244  enterProtected();
245 }
246 
247 // virtual
249 {
250  mpBtnCommit->setFocus();
251  leave();
252  enterProtected();
253 }
254 
255 // virtual
257 {
258  if (mpParameterSet == NULL)
259  {
260  return;
261  }
262 
263  CModel * pModel = mpParameterSet->getModel();
264 
265  if (pModel == NULL)
266  {
267  return;
268  }
269 
271 
273 
274  // Notify the GUI of the delete.
276 }
277 
278 // virtual
280 {
281  // This button is shown as Apply and is used to assign the parameter values to the model.
282 
283  // commit all changes
284  slotBtnCommit();
285 
286  // Sanity checks
287  if (mpParameterSet == NULL)
288  {
289  return;
290  }
291 
292  CModel * pModel = mpParameterSet->getModel();
293 
294  if (pModel == NULL)
295  {
296  return;
297  }
298 
299  // We first asked whether the user wants to save the current model values
300  if (CQMessageBox::question(this, "Save current Model Parameters?",
301  "You are about to overwrite the current model values.\n"
302  "Do you want to save them?",
303  QMessageBox::Save | QMessageBox::Discard,
304  QMessageBox::Save) == QMessageBox::Save)
305  {
306  // Save the parameter set to a new or existing set
307  CQNameSelectionDialog Dialog(this);
308 
310 
313  QStringList SelectionList;
314 
315  for (; it != end; ++it)
316  {
317  SelectionList.append(FROM_UTF8((*it)->getName()));
318  }
319 
320  Dialog.setSelectionList(SelectionList);
321  QString Name;
322 
323  if (Dialog.exec() != QDialog::Rejected &&
324  (Name = Dialog.getSelection()) != "")
325  {
326  if (SelectionList.indexOf(Name) == -1)
327  {
328  CModelParameterSet * pNew = new CModelParameterSet(pModel->getModelParameterSet(), pModel, false);
329  pNew->setObjectName(TO_UTF8(Name));
330  Sets.add(pNew, true);
331 
332  // Notify the GUI of the insert
334  }
335  else
336  {
337  CModelParameterSet * pExisting = Sets[TO_UTF8(Name)];
338  pExisting->assignSetContent(pModel->getModelParameterSet(), false);
339 
340  // Notify the GUI of the insert
342  }
343  }
344  }
345 
347 
348  // Notify the GUI that the model state has changed.
350 
351  enterProtected();
352 }
353 
355 {
356  // commit all changes
357  slotBtnCommit();
358 
359  // Sanity checks
360  if (mpParameterSet == NULL)
361  {
362  return;
363  }
364 
365  CModel * pModel = mpParameterSet->getModel();
366 
367  if (pModel == NULL)
368  {
369  return;
370  }
371 
373  std::string Name = "Parameter Set";
374  int i = 0;
375 
376  while (Sets.getIndex(Name) != C_INVALID_INDEX)
377  {
378  i++;
379  Name = "Parameter Set ";
380  Name += TO_UTF8(QString::number(i));
381  }
382 
383  CModelParameterSet * pNew = new CModelParameterSet(pModel->getModelParameterSet(), pModel, false);
384  pNew->setObjectName(Name);
385  Sets.add(pNew, true);
386 
387  // Notify the GUI of the insert
389 
391 }
392 
393 // virtual
395 {
396  // Commit all changes
397  slotBtnCommit();
398 
399  C_INT32 Answer = QMessageBox::No;
400  QString fileName;
401 
402  while (Answer == QMessageBox::No)
403  {
404  fileName =
405  CopasiFileDialog::getSaveFileName(this, "Save File Dialog",
406  "untitled.tsv", "Tab Separated Files (*.tsv);;Comma Separated Files (*.csv);;TEXT Files (*.txt)", "Save as");
407 
408  if (fileName.isEmpty()) return;
409 
410  // Checks whether the file exists
411  Answer = checkSelection(fileName);
412 
413  if (Answer == QMessageBox::Cancel) return;
414  }
415 
416  std::ofstream file(CLocaleString::fromUtf8(TO_UTF8(fileName)).c_str());
417 
418  if (file.fail()) return;
419 
420  std::string mode;
421  std::string separator;
422 
423  if (fileName.endsWith(".txt"))
424  {
425  mode = "report";
426  separator = "\t";
427  }
428  else if (fileName.endsWith(".csv"))
429  {
430  mode = "table";
431  separator = ",";
432  }
433  else
434  {
435  mode = "table";
436  separator = "\t";
437  }
438 
439  mpParameterSet->saveToStream(file, static_cast< CModelParameter::Framework >(mFramework), mode, separator);
440 
441  file.close();
442 }
443 
444 // virtual
446 {
447  // commit all changes
448  slotBtnCommit();
449 
450  if (mpParameterSet == NULL)
451  {
452  return;
453  }
454 
455  CModel * pModel = mpParameterSet->getModel();
456 
457  if (pModel == NULL)
458  {
459  return;
460  }
461 
462  // Save the parameter set to a new or existing set
463  CQNameSelectionDialog Dialog(this);
464 
466 
469  QStringList SelectionList;
470 
471  for (; it != end; ++it)
472  {
473  SelectionList.append(FROM_UTF8((*it)->getName()));
474  }
475 
476  Dialog.setSelectionList(SelectionList);
477 
478  if (Dialog.exec() == QDialog::Rejected)
479  {
480  return;
481  }
482 
483  QString Name = Dialog.getSelection();
484 
485  if (Name == "")
486  {
487  return;
488  }
489 
490  if (SelectionList.indexOf(Name) == -1)
491  {
492  CModelParameterSet * pNew = new CModelParameterSet(*mpParameterSet, pModel, false);
493  pNew->setObjectName(TO_UTF8(Name));
494  Sets.add(pNew, true);
495 
496  // Notify the GUI of the insert
498  }
499  else
500  {
501  CModelParameterSet * pExisting = Sets[TO_UTF8(Name)];
502  pExisting->assignSetContent(*mpParameterSet, false);
503 
504  // Notify the GUI of the insert
506  }
507 
508  return;
509 }
510 
511 void CQParameterOverviewWidget::slotOpenEditor(const QModelIndex & index)
512 {
513  QModelIndex Tmp = index;
514  const QAbstractItemModel *pModel = Tmp.model();
515 
516  while (pModel->inherits("QSortFilterProxyModel"))
517  {
518  Tmp = static_cast< const QSortFilterProxyModel *>(pModel)->mapToSource(index);
519  pModel = Tmp.model();
520  }
521 
522  mpTreeView->openPersistentEditor(mpParameterSetSortDM->mapFromSource(Tmp));
523 }
524 
525 void CQParameterOverviewWidget::slotCloseEditor(const QModelIndex & index)
526 {
527  QModelIndex Tmp = index;
528  const QAbstractItemModel *pModel = Tmp.model();
529 
530  while (pModel->inherits("QSortFilterProxyModel"))
531  {
532  Tmp = static_cast< const QSortFilterProxyModel *>(pModel)->mapToSource(index);
533  pModel = Tmp.model();
534  }
535 
536  mpTreeView->closePersistentEditor(Tmp);
537 }
538 
539 void CQParameterOverviewWidget::slotResolve(const QModelIndex & index)
540 {
541  CModelParameter * pModelParameter = mpParameterSetDM->nodeFromIndex(index);
542 
543  if (pModelParameter == NULL)
544  {
545  return;
546  }
547 
548  pModelParameter->refreshFromModel(true);
549 
550  mpParameterSetCopy->compareWithModel(static_cast< CModelParameter::Framework >(mFramework));
551 
553 
555 
556  mpTreeView->expandAll();
557  mpTreeView->resizeColumnToContents(3);
558 }
void setSelectionList(const QStringList &selectionList)
const CModelParameterSet & getModelParameterSet() const
Definition: CModel.cpp:1072
#define pdelete(p)
Definition: copasi.h:215
objectType
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
void switchToOtherWidget(const size_t &id, const std::string &key)
Definition: listviews.cpp:926
bool compareWithModel(const CModelParameter::Framework &framework)
const std::string & getObjectName() const
CModelParameter * getModelParameter(const std::string &cn) const
iterator begin()
CQParameterOverviewDM * mpParameterSetDM
CCopasiObject * mpObject
Definition: copasiWidget.h:64
#define C_INVALID_INDEX
Definition: copasi.h:222
void slotOpenEditor(const QModelIndex &index)
virtual size_t getIndex(const std::string &name) const
#define C_INT32
Definition: copasi.h:90
bool saveToStream(std::ostream &os, const CModelParameter::Framework &framework, const std::string &mode, const std::string &separator)
bool mIgnoreUpdates
Definition: copasiWidget.h:67
iterator end()
virtual bool add(const CType &src)
void slotResolve(const QModelIndex &index)
virtual const std::string & getKey() const
CQParameterOverviewWidget(QWidget *parent=0, const char *name=0)
ListViews * mpListView
Definition: copasiWidget.h:62
void slotCloseEditor(const QModelIndex &index)
C_INT32 checkSelection(const QString &file)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
QSortFilterProxyModel * mpParameterSetSortDM
const QString & getSelection() const
const CCopasiVectorN< CModelParameterSet > & getModelParameterSets() const
Definition: CModel.cpp:1066
const std::string & getKey() const
Definition: CModel.cpp:1142
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
static CModelParameter * nodeFromIndex(const QModelIndex &index)
virtual void remove(const std::string &name)
virtual bool refreshFromModel(const bool &modifyExistence)
void setFramework(const int &framework)
CCopasiDataModel * mpDataModel
Definition: copasiWidget.h:65
virtual void setFramework(int framework)
std::vector< CModelParameter * >::const_iterator const_iterator
virtual const CompareResult & diff(const CModelParameter &other, const CModelParameter::Framework &framework, const bool &createMissing=false)
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
CModelParameterSet * mpParameterSetCopy
Definition: CModel.h:50
static CLocaleString fromUtf8(const std::string &utf8)
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
bool setObjectName(const std::string &name)
virtual void setFramework(int framework)
std::string mKey
Definition: copasiWidget.h:63
void assignSetContent(const CModelParameterSet &src, const bool &createMissing)
virtual bool updateModel()
void setModelParameterset(CModelParameterSet *pModelParameterSet)
CModel * getModel() const
static StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
static QString getSaveFileName(QWidget *parent=0, const char *name=0, const QString &startWith=QString::null, const QString &filter=QString::null, const QString &caption=QString::null, QString *pSelectedFilter=NULL, QFileDialog::Options options=0)