COPASI API  4.16.103
CQEventDM.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 "report/CKeyFactory.h"
16 
17 #include "CQMessageBox.h"
18 #include "CQEventDM.h"
19 #include "qtUtilities.h"
20 
21 CQEventDM::CQEventDM(QObject *parent)
22  : CQBaseDataModel(parent)
23 
24 {
25 }
26 
27 int CQEventDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
28 {
29  return (int)(*CCopasiRootContainer::getDatamodelList())[0]->getModel()->getEvents().size() + 1;
30 }
31 int CQEventDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
32 {
33  return TOTAL_COLS_EVENTS;
34 }
35 
36 Qt::ItemFlags CQEventDM::flags(const QModelIndex &index) const
37 {
38  if (!index.isValid())
39  return Qt::ItemIsEnabled;
40 
41  if (index.column() == COL_NAME_EVENTS)
42  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
43  else
44  return QAbstractItemModel::flags(index);
45 }
46 
47 QVariant CQEventDM::data(const QModelIndex &index, int role) const
48 {
49  CExpression * pExpression = NULL;
50 
51  if (!index.isValid())
52  return QVariant();
53 
54  if (index.row() >= rowCount())
55  return QVariant();
56 
57  if (index.column() > 0 && role == Qt::ForegroundRole && !(flags(index) & Qt::ItemIsEditable))
58  return QColor(Qt::darkGray);
59 
60  if (role == Qt::DisplayRole || role == Qt::EditRole)
61  {
62  if (isDefaultRow(index))
63  {
64  switch (index.column())
65  {
66  case COL_ROW_NUMBER:
67  return QVariant(QString(""));
68 
69  case COL_NAME_EVENTS:
70  return QVariant(QString("New Event"));
71 
72  default:
73  return QVariant(QString(""));
74  }
75  }
76  else
77  {
78  CEvent *pEvent = (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->getEvents()[index.row()];
79  QString assignmentTarget = "";
80  QString assignmentExpression = "";
81 
82  if (index.column() == COL_ASSIGNTARGET_EVENTS || index.column() == COL_ASSIGNEXPRESSION_EVENTS)
83  {
87 
88  for (; it != end; ++it)
89  {
90  const CModelEntity * pEntity =
91  dynamic_cast< CModelEntity * >(CCopasiRootContainer::getKeyFactory()->get((*it)->getTargetKey()));
92 
93  if (pEntity != NULL)
94  {
95  if (it != begin)
96  {
97  assignmentTarget += "\n";
98  assignmentExpression += "\n";
99  }
100 
101  assignmentTarget += FROM_UTF8(pEntity->getObjectDisplayName());
102  pExpression = (*it)->getExpressionPtr();
103 
104  if (pExpression != NULL)
105  assignmentExpression += pExpression->getDisplayString().c_str();
106  else
107  assignmentExpression += FROM_UTF8((*it)->getExpression());
108  }
109  }
110  }
111 
112  switch (index.column())
113  {
114  case COL_ROW_NUMBER:
115  return QVariant(index.row() + 1);
116 
117  case COL_NAME_EVENTS:
118  return QVariant(FROM_UTF8(pEvent->getObjectName()));
119 
120  case COL_TRIGGER_EVENTS:
121  pExpression = pEvent->getTriggerExpressionPtr();
122 
123  if (pExpression != NULL)
124  return QVariant(FROM_UTF8(pExpression->getDisplayString()));
125  else
126  return QVariant(FROM_UTF8(pEvent->getTriggerExpression()));
127 
128  case COL_DELAYED_EVENTS:
129  {
130  if (pEvent->getDelayExpression() == "")
131  return QVariant(QString("No"));
132  else if (pEvent->getDelayAssignment())
133  return QVariant(QString("Assignment"));
134  else
135  return QVariant(QString("Calculation"));
136  }
137 
139  pExpression = pEvent->getDelayExpressionPtr();
140 
141  if (pExpression != NULL)
142  return QVariant(FROM_UTF8(pExpression->getDisplayString()));
143  else
144  return QVariant(FROM_UTF8(pEvent->getDelayExpression()));
145 
147  return QVariant(assignmentTarget);
148 
150  return QVariant(assignmentExpression);
151  }
152  }
153  }
154 
155  return QVariant();
156 }
157 
158 QVariant CQEventDM::headerData(int section, Qt::Orientation orientation,
159  int role) const
160 {
161  if (role != Qt::DisplayRole)
162  return QVariant();
163 
164  if (orientation == Qt::Horizontal)
165  {
166  switch (section)
167  {
168  case COL_ROW_NUMBER:
169  return QVariant(QString("#"));
170 
171  case COL_NAME_EVENTS:
172  return QVariant(QString("Name"));
173 
174  case COL_TRIGGER_EVENTS:
175  return QVariant(QString("Trigger Expression"));
176 
177  case COL_DELAYED_EVENTS:
178  return QVariant("Delayed");
179 
181  return QVariant("Delay Expression");
182 
184  return QVariant("Assignment Target");
185 
187  return QVariant("Assignment Expression");
188 
189  default:
190  return QVariant();
191  }
192  }
193  else //Vertical header
194  return QString("%1").arg(section + 1);
195 }
196 
197 bool CQEventDM::setData(const QModelIndex &index, const QVariant &value,
198  int role)
199 {
200  if (index.isValid() && role == Qt::EditRole)
201  {
202  bool defaultRow = isDefaultRow(index);
203 
204  if (defaultRow)
205  {
206  if (index.data() != value)
207  insertRow();
208  else
209  return false;
210  }
211 
212  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
213  CEvent *pEvent = (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->getEvents()[index.row()];
214 
215  if (index.column() == COL_NAME_EVENTS)
216  pEvent->setObjectName(TO_UTF8(value.toString()));
217 
218  if (defaultRow && this->index(index.row(), COL_NAME_EVENTS).data().toString() == "event")
220 
221  emit dataChanged(index, index);
223  }
224 
225  return true;
226 }
227 
228 bool CQEventDM::insertRows(int position, int rows, const QModelIndex&)
229 {
230  beginInsertRows(QModelIndex(), position, position + rows - 1);
231 
232  for (int row = 0; row < rows; ++row)
233  {
234  CEvent *pEvent =
235  (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->createEvent(TO_UTF8(createNewName("event", COL_NAME_EVENTS)));
237  }
238 
239  endInsertRows();
240 
241  return true;
242 }
243 
244 bool CQEventDM::removeRows(int position, int rows, const QModelIndex&)
245 {
246  if (rows <= 0)
247  return true;
248 
249  beginRemoveRows(QModelIndex(), position, position + rows - 1);
250 
251  CModel * pModel = (*CCopasiRootContainer::getDatamodelList())[0]->getModel();
252 
253  std::vector< std::string > DeletedKeys;
254  DeletedKeys.resize(rows);
255 
256  std::vector< std::string >::iterator itDeletedKey;
257  std::vector< std::string >::iterator endDeletedKey = DeletedKeys.end();
258 
259  CCopasiVector< CEvent >::const_iterator itRow = pModel->getEvents().begin() + position;
260 
261  for (itDeletedKey = DeletedKeys.begin(); itDeletedKey != endDeletedKey; ++itDeletedKey, ++itRow)
262  {
263  *itDeletedKey = (*itRow)->getKey();
264  }
265 
266  for (itDeletedKey = DeletedKeys.begin(); itDeletedKey != endDeletedKey; ++itDeletedKey)
267  {
268  (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->removeEvent(*itDeletedKey);
269  emit notifyGUI(ListViews::EVENT, ListViews::DELETE, *itDeletedKey);
270  }
271 
272  endRemoveRows();
273 
274  return true;
275 }
276 
277 bool CQEventDM::removeRows(QModelIndexList rows, const QModelIndex&)
278 {
279  if (rows.isEmpty())
280  return false;
281 
282  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
284  assert(pDataModel != NULL);
285  CModel * pModel = pDataModel->getModel();
286 
287  if (pModel == NULL)
288  return false;
289 
290 //Build the list of pointers to items to be deleted
291 //before actually deleting any item.
292  QList <CEvent *> pEvents;
293  QModelIndexList::const_iterator i;
294 
295  for (i = rows.begin(); i != rows.end(); ++i)
296  {
297  if (!isDefaultRow(*i) && pModel->getEvents()[(*i).row()])
298  pEvents.append(pModel->getEvents()[(*i).row()]);
299  }
300 
301  QList <CEvent *>::const_iterator j;
302 
303  for (j = pEvents.begin(); j != pEvents.end(); ++j)
304  {
305  CEvent * pEvent = *j;
306 
307  size_t delRow =
308  pModel->getEvents().CCopasiVector< CEvent >::getIndex(pEvent);
309 
310  if (delRow != C_INVALID_INDEX)
311  {
312  std::set< const CCopasiObject * > deletedObjects;
313  QMessageBox::StandardButton choice =
314  CQMessageBox::confirmDelete(NULL, "event",
315  FROM_UTF8(pEvent->getObjectName()),
316  deletedObjects);
317 
318  if (choice == QMessageBox::Ok)
319  removeRow((int) delRow);
320  }
321  }
322 
323  return true;
324 }
Definition: CEvent.h:152
#define COL_TRIGGER_EVENTS
Definition: CQEventDM.h:26
const CCopasiVectorN< CEventAssignment > & getAssignments() const
Definition: CEvent.cpp:678
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
const bool & getDelayAssignment() const
Definition: CEvent.cpp:419
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: CQEventDM.cpp:197
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQEventDM.cpp:27
CCopasiVectorN< CEvent > & getEvents()
Definition: CModel.cpp:1110
void notifyGUI(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
const std::string & getObjectName() const
CCopasiObject * get(const std::string &key)
#define COL_ASSIGNTARGET_EVENTS
Definition: CQEventDM.h:29
iterator begin()
const std::string & getDisplayString() const
#define C_INVALID_INDEX
Definition: copasi.h:222
std::string getTriggerExpression() const
Definition: CEvent.cpp:524
#define COL_ASSIGNEXPRESSION_EVENTS
Definition: CQEventDM.h:30
#define C_UNUSED(p)
Definition: copasi.h:220
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: CQEventDM.cpp:158
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQEventDM.cpp:31
CExpression * getDelayExpressionPtr()
Definition: CEvent.cpp:606
bool removeRow(int position)
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
Definition: CQEventDM.cpp:228
iterator end()
static StandardButton confirmDelete(QWidget *parent, const QString &objectType, const QString &objects, const std::set< const CCopasiObject * > &deletedObjects)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
#define TOTAL_COLS_EVENTS
Definition: CQEventDM.h:31
QString createNewName(const QString name, const int nameCol)
std::string getDelayExpression() const
Definition: CEvent.cpp:591
const CExpression * getTriggerExpressionPtr() const
Definition: CEvent.cpp:534
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
Definition: CQEventDM.cpp:277
static CKeyFactory * getKeyFactory()
QVariant data(const QModelIndex &index, int role) const
Definition: CQEventDM.cpp:47
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: CQEventDM.cpp:36
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
Definition: CModel.h:50
virtual bool isDefaultRow(const QModelIndex &i) const
bool setObjectName(const std::string &name)
virtual const std::string & getKey() const
Definition: CEvent.cpp:287
#define COL_DELAYED_EVENTS
Definition: CQEventDM.h:27
CQEventDM(QObject *parent=0)
Definition: CQEventDM.cpp:21
#define COL_NAME_EVENTS
Definition: CQEventDM.h:25
#define COL_DELAY_EXPRESSION_EVENTS
Definition: CQEventDM.h:28
#define COL_ROW_NUMBER