COPASI API  4.16.103
CQCompartmentDM.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 
13 #include "CQCompartmentDM.h"
14 
15 #include "CQMessageBox.h"
16 #include "qtUtilities.h"
17 
18 #include "copasi.h"
19 
22 #include "model/CCompartment.h"
23 #include "model/CModel.h"
24 #include "function/CExpression.h"
25 
27  : CQBaseDataModel(parent)
28 
29 {
33 
34  mItemToType.push_back(CModelEntity::FIXED);
35  mItemToType.push_back(CModelEntity::ASSIGNMENT);
36  mItemToType.push_back(CModelEntity::ODE);
37 }
38 
39 const QStringList& CQCompartmentDM::getTypes()
40 {
41  return mTypes;
42 }
43 
44 const std::vector< unsigned C_INT32 >& CQCompartmentDM::getItemToType()
45 {
46  return mItemToType;
47 }
48 
49 int CQCompartmentDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
50 {
51  return (int)(*CCopasiRootContainer::getDatamodelList())[0]->getModel()->getCompartments().size() + 1;
52 }
53 int CQCompartmentDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
54 {
56 }
57 
58 Qt::ItemFlags CQCompartmentDM::flags(const QModelIndex &index) const
59 {
60  if (!index.isValid())
61  return Qt::ItemIsEnabled;
62 
63  if (index.column() == COL_NAME_COMPARTMENTS || index.column() == COL_TYPE_COMPARTMENTS)
64  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
65  else if (index.column() == COL_IVOLUME)
66  {
67  if (this->index(index.row(), COL_TYPE_COMPARTMENTS).data() == QString(FROM_UTF8(CModelEntity::StatusName[CModelEntity::ASSIGNMENT])))
68  return QAbstractItemModel::flags(index) & ~Qt::ItemIsEnabled;
69  else
70  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled;
71  }
72  else
73  return QAbstractItemModel::flags(index);
74 }
75 
76 QVariant CQCompartmentDM::data(const QModelIndex &index, int role) const
77 {
78  if (!index.isValid())
79  return QVariant();
80 
81  if (index.row() >= rowCount())
82  return QVariant();
83 
84  if (index.column() > 0 && role == Qt::ForegroundRole && !(flags(index) & Qt::ItemIsEditable))
85  return QColor(Qt::darkGray);
86 
87  if (role == Qt::DisplayRole || role == Qt::EditRole)
88  {
89  if (isDefaultRow(index))
90  {
91  switch (index.column())
92  {
93  case COL_ROW_NUMBER:
94  return QVariant(QString(""));
95 
97  return QVariant(QString("New Compartment"));
98 
100  return QVariant(QString(FROM_UTF8(CModelEntity::StatusName[mItemToType[0]])));
101 
102  case COL_IVOLUME:
103  return QVariant(QString::number(1.0, 'g', 10));
104 
105  default:
106  return QVariant(QString(""));
107  }
108  }
109  else
110  {
111  CCompartment *pComp = (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->getCompartments()[index.row()];
112  const CExpression * pExpression = NULL;
113 
114  switch (index.column())
115  {
116  case COL_ROW_NUMBER:
117  return QVariant(index.row() + 1);
118 
120  return QVariant(QString(FROM_UTF8(pComp->getObjectName())));
121 
123  return QVariant(QString(FROM_UTF8(CModelEntity::StatusName[pComp->getStatus()])));
124 
125  case COL_IVOLUME:
126 
127  if (role == Qt::EditRole)
128  return QVariant(QString::number(pComp->getInitialValue(), 'g', 10));
129  else
130  return QVariant(pComp->getInitialValue());
131 
132  case COL_VOLUME:
133  return QVariant(pComp->getValue());
134 
136  return QVariant(pComp->getRate());
137 
139  {
140  pExpression = pComp->getInitialExpressionPtr();
141 
142  if (pExpression != NULL)
143  return QVariant(QString(FROM_UTF8(pExpression->getDisplayString())));
144  else
145  return QVariant();
146  }
147 
149  {
150  pExpression = pComp->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 CQCompartmentDM::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  QString ValueUnits, RateUnits, ExpressionUnits;
173  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
174  const CModel * pModel = (*CCopasiRootContainer::getDatamodelList())[0]->getModel();
175 
176  if (pModel)
177  {
178  ValueUnits = FROM_UTF8(pModel->getVolumeUnitsDisplayString());
179 
180  if (!ValueUnits.isEmpty())
181  ValueUnits = "\n(" + ValueUnits + ")";
182 
183  RateUnits = FROM_UTF8(pModel->getVolumeRateUnitsDisplayString());
184 
185  if (!RateUnits.isEmpty())
186  RateUnits = "\n(" + RateUnits + ")";
187 
188  if (!ValueUnits.isEmpty() && !RateUnits.isEmpty())
189  {
190  if (ValueUnits == RateUnits)
191  ExpressionUnits = ValueUnits;
192  else
193  ExpressionUnits = "\n(" + FROM_UTF8(pModel->getVolumeUnitsDisplayString()) + " or " + FROM_UTF8(pModel->getVolumeRateUnitsDisplayString()) + ")";
194  }
195  else if (!ValueUnits.isEmpty())
196  ExpressionUnits = "\n(" + FROM_UTF8(pModel->getVolumeUnitsDisplayString()) + " or 1)";
197  else if (!RateUnits.isEmpty())
198  ExpressionUnits = "\n(1 or " + FROM_UTF8(pModel->getVolumeRateUnitsDisplayString()) + ")";
199  }
200 
201  switch (section)
202  {
203  case COL_ROW_NUMBER:
204  return QVariant(QString("#"));
205 
207  return QVariant(QString("Name"));
208 
210  return QVariant(QString(" Type "));
211 
212  case COL_IVOLUME:
213  return QVariant("Initial Volume" + ValueUnits);
214 
215  case COL_VOLUME:
216  return QVariant("Volume" + ValueUnits);
217 
219  return QVariant("Rate" + RateUnits);
220 
222  return QVariant("Initial Expression" + ValueUnits);
223 
225  return QVariant("Expression" + ExpressionUnits);
226 
227  default:
228  return QVariant();
229  }
230  }
231  else //Vertical header
232  return QString("%1").arg(section + 1);
233 }
234 
235 bool CQCompartmentDM::setData(const QModelIndex &index, const QVariant &value,
236  int role)
237 {
238  if (index.isValid() && role == Qt::EditRole)
239  {
240  bool defaultRow = isDefaultRow(index);
241 
242  if (defaultRow)
243  {
244  if (index.column() == COL_TYPE_COMPARTMENTS)
245  {
246  if (index.data().toString() != QString(FROM_UTF8(CModelEntity::StatusName[mItemToType[value.toInt()]])))
247  insertRow();
248  else
249  return false;
250  }
251  else if (index.data() != value)
252  insertRow();
253  else
254  return false;
255  }
256 
257  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
258  CCompartment *pComp = (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->getCompartments()[index.row()];
259 
260  if (index.column() == COL_NAME_COMPARTMENTS)
261  pComp->setObjectName(TO_UTF8(value.toString()));
262  else if (index.column() == COL_TYPE_COMPARTMENTS)
263  pComp->setStatus((CModelEntity::Status) mItemToType[value.toInt()]);
264  else if (index.column() == COL_IVOLUME)
265  pComp->setInitialValue(value.toDouble());
266 
267  if (defaultRow && this->index(index.row(), COL_NAME_COMPARTMENTS).data().toString() == "compartment")
268  pComp->setObjectName(TO_UTF8(createNewName("compartment", COL_NAME_COMPARTMENTS)));
269 
270  emit dataChanged(index, index);
272  }
273 
274  return true;
275 }
276 
277 bool CQCompartmentDM::insertRows(int position, int rows, const QModelIndex&)
278 {
279  beginInsertRows(QModelIndex(), position, position + rows - 1);
280 
281  for (int row = 0; row < rows; ++row)
282  {
283  CCompartment * pComp = (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->createCompartment(TO_UTF8(createNewName("compartment", COL_NAME_COMPARTMENTS)));
285  }
286 
287  endInsertRows();
288 
289  return true;
290 }
291 
292 bool CQCompartmentDM::removeRows(int position, int rows, const QModelIndex&)
293 {
294  if (rows <= 0)
295  return true;
296 
297  beginRemoveRows(QModelIndex(), position, position + rows - 1);
298 
299  CModel * pModel = (*CCopasiRootContainer::getDatamodelList())[0]->getModel();
300 
301  std::vector< std::string > DeletedKeys;
302  DeletedKeys.resize(rows);
303 
304  std::vector< std::string >::iterator itDeletedKey;
305  std::vector< std::string >::iterator endDeletedKey = DeletedKeys.end();
306 
308 
309  for (itDeletedKey = DeletedKeys.begin(); itDeletedKey != endDeletedKey; ++itDeletedKey, ++itRow)
310  {
311  *itDeletedKey = (*itRow)->getKey();
312  }
313 
314  for (itDeletedKey = DeletedKeys.begin(); itDeletedKey != endDeletedKey; ++itDeletedKey)
315  {
316  (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->removeCompartment(*itDeletedKey);
317  emit notifyGUI(ListViews::COMPARTMENT, ListViews::DELETE, *itDeletedKey);
319  emit notifyGUI(ListViews::METABOLITE, ListViews::DELETE, ""); //Refresh all as there may be dependencies.
320  }
321 
322  endRemoveRows();
323 
324  return true;
325 }
326 
327 bool CQCompartmentDM::removeRows(QModelIndexList rows, const QModelIndex&)
328 {
329  if (rows.isEmpty())
330  return false;
331 
332  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
334  assert(pDataModel != NULL);
335  CModel * pModel = pDataModel->getModel();
336 
337  if (pModel == NULL)
338  return false;
339 
340 //Build the list of pointers to items to be deleted
341 //before actually deleting any item.
342  QList <CCompartment *> pCompartments;
343  QModelIndexList::const_iterator i;
344 
345  for (i = rows.begin(); i != rows.end(); ++i)
346  {
347  if (!isDefaultRow(*i) && pModel->getCompartments()[(*i).row()])
348  pCompartments.append(pModel->getCompartments()[(*i).row()]);
349  }
350 
351  QList <CCompartment *>::const_iterator j;
352 
353  for (j = pCompartments.begin(); j != pCompartments.end(); ++j)
354  {
355  CCompartment * pCompartment = *j;
356 
357  size_t delRow =
358  pModel->getCompartments().CCopasiVector< CCompartment >::getIndex(pCompartment);
359 
360  if (delRow != C_INVALID_INDEX)
361  {
362  QMessageBox::StandardButton choice =
363  CQMessageBox::confirmDelete(NULL, "compartment",
364  FROM_UTF8(pCompartment->getObjectName()),
365  pCompartment->getDeletedObjects());
366 
367  if (choice == QMessageBox::Ok)
368  removeRow((int) delRow);
369  }
370  }
371 
372  return true;
373 }
const CExpression * getExpressionPtr() const
Header file of class CExpression.
#define COL_VOLUME
virtual Qt::ItemFlags flags(const QModelIndex &index) const
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
#define COL_NAME_COMPARTMENTS
#define COL_TYPE_COMPARTMENTS
#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
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
std::vector< unsigned C_INT32 > mItemToType
iterator begin()
virtual QVariant data(const QModelIndex &index, int role) const
const std::vector< unsigned C_INT32 > & getItemToType()
std::string getVolumeRateUnitsDisplayString() const
Definition: CModel.cpp:4571
const std::string & getDisplayString() const
#define C_INVALID_INDEX
Definition: copasi.h:222
#define COL_IEXPRESSION_COMPARTMENTS
#define C_UNUSED(p)
Definition: copasi.h:220
#define COL_IVOLUME
virtual std::set< const CCopasiObject * > getDeletedObjects() const
bool removeRow(int position)
const C_FLOAT64 & getInitialValue() const
std::vector< CType * >::const_iterator const_iterator
Definition: CCopasiVector.h:57
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
std::string getVolumeUnitsDisplayString() const
Definition: CModel.cpp:4547
const QStringList & getTypes()
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)
#define COL_EXPRESSION_COMPARTMENTS
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
QString createNewName(const QString name, const int nameCol)
#define TOTAL_COLS_COMPARTMENTS
CCopasiVectorNS< CCompartment > & getCompartments()
Definition: CModel.cpp:1145
const C_FLOAT64 & getValue() const
CQCompartmentDM(QObject *parent=0)
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
#define COL_RATE_COMPARTMENTS
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
virtual void setInitialValue(const C_FLOAT64 &initialValue)
Definition: CModel.h:50
const CModelEntity::Status & getStatus() const
virtual bool isDefaultRow(const QModelIndex &i) const
QStringList mTypes
bool setObjectName(const std::string &name)
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
const CExpression * getInitialExpressionPtr() const
#define COL_ROW_NUMBER