COPASI API  4.16.103
CQModelValue.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 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) 2006 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include "CQModelValue.h"
16 
17 #include "CQExpressionWidget.h"
18 
19 #include "UI/CQMessageBox.h"
20 #include "UI/qtUtilities.h"
21 
22 #include "model/CModel.h"
23 #include "model/CModelValue.h"
24 #include "function/CExpression.h"
25 #include "report/CKeyFactory.h"
27 
28 /*
29  * Constructs a CQModelValue which is a child of 'parent', with the
30  * name 'name'.'
31  */
32 CQModelValue::CQModelValue(QWidget* parent, const char* name)
33  : CopasiWidget(parent, name),
34  mKeyToCopy("")
35 {
36  setupUi(this);
37 
38  init();
39 }
40 
41 /*
42  * Destroys the object and frees any allocated resources
43  */
45 {
46  destroy();
47  // no need to delete child widgets, Qt does it all for us
48 }
49 
50 /// Slot to create a new quantity; activated whenever the New button is clicked
52 {
53  // save the current setting values
54  leave();
55 
56  // standard name
57  std::string name = "quantity_1";
58 
59  // if the standard name already exists then creating the new event will fail
60  // thus, a growing index will automatically be added to the standard name
61  int i = 1;
62  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
63 
64  while (!(mpModelValue = (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->createModelValue(name)))
65  {
66  i++;
67  name = "quantity_";
68  name += TO_UTF8(QString::number(i));
69  }
70 
71  std::string key = mpModelValue->getKey();
74 }
75 
77 {
78  mKeyToCopy = mKey;
79 }
80 
82 {
83  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
85  assert(pDataModel != NULL);
86  CModel * pModel = pDataModel->getModel();
87 
88  if (pModel == NULL)
89  return;
90 
91  if (mpModelValue == NULL)
92  return;
93 
94  QMessageBox::StandardButton choice =
95  CQMessageBox::confirmDelete(this, "quantity",
98 
99  switch (choice)
100  {
101  case QMessageBox::Ok:
102  {
103  pDataModel->getModel()->removeModelValue(mKey);
104  mpModelValue = NULL;
105 
106 #undef DELETE
108  protectedNotify(ListViews::MODELVALUE, ListViews::DELETE, "");//Refresh all as there may be dependencies.
109  break;
110  }
111 
112  default:
113  break;
114  }
115 }
116 
117 /*!
118  If the simulation type is changed then COPASI will automatically adjust its appearance,
119  especially correlating to the Expression Widget and its buttons.
120  */
122 {
123  switch ((CModelEntity::Status) mItemToType[type])
124  {
125  case CModelEntity::FIXED:
126  // hide label, widget, and all buttons
127  mpLblExpression->hide();
128  mpExpressionEMW->hide();
129 
130  // enable the option of use Initial Expression
131  mpBoxUseInitialExpression->setEnabled(true);
132  slotInitialTypeChanged(mpBoxUseInitialExpression->isChecked());
133 
134  // we don't need to update the expression widget as it is already hidden
135 
136  break;
137 
139  // show label, widget, and correct buttons
140  mpLblExpression->show(); // show the label
141  mpExpressionEMW->show(); // show the widget
142 
143  // disable the option of use Initial Expression
144  mpBoxUseInitialExpression->setEnabled(false);
145  slotInitialTypeChanged(false);
146 
147  // update the expression widget
148  mpExpressionEMW->updateWidget();
149 
150  break;
151 
152  case CModelEntity::ODE:
153  // show label, widget, and correct buttons
154  mpLblExpression->show(); // show the label
155  mpExpressionEMW->show(); // show the widget
156 
157  // enable the option of use Initial Expression
158  mpBoxUseInitialExpression->setEnabled(true);
159  slotInitialTypeChanged(mpBoxUseInitialExpression->isChecked());
160 
161  // update the expression widget
162  mpExpressionEMW->updateWidget();
163 
164  break;
165 
166  default:
167  break;
168  }
169 }
170 
171 /*!
172  This function is used in case of not FIXED type
173  */
175 {
176  mExpressionValid = valid;
177 }
178 
180 {
181  mInitialExpressionValid = valid;
182 }
183 
185 {
186  mpComboBoxType->insertItem(mpComboBoxType->count(), FROM_UTF8(CModelEntity::StatusName[CModelEntity::FIXED]));
187  mpComboBoxType->insertItem(mpComboBoxType->count(), FROM_UTF8(CModelEntity::StatusName[CModelEntity::ASSIGNMENT]));
188  mpComboBoxType->insertItem(mpComboBoxType->count(), FROM_UTF8(CModelEntity::StatusName[CModelEntity::ODE]));
189 
190  mItemToType.push_back(CModelEntity::FIXED);
191  mItemToType.push_back(CModelEntity::ASSIGNMENT);
192  mItemToType.push_back(CModelEntity::ODE);
193 
194  mExpressionValid = false;
195  mpExpressionEMW->mpExpressionWidget->setExpressionType(CQExpressionWidget::TransientExpression);
196 
197  mInitialExpressionValid = false;
198  mpInitialExpressionEMW->mpExpressionWidget->setExpressionType(CQExpressionWidget::InitialExpression);
199 }
200 
202 {}
203 
205  ListViews::Action action,
206  const std::string & key)
207 {
208  switch (objectType)
209  {
210  case ListViews::MODEL:
211 
212  // For a new model we need to remove references to no longer existing metabolites
213  if (action == ListViews::ADD)
214  {
215  mKey = "";
216  mpObject = NULL;
217  mpModelValue = NULL;
218  }
219 
220  break;
221 
223 
224  // If the currently displayed metabolite is deleted we need to remove its references.
225  if (action == ListViews::DELETE && mKey == key)
226  {
227  mKey = "";
228  mpObject = NULL;
229  mpModelValue = NULL;
230  }
231 
232  break;
233 
234  case ListViews::STATE:
235  break;
236 
237  default:
238  return true;
239  break;
240  }
241 
242  if (isVisible() && !mIgnoreUpdates)
243  load();
244 
245  return true;
246 }
247 
249 {
250  if ((CModelEntity::Status) mItemToType[mpComboBoxType->currentIndex()] != CModelEntity::FIXED)
251  {
252  // -- Expression --
253  mpExpressionEMW->updateWidget();
254  }
255 
256  if (mpBoxUseInitialExpression->isChecked())
257  {
258  // -- Initial Expression --
259  mpInitialExpressionEMW->updateWidget();
260  }
261 
262  save();
263 
264  return true;
265 }
266 
268 {
269  mpModelValue = NULL;
270 
271  if (mKeyToCopy != "")
272  {
274  mKeyToCopy = "";
275  }
276  else
277  {
278  mpModelValue = dynamic_cast<CModelValue *>(mpObject);
279  }
280 
281  if (!mpModelValue)
282  {
284  return false;
285  }
286 
287  load();
288 
289  mpModelValue = dynamic_cast<CModelValue *>(mpObject);
290 
291  return true;
292 }
293 
294 /*!
295  Load any saved values to the screen
296  */
298 {
299  if (mpModelValue == NULL) return;
300 
301  // Type
302  mpComboBoxType->setCurrentIndex(mpComboBoxType->findText(FROM_UTF8(CModelEntity::StatusName[mpModelValue->getStatus()])));
303 
304  // Initial Value
305  mpEditInitialValue->setText(QString::number(mpModelValue->getInitialValue(), 'g', 10));
306 
307  // Current Value
308  mpEditCurrentValue->setText(QString::number(mpModelValue->getValue(), 'g', 10));
309 
310  // Rate
311  mpEditRate->setText(QString::number(mpModelValue->getRate(), 'g', 10));
312 
313  // Expression
314  mpExpressionEMW->mpExpressionWidget->setExpression(mpModelValue->getExpression());
315 
316  // Update Expression Widget
317  mpExpressionEMW->updateWidget();
318 
319  // Initial Expression
320  mpInitialExpressionEMW->mpExpressionWidget->setExpression(mpModelValue->getInitialExpression());
321 
322  // Update Initial Expression Widget
323  mpInitialExpressionEMW->updateWidget();
324 
325  // Type dependent display of values
326  slotTypeChanged(mpComboBoxType->currentIndex());
327 
328  // Use Initial Expression
331  {
332  mpBoxUseInitialExpression->setChecked(false);
333  // slotInitialTypeChanged(false);
334  }
335  else
336  {
337  mpBoxUseInitialExpression->setChecked(true);
338  // slotInitialTypeChanged(true);
339  }
340 
341  mChanged = false;
342 }
343 
344 /*!
345  Save any inputs and set them as initial setting values
346  */
348 {
349  if (mpModelValue == NULL) return;
350 
351  // set status
352  if (mpModelValue->getStatus() != (CModelEntity::Status) mItemToType[mpComboBoxType->currentIndex()])
353  {
354  mpModelValue->setStatus((CModelEntity::Status) mItemToType[mpComboBoxType->currentIndex()]);
355  mChanged = true;
356  }
357 
358  // set initial value
359  if (QString::number(mpModelValue->getInitialValue(), 'g', 10) != mpEditInitialValue->text() &&
361  {
362  mpModelValue->setInitialValue(mpEditInitialValue->text().toDouble());
363  mChanged = true;
364  }
365 
366  // set expression
367  if (mpModelValue->getExpression() != mpExpressionEMW->mpExpressionWidget->getExpression())
368  {
369  // mpModelValue->setExpression(((CQExpressionWidget *)mpEditExpression->widget(0))->getExpression());
370  mpModelValue->setExpression(mpExpressionEMW->mpExpressionWidget->getExpression());
371  mChanged = true;
372  }
373 
374  // set initial expression
375  if ((CModelEntity::Status) mItemToType[mpComboBoxType->currentIndex()] != CModelEntity::ASSIGNMENT)
376  {
377  if (mpBoxUseInitialExpression->isChecked() &&
378  mpModelValue->getInitialExpression() != mpInitialExpressionEMW->mpExpressionWidget->getExpression())
379  {
380  mpModelValue->setInitialExpression(mpInitialExpressionEMW->mpExpressionWidget->getExpression());
381  mChanged = true;
382  }
383  else if (!mpBoxUseInitialExpression->isChecked() &&
385  {
387  mChanged = true;
388  }
389  }
390 
391  if (mChanged)
392  {
393  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
394  (*CCopasiRootContainer::getDatamodelList())[0]->changed();
396  }
397 
398  mChanged = false;
399 }
400 
401 /*!
402  If the initial expression is chosen to be used by checking the mpBoxUseInitialExpression check box being represented by
403  the boolean parameter useInitialAssignment (true if checked; false otherwise), COPASI will show the Initial Expression
404  Widget with its correct push buttons. Otherwise, the widget and its buttons will be hidden.
405  */
406 void CQModelValue::slotInitialTypeChanged(bool useInitialAssignment)
407 {
408  if (useInitialAssignment) // use Initial Expression (ie. the mpBoxUseInitialExpression is checked)
409  {
410  // show label, widget, and the correct buttons
411  mpLblInitialExpression->show(); // show the label
412  mpInitialExpressionEMW->show(); // show the widget
413 
414  // enable the option of use Initial Value
415  mpEditInitialValue->setEnabled(false);
416 
417  // update the Initial Expression Widget
418  mpInitialExpressionEMW->updateWidget();
419  }
420  else // mpBoxUseInitialExpression is not checked
421  {
422  // hide label, widget, and all buttons
423  mpLblInitialExpression->hide();
424  mpInitialExpressionEMW->hide();
425 
426  // enable the option of use Initial Value
427  mpEditInitialValue->setEnabled((CModelEntity::Status) mItemToType[mpComboBoxType->currentIndex()] != CModelEntity::ASSIGNMENT);
428 
429  // we don't need to update the Initial Expression Widget
430  }
431 }
Header file of class CExpression.
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
Header file of class CModelEntity and CModelValue.
objectType
void slotBtnDelete()
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
std::vector< int > mItemToType
Definition: CQModelValue.h:45
static const std::string StatusName[]
Definition: CModelValue.h:67
void switchToOtherWidget(const size_t &id, const std::string &key)
Definition: listviews.cpp:926
const C_FLOAT64 & getRate() const
const std::string & getObjectName() const
void slotBtnCopy()
CCopasiObject * get(const std::string &key)
CCopasiObject * mpObject
Definition: copasiWidget.h:64
bool setInitialExpression(const std::string &expression)
#define C_INVALID_INDEX
Definition: copasi.h:222
std::string getExpression() const
void slotInitialExpressionValid(bool valid)
std::string mKeyToCopy
Definition: CQModelValue.h:55
void slotTypeChanged(int type)
bool mIgnoreUpdates
Definition: copasiWidget.h:67
void slotBtnNew()
Slot to create a new quantity; activated whenever the New button is clicked.
bool removeModelValue(const CModelValue *pModelValue, const bool &recursive=true)
Definition: CModel.cpp:2903
const C_FLOAT64 & getInitialValue() const
CModelValue * mpModelValue
Definition: CQModelValue.h:46
void slotInitialTypeChanged(bool useInitialAssignment)
ListViews * mpListView
Definition: copasiWidget.h:62
virtual const std::string & getKey() const
virtual void setStatus(const CModelEntity::Status &status)
static StandardButton confirmDelete(QWidget *parent, const QString &objectType, const QString &objects, const std::set< const CCopasiObject * > &deletedObjects)
std::string getInitialExpression() const
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
CQModelValue(QWidget *parent=0, const char *name=0)
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
virtual std::set< const CCopasiObject * > getDeletedObjects() const
const C_FLOAT64 & getValue() const
static CKeyFactory * getKeyFactory()
virtual bool leave()
bool mInitialExpressionValid
Definition: CQModelValue.h:49
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
virtual void setInitialValue(const C_FLOAT64 &initialValue)
virtual bool enterProtected()
void slotExpressionValid(bool valid)
Definition: CModel.h:50
const CModelEntity::Status & getStatus() const
bool setExpression(const std::string &expression)
std::string mKey
Definition: copasiWidget.h:63
bool mExpressionValid
Definition: CQModelValue.h:48