COPASI API  4.16.103
CQBiologicalDescriptionDM.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/MIRIAMUI/CQBiologicalDescriptionDM.cpp,v $
3 // $Revision: 1.9 $
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 
20 
21 #include "UI/CQMessageBox.h"
22 #include "UI/qtUtilities.h"
23 
25 
27  : CQBaseDataModel(parent)
28 
29 {
30  mpMIRIAMInfo = MIRIAMInfo;
31 }
32 
33 int CQBiologicalDescriptionDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
34 {
35  return (int) mpMIRIAMInfo->getBiologicalDescriptions().size() + 1;
36 }
37 int CQBiologicalDescriptionDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
38 {
40 }
41 
42 QVariant CQBiologicalDescriptionDM::data(const QModelIndex &index, int role) const
43 {
44  if (!index.isValid())
45  return QVariant();
46 
47  if (index.row() >= rowCount())
48  return QVariant();
49 
50  if (role == Qt::DisplayRole || role == Qt::EditRole)
51  {
52  if (isDefaultRow(index))
53  {
54  if (index.column() == COL_RELATIONSHIP || index.column() == COL_RESOURCE_BD)
55  return QVariant(QString("-- select --"));
56  else if (index.column() == COL_ROW_NUMBER)
57  return QVariant(QString(""));
58  else
59  return QVariant(QString(""));
60  }
61  else
62  {
63  switch (index.column())
64  {
65  case COL_ROW_NUMBER:
66  return QVariant(index.row() + 1);
67  case COL_RELATIONSHIP:
68  return QVariant(QString(FROM_UTF8(mpMIRIAMInfo->getBiologicalDescriptions()[index.row()]->getPredicate())));
69  case COL_RESOURCE_BD:
70  return QVariant(QString(FROM_UTF8(mpMIRIAMInfo->getBiologicalDescriptions()[index.row()]->getResource())));
71  case COL_ID_BD:
72  return QVariant(QString(FROM_UTF8(mpMIRIAMInfo->getBiologicalDescriptions()[index.row()]->getId())));
73  }
74  }
75  }
76 
77  return QVariant();
78 }
79 
80 QVariant CQBiologicalDescriptionDM::headerData(int section, Qt::Orientation orientation,
81  int role) const
82 {
83  if (role != Qt::DisplayRole)
84  return QVariant();
85 
86  if (orientation == Qt::Horizontal)
87  {
88  switch (section)
89  {
90  case COL_ROW_NUMBER:
91  return QVariant(QString("#"));
92  case COL_RELATIONSHIP:
93  return QVariant(QString("Relationship"));
94  case COL_RESOURCE_BD:
95  return QVariant(QString(" Resource "));
96  case COL_ID_BD:
97  return QVariant(QString("ID"));
98  default:
99  return QVariant();
100  }
101  }
102  else
103  return QString("%1").arg(section + 1);
104 }
105 
106 bool CQBiologicalDescriptionDM::setData(const QModelIndex &index, const QVariant &value,
107  int role)
108 {
109  if (index.isValid() && role == Qt::EditRole)
110  {
111  if (isDefaultRow(index))
112  {
113  if (index.data() != value)
114  insertRow();
115  else
116  return false;
117  }
118 
119  switch (index.column())
120  {
121  case COL_RELATIONSHIP:
122  mpMIRIAMInfo->getBiologicalDescriptions()[index.row()]->setPredicate(TO_UTF8(value.toString()));
123  break;
124  case COL_RESOURCE_BD:
125  mpMIRIAMInfo->getBiologicalDescriptions()[index.row()]->setResource(TO_UTF8(value.toString()));
126  break;
127  case COL_ID_BD:
128  mpMIRIAMInfo->getBiologicalDescriptions()[index.row()]->setId(TO_UTF8(value.toString()));
129  break;
130  }
131 
132  emit dataChanged(index, index);
134  return true;
135  }
136 
137  return false;
138 }
139 
140 bool CQBiologicalDescriptionDM::insertRows(int position, int rows, const QModelIndex&)
141 {
142  beginInsertRows(QModelIndex(), position, position + rows - 1);
143 
144  for (int row = 0; row < rows; ++row)
145  {
147  }
148 
149  endInsertRows();
151  return true;
152 }
153 
154 bool CQBiologicalDescriptionDM::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  {
164  }
165 
166  endRemoveRows();
167 
169  return true;
170 }
171 
172 bool CQBiologicalDescriptionDM::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 <CBiologicalDescription *> pBiologicalDescriptions;
180  QModelIndexList::const_iterator i;
181 
182  for (i = rows.begin(); i != rows.end(); ++i)
183  {
184  if (!isDefaultRow(*i) && mpMIRIAMInfo->getBiologicalDescriptions()[(*i).row()])
185  pBiologicalDescriptions.append(mpMIRIAMInfo->getBiologicalDescriptions()[(*i).row()]);
186  }
187 
188  bool retVal = false, askEveryItem = true;
189  QMessageBox::StandardButton choice = QMessageBox::NoToAll;
190  QList <CBiologicalDescription *>::const_iterator j;
191 
192  for (j = pBiologicalDescriptions.begin(); j != pBiologicalDescriptions.end(); ++j)
193  {
194  CBiologicalDescription * pBiologicalDescription = *j;
195 
196  size_t delRow =
197  mpMIRIAMInfo->getBiologicalDescriptions().CCopasiVector< CBiologicalDescription >::getIndex(pBiologicalDescription);
198 
199  if (delRow != C_INVALID_INDEX)
200  {
201  if (askEveryItem)
202  {
203 
204  QString resource = data(this->index((int) delRow, COL_RESOURCE_BD), Qt::DisplayRole).toString();
205  QString Id = data(this->index((int) delRow, COL_ID_BD), Qt::DisplayRole).toString();
206 
207  QString msg = "Do you want to delete Description '";
208 
209  if (!resource.isNull())
210  {
211  msg.append(resource);
212  }
213 
214  if (!Id.isNull())
215  {
216  msg.append(":");
217  msg.append(Id);
218  }
219 
220  msg.append("'?");
221 
222  choice = CQMessageBox::question(NULL, tr("Confirm Delete"), msg,
223  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll,
224  QMessageBox::No);
225  }
226 
227  if (choice == QMessageBox::NoToAll)
228  {return retVal;}
229  else if (choice == QMessageBox::Yes)
230  {retVal = removeRow((int) delRow);}
231  else if (choice == QMessageBox::YesToAll)
232  {
233  askEveryItem = false;
234  retVal = removeRow((int) delRow);
235  }
236  }
237  }
238 
239  return true;
240 }
#define COL_ID_BD
#define COL_RESOURCE_BD
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
void notifyGUI(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
QVariant data(const QModelIndex &index, int role) const
const CCopasiVector< CBiologicalDescription > & getBiologicalDescriptions() const
virtual size_t size() const
#define TOTAL_COLS_BIOLOGICAL_DESCRIPTIONS
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
#define C_INVALID_INDEX
Definition: copasi.h:222
#define C_UNUSED(p)
Definition: copasi.h:220
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
bool removeRow(int position)
#define COL_RELATIONSHIP
CQBiologicalDescriptionDM(CMIRIAMInfo *MIRIAMInfo, QObject *parent=0)
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
bool removeBiologicalDescription(int position)
virtual bool isDefaultRow(const QModelIndex &i) const
CBiologicalDescription * createBiologicalDescription()
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
static StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
#define COL_ROW_NUMBER