COPASI API  4.16.103
CQReportDM.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 #include <QtCore/QList>
13 
18 
19 #include "CQMessageBox.h"
20 #include "CQReportDM.h"
21 #include "qtUtilities.h"
22 
23 CQReportDM::CQReportDM(QObject *parent)
24  : CQBaseDataModel(parent)
25 
26 {
27 }
28 
29 int CQReportDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
30 {
31  return (int)(*CCopasiRootContainer::getDatamodelList())[0]->getReportDefinitionList()->size() + 1;
32 }
33 int CQReportDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
34 {
35  return TOTAL_COLS_REPORTS;
36 }
37 
38 Qt::ItemFlags CQReportDM::flags(const QModelIndex &index) const
39 {
40  if (!index.isValid())
41  return Qt::ItemIsEnabled;
42 
43  if (index.column() == COL_NAME_REPORTS)
44  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
45  else
46  return QAbstractItemModel::flags(index);
47 }
48 
49 QVariant CQReportDM::data(const QModelIndex &index, int role) const
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  case COL_NAME_REPORTS:
69  return QVariant(QString("New Report"));
70  default:
71  return QVariant(QString(""));
72  }
73  }
74  else
75  {
76  CReportDefinition *pRepDef = (*CCopasiRootContainer::getDatamodelList())[0]->getReportDefinitionList()->operator[](index.row());
77 
78  switch (index.column())
79  {
80  case COL_ROW_NUMBER:
81  return QVariant(index.row() + 1);
82 
83  case COL_NAME_REPORTS:
84  return QVariant(QString(FROM_UTF8(pRepDef->getObjectName())));
85  }
86  }
87  }
88 
89  return QVariant();
90 }
91 
92 QVariant CQReportDM::headerData(int section, Qt::Orientation orientation,
93  int role) const
94 {
95  if (role != Qt::DisplayRole)
96  return QVariant();
97 
98  if (orientation == Qt::Horizontal)
99  {
100  switch (section)
101  {
102  case COL_ROW_NUMBER:
103  return QVariant(QString("#"));
104  case COL_NAME_REPORTS:
105  return QVariant(QString("Name"));
106  default:
107  return QVariant();
108  }
109  }
110  else
111  return QString("%1").arg(section + 1);
112 }
113 
114 bool CQReportDM::setData(const QModelIndex &index, const QVariant &value,
115  int role)
116 {
117  if (index.isValid() && role == Qt::EditRole)
118  {
119  bool defaultRow = isDefaultRow(index);
120 
121  if (defaultRow)
122  {
123  if (index.data() != value)
124  insertRow();
125  else
126  return false;
127  }
128 
129  CReportDefinition *pRepDef = (*CCopasiRootContainer::getDatamodelList())[0]->getReportDefinitionList()->operator[](index.row());
130 
131  if (index.column() == COL_NAME_REPORTS)
132  pRepDef->setObjectName(TO_UTF8(value.toString()));
133 
134  if (defaultRow && this->index(index.row(), COL_NAME_REPORTS).data().toString() == "report")
135  pRepDef->setObjectName(TO_UTF8(createNewName("report", COL_NAME_REPORTS)));
136 
137  emit dataChanged(index, index);
139  }
140 
141  return true;
142 }
143 
144 bool CQReportDM::insertRows(int position, int rows, const QModelIndex&)
145 {
146  beginInsertRows(QModelIndex(), position, position + rows - 1);
147 
148  for (int row = 0; row < rows; ++row)
149  {
150  CReportDefinition *pRepDef =
151  (*CCopasiRootContainer::getDatamodelList())[0]->getReportDefinitionList()->createReportDefinition(TO_UTF8(createNewName("report", COL_NAME_REPORTS)), "");
153  }
154 
155  endInsertRows();
156 
157  return true;
158 }
159 
160 bool CQReportDM::removeRows(int position, int rows, const QModelIndex&)
161 {
162  if (rows <= 0)
163  return true;
164 
166 
167  if (pDataModel == NULL)
168  return false;
169 
170  CCopasiVector< CReportDefinition > * pReportList = pDataModel->getReportDefinitionList();
171 
172  if (pReportList == NULL)
173  return false;
174 
175  beginRemoveRows(QModelIndex(), position, position + rows - 1);
176 
177  for (int row = 0; row < rows; ++row)
178  {
179  CReportDefinition * pReport = (*pReportList)[position];
180 
181  if (pReport == NULL)
182  continue;
183 
184  std::set< const CCopasiObject * > Tasks;
185  std::set< const CCopasiObject * > DeletedObjects;
186  DeletedObjects.insert(pReport);
187 
188  if (pDataModel->appendDependentTasks(DeletedObjects, Tasks))
189  {
190  std::set< const CCopasiObject * >::iterator it = Tasks.begin();
191  std::set< const CCopasiObject * >::iterator end = Tasks.end();
192 
193  for (; it != end; ++it)
194  {
195  const CCopasiTask * pTask = static_cast< const CCopasiTask *>(*it);
196  const_cast< CCopasiTask * >(pTask)->getReport().setReportDefinition(NULL);
197  }
198  }
199 
200  std::string deletedKey = pReport->getKey();
201  pReportList->remove(pReport);
202  emit notifyGUI(ListViews::REPORT, ListViews::DELETE, deletedKey);
203  }
204 
205  endRemoveRows();
206 
207  return true;
208 }
209 
210 bool CQReportDM::removeRows(QModelIndexList rows, const QModelIndex&)
211 {
212  if (rows.isEmpty())
213  return false;
214 
216  assert(pDataModel != NULL);
217 
218  CCopasiVector< CReportDefinition > * pReportList = pDataModel->getReportDefinitionList();
219 
220  if (pReportList == NULL)
221  return false;
222 
223  QList< CReportDefinition * > Reports;
224 
225  QModelIndexList::const_iterator i;
226 
227  for (i = rows.begin(); i != rows.end(); ++i)
228  {
229  if (!isDefaultRow(*i) && (*pReportList)[(*i).row()])
230  Reports.append((*pReportList)[(*i).row()]);
231  }
232 
233  QList< CReportDefinition * >::const_iterator j;
234 
235  for (j = Reports.begin(); j != Reports.end(); ++j)
236  {
237  CReportDefinition * pReport = *j;
238 
239  size_t delRow = pReportList->getIndex(pReport);
240 
241  if (delRow != C_INVALID_INDEX)
242  {
243  std::set< const CCopasiObject * > DeletedObjects;
244  DeletedObjects.insert(pReport);
245 
246  QMessageBox::StandardButton choice =
247  CQMessageBox::confirmDelete(NULL, "report",
248  FROM_UTF8(pReport->getObjectName()),
249  DeletedObjects);
250 
251  if (choice == QMessageBox::Ok)
252  {
253  removeRow((int) delRow);
254  }
255  }
256  }
257 
258  return true;
259 }
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: CQReportDM.cpp:38
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
void notifyGUI(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
const std::string & getObjectName() const
bool appendDependentTasks(std::set< const CCopasiObject * > candidates, std::set< const CCopasiObject * > &dependentTasks) const
CQReportDM(QObject *parent=0)
Definition: CQReportDM.cpp:23
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: CQReportDM.cpp:114
#define C_INVALID_INDEX
Definition: copasi.h:222
#define COL_NAME_REPORTS
Definition: CQReportDM.h:25
#define C_UNUSED(p)
Definition: copasi.h:220
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQReportDM.cpp:29
const CReportDefinitionVector * getReportDefinitionList() const
bool removeRow(int position)
CTSSATask * pTask
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQReportDM.cpp:33
static StandardButton confirmDelete(QWidget *parent, const QString &objectType, const QString &objects, const std::set< const CCopasiObject * > &deletedObjects)
QVariant data(const QModelIndex &index, int role) const
Definition: CQReportDM.cpp:49
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
QString createNewName(const QString name, const int nameCol)
#define TOTAL_COLS_REPORTS
Definition: CQReportDM.h:26
virtual const std::string & getKey() const
virtual void remove(const size_t &index)
virtual size_t getIndex(const CCopasiObject *pObject) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: CQReportDM.cpp:92
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
Definition: CQReportDM.cpp:144
virtual bool isDefaultRow(const QModelIndex &i) const
bool setObjectName(const std::string &name)
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
Definition: CQReportDM.cpp:210
#define COL_ROW_NUMBER