COPASI API  4.16.103
CQMoietiesTaskResult.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 #include "CQMoietiesTaskResult.h"
12 
13 #include "copasi.h"
14 
16 #include "CQPushButtonDelegate.h"
17 #include "CopasiFileDialog.h"
18 #include "CQMessageBox.h"
19 #include "qtUtilities.h"
20 
21 #include "function/CExpression.h"
22 #include "model/CModel.h"
23 #include "moieties/CMoietiesTask.h"
24 #include "report/CKeyFactory.h"
25 #include "utilities/utility.h"
28 
29 #define COL_SPECIES 0
30 #define COL_NUMBER 1
31 #define COL_AMOUNT 2
32 #define COL_BTN 3
33 #define COL_EQUATION 4
34 
35 /*
36  * Constructs a CQMoietiesTaskResult which is a child of 'parent', with the
37  * name 'name'.'
38  */
39 CQMoietiesTaskResult::CQMoietiesTaskResult(QWidget* parent, const char* name)
40  : CopasiWidget(parent, name)
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 
56 {
57  mpMoietiesTask = NULL;
58 
59  QImage ToolBtn;
60 
61  // Initialize the moieties tab
62  mpMoieties->setColumnCount(5);
63 
64  QTableWidgetItem * pItem = new QTableWidgetItem("Dependent Species");
65  mpMoieties->setHorizontalHeaderItem(COL_SPECIES, pItem);
66  pItem = new QTableWidgetItem("Total Particle Number");
67  mpMoieties->setHorizontalHeaderItem(COL_NUMBER, pItem);
68  pItem = new QTableWidgetItem("Total Amount");
69  mpMoieties->setHorizontalHeaderItem(COL_AMOUNT, pItem);
70  pItem = new QTableWidgetItem("");
71  mpMoieties->setHorizontalHeaderItem(COL_BTN, pItem);
72  pItem = new QTableWidgetItem("Expression");
73  mpMoieties->setHorizontalHeaderItem(COL_EQUATION, pItem);
74 
76  mpMoieties->setItemDelegateForColumn(COL_BTN, pDelegate);
77 
78  connect(pDelegate, SIGNAL(clicked(const QModelIndex &)), this, SLOT(slotCreateGlobalQuantity(const QModelIndex &)));
79 
80  // Initialize the stoichiometry tab
81  mpStoichiometry->setLegendEnabled(true);
82 
83  // Initialize the stoichiometry tab
84  mpLinkMatrix->setLegendEnabled(true);
85 
86  // Initialize the stoichiometry tab
87  mpReducedStoichiometry->setLegendEnabled(true);
88 }
89 
91  ListViews::Action action,
92  const std::string & /* key */)
93 {
94  // :TODO:
95  switch (objectType)
96  {
97  case ListViews::MODEL:
98 
99  switch (action)
100  {
101  case ListViews::ADD:
102  clear();
103  break;
104 
105  case ListViews::CHANGE:
106  break;
107 
108  default:
109  break;
110  }
111 
112  break;
113 
114  default:
115  break;
116  }
117 
118  return true;
119 }
120 
122 {
123  mpMoieties->setRowCount(0);
124  mpStoichiometry->setArrayAnnotation(NULL);
125  mpLinkMatrix->setArrayAnnotation(NULL);
126  mpReducedStoichiometry->setArrayAnnotation(NULL);
127 }
128 
130 {return true;}
131 
133 {
134  mpMoietiesTask = dynamic_cast< CMoietiesTask * >(mpObject);
135 
136  load();
137 
138  return true;
139 }
140 
142 {
143  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
145  assert(pDataModel != NULL);
146  CModel * pModel = pDataModel->getModel();
147 
148  if (pModel == NULL) return;
149 
150  // Set the units for the amount column
151  QString AmountUnits = FROM_UTF8(pModel->getQuantityUnitsDisplayString());
152 
153  if (!AmountUnits.isEmpty())
154  AmountUnits = "\n(" + AmountUnits + ")";
155 
156  mpMoieties->horizontalHeaderItem(COL_AMOUNT)->setText("Total Amount" + AmountUnits);
157 
158  // Fill the moieties table
161  mpMoieties->setRowCount(end - it);
162 
163  QTableWidgetItem * pItem;
164  int i = 0;
165 
166  for (; it != end; ++it, i++)
167  {
168  pItem = new QTableWidgetItem(FROM_UTF8((*it)->getObjectName()));
169  mpMoieties->setItem(i, COL_SPECIES, pItem);;
170 
171  pItem = new QTableWidgetItem(QString::number((*it)->getNumber()));
172  mpMoieties->setItem(i, COL_NUMBER, pItem);
173 
174  (*it)->refreshAmount();
175  pItem = new QTableWidgetItem(QString::number((*it)->getAmount()));
176  mpMoieties->setItem(i, COL_AMOUNT, pItem);
177 
178  pItem = new QTableWidgetItem("");
179  mpMoieties->setItem(i, COL_BTN, pItem);
180 
181  // Show the Button
182  pItem = mpMoieties->item(i, COL_BTN);
183  pItem->setFlags(pItem->flags() | Qt::ItemIsEditable | Qt::ItemIsEnabled);
184  mpMoieties->openPersistentEditor(pItem);
185 
186  pItem = new QTableWidgetItem(FROM_UTF8((*it)->getDescription(pModel)));
187  mpMoieties->setItem(i, COL_EQUATION, pItem);
188  }
189 
190  mpTabWidget->setTabText(mpTabWidget->indexOf(mpMoieties), "Moieties (" + QString::number(i) + ")");
191 
192  mpMoieties->resizeColumnsToContents();
193  mpMoieties->resizeRowsToContents();
194 
195  // Fill the stoichiometry matrix
196  CColorScaleBiLog * tcs = NULL;
197  const CArrayAnnotation * pAnnotation =
198  static_cast< const CArrayAnnotation * >(pModel->getObject(CCopasiObjectName("Array=Stoichiometry(ann)")));
199  tcs = new CColorScaleBiLog();
200  mpStoichiometry->setColorCoding(tcs);
201  mpStoichiometry->setColorScalingAutomatic(true);
202  mpStoichiometry->setArrayAnnotation(pAnnotation);
203 
204  // Fill the link matrix
205  pAnnotation =
206  static_cast< const CArrayAnnotation * >(pModel->getObject(CCopasiObjectName("Array=Link matrix(ann)")));
207  tcs = new CColorScaleBiLog();
208  mpLinkMatrix->setColorCoding(tcs);
209  mpLinkMatrix->setColorScalingAutomatic(true);
210  mpLinkMatrix->setArrayAnnotation(pAnnotation);
211 
212  // Fill the reduced stoichiometry matrix
213  pAnnotation =
214  static_cast< const CArrayAnnotation * >(pModel->getObject(CCopasiObjectName("Array=Reduced stoichiometry(ann)")));
215  tcs = new CColorScaleBiLog();
216  mpReducedStoichiometry->setColorCoding(tcs);
217  mpReducedStoichiometry->setColorScalingAutomatic(true);
218  mpReducedStoichiometry->setArrayAnnotation(pAnnotation);
219 
220  return;
221 }
222 
224 {
225  C_INT32 Answer = QMessageBox::No;
226  QString fileName;
227 
228  while (Answer == QMessageBox::No)
229  {
230  fileName =
231  CopasiFileDialog::getSaveFileName(this, "Save File Dialog",
232  "untitled.txt", "TEXT Files (*.txt)", "Save to");
233 
234  if (fileName.isEmpty()) return;
235 
236  // Checks whether the file exists
237  Answer = checkSelection(fileName);
238 
239  if (Answer == QMessageBox::Cancel) return;
240  }
241 
242  std::ofstream file(CLocaleString::fromUtf8(TO_UTF8(fileName)).c_str());
243 
244  if (file.fail())
245  return;
246 
247  if (mpMoietiesTask != NULL)
248  file << mpMoietiesTask->getResult();
249 
250  return;
251 }
252 
253 void CQMoietiesTaskResult::slotCreateGlobalQuantity(const QModelIndex & index)
254 {
255  int row = index.row();
256 
257  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
258  CModel * pModel = (*CCopasiRootContainer::getDatamodelList())[0]->getModel();
259 
260  if (pModel == NULL) return;
261 
262  CCopasiVector< CMoiety > Moieties = pModel->getMoieties();
263 
264  if (row >= (C_INT32) Moieties.size()) return;
265 
266  const CMoiety * pMoiety = Moieties[row];
267 
268  CModelValue * pMV = pModel->createModelValue("Moiety[" + pMoiety->getObjectName() + "].TotalAmount");
269 
270  int i = 0;
271 
272  while (pMV == NULL)
273  pMV = pModel->createModelValue("Moiety[" + pMoiety->getObjectName() + "].TotalAmount_" + TO_UTF8(QString::number(++i)));
274 
275  pMV->setInitialExpression(pMoiety->getExpression());
277 }
278 
279 // virtual
281 {
282  CopasiWidget::setFramework(framework);
283 
284  switch (mFramework)
285  {
286  case 0: // Concentration
287  mpMoieties->showColumn(COL_AMOUNT);
288  mpMoieties->hideColumn(COL_NUMBER);
289  break;
290 
291  case 1: // Particle Number
292  mpMoieties->hideColumn(COL_AMOUNT);
293  mpMoieties->showColumn(COL_NUMBER);
294  break;
295  }
296 }
Header file of class CExpression.
objectType
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
const std::string & getObjectName() const
virtual size_t size() const
iterator begin()
#define COL_EQUATION
CCopasiObject * mpObject
Definition: copasiWidget.h:64
bool setInitialExpression(const std::string &expression)
CModelValue * createModelValue(const std::string &name, const C_FLOAT64 &value=0.0)
Definition: CModel.cpp:2838
#define C_INT32
Definition: copasi.h:90
CQMoietiesTaskResult(QWidget *parent=0, const char *name=0)
const CCopasiVector< CMoiety > & getMoieties() const
Definition: CModel.cpp:1163
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
iterator end()
virtual void setFramework(int framework)
#define COL_NUMBER
std::string getExpression() const
Definition: CMoiety.cpp:228
C_INT32 checkSelection(const QString &file)
static const QIcon & icon(const IconID &id)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
std::string getQuantityUnitsDisplayString() const
Definition: CModel.cpp:4647
const CResult & getResult() const
#define COL_AMOUNT
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
CMoietiesTask * mpMoietiesTask
#define COL_SPECIES
Definition: CModel.h:50
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
static CLocaleString fromUtf8(const std::string &utf8)
#define COL_BTN
virtual void setFramework(int framework)
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)
void slotCreateGlobalQuantity(const QModelIndex &index)