COPASI API  4.16.103
CQReferenceDM.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/MIRIAMUI/CQReferenceDM.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 "UI/CQMessageBox.h"
20 #include "UI/qtUtilities.h"
21 #include "CQReferenceDM.h"
22 
24 
25 CQReferenceDM::CQReferenceDM(CMIRIAMInfo* MIRIAMInfo, QObject *parent)
26  : CQBaseDataModel(parent)
27 
28 {
29  mpMIRIAMInfo = MIRIAMInfo;
30 }
31 
32 int CQReferenceDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
33 {
34  return (int) mpMIRIAMInfo->getReferences().size() + 1;
35 }
36 int CQReferenceDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
37 {
38  return TOTAL_COLS_REFERENCES;
39 }
40 
41 QVariant CQReferenceDM::data(const QModelIndex &index, int role) const
42 {
43  if (!index.isValid())
44  return QVariant();
45 
46  if (index.row() >= rowCount())
47  return QVariant();
48 
49  if (role == Qt::DisplayRole || role == Qt::EditRole)
50  {
51  if (isDefaultRow(index))
52  {
53  if (index.column() == COL_RESOURCE_REFERENCE)
54  return QVariant(QString("-- select --"));
55  else if (index.column() == COL_ROW_NUMBER)
56  return QVariant(QString(""));
57  else
58  return QVariant(QString(""));
59  }
60  else
61  {
62  switch (index.column())
63  {
64  case COL_ROW_NUMBER:
65  return QVariant(index.row() + 1);
67  return QVariant(QString(FROM_UTF8(mpMIRIAMInfo->getReferences()[index.row()]->getResource())));
68  case COL_ID_REFERENCE:
69  return QVariant(QString(FROM_UTF8(mpMIRIAMInfo->getReferences()[index.row()]->getId())));
70  case COL_DESCRIPTION:
71  return QVariant(QString(FROM_UTF8(mpMIRIAMInfo->getReferences()[index.row()]->getDescription())));
72  }
73  }
74  }
75 
76  return QVariant();
77 }
78 
79 QVariant CQReferenceDM::headerData(int section, Qt::Orientation orientation,
80  int role) const
81 {
82  if (role != Qt::DisplayRole)
83  return QVariant();
84 
85  if (orientation == Qt::Horizontal)
86  {
87  switch (section)
88  {
89  case COL_ROW_NUMBER:
90  return QVariant(QString("#"));
92  return QVariant(QString(" Resource "));
93  case COL_ID_REFERENCE:
94  return QVariant(QString("ID "));
95  case COL_DESCRIPTION:
96  return QVariant(QString("Description"));
97  default:
98  return QVariant();
99  }
100  }
101  else
102  return QString("%1").arg(section + 1);
103 }
104 
105 bool CQReferenceDM::setData(const QModelIndex &index, const QVariant &value,
106  int role)
107 {
108  if (index.isValid() && role == Qt::EditRole)
109  {
110  if (isDefaultRow(index))
111  {
112  if (index.data() != value)
113  insertRow();
114  else
115  return false;
116  }
117 
118  switch (index.column())
119  {
121  mpMIRIAMInfo->getReferences()[index.row()]->setResource(TO_UTF8(value.toString()));
122  break;
123  case COL_ID_REFERENCE:
124  mpMIRIAMInfo->getReferences()[index.row()]->setId(TO_UTF8(value.toString()));
125  break;
126  case COL_DESCRIPTION:
127  mpMIRIAMInfo->getReferences()[index.row()]->setDescription(TO_UTF8(value.toString()));
128  break;
129  }
130 
131  emit dataChanged(index, index);
133  return true;
134  }
135 
136  return false;
137 }
138 
139 bool CQReferenceDM::insertRows(int position, int rows, const QModelIndex&)
140 {
141  beginInsertRows(QModelIndex(), position, position + rows - 1);
142 
143  for (int row = 0; row < rows; ++row)
144  {
146  }
147 
148  endInsertRows();
149 
151  return true;
152 }
153 
154 bool CQReferenceDM::removeRows(int position, int rows, const QModelIndex&)
155 {
156  if (rows <= 0)
157  return true;
158 
159  beginRemoveRows(QModelIndex(), position, position + rows - 1);
160 
161  for (int row = 0; row < rows; ++row)
162  {
163  mpMIRIAMInfo->removeReference(position);
164  }
165 
166  endRemoveRows();
167 
169  return true;
170 }
171 
172 bool CQReferenceDM::removeRows(QModelIndexList rows, const QModelIndex&)
173 {
174  if (rows.isEmpty())
175  return false;
176 
177 //Build the list of pointers to items to be deleted
178 //before actually deleting any item.
179  QList <CReference *> pReferences;
180  QModelIndexList::const_iterator i;
181 
182  for (i = rows.begin(); i != rows.end(); ++i)
183  {
184  if (!isDefaultRow(*i) && mpMIRIAMInfo->getReferences()[(*i).row()])
185  pReferences.append(mpMIRIAMInfo->getReferences()[(*i).row()]);
186  }
187 
188  bool retVal = false, askEveryItem = true;
189  QMessageBox::StandardButton choice = QMessageBox::NoToAll;
190  QList <CReference *>::const_iterator j;
191 
192  for (j = pReferences.begin(); j != pReferences.end(); ++j)
193  {
194  CReference * pReference = *j;
195 
196  size_t delRow =
197  mpMIRIAMInfo->getReferences().CCopasiVector< CReference >::getIndex(pReference);
198 
199  if (delRow != C_INVALID_INDEX)
200  {
201  if (askEveryItem)
202  {
203  QString resource = data(this->index((int) delRow, COL_RESOURCE_REFERENCE), Qt::DisplayRole).toString();
204  QString Id = data(this->index((int) delRow, COL_ID_REFERENCE), Qt::DisplayRole).toString();
205  QString msg = "Do you want to delete Reference '";
206 
207  if (!resource.isNull())
208  {
209  msg.append(resource);
210  }
211 
212  if (!Id.isNull())
213  {
214  msg.append(":");
215  msg.append(Id);
216  }
217 
218  msg.append("'?");
219 
220  choice = CQMessageBox::question(NULL, tr("Confirm Delete"), msg,
221  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll,
222  QMessageBox::No);
223  }
224 
225  if (choice == QMessageBox::NoToAll)
226  {return retVal;}
227  else if (choice == QMessageBox::Yes)
228  {retVal = removeRow((int) delRow);}
229  else if (choice == QMessageBox::YesToAll)
230  {
231  askEveryItem = false;
232  retVal = removeRow((int) delRow);
233  }
234  }
235  }
236 
237  return true;
238 }
#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
#define TOTAL_COLS_REFERENCES
Definition: CQReferenceDM.h:29
#define C_INVALID_INDEX
Definition: copasi.h:222
#define C_UNUSED(p)
Definition: copasi.h:220
CReference * createReference(const std::string &objectName)
bool removeRow(int position)
bool removeReference(int position)
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
#define COL_RESOURCE_REFERENCE
Definition: CQReferenceDM.h:26
const CCopasiVector< CReference > & getReferences() const
QVariant data(const QModelIndex &index, int role) const
CMIRIAMInfo * mpMIRIAMInfo
Definition: CQReferenceDM.h:49
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
virtual bool isDefaultRow(const QModelIndex &i) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
#define COL_DESCRIPTION
Definition: CQReferenceDM.h:28
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_ID_REFERENCE
Definition: CQReferenceDM.h:27
#define COL_ROW_NUMBER
CQReferenceDM(CMIRIAMInfo *MIRIAMInfo, QObject *parent=0)