COPASI API  4.16.103
CQEventWidget1.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) 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include "CQEventWidget1.h"
16 
17 #include <utility>
18 #include <vector>
19 #include <string.h>
20 
21 #include "UI/qtUtilities.h"
22 
23 #include "CQMessageBox.h"
24 #include "CCopasiSelectionDialog.h"
26 
28 #include "model/CModel.h"
29 #include "model/CEvent.h"
30 #include "report/CKeyFactory.h"
33 
34 /*
35  * Constructs a CQEventWidget1 which is a child of 'parent', with the
36  * name 'name'.'
37  */
38 CQEventWidget1::CQEventWidget1(QWidget * parent, const char * name):
39  CopasiWidget(parent, name),
40  mKeyToCopy("")
41 {
42  setupUi(this);
43 
44  init();
45 }
46 
47 /*
48  * Destroys the object and frees any allocated resources
49  */
51 {
52  // no need to delete child widgets, Qt does it all for us
53 }
54 
55 /*! Slot to delete the active event widget */
57 {
58  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
60 
61  CModel * pModel = pDataModel->getModel();
62 
63  if (pModel == NULL)
64  return;
65 
66  pDataModel->getModel()->removeEvent(mKey);
67 
68  mpEvent = NULL;
69 
71 }
72 
73 /// Slot to create a new event; activated whenever the New button is clicked
75 {
76  // save the current setting values
77  saveToEvent();
78 
79  // standard name
80  std::string name = "event_1";
81 
82  // if the standard name already exists then creating the new event will fail
83  // thus, a growing index will automatically be added to the standard name
84  int i = 1;
85  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
86 
87  while (!(*CCopasiRootContainer::getDatamodelList())[0]->getModel()->createEvent(name))
88  {
89  i++;
90  name = "event_";
91  name += TO_UTF8(QString::number(i));
92  }
93 
94  std::string key = (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->getEvents()[name]->getKey();
97 }
98 
100 {
101  mKeyToCopy = mKey;
102 }
103 
104 /*! */
106 {
107  showDelayExpression(false);
108 
109  // SIGNAL-SLOT connections
110  connect(mpComboBoxDelay, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChooseDelay(int)));
111  connect(mpLBTarget, SIGNAL(currentRowChanged(int)), this, SLOT(slotActualizeAssignmentExpression(int)));
112 
113 // mpExpressionTrigger->mpExpressionWidget->setBoolean(true);
114  mpExpressionTrigger->mpExpressionWidget->setBoolean(true);
115  mExpressionDelayValid = true;
116  mpExpressionDelay->mpExpressionWidget->setExpressionType(CQExpressionWidget::TransientExpression);
117 
118  mExpressionTriggerValid = false;
119  // mpExpressionTrigger->mpExpressionWidget->setExpressionType(CQSimpleSelectionTree::TRANSIENT_EXPRESSION);
120 
121  mExpressionEAValid = false;
122  mpExpressionEA->mpExpressionWidget->setExpressionType(CQExpressionWidget::TransientExpression);
123 
124  // ----- correlated to GUI layout of the event assignment ----
125 
126  // hide the label and widget with respect to expression
127  mpLabelEA->setEnabled(false);
128  mpExpressionEA->setEnabled(false);
129 
130  // Set the icons for the buttons
131  mpBtnSelectObject->setIcon(CQIconResource::icon(CQIconResource::copasi));
132  mpBtnAddTarget->setIcon(CQIconResource::icon(CQIconResource::editAdd));
133  mpBtnDeleteTarget->setIcon(CQIconResource::icon(CQIconResource::editDelete));
134 }
135 
136 /*! Slot to add a new target without object target nor its expression
137  * Impact only on the appearance.
138  */
140 {
143 
144  const CCopasiObject * pObject =
146 
147  if (pObject == NULL) return;
148 
149  const CModelEntity * pME = dynamic_cast< const CModelEntity * >(pObject->getObjectParent());
150 
151  if (pME == NULL) return;
152 
153  mAssignments.add(new CEventAssignment(pME->getKey()), true);
154  mpLBTarget->addItem(FROM_UTF8(pME->getObjectDisplayName()));
155 
156  mpLBTarget->setCurrentRow((int)(mAssignments.size() - 1));
157 }
158 
159 /*! Slot to remove the active target from the appearance
160  * Impact only on the appearance.
161  */
163 {
164  if (mCurrentTarget > mAssignments.size() - 1 ||
165  mAssignments.size() == 0) return;
166 
167  size_t ToBeDeleted = mCurrentTarget;
168 
169  QListWidgetItem * pItem = mpLBTarget->takeItem((int) ToBeDeleted);
170  pdelete(pItem);
171 
172  mAssignments.remove(ToBeDeleted);
173 
174  mCurrentTarget = mpLBTarget->currentIndex().row();
175 }
176 
177 /*! Load all values with respect to a chosen saved event */
179 {
180  if (mpEvent == NULL) return false;
181 
182  const CModel * pModel =
183  dynamic_cast< const CModel * >(mpEvent->getObjectAncestor("Model"));
184 
185  if (pModel == NULL) return false;
186 
187  // *** Expression of Trigger
188  mpExpressionTrigger->mpExpressionWidget->setExpression(mpEvent->getTriggerExpression());
189  mpExpressionTrigger->updateWidget(); // bring into view mode
190 
191  // *** Expression of Delay
192  mpExpressionDelay->mpExpressionWidget->setExpression(mpEvent->getDelayExpression());
193  mpExpressionDelay->updateWidget(); // bring into view mode
194 
195  if (mpEvent->getDelayExpression() == "")
196  {
197  mpComboBoxDelay->setCurrentIndex(0); // Set Delay to "None"
198  }
199  else if (mpEvent->getDelayAssignment())
200  {
201  mpComboBoxDelay->setCurrentIndex(2); // Assignment only
202  }
203  else
204  {
205  mpComboBoxDelay->setCurrentIndex(1); // Calculation and Assignment
206  }
207 
208  // copy assignment from event
211 
213  QStringList Targets;
214 
215  int ijk = 0;
216 
217  for (; it != end; ++it, ijk++)
218  {
219  const CModelEntity * pEntity =
220  dynamic_cast< CModelEntity * >(CCopasiRootContainer::getKeyFactory()->get((*it)->getTargetKey()));
221 
222  if (pEntity != NULL)
223  {
224  Targets.append(FROM_UTF8(pEntity->getObjectDisplayName()));
225  mAssignments.add(**it);
226 
227 #ifdef XXXX // Add type dependent information
228 
229  if (sObjectName == "Compartment")
230  {
231  sName = FROM_UTF8(CCopasiRootContainer::getKeyFactory()->get(it->first)->getObjectDisplayName() + ".Volume");
232  }
233 
234  if (sObjectName == "Metabolite")
235  {
236  sName = FROM_UTF8("[" + CCopasiRootContainer::getKeyFactory()->get(it->first)->getObjectDisplayName() + "]");
237  }
238 
239  if (sObjectName.contains("ModelValue"))
240  {
241  sName = FROM_UTF8(CCopasiRootContainer::getKeyFactory()->get(it->first)->getObjectDisplayName());
242  }
243 
244 #endif // XXXX
245  }
246  }
247 
248  // fill the list box and the expression widget with correct assignments
249  mpLBTarget->clear();
250  mpLBTarget->insertItems(0, Targets);
251 
252  size_t NewTarget = mCurrentTarget;
253 
255  mAssignments.size() > 0)
256  {
257  NewTarget = 0;
258  }
259 
260  mpLBTarget->setCurrentRow((int) NewTarget);
261 
262  mChanged = false;
263 
264  return true;
265 }
266 
267 /*! The slot to save all current values of the active event widget */
269 {
270  if (mpEvent == NULL) return;
271 
272  const CModel * pModel =
273  dynamic_cast< const CModel * >(mpEvent->getObjectAncestor("Model"));
274 
275  if (pModel == NULL) return;
276 
277  if (mpEvent->getTriggerExpression() != mpExpressionTrigger->mpExpressionWidget->getExpression())
278  {
279  mpEvent->setTriggerExpression(mpExpressionTrigger->mpExpressionWidget->getExpression());
280  mChanged = true;
281  }
282 
283  switch (mpComboBoxDelay->currentIndex())
284  {
285  case 0:
287 
288  if (mpEvent->getDelayAssignment() != false)
289  {
290  mpEvent->setDelayAssignment(false);
291  mChanged = true;
292  }
293 
294  break;
295 
296  case 1:
297 
298  if (mpEvent->getDelayExpression() != mpExpressionDelay->mpExpressionWidget->getExpression())
299  {
300  mpEvent->setDelayExpression(mpExpressionDelay->mpExpressionWidget->getExpression());
301  mChanged = true;
302  }
303 
304  if (mpEvent->getDelayAssignment() != false)
305  {
306  mpEvent->setDelayAssignment(false);
307  mChanged = true;
308  }
309 
310  break;
311 
312  case 2:
313 
314  if (mpEvent->getDelayExpression() != mpExpressionDelay->mpExpressionWidget->getExpression())
315  {
316  mpEvent->setDelayExpression(mpExpressionDelay->mpExpressionWidget->getExpression());
317  mChanged = true;
318  }
319 
320  if (mpEvent->getDelayAssignment() != true)
321  {
323  mChanged = true;
324  }
325 
326  break;
327  }
328 
329  // Save the event assignments
330  // First we make sure that the current assignment is saved
332  {
333  mAssignments[mCurrentTarget]->setExpression(mpExpressionEA->mpExpressionWidget->getExpression());
334  }
335 
338 
340  size_t Found;
341 
342  // We first update all assignments.
343  for (; it != end; ++it)
344  {
345  Found = OldAssignments.getIndex((*it)->getTargetKey());
346 
347  if (Found == C_INVALID_INDEX)
348  {
349  OldAssignments.add(**it);
350  mChanged = true;
351  }
352  else if (OldAssignments[Found]->getExpression() != (*it)->getExpression())
353  {
354  OldAssignments[Found]->setExpression((*it)->getExpression());
355  mChanged = true;
356  }
357  }
358 
359  // Find the deleted assignments and mark them.
362 
363  size_t DeleteCount = 0;
364 
365  if (OldAssignments.size() > mAssignments.size())
366  {
367  DeleteCount = OldAssignments.size() - mAssignments.size();
368  }
369 
370  std::vector< std::string > ToBeDeleted;
371 
372  for (; itOld != endOld && DeleteCount > 0; ++itOld)
373  {
374  const std::string & key = (*itOld)->getTargetKey();
375 
376  for (it = mAssignments.begin(); it != end; ++it)
377  {
378  if (key == (*it)->getTargetKey()) break;
379  }
380 
381  if (it == end)
382  {
383  ToBeDeleted.push_back(key);
384  DeleteCount--;
385  mChanged = true;
386  }
387  }
388 
389  // Delete the assignments marked to be deleted.
390  std::vector< std::string >::const_iterator itDelete = ToBeDeleted.begin();
391  std::vector< std::string >::const_iterator endDelete = ToBeDeleted.end();
392 
393  for (; itDelete != endDelete; ++itDelete)
394  {
395  OldAssignments.remove(*itDelete);
396  }
397 
398  if (mChanged)
399  {
400  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
401  (*CCopasiRootContainer::getDatamodelList())[0]->changed();
403  }
404 
405  mChanged = false;
406 }
407 
408 /*! The slot to update the active event widget */
409 bool CQEventWidget1::update(ListViews::ObjectType /* objectType */, ListViews::Action /* action */, const std::string & /* key */)
410 {
411  return true;
412 }
413 
414 /*! Function to interact with an object of class CEvent */
416 {
417  bool success = true;
418 
419  if (mKeyToCopy != "")
420  {
422  mKeyToCopy = "";
423  }
424  else
425  {
426  mpEvent = dynamic_cast<CEvent*>(mpObject);
427  }
428 
430 
431  if (mpEvent)
432  {
433  success = loadFromEvent();
434  }
435 
436  mpEvent = dynamic_cast<CEvent*>(mpObject);
437 
438  if (!success)
439  {
440  mpListView->switchToOtherWidget(114, ""); //TODO
441  }
442 
443  return success;
444 }
445 
446 /*! The slot to be done before leaving the active event widget */
448 {
449  saveToEvent();
450 
451  return true;
452 }
453 
454 /// Slot to select an object from the existing ones -only- for target.
456 {
458  return slotAddTarget();
459 
462 
463  const CCopasiObject * pObject =
465 
466  if (pObject == NULL) return;
467 
468  const CModelEntity * pME = dynamic_cast< const CModelEntity * >(pObject->getObjectParent());
469 
470  if (pME == NULL) return;
471 
472  if (mAssignments[mCurrentTarget]->setTargetKey(pME->getKey()))
473  {
474  // If the target key change was successful we need to update the label.
475  mpLBTarget->item((int) mCurrentTarget)->setText(FROM_UTF8(pME->getObjectDisplayName()));
476  }
477 }
478 
479 /// Slot to actualize the assignment expression widget of event assignment according to the target
481 {
482  size_t NewTarget = (size_t) index;
483 
484  if (NewTarget != C_INVALID_INDEX &&
485  NewTarget >= mAssignments.size())
486  {
487  NewTarget = mAssignments.size() - 1;
488  }
489 
490  // Save the current assignment
491  if (NewTarget != mCurrentTarget &&
493  {
494  mAssignments[mCurrentTarget]->setExpression(mpExpressionEA->mpExpressionWidget->getExpression());
495  }
496 
497  mCurrentTarget = NewTarget;
498 
499  if (mCurrentTarget == C_INVALID_INDEX) // no target anymore
500  {
501  // hide the label and widget with respect to expression
502  mpLabelEA->setEnabled(false);
503  mpExpressionEA->setEnabled(false);
504 
505  return;
506  }
507  else
508  {
509  // show the label and widget with respect to expression
510  mpLabelEA->setEnabled(true);
511  mpExpressionEA->setEnabled(true);
512 
513  mpExpressionEA->mpExpressionWidget->setExpression(mAssignments[mCurrentTarget]->getExpression());
514  mpExpressionEA->updateWidget();
515  }
516 }
517 
518 /*! Function to apply the Delay Expression Widget */
520 {
521  if (show)
522  {
523  mpLabelDelayExpression->show();
524  mpExpressionDelay->show();
525  }
526  else
527  {
528  mpLabelDelayExpression->hide();
529  mpExpressionDelay->hide();
530  }
531 }
532 
533 //Slot to handle delay options
535 {
536  if (choice != 0) // Not "None"
537  {
538  showDelayExpression(true);
539  }
540  else
541  {
542  showDelayExpression(false);
543  }
544 }
Definition: CEvent.h:152
CCopasiContainer * getObjectAncestor(const std::string &type) const
const CCopasiVectorN< CEventAssignment > & getAssignments() const
Definition: CEvent.cpp:678
void slotBtnNew()
Slot to create a new event; activated whenever the New button is clicked.
bool mExpressionDelayValid
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
void slotSelectObject()
Slot to select an object from the existing ones -only- for target.
const bool & getDelayAssignment() const
Definition: CEvent.cpp:419
#define pdelete(p)
Definition: copasi.h:215
virtual bool leave()
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
void switchToOtherWidget(const size_t &id, const std::string &key)
Definition: listviews.cpp:926
virtual bool enterProtected()
virtual size_t size() const
std::string mKeyToCopy
CCopasiObject * get(const std::string &key)
bool mExpressionTriggerValid
iterator begin()
void slotChooseDelay(int choice)
CCopasiObject * mpObject
Definition: copasiWidget.h:64
#define C_INVALID_INDEX
Definition: copasi.h:222
std::string getTriggerExpression() const
Definition: CEvent.cpp:524
CQEventWidget1(QWidget *parent=0, const char *name=0)
virtual size_t getIndex(const std::string &name) const
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
CEvent * mpEvent
size_t mCurrentTarget
iterator end()
void slotActualizeAssignmentExpression(int index)
Slot to actualize the assignment expression widget of event assignment according to the target...
virtual bool add(const CType &src)
ListViews * mpListView
Definition: copasiWidget.h:62
virtual const std::string & getKey() const
bool setTriggerExpression(const std::string &expression)
Definition: CEvent.cpp:474
virtual bool add(const CType &src)
static const QIcon & icon(const IconID &id)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
void setDelayAssignment(const bool &delayCalculation)
Definition: CEvent.cpp:408
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
std::string getDelayExpression() const
Definition: CEvent.cpp:591
void showDelayExpression(bool display)
virtual void remove(const std::string &name)
virtual void remove(const size_t &index)
static CKeyFactory * getKeyFactory()
bool setDelayExpression(const std::string &expression)
Definition: CEvent.cpp:544
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
virtual void clear()
Definition: CModel.h:50
Header file of class CEvent.
bool setExpression(const std::string &expression)
std::string mKey
Definition: copasiWidget.h:63
CCopasiVector< CEventAssignment > mAssignments
static const CCopasiObject * getObjectSingle(QWidget *pParent, const CQSimpleSelectionTree::ObjectClasses &classes, const CCopasiObject *pCurrentObject=NULL)
CCopasiContainer * getObjectParent() const
bool removeEvent(const size_t index, const bool &recursive=true)
Definition: CModel.cpp:2945