COPASI API  4.16.103
CQReportDefinition.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) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include "CQReportDefinition.h"
16 
17 #include "CCopasiSelectionDialog.h"
18 #include "CQTextDialog.h"
19 #include "CQMessageBox.h"
20 #include "qtUtilities.h"
21 #include "CQReportListItem.h"
22 
23 #include "report/CKeyFactory.h"
29 
30 /*
31  * Constructs a CQReportDefinition which is a child of 'parent', with the
32  * name 'name'.'
33  */
34 CQReportDefinition::CQReportDefinition(QWidget* parent, const char* name)
35  : CopasiWidget(parent, name)
36 {
37  setupUi(this);
38 
39  mKey = "";
40  mpReportDefinition = NULL;
41 
42  // We start with the table since this is simpler.
43  setAdvancedMode(false);
44 
45  unsigned C_INT32 i;
46 
47  QStringList TaskNames;
48 
49  for (i = 0; CCopasiTask::TypeName[i] != ""; i++)
50  {
51  TaskNames.append(FROM_UTF8(CCopasiTask::TypeName[i]));
52  }
53 
54  mpTaskBox->addItems(TaskNames);
55 }
56 
57 /*
58  * Destroys the object and frees any allocated resources
59  */
61 {
62  destroy();
63  // no need to delete child widgets, Qt does it all for us
64 }
65 
66 void CQReportDefinition::nameChanged(const QString & /* string */)
67 {mChanged = true;}
68 
69 void CQReportDefinition::taskChanged(const QString & /* string */)
70 {mChanged = true;}
71 
73 {mChanged = true;}
74 
75 void CQReportDefinition::separatorChanged(const QString & /* string */)
76 {mChanged = true;}
77 
78 void CQReportDefinition::precisionChanged(const QString & /* string */)
79 {mChanged = true;}
80 
82 {
83  mChanged = true;
84 
85  if (mpTabCheck->isChecked())
86  mpSeparator->setEnabled(false);
87  else
88  mpSeparator->setEnabled(true);
89 }
90 
92 {
93  if (mAdvanced)
94  {
95  if (CQMessageBox::question(NULL, "Report Conversion",
96  "Converting an advanced report to a table may result in loss of customization.\n"
97  "Do you want to proceed?",
98  QMessageBox::Ok | QMessageBox::Cancel,
99  QMessageBox::Cancel) == QMessageBox::Ok)
100  {
101  // We convert the body without the separators to a table.
102  mpTableList->clear();
103 
104  unsigned C_INT32 i, imax;
105 
106  for (i = 0, imax = mpBodyList->count(); i < imax; i++)
107  if (static_cast<CQReportListItem *>(mpBodyList->item(i))->getCN().getObjectType()
108  != "Separator")
109  new CQReportListItem(static_cast<CQReportListItem *>(mpBodyList->item(i))->getCN());
110 
111  mpHeaderList->clear();
112  mpBodyList->clear();
113  mpFooterList->clear();
114 
115  setAdvancedMode(false);
116 
117  mChanged = true;
118  }
119  }
120  else
121  {
122  // To achieve the same result as with the table we use the preCompileTable
123  // method of CReportDefinition. Since we must not change the existing report,
124  // which may only be done by btnCommitClicked or leave, we create a temporary
125  // copy.
127 
128  // We avoid the renaming signal.
130 
131  mChanged = true;
132  save();
133 
136 
137  load();
138 
139  delete mpReportDefinition;
140 
141  mpReportDefinition = pStore;
142 
143  mChanged = true;
144  }
145 }
146 
148 {
149  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
150  CModel* pModel = (*CCopasiRootContainer::getDatamodelList())[0]->getModel();
151 
152  if (!pModel) return;
153 
154  std::vector< const CCopasiObject * > SelectedVector =
155  // CCopasiSelectionDialog::getObjectVector(this, CQSimpleSelectionTree::NO_RESTRICTION);
157 
158  CQReportListItem * pItem;
159 
160  if (SelectedVector.size() != 0)
161  {
162  QListWidget * pList = static_cast< QListWidget * >(mpReportSectionTab->currentWidget());
163  std::vector< const CCopasiObject * >::const_iterator it = SelectedVector.begin();
164  std::vector< const CCopasiObject * >::const_iterator end = SelectedVector.end();
165 
166  for (; it != end; ++it)
167  {
168  pItem = new CQReportListItem(*it);
169  pList->addItem(pItem);
170  }
171 
172  mChanged = true;
173  }
174 
175  return;
176 }
177 
179 {
180  CCopasiReportSeparator Separator;
181 
182  if (mpTabCheck->isChecked())
183  Separator = "\t";
184  else
185  Separator = TO_UTF8(mpSeparator->text());
186 
187  static_cast<QListWidget *>(mpReportSectionTab->currentWidget())->addItem(new CQReportListItem(Separator.getCN()));
188 
189  mChanged = true;
190 
191  return;
192 }
193 
195 {
196  CQTextDialog * pDialog = new CQTextDialog(this);
197 
198  if (pDialog->exec() == QDialog::Accepted &&
199  pDialog->getText() != "")
200  {
201  CCopasiStaticString Text(TO_UTF8(pDialog->getText()));
202 
203  static_cast<QListWidget *>(mpReportSectionTab->currentWidget())->addItem(new CQReportListItem(Text.getCN()));
204  }
205 
206  delete pDialog;
207 
208  mChanged = true;
209 
210  return;
211 }
212 
214 {
215  QListWidget * pList = static_cast< QListWidget * >(mpReportSectionTab->currentWidget());
216 
217  QListWidgetItem * pNewSelection = NULL;
218 
219  int i, multipleSelection;
220 
221  for (i = pList->count() - 1, multipleSelection = 0; 0 <= i; i--)
222  if (pList->item(i)->isSelected())
223  {
224  delete pList->takeItem(i);
225 
226  if (!pNewSelection && i < pList->count())
227  {
228  pNewSelection = pList->item(i); // We select the next.
229  }
230 
231  multipleSelection++;
232  }
233 
234  if (multipleSelection == 0) return; // Nothing selected,
235 
236  mChanged = true;
237  pList->clearSelection();
238 
239  if (multipleSelection > 1) return;
240 
241  // Only one item was select and we move the selection to the next
242  if (!pNewSelection && pList->count()) // We have removed item at the end.
243  pNewSelection = pList->item(pList->count() - 1);
244 
245  // pNewSelection is NULL if the list is empty
246  if (pNewSelection)
247  {
248  pNewSelection->setSelected(true);
249  }
250 
251  return;
252 }
253 
255 {
256  QListWidget * pList = static_cast< QListWidget * >(mpReportSectionTab->currentWidget());
257  int i, to, multipleSelection;
258 
259  QListWidgetItem * pMove;
260 
261  for (i = pList->count() - 1, to = -1, multipleSelection = 0; i >= 0; i--)
262  if (pList->item(i)->isSelected())
263  {
264  if (multipleSelection == 0)
265  {
266  to = i;
267  }
268 
269  multipleSelection++;
270  }
271  else if (multipleSelection > 0)
272  {
273  pMove = pList->takeItem(i);
274 
275  if (pMove)
276  {
277  pList->insertItem(to, pMove);
278 
279  multipleSelection = 0;
280  mChanged = true;
281  }
282  }
283 
284  // Unselect things we can not move.
285  for (i = 0; i < multipleSelection; i++)
286  pList->item(i)->setSelected(false);
287 
288  return;
289 }
290 
292 {
293  QListWidget * pList = static_cast< QListWidget * >(mpReportSectionTab->currentWidget());
294  int i, imax, to, multipleSelection;
295 
296  QListWidgetItem * pMove;
297 
298  // Find the index of the first selected item.
299  for (i = 0, imax = pList->count(), to = -1, multipleSelection = 0; i < imax; i++)
300  if (pList->item(i)->isSelected())
301  {
302  if (multipleSelection == 0) to = i;
303 
304  multipleSelection++;
305  }
306  else if (multipleSelection > 0)
307  {
308  pMove = pList->takeItem(i);
309 
310  if (pMove)
311  {
312  pList->insertItem(to, pMove);
313 
314  multipleSelection = 0;
315  mChanged = true;
316  }
317  }
318 
319  // Unselect things we can not move.
320  for (i = pList->count() - multipleSelection, imax = pList->count(); i < imax; i++)
321  pList->item(i)->setSelected(false);
322 
323  return;
324 }
325 
327 {mChanged = true;}
328 
330 {
331  if (mpDataModel == NULL)
332  return;
333 
334  std::set< const CCopasiObject * > Tasks;
335  std::set< const CCopasiObject * > DeletedObjects;
336  DeletedObjects.insert(mpObject);
337 
338  QMessageBox::StandardButton choice =
339  CQMessageBox::confirmDelete(this, "report",
341  DeletedObjects);
342 
343  switch (choice)
344  {
345  case QMessageBox::Ok:
346  {
348 
349  if (pReportList == NULL)
350  return;
351 
352  if (mpDataModel->appendDependentTasks(DeletedObjects, Tasks))
353  {
354  std::set< const CCopasiObject * >::iterator it = Tasks.begin();
355  std::set< const CCopasiObject * >::iterator end = Tasks.end();
356 
357  for (; it != end; ++it)
358  {
359  const CCopasiTask * pTask = static_cast< const CCopasiTask *>(*it);
360  const_cast< CCopasiTask * >(pTask)->getReport().setReportDefinition(NULL);
361  }
362  }
363 
364  size_t Index = pReportList->getIndex(mpObject);
365  std::string DeletedKey = mpObject->getKey();
366 
367  pReportList->remove(Index);
368 
369  size_t Size = pReportList->size();
370 
371  if (Size > 0)
372  enter((*pReportList)[std::min(Index, Size - 1)]->getKey());
373  else
374  enter("");
375 
377  break;
378  }
379 
380  default:
381  break;
382  }
383 }
384 
386 {
388 
389  std::string Name = "report";
390 
391  int i = 0;
392  CReportDefinition* pRep;
393  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
394 
395  while (!(pRep = (*CCopasiRootContainer::getDatamodelList())[0]->getReportDefinitionList()->createReportDefinition(Name, "")))
396  {
397  i++;
398  Name = "report_";
399  Name += TO_UTF8(QString::number(i));
400  }
401 
402  std::string key = pRep->getKey();
404  enter(key);
406 }
407 
409 {
411 
413  if (pDataModel == NULL) return;
414 
415  CReportDefinition * pRep = new CReportDefinition(*dynamic_cast<CReportDefinition*>(CCopasiRootContainer::getKeyFactory()->get(mKey)));
416 
417  std::string baseName = pRep->getObjectName() + "_copy";
418  std::string name = baseName;
419 
420  int i =1;
421 
422  while (pDataModel->getReportDefinitionList()->getIndex(name) != C_INVALID_INDEX)
423  {
424  i++;
425  name = baseName + TO_UTF8(QString::number(i));
426  }
427  pRep->setObjectName(name);
428 
429  pDataModel->getReportDefinitionList()->add(pRep, true);
430 
431  std::string key = pRep->getKey();
433  enter(key);
435 }
436 
437 //CCopasiDataModel* pDataModel = mpObject->getObjectDataModel();
438 //if (pDataModel == NULL) return;
439 
440 //CPlotSpecification * pPl = new CPlotSpecification(*dynamic_cast<CPlotSpecification*>(CCopasiRootContainer::getKeyFactory()->get(mKey)));
441 
442 //std::string baseName = pPl->getObjectName() + "_copy";
443 //std::string name = baseName;
444 
445 //int i = 1;
446 
447 //while (pDataModel->getPlotDefinitionList()->getIndex(name) != C_INVALID_INDEX)
448 // {
449 // i++;
450 // name = baseName + TO_UTF8(QString::number(i));
451 // }
452 
453 //pPl->setObjectName(name);
454 
455 //pDataModel->getPlotDefinitionList()->add(pPl, true);
456 
457 //std::string key = pPl->CCopasiParameter::getKey();
458 //protectedNotify(ListViews::PLOT, ListViews::ADD, key);
459 //enter(key);
460 //mpListView->switchToOtherWidget(C_INVALID_INDEX, key);
461 
462 
463 
465 {load();}
466 
468 {save();}
469 
471  ListViews::Action action,
472  const std::string & key)
473 {
474  // If the model is deleted or a new model is loaded the existing pointer
475  // becomes invalid.
476  if (objectType == ListViews::MODEL &&
477  (action == ListViews::DELETE ||
478  action == ListViews::ADD))
479  {
480  mpReportDefinition = NULL;
481  return true;
482  }
483 
484  if (mIgnoreUpdates ||
485  objectType != ListViews::REPORT ||
486  key != mKey ||
487  action == ListViews::DELETE)
488  return true;
489 
490  return load();
491 }
492 
494 {
495  mpBtnCommit->setFocus();
496 
497  save();
498 
499  mpNotes->leave();
500 
501  return true;
502 }
503 
505 {
506  mpReportDefinition = dynamic_cast<CReportDefinition *>(mpObject);
507 
508  if (!mpReportDefinition)
509  {
511  return false;
512  }
513 
514  load();
515 
516  mpNotes->enter(mKey);
517 
518  return true;
519 }
520 
522 {
523  if (!mpReportDefinition) return false;
524 
525  // Reset everything.
526  mpHeaderList->clear();
527  mpBodyList->clear();
528  mpFooterList->clear();
529  mpTableList->clear();
530 
531  mpName->setText(FROM_UTF8(mpReportDefinition->getObjectName()));
532  mpTaskBox->setCurrentIndex(mpReportDefinition->getTaskType());
533 
534  //separator
536  {
537  mpSeparator->setEnabled(false);
538  mpTabCheck->setChecked(true);
539  }
540  else
541  {
542  mpSeparator->setEnabled(true);
543  mpTabCheck->setChecked(false);
544  mpSeparator->setText(FROM_UTF8(mpReportDefinition->getSeparator().getStaticString()));
545  }
546 
547  mpPrecision->setText(QString::number(mpReportDefinition->getPrecision()));
548 
549  std::vector< CRegisteredObjectName > * pList = NULL;
550  std::vector< CRegisteredObjectName >::const_iterator it;
551  std::vector< CRegisteredObjectName >::const_iterator end;
552 
553  // Toggle the display mode.
555  {
556  setAdvancedMode(false);
557 
558  mpTitleCheck->setChecked(mpReportDefinition->getTitle());
559 
560  pList = mpReportDefinition->getTableAddr();
561 
562  for (it = pList->begin(), end = pList->end(); it != end; ++it)
563  mpTableList->addItem(new CQReportListItem(*it));
564  }
565  else
566  {
567  setAdvancedMode(true);
568 
570 
571  for (it = pList->begin(), end = pList->end(); it != end; ++it)
572  mpHeaderList->addItem(new CQReportListItem(*it));
573 
574  pList = mpReportDefinition->getBodyAddr();
575 
576  for (it = pList->begin(), end = pList->end(); it != end; ++it)
577  mpBodyList->addItem(new CQReportListItem(*it));
578 
580 
581  for (it = pList->begin(), end = pList->end(); it != end; ++it)
582  mpFooterList->addItem(new CQReportListItem(*it));
583  }
584 
585  mChanged = false;
586  return true;
587 }
588 
590 {
591  if (!mChanged) return true;
592 
593  if (!mpReportDefinition) return false;
594 
595  if (mpReportDefinition->getObjectName() != TO_UTF8(mpName->text()))
596  {
597  if (!mpReportDefinition->setObjectName(TO_UTF8(mpName->text())))
598  {
599  QString msg;
600  msg = "Unable to rename report '" + FROM_UTF8(mpReportDefinition->getObjectName()) + "'\n"
601  + "to '" + mpName->text() + "' since a report with that name already exists.";
602 
604  "Unable to rename Report",
605  msg,
606  QMessageBox::Ok, QMessageBox::Ok);
607 
608  mpName->setText(FROM_UTF8(mpReportDefinition->getObjectName()));
609  }
610  else
612  }
613 
614  mpReportDefinition->setTaskType((CCopasiTask::Type) mpTaskBox->currentIndex());
615 
616  CCopasiReportSeparator Separator;
617 
618  if (mpTabCheck->isChecked())
619  Separator = "\t";
620  else
621  Separator = TO_UTF8(mpSeparator->text());
622 
623  mpReportDefinition->setSeparator(Separator);
624 
625  mpReportDefinition->setPrecision(mpPrecision->text().toULong());
626 
627  mpReportDefinition->getHeaderAddr()->clear();
628  mpReportDefinition->getBodyAddr()->clear();
629  mpReportDefinition->getFooterAddr()->clear();
630  mpReportDefinition->getTableAddr()->clear();
631 
632  std::vector< CRegisteredObjectName > * pList = NULL;
633  unsigned C_INT32 i, imax;
634 
635  if (mAdvanced)
636  {
638 
640 
641  for (i = 0, imax = mpHeaderList->count(); i < imax; i++)
642  if (static_cast<CQReportListItem *>(mpHeaderList->item(i))->getCN().getObjectType()
643  == "Separator")
644  pList->push_back(Separator.getCN());
645  else
646  pList->push_back(static_cast<CQReportListItem *>(mpHeaderList->item(i))->getCN());
647 
648  pList = mpReportDefinition->getBodyAddr();
649 
650  for (i = 0, imax = mpBodyList->count(); i < imax; i++)
651  if (static_cast<CQReportListItem *>(mpBodyList->item(i))->getCN().getObjectType()
652  == "Separator")
653  pList->push_back(Separator.getCN());
654  else
655  pList->push_back(static_cast<CQReportListItem *>(mpBodyList->item(i))->getCN());
656 
658 
659  for (i = 0, imax = mpFooterList->count(); i < imax; i++)
660  if (static_cast<CQReportListItem *>(mpFooterList->item(i))->getCN().getObjectType()
661  == "Separator")
662  pList->push_back(Separator.getCN());
663  else
664  pList->push_back(static_cast<CQReportListItem *>(mpFooterList->item(i))->getCN());
665  }
666  else
667  {
669 
670  mpReportDefinition->setTitle(mpTitleCheck->isChecked());
671 
672  pList = mpReportDefinition->getTableAddr();
673 
674  for (i = 0, imax = mpTableList->count(); i < imax; i++)
675  pList->push_back(static_cast<CQReportListItem *>(mpTableList->item(i))->getCN());
676  }
677 
678  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
679  (*CCopasiRootContainer::getDatamodelList())[0]->changed();
680 
681  mChanged = false;
682  return false;
683 }
684 
685 bool CQReportDefinition::setAdvancedMode(const bool & advanced)
686 {
687  if (advanced)
688  {
689  mAdvanced = true;
690 
691  mpBtnAdvanced->setText("Advanced <<");
692  mpReportSectionTab->setTabEnabled(mpReportSectionTab->indexOf(mpTableList), false);
693  mpReportSectionTab->setTabEnabled(mpReportSectionTab->indexOf(mpHeaderList), true);
694  mpReportSectionTab->setTabEnabled(mpReportSectionTab->indexOf(mpBodyList), true);
695  mpReportSectionTab->setTabEnabled(mpReportSectionTab->indexOf(mpFooterList), true);
696  mpReportSectionTab->setCurrentIndex(2);
697  mpBtnSeparator->show();
698  mpBtnText->show();
699  mpTitleCheck->hide();
700  }
701  else
702  {
703  mAdvanced = false;
704 
705  mpBtnAdvanced->setText("Advanced >>");
706  mpReportSectionTab->setTabEnabled(mpReportSectionTab->indexOf(mpTableList), true);
707  mpReportSectionTab->setTabEnabled(mpReportSectionTab->indexOf(mpHeaderList), false);
708  mpReportSectionTab->setTabEnabled(mpReportSectionTab->indexOf(mpBodyList), false);
709  mpReportSectionTab->setTabEnabled(mpReportSectionTab->indexOf(mpFooterList), false);
710  mpReportSectionTab->setCurrentIndex(0);
711  mpBtnSeparator->hide();
712  mpBtnText->hide();
713  mpTitleCheck->show();
714  }
715 
716  return true;
717 }
CCopasiDataModel * getObjectDataModel()
objectType
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
static const std::string TypeName[]
Definition: CCopasiTask.h:67
void switchToOtherWidget(const size_t &id, const std::string &key)
Definition: listviews.cpp:926
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
virtual CCopasiObjectName getCN() const
bool setTaskType(const CCopasiTask::Type &taskType)
const std::string & getObjectName() const
bool appendDependentTasks(std::set< const CCopasiObject * > candidates, std::set< const CCopasiObject * > &dependentTasks) const
virtual size_t size() const
void setTitle(bool title)
const CCopasiTask::Type & getTaskType() const
static std::vector< const CCopasiObject * > getObjectVector(QWidget *pParent, const CQSimpleSelectionTree::ObjectClasses &classes, const std::vector< const CCopasiObject * > *pCurrentSelection=NULL)
const CCopasiReportSeparator & getSeparator() const
CCopasiObject * mpObject
Definition: copasiWidget.h:64
void nameChanged(const QString &string)
static StandardButton information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
#define C_INVALID_INDEX
Definition: copasi.h:222
virtual bool enterProtected()
virtual size_t getIndex(const std::string &name) const
void setSeparator(const CCopasiReportSeparator &Separator)
#define C_INT32
Definition: copasi.h:90
CQReportDefinition(QWidget *parent=0, const char *name=0)
const unsigned C_INT32 & getPrecision() const
void setIsTable(bool table)
bool mIgnoreUpdates
Definition: copasiWidget.h:67
const CReportDefinitionVector * getReportDefinitionList() const
virtual const std::string & getKey() const
CTSSATask * pTask
const std::string & getStaticString() const
void taskChanged(const QString &string)
virtual bool add(const CType &src)
void separatorChanged(const QString &string)
ListViews * mpListView
Definition: copasiWidget.h:62
std::vector< CRegisteredObjectName > * getTableAddr()
std::vector< CRegisteredObjectName > * getFooterAddr()
static StandardButton confirmDelete(QWidget *parent, const QString &objectType, const QString &objects, const std::set< const CCopasiObject * > &deletedObjects)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
virtual const std::string & getKey() const
virtual void remove(const size_t &index)
CCopasiDataModel * mpDataModel
Definition: copasiWidget.h:65
virtual size_t getIndex(const CCopasiObject *pObject) const
static CKeyFactory * getKeyFactory()
void setPrecision(const unsigned C_INT32 &precision)
CReportDefinition * mpReportDefinition
std::vector< CRegisteredObjectName > * getBodyAddr()
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
Definition: CModel.h:50
void precisionChanged(const QString &string)
bool setAdvancedMode(const bool &advanced)
bool setObjectName(const std::string &name)
std::string mKey
Definition: copasiWidget.h:63
std::vector< CRegisteredObjectName > * getHeaderAddr()
#define min(a, b)
Definition: f2c.h:175
bool preCompileTable(const std::vector< CCopasiContainer * > &listOfContainer=CCopasiContainer::EmptyList)
QString getText()
static StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
bool enter(const std::string &key)