COPASI API  4.16.103
CQGlobalQuantityDM.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) 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 <QtCore/QString>
12 
15 #include "model/CModelValue.h"
16 #include "function/CExpression.h"
17 
18 #include "CQMessageBox.h"
19 #include "CQGlobalQuantityDM.h"
20 #include "qtUtilities.h"
21 
23  : CQBaseDataModel(parent)
24 
25 {
29 
30  mItemToType.push_back(CModelEntity::FIXED);
31  mItemToType.push_back(CModelEntity::ASSIGNMENT);
32  mItemToType.push_back(CModelEntity::ODE);
33 }
34 
35 const QStringList& CQGlobalQuantityDM::getTypes()
36 {
37  return mTypes;
38 }
39 
40 const std::vector< unsigned C_INT32 >& CQGlobalQuantityDM::getItemToType()
41 {
42  return mItemToType;
43 }
44 
45 int CQGlobalQuantityDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
46 {
47  return (int)(*CCopasiRootContainer::getDatamodelList())[0]->getModel()->getModelValues().size() + 1;
48 }
49 int CQGlobalQuantityDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
50 {
51  return TOTAL_COLS_GQ;
52 }
53 
54 Qt::ItemFlags CQGlobalQuantityDM::flags(const QModelIndex &index) const
55 {
56  if (!index.isValid())
57  return Qt::ItemIsEnabled;
58 
59  if (index.column() == COL_NAME_GQ || index.column() == COL_TYPE_GQ)
60  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
61  else if (index.column() == COL_INITIAL_GQ)
62  {
63  if (this->index(index.row(), COL_TYPE_GQ).data() == QString(FROM_UTF8(CModelEntity::StatusName[CModelEntity::ASSIGNMENT])))
64  return QAbstractItemModel::flags(index) & ~Qt::ItemIsEnabled;
65  else
66  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled;
67  }
68  else
69  return QAbstractItemModel::flags(index);
70 }
71 
72 QVariant CQGlobalQuantityDM::data(const QModelIndex &index, int role) const
73 {
74  if (!index.isValid())
75  return QVariant();
76 
77  if (index.row() >= rowCount())
78  return QVariant();
79 
80  if (index.column() > 0 && role == Qt::ForegroundRole && !(flags(index) & Qt::ItemIsEditable))
81  return QColor(Qt::darkGray);
82 
83  if (role == Qt::DisplayRole || role == Qt::EditRole)
84  {
85  if (isDefaultRow(index))
86  {
87  switch (index.column())
88  {
89  case COL_ROW_NUMBER:
90  return QVariant(QString(""));
91 
92  case COL_NAME_GQ:
93  return QVariant(QString("New Quantity"));
94 
95  case COL_TYPE_GQ:
96  return QVariant(QString(FROM_UTF8(CModelEntity::StatusName[mItemToType[0]])));
97 
98  case COL_INITIAL_GQ:
99  return QVariant(QString::number(0.0, 'g', 10));
100 
101  default:
102  return QVariant(QString(""));
103  }
104  }
105  else
106  {
107  CModelValue *pGQ = (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->getModelValues()[index.row()];
108  const CExpression * pExpression = NULL;
109 
110  switch (index.column())
111  {
112  case COL_ROW_NUMBER:
113  return QVariant(index.row() + 1);
114 
115  case COL_NAME_GQ:
116  return QVariant(QString(FROM_UTF8(pGQ->getObjectName())));
117 
118  case COL_TYPE_GQ:
119  return QVariant(QString(FROM_UTF8(CModelEntity::StatusName[pGQ->getStatus()])));
120 
121  case COL_INITIAL_GQ:
122 
123  if (role == Qt::EditRole)
124  return QVariant(QString::number(pGQ->getInitialValue(), 'g', 10));
125  else
126  return QVariant(pGQ->getInitialValue());
127 
128  case COL_TRANSIENT_GQ:
129  return QVariant(pGQ->getValue());
130 
131  case COL_RATE_GQ:
132  return QVariant(pGQ->getRate());
133 
134  case COL_IEXPRESSION_GQ:
135 
136  if (pGQ->getInitialExpression() != "")
137  {
138  pExpression = pGQ->getInitialExpressionPtr();
139 
140  if (pExpression != NULL)
141  return QVariant(QString(FROM_UTF8(pExpression->getDisplayString())));
142  else
143  return QVariant();
144  }
145 
146  break;
147 
148  case COL_EXPRESSION_GQ:
149  {
150  pExpression = pGQ->getExpressionPtr();
151 
152  if (pExpression != NULL)
153  return QVariant(QString(FROM_UTF8(pExpression->getDisplayString())));
154  else
155  return QVariant();
156  }
157  }
158  }
159  }
160 
161  return QVariant();
162 }
163 
164 QVariant CQGlobalQuantityDM::headerData(int section, Qt::Orientation orientation,
165  int role) const
166 {
167  if (role != Qt::DisplayRole)
168  return QVariant();
169 
170  if (orientation == Qt::Horizontal)
171  {
172  switch (section)
173  {
174  case COL_ROW_NUMBER:
175  return QVariant(QString("#"));
176 
177  case COL_NAME_GQ:
178  return QVariant(QString("Name"));
179 
180  case COL_TYPE_GQ:
181  return QVariant(QString(" Type "));
182 
183  case COL_INITIAL_GQ:
184  return QVariant("Initial Value");
185 
186  case COL_TRANSIENT_GQ:
187  return QVariant("Transient Value");
188 
189  case COL_RATE_GQ:
190  return QVariant("Rate");
191 
192  case COL_IEXPRESSION_GQ:
193  return QVariant("Initial Expression");
194 
195  case COL_EXPRESSION_GQ:
196  return QVariant("Expression");
197 
198  default:
199  return QVariant();
200  }
201  }
202  else //Vertical header
203  return QString("%1").arg(section + 1);
204 }
205 
206 bool CQGlobalQuantityDM::setData(const QModelIndex &index, const QVariant &value,
207  int role)
208 {
209  if (index.isValid() && role == Qt::EditRole)
210  {
211  bool defaultRow = isDefaultRow(index);
212 
213  if (defaultRow)
214  {
215  if (index.column() == COL_TYPE_GQ)
216  {
217  if (index.data().toString() != QString(FROM_UTF8(CModelEntity::StatusName[mItemToType[value.toInt()]])))
218  insertRow();
219  else
220  return false;
221  }
222  else if (index.data() != value)
223  insertRow();
224  else
225  return false;
226  }
227 
228  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
229  CModelValue *pGQ = (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->getModelValues()[index.row()];
230 
231  if (index.column() == COL_NAME_GQ)
232  pGQ->setObjectName(TO_UTF8(value.toString()));
233  else if (index.column() == COL_TYPE_GQ)
234  pGQ->setStatus((CModelEntity::Status) mItemToType[value.toInt()]);
235  else if (index.column() == COL_INITIAL_GQ)
236  pGQ->setInitialValue(value.toDouble());
237 
238  if (defaultRow && this->index(index.row(), COL_NAME_GQ).data().toString() == "quantity")
239  pGQ->setObjectName(TO_UTF8(createNewName("quantity", COL_NAME_GQ)));
240 
241  emit dataChanged(index, index);
243  }
244 
245  return true;
246 }
247 
248 bool CQGlobalQuantityDM::insertRows(int position, int rows, const QModelIndex&)
249 {
250  beginInsertRows(QModelIndex(), position, position + rows - 1);
251 
252  for (int row = 0; row < rows; ++row)
253  {
254  CModelValue *pGQ = (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->createModelValue(TO_UTF8(createNewName("quantity", COL_NAME_GQ)));
256  }
257 
258  endInsertRows();
259 
260  return true;
261 }
262 
263 bool CQGlobalQuantityDM::removeRows(int position, int rows, const QModelIndex&)
264 {
265  if (rows <= 0)
266  return true;
267 
268  beginRemoveRows(QModelIndex(), position, position + rows - 1);
269 
270  CModel * pModel = (*CCopasiRootContainer::getDatamodelList())[0]->getModel();
271 
272  std::vector< std::string > DeletedKeys;
273  DeletedKeys.resize(rows);
274 
275  std::vector< std::string >::iterator itDeletedKey;
276  std::vector< std::string >::iterator endDeletedKey = DeletedKeys.end();
277 
279 
280  for (itDeletedKey = DeletedKeys.begin(); itDeletedKey != endDeletedKey; ++itDeletedKey, ++itRow)
281  {
282  *itDeletedKey = (*itRow)->getKey();
283  }
284 
285  for (itDeletedKey = DeletedKeys.begin(); itDeletedKey != endDeletedKey; ++itDeletedKey)
286  {
287  pModel->removeModelValue(*itDeletedKey);
288  emit notifyGUI(ListViews::MODELVALUE, ListViews::DELETE, *itDeletedKey);
289  emit notifyGUI(ListViews::MODELVALUE, ListViews::DELETE, ""); //Refresh all as there may be dependencies.
290  }
291 
292  endRemoveRows();
293 
294  return true;
295 }
296 
297 bool CQGlobalQuantityDM::removeRows(QModelIndexList rows, const QModelIndex&)
298 {
299  if (rows.isEmpty())
300  return false;
301 
302  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
304  assert(pDataModel != NULL);
305  CModel * pModel = pDataModel->getModel();
306 
307  if (pModel == NULL)
308  return false;
309 
310 //Build the list of pointers to items to be deleted
311 //before actually deleting any item.
312  QList <CModelValue *> pGlobalQuantities;
313  QModelIndexList::const_iterator i;
314 
315  for (i = rows.begin(); i != rows.end(); ++i)
316  {
317  if (!isDefaultRow(*i) && pModel->getModelValues()[(*i).row()])
318  pGlobalQuantities.append(pModel->getModelValues()[(*i).row()]);
319  }
320 
321  QList <CModelValue *>::const_iterator j;
322 
323  for (j = pGlobalQuantities.begin(); j != pGlobalQuantities.end(); ++j)
324  {
325  CModelValue * pGQ = *j;
326 
327  size_t delRow =
328  pModel->getModelValues().CCopasiVector< CModelValue >::getIndex(pGQ);
329 
330  if (delRow != C_INVALID_INDEX)
331  {
332  QMessageBox::StandardButton choice =
333  CQMessageBox::confirmDelete(NULL, "quantity",
334  FROM_UTF8(pGQ->getObjectName()),
335  pGQ->getDeletedObjects());
336 
337  if (choice == QMessageBox::Ok)
338  removeRow((int) delRow);
339  }
340  }
341 
342  return true;
343 }
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
const CExpression * getExpressionPtr() const
Header file of class CExpression.
const QStringList & getTypes()
#define COL_EXPRESSION_GQ
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Header file of class CModelEntity and CModelValue.
#define COL_NAME_GQ
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
static const std::string StatusName[]
Definition: CModelValue.h:67
const C_FLOAT64 & getRate() const
void notifyGUI(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
const std::string & getObjectName() const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
const CCopasiVectorN< CModelValue > & getModelValues() const
Definition: CModel.cpp:1060
iterator begin()
const std::string & getDisplayString() const
#define C_INVALID_INDEX
Definition: copasi.h:222
#define COL_INITIAL_GQ
#define C_UNUSED(p)
Definition: copasi.h:220
QVariant data(const QModelIndex &index, int role) const
bool removeRow(int position)
bool removeModelValue(const CModelValue *pModelValue, const bool &recursive=true)
Definition: CModel.cpp:2903
const C_FLOAT64 & getInitialValue() const
std::vector< CType * >::const_iterator const_iterator
Definition: CCopasiVector.h:57
CQGlobalQuantityDM(QObject *parent=0)
virtual const std::string & getKey() const
virtual void setStatus(const CModelEntity::Status &status)
#define COL_RATE_GQ
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()
QString createNewName(const QString name, const int nameCol)
virtual std::set< const CCopasiObject * > getDeletedObjects() const
const C_FLOAT64 & getValue() const
#define COL_TYPE_GQ
Qt::ItemFlags flags(const QModelIndex &index) const
#define COL_IEXPRESSION_GQ
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
virtual void setInitialValue(const C_FLOAT64 &initialValue)
Definition: CModel.h:50
const CModelEntity::Status & getStatus() const
std::vector< unsigned C_INT32 > mItemToType
virtual bool isDefaultRow(const QModelIndex &i) const
bool setObjectName(const std::string &name)
#define COL_TRANSIENT_GQ
const std::vector< unsigned C_INT32 > & getItemToType()
const CExpression * getInitialExpressionPtr() const
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
#define COL_ROW_NUMBER
#define TOTAL_COLS_GQ