COPASI API  4.16.103
CQModifiedDM.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/MIRIAMUI/CQModifiedDM.cpp,v $
3 // $Revision: 1.12 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/05/02 18:56:24 $
7 // End CVS Header
8 
9 // Copyright (C) 2012 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 #include <QtCore/QDateTime>
20 
21 #include "UI/qtUtilities.h"
22 #include "UI/CQMessageBox.h"
23 
24 #include "CQModifiedDM.h"
25 
27 
28 CQModifiedDM::CQModifiedDM(CMIRIAMInfo* MIRIAMInfo, QObject *parent)
29  : CQBaseDataModel(parent)
30 
31 {
32  mpMIRIAMInfo = MIRIAMInfo;
33 }
34 
35 int CQModifiedDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
36 {
37  return (int) mpMIRIAMInfo->getModifications().size() + 1;
38 }
39 int CQModifiedDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
40 {
41  return TOTAL_COLS_MODIFIEDS;
42 }
43 
44 QVariant CQModifiedDM::data(const QModelIndex &index, int role) const
45 {
46  if (!index.isValid())
47  return QVariant();
48 
49  if (index.row() >= rowCount())
50  return QVariant();
51 
52  if (role == Qt::DisplayRole || role == Qt::EditRole)
53  {
54  if (isDefaultRow(index))
55  {
56  if (index.column() == COL_ROW_NUMBER)
57  return QVariant(QString(""));
58  else if (index.column() == COL_DATE_MODIFIED)
59  {
60  if (role == Qt::DisplayRole)
61  return QVariant(QDateTime());
62  else
63  return QVariant(QDateTime::currentDateTime());
64  }
65  else
66  return QVariant(QString(""));
67  }
68  else
69  {
70  switch (index.column())
71  {
72  case COL_ROW_NUMBER:
73  return QVariant(index.row() + 1);
74  case COL_DATE_MODIFIED:
75  QDateTime dt(QDateTime::fromString(FROM_UTF8(mpMIRIAMInfo->getModifications()[index.row()]->getDate()), Qt::ISODate));
76 
77  if (dt.isValid())
78  return QVariant(dt);
79  }
80  }
81  }
82 
83  return QVariant();
84 }
85 
86 QVariant CQModifiedDM::headerData(int section, Qt::Orientation orientation,
87  int role) const
88 {
89  if (role != Qt::DisplayRole)
90  return QVariant();
91 
92  if (orientation == Qt::Horizontal)
93  {
94  switch (section)
95  {
96  case COL_ROW_NUMBER:
97  return QVariant(QString("#"));
98  case COL_DATE_MODIFIED:
99  return QVariant(QString("Date and Time Modified"));
100  default:
101  return QVariant();
102  }
103  }
104  else
105  return QString("%1").arg(section + 1);
106 }
107 
108 bool CQModifiedDM::setData(const QModelIndex &index, const QVariant &value,
109  int role)
110 {
111  if (index.isValid() && role == Qt::EditRole)
112  {
113  if (isDefaultRow(index))
114  {
115  if (index.data() != value)
116  insertRow();
117  else
118  return false;
119  }
120 
121  switch (index.column())
122  {
123  case COL_DATE_MODIFIED:
124  mpMIRIAMInfo->getModifications()[index.row()]->setDate(TO_UTF8(value.toDateTime().toString(Qt::ISODate)));
125  break;
126  }
127 
128  emit dataChanged(index, index);
130  return true;
131  }
132 
133  return false;
134 }
135 
136 bool CQModifiedDM::insertRows(int position, int rows, const QModelIndex&)
137 {
138  beginInsertRows(QModelIndex(), position, position + rows - 1);
139 
140  for (int row = 0; row < rows; ++row)
141  {
142  //mpMIRIAMInfo->createModification(TO_UTF8(QDateTime::currentDateTime().toString(Qt::ISODate)));
144  }
145 
146  endInsertRows();
147 
149  return true;
150 }
151 
152 bool CQModifiedDM::removeRows(int position, int rows, const QModelIndex&)
153 {
154  if (rows <= 0)
155  return true;
156 
157  beginRemoveRows(QModelIndex(), position, position + rows - 1);
158 
159  for (int row = 0; row < rows; ++row)
160  {
161  mpMIRIAMInfo->removeModification(position);
162  }
163 
164  endRemoveRows();
165 
167  return true;
168 }
169 
170 bool CQModifiedDM::removeRows(QModelIndexList rows, const QModelIndex&)
171 {
172  if (rows.isEmpty())
173  return false;
174 
175 //Build the list of pointers to items to be deleted
176 //before actually deleting any item.
177  QList < CModification * > pModifieds;
178  QModelIndexList::const_iterator i;
179 
180  for (i = rows.begin(); i != rows.end(); ++i)
181  {
182  if (!isDefaultRow(*i) && mpMIRIAMInfo->getModifications()[(*i).row()])
183  pModifieds.append(mpMIRIAMInfo->getModifications()[(*i).row()]);
184  }
185 
186  bool retVal = false, askEveryItem = true;
187  QMessageBox::StandardButton choice = QMessageBox::NoToAll;
188  QList <CModification *>::const_iterator j;
189 
190  for (j = pModifieds.begin(); j != pModifieds.end(); ++j)
191  {
192  CModification * pModified = *j;
193 
194  size_t delRow =
195  mpMIRIAMInfo->getModifications().CCopasiVector< CModification >::getIndex(pModified);
196 
197  if (delRow != C_INVALID_INDEX)
198  {
199  if (askEveryItem)
200  {
201  QString dateModified = data(this->index((int) delRow, COL_DATE_MODIFIED), Qt::DisplayRole).toString();
202  QString msg = "Do you want to delete Date/Time Modified '";
203 
204  if (!dateModified.isNull())
205  {
206  msg.append(dateModified);
207  }
208 
209  msg.append("'?");
210 
211  choice = CQMessageBox::question(NULL, tr("Confirm Delete"), msg,
212  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll,
213  QMessageBox::No);
214  }
215 
216  if (choice == QMessageBox::NoToAll)
217  {return retVal;}
218  else if (choice == QMessageBox::Yes)
219  {
220  retVal = removeRow((int) delRow);
221  }
222  else if (choice == QMessageBox::YesToAll)
223  {
224  askEveryItem = false;
225  retVal = removeRow((int) delRow);
226  }
227  }
228  }
229 
230  return retVal;
231 }
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
void notifyGUI(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
virtual size_t size() const
QVariant data(const QModelIndex &index, int role) const
#define C_INVALID_INDEX
Definition: copasi.h:222
bool removeModification(int position)
#define C_UNUSED(p)
Definition: copasi.h:220
bool removeRow(int position)
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
#define TOTAL_COLS_MODIFIEDS
Definition: CQModifiedDM.h:27
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
CMIRIAMInfo * mpMIRIAMInfo
Definition: CQModifiedDM.h:47
CQModifiedDM(CMIRIAMInfo *MIRIAMInfo, QObject *parent=0)
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
CModification * createModification(const std::string &objectName)
virtual bool isDefaultRow(const QModelIndex &i) const
#define COL_DATE_MODIFIED
Definition: CQModifiedDM.h:26
const CCopasiVector< CModification > & getModifications() const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
static StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
#define COL_ROW_NUMBER