COPASI API  4.16.103
CQParameterOverviewDM.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 /*
7  * CQParameterOverviewDM.cpp
8  *
9  * Created on: Jan 9, 2012
10  * Author: shoops
11  */
12 
13 #include "CQParameterOverviewDM.h"
14 #include "qtUtilities.h"
15 
17 #include "function/CExpression.h"
19 
20 #define COL_NAME 0
21 #define COL_DIFF 1
22 #define COL_TYPE 2
23 #define COL_VALUE 3
24 #define COL_UNIT 4
25 #define COL_ASSIGNMENT 5
26 
27 #define COLUMN_COUNT 6
28 
30  QAbstractItemModel(pParent),
31  mpModelParameterSet(NULL),
32  mFramework(0)
33 {
34 }
35 
36 // virtual
38 {
39 }
40 
41 // virtual
42 int CQParameterOverviewDM::columnCount(const QModelIndex & /* parent */) const
43 {
44  return COLUMN_COUNT;
45 }
46 
47 // virtual
48 QVariant CQParameterOverviewDM::data(const QModelIndex & index, int role) const
49 {
50  CModelParameter * pNode = nodeFromIndex(index);
51 
52  if (pNode == NULL) return QVariant();
53 
54  switch (index.column())
55  {
56  case COL_NAME:
57  return nameData(pNode, role);
58  break;
59 
60  case COL_DIFF:
61  return diffData(pNode, role);
62  break;
63 
64  case COL_TYPE:
65  return typeData(pNode, role);
66  break;
67 
68  case COL_VALUE:
69  return valueData(pNode, role);
70  break;
71 
72  case COL_UNIT:
73  return unitData(pNode, role);
74  break;
75 
76  case COL_ASSIGNMENT:
77  return assignmentData(pNode, role);
78  break;
79  }
80 
81  return QVariant();
82 }
83 
84 // virtual
85 Qt::ItemFlags CQParameterOverviewDM::flags(const QModelIndex &index) const
86 {
87  CModelParameter * pNode = nodeFromIndex(index);
88 
89  if (pNode == NULL)
90  {
91  return Qt::ItemIsEnabled;
92  }
93 
94  if (index.column() == COL_VALUE &&
95  !pNode->isReadOnly())
96  {
97  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled;
98  }
99 
100  if (index.column() == COL_DIFF)
101  {
103  pNode->getType() != CModelParameter::Group &&
104  pNode->getType() != CModelParameter::Set)
105  {
106  emit signalOpenEditor(index);
107 
108  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled;
109  }
110 
111  emit signalCloseEditor(index);
112  }
113 
114  if (index.column() == COL_ASSIGNMENT)
115  {
117  {
118  emit signalOpenEditor(index);
119 
120  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled;
121  }
122 
123  emit signalCloseEditor(index);
124  }
125 
126  return QAbstractItemModel::flags(index) & ~Qt::ItemIsEditable;
127 }
128 
129 // virtual
130 QVariant CQParameterOverviewDM::headerData(int section, Qt::Orientation /* orientation */, int role) const
131 {
132  if (role != Qt::DisplayRole)
133  return QVariant();
134 
135  switch (section)
136  {
137  case COL_NAME:
138  return QVariant("Name");
139  break;
140 
141  case COL_DIFF:
142  return QVariant("");
143  break;
144 
145  case COL_TYPE:
146  return QVariant("Type");
147  break;
148 
149  case COL_VALUE:
150  return QVariant("Value");
151  break;
152 
153  case COL_UNIT:
154  return QVariant("Unit");
155  break;
156 
157  case COL_ASSIGNMENT:
158  return QVariant("Expression");
159  break;
160  }
161 
162  return QVariant();
163 }
164 
165 // virtual
166 QModelIndex CQParameterOverviewDM::index(int row, int column, const QModelIndex & parent) const
167 {
168  CModelParameterGroup * pParent = static_cast< CModelParameterGroup *>(nodeFromIndex(parent));
169 
170  if (pParent == NULL)
171  {
172  return createIndex(row, column, *(mpModelParameterSet->begin() + row));
173  }
174 
175  if (row < (int) pParent->size())
176  return createIndex(row, column, *(pParent->begin() + row));
177  else
178  return QModelIndex();
179 }
180 
181 // virtual
182 QModelIndex CQParameterOverviewDM::parent(const QModelIndex & index) const
183 {
184  CModelParameter * pNode = nodeFromIndex(index);
185 
186  if (pNode == NULL || pNode == mpModelParameterSet)
187  {
188  return QModelIndex();
189  }
190 
191  if (pNode->getParent() == mpModelParameterSet)
192  {
193  return QModelIndex();
194  }
195 
196  CModelParameter * pParent = static_cast< CModelParameter * >(pNode->getParent());
197 
198  return createIndex(getRow(pParent), 0, pParent);
199 }
200 
201 // virtual
202 int CQParameterOverviewDM::rowCount(const QModelIndex & parent) const
203 {
204  if (!parent.isValid())
205  {
206  if (mpModelParameterSet != NULL)
207  return 5;
208  else
209  return 0;
210  }
211 
212  CModelParameter * pParent = nodeFromIndex(parent);
213 
214  switch (pParent->getType())
215  {
219  return (int) static_cast< CModelParameterGroup * >(pParent) ->size();
220  break;
221 
222  default:
223  break;
224  }
225 
226  return 0;
227 }
228 
229 // virtual
230 bool CQParameterOverviewDM::setData(const QModelIndex &_index, const QVariant &value, int role)
231 {
232  CModelParameter * pNode = nodeFromIndex(_index);
233  bool success = false;
234 
235  if (pNode != NULL &&
236  role == Qt::EditRole)
237  {
238  switch (_index.column())
239  {
240  case COL_VALUE:
241  pNode->setValue(value.toDouble(), static_cast< CModelParameter::Framework >(mFramework));
242  success = true;
243  break;
244 
245  case COL_ASSIGNMENT:
246  {
247  CModelParameter * pGlobalQuantity = pNode->getSet()->getModelParameter(TO_UTF8(value.toString()), CModelParameter::ModelValue);
248 
249  if (pGlobalQuantity != NULL)
250  {
251  static_cast< CModelParameterReactionParameter * >(pNode)->setGlobalQuantityCN(pGlobalQuantity->getCN());
252  }
253  else
254  {
255  static_cast< CModelParameterReactionParameter * >(pNode)->setGlobalQuantityCN("");
256  }
257  }
258 
259  success = true;
260  break;
261  }
262  }
263 
264  return success;
265 }
266 
268 {
269  if (mpModelParameterSet != pModelParameterSet)
270  {
271  beginResetModel();
272  mpModelParameterSet = pModelParameterSet;
273  endResetModel();
274  }
275 }
276 
277 void CQParameterOverviewDM::setFramework(const int & framework)
278 {
279  if (mpModelParameterSet != NULL)
280  {
281  beginResetModel();
282  mFramework = framework;
283  endResetModel();
284  }
285 }
286 
288 {
289  if (pNode == NULL)
290  {
291  return QModelIndex();
292  }
293 
294  if (pNode == mpModelParameterSet)
295  {
296  return index(0, 0, QModelIndex());
297  }
298 
299  QModelIndex Parent = index(static_cast< CModelParameter * >(pNode->getParent()));
300 
301  return index(getRow(pNode), 0, Parent);
302 }
303 
304 // static
306 {
307  if (!index.isValid()) return NULL;
308 
309  QModelIndex Tmp = index;
310  const QAbstractItemModel *pModel = Tmp.model();
311 
312  while (pModel->inherits("QSortFilterProxyModel"))
313  {
314  Tmp = static_cast< const QSortFilterProxyModel *>(pModel)->mapToSource(index);
315  pModel = Tmp.model();
316  }
317 
318  return static_cast< CModelParameter * >(Tmp.internalPointer());
319 }
320 
321 // static
323 {
324  if (pNode == NULL)
325  {
326  return -1;
327  }
328 
329  CModelParameterGroup * pParent = pNode->getParent();
330 
331  if (pParent == NULL)
332  {
333  return 0;
334  }
335 
337  CModelParameterGroup::const_iterator end = pParent->end();
338 
339  for (int i = 0; it != end; ++it, ++i)
340  {
341  if (*it == pNode)
342  {
343  return i;
344  }
345  }
346 
347  return -1;
348 }
349 
350 // static
351 QVariant CQParameterOverviewDM::nameData(const CModelParameter * pNode, int role)
352 {
353  if (role == Qt::DisplayRole)
354  {
355  return QVariant(QString(FROM_UTF8(pNode->getName())));
356  }
357 
358  return QVariant();
359 }
360 
361 // static
362 QVariant CQParameterOverviewDM::diffData(const CModelParameter * pNode, int role)
363 {
364  if (pNode->getType() == CModelParameter::Group ||
365  pNode->getType() == CModelParameter::Set)
366  {
367  return QVariant();
368  }
369 
370  switch (role)
371  {
372  case Qt::UserRole:
373 
374  switch (pNode->getCompareResult())
375  {
378  break;
379 
382  break;
383 
386  break;
387 
390  break;
391 
393  return QVariant();
394  break;
395  }
396 
397  break;
398 
399  case Qt::ToolTipRole:
400 
401  switch (pNode->getCompareResult())
402  {
404  return QVariant(QString("The item is no longer present in the model. \n\n"
405  "Click to remove from the set."));
406  break;
407 
409  return QVariant(QString("The item is present in the model but is missing in the parameter set. \n\n"
410  "Click to add to the set."));
411  break;
412 
414  return QVariant(QString("The item's value differs from the current model. \n\n"
415  "Click to update the sets value."));
416  break;
417 
419  return QVariant(QString("The item's value cannot be assigned to the model object since the\n"
420  "object is determined by an assignment. \n\n"
421  "Click to resolve conflict."));
422  break;
423 
425  return QVariant(QString("The item's value is identical with the current model."));
426  break;
427  }
428 
429  break;
430 
431  case Qt::SizeHintRole:
432  return QVariant(QSize(20, 20));
433  break;
434  }
435 
436  return QVariant();
437 }
438 
439 // static
440 QVariant CQParameterOverviewDM::typeData(const CModelParameter * pNode, int role)
441 {
442  switch (pNode->getType())
443  {
447  break;
448 
449  default:
450 
451  if (role == Qt::DisplayRole)
452  {
453  return QVariant(QString(FROM_UTF8(CModelEntity::StatusName[pNode->getSimulationType()])));
454  }
455 
456  break;
457  }
458 
459  return QVariant();
460 }
461 
462 QVariant CQParameterOverviewDM::valueData(const CModelParameter * pNode, int role) const
463 {
464  switch (pNode->getType())
465  {
469  break;
470 
471  default:
472 
473  if (role == Qt::EditRole)
474  return QVariant(QString::number(pNode->getValue(static_cast< CModelParameter::Framework >(mFramework)), 'g', 10));
475  else if (role == Qt::DisplayRole)
476  return QVariant(pNode->getValue(static_cast< CModelParameter::Framework >(mFramework)));
477 
478  break;
479  }
480 
481  return QVariant();
482 }
483 
484 QVariant CQParameterOverviewDM::unitData(const CModelParameter * pNode, int role) const
485 {
486  if (role == Qt::DisplayRole)
487  {
488  return QVariant(QString(FROM_UTF8(pNode->getUnit(static_cast< CModelParameter::Framework >(mFramework)))));
489  }
490 
491  return QVariant();
492 }
493 
494 // static
496 {
497  if (role == Qt::DisplayRole)
498  {
500  {
501  CCopasiObjectName GlobalQuantityCN = static_cast< const CModelParameterReactionParameter *>(pNode)->getGlobalQuantityCN();
502  CModelParameter * pGlobalQuantity = pNode->getSet()->getModelParameter(GlobalQuantityCN);
503 
504  if (pGlobalQuantity != NULL)
505  {
506  return QVariant(QString(FROM_UTF8(pGlobalQuantity->getName())));
507  }
508 
509  return QVariant(QString(FROM_UTF8(GlobalQuantityCN)));
510  }
511 
512  const CExpression* pExpression = pNode->getInitialExpressionPtr();
513 
514  if (pExpression != NULL)
515  return QVariant(QString(FROM_UTF8(pExpression->getDisplayString())));
516  else
517  return QVariant(QString(FROM_UTF8(pNode->getInitialExpression())));
518  }
519 
520  return QVariant();
521 }
Header file of class CExpression.
std::string getInitialExpression() const
virtual Qt::ItemFlags flags(const QModelIndex &index) const
void signalOpenEditor(const QModelIndex &) const
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
virtual std::string getName() const
static const std::string StatusName[]
Definition: CModelValue.h:67
const CompareResult & getCompareResult() const
virtual QModelIndex parent(const QModelIndex &index) const
CModelParameter * getModelParameter(const std::string &cn) const
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
const std::string & getDisplayString() const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
static QVariant typeData(const CModelParameter *pNode, int role)
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
const std::string getUnit(const Framework &framework) const
const CCopasiObjectName & getCN() const
QVariant valueData(const CModelParameter *pNode, int role) const
#define COL_DIFF
static QVariant diffData(const CModelParameter *pNode, int role)
bool isReadOnly() const
virtual void setValue(const double &value, const Framework &framework)
static QVariant assignmentData(const CModelParameter *pNode, int role)
#define COL_ASSIGNMENT
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
#define COL_UNIT
CModelParameterSet * getSet() const
static const QIcon & icon(const IconID &id)
void signalCloseEditor(const QModelIndex &) const
virtual const double & getValue(const Framework &framework) const
static CModelParameter * nodeFromIndex(const QModelIndex &index)
void setFramework(const int &framework)
const CModelEntity::Status & getSimulationType() const
const CExpression * getInitialExpressionPtr() const
std::vector< CModelParameter * >::const_iterator const_iterator
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
#define COLUMN_COUNT
const Type & getType() const
static int getRow(const CModelParameter *pNode)
CQParameterOverviewDM(QObject *pParent)
CModelParameterGroup * mpModelParameterSet
#define COL_VALUE
#define COL_TYPE
QVariant unitData(const CModelParameter *pNode, int role) const
#define COL_NAME
void setModelParameterset(CModelParameterSet *pModelParameterSet)
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
static QVariant nameData(const CModelParameter *pNode, int role)
CModelParameterGroup * getParent() const