COPASI API  4.16.103
CQCreatorDM.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/MIRIAMUI/CQCreatorDM.cpp,v $
3 // $Revision: 1.11 $
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 "CQCreatorDM.h"
22 
24 
25 CQCreatorDM::CQCreatorDM(CMIRIAMInfo* MIRIAMInfo, QObject *parent)
26  : CQBaseDataModel(parent)
27 
28 {
29  mpMIRIAMInfo = MIRIAMInfo;
30 }
31 
32 int CQCreatorDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
33 {
34  return (int) mpMIRIAMInfo->getCreators().size() + 1;
35 }
36 int CQCreatorDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
37 {
38  return TOTAL_COLS_CREATORS;
39 }
40 
41 QVariant CQCreatorDM::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_ROW_NUMBER)
54  return QVariant(QString(""));
55  else
56  return QVariant(QString(""));
57  }
58  else
59  {
60  switch (index.column())
61  {
62  case COL_ROW_NUMBER:
63  return QVariant(index.row() + 1);
64  case COL_FAMILY_NAME:
65  return QVariant(QString(FROM_UTF8(mpMIRIAMInfo->getCreators()[index.row()]->getFamilyName())));
66  case COL_GIVEN_NAME:
67  return QVariant(QString(FROM_UTF8(mpMIRIAMInfo->getCreators()[index.row()]->getGivenName())));
68  case COL_EMAIL:
69  return QVariant(QString(FROM_UTF8(mpMIRIAMInfo->getCreators()[index.row()]->getEmail())));
70  case COL_ORG:
71  return QVariant(QString(FROM_UTF8(mpMIRIAMInfo->getCreators()[index.row()]->getORG())));
72  }
73  }
74  }
75 
76  return QVariant();
77 }
78 
79 QVariant CQCreatorDM::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("#"));
91  case COL_FAMILY_NAME:
92  return QVariant(QString("Family Name"));
93  case COL_GIVEN_NAME:
94  return QVariant(QString("Given Name"));
95  case COL_EMAIL:
96  return QVariant(QString("Email"));
97  case COL_ORG:
98  return QVariant(QString("Organization"));
99  default:
100  return QVariant();
101  }
102  }
103  else
104  return QString("%1").arg(section + 1);
105 }
106 
107 bool CQCreatorDM::setData(const QModelIndex &index, const QVariant &value,
108  int role)
109 {
110  if (index.isValid() && role == Qt::EditRole)
111  {
112  if (isDefaultRow(index))
113  {
114  if (index.data() != value)
115  insertRow();
116  else
117  return false;
118  }
119 
120  switch (index.column())
121  {
122  case COL_FAMILY_NAME:
123  mpMIRIAMInfo->getCreators()[index.row()]->setFamilyName(TO_UTF8(value.toString()));
124  break;
125  case COL_GIVEN_NAME:
126  mpMIRIAMInfo->getCreators()[index.row()]->setGivenName(TO_UTF8(value.toString()));
127  break;
128  case COL_EMAIL:
129  mpMIRIAMInfo->getCreators()[index.row()]->setEmail(TO_UTF8(value.toString()));
130  break;
131  case COL_ORG:
132  mpMIRIAMInfo->getCreators()[index.row()]->setORG(TO_UTF8(value.toString()));
133  break;
134  }
135 
136  emit dataChanged(index, index);
138  return true;
139  }
140 
141  return false;
142 }
143 
144 bool CQCreatorDM::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  {
151  }
152 
153  endInsertRows();
154 
156  return true;
157 }
158 
159 bool CQCreatorDM::removeRows(int position, int rows, const QModelIndex&)
160 {
161  if (rows <= 0)
162  return true;
163 
164  beginRemoveRows(QModelIndex(), position, position + rows - 1);
165 
166  for (int row = 0; row < rows; ++row)
167  {
168  mpMIRIAMInfo->removeCreator(position);
169  }
170 
171  endRemoveRows();
172 
174  return true;
175 }
176 
177 bool CQCreatorDM::removeRows(QModelIndexList rows, const QModelIndex&)
178 {
179  if (rows.isEmpty())
180  return false;
181 
182 //Build the list of pointers to items to be deleted
183 //before actually deleting any item.
184  QList <CCreator *> pCreators;
185  QModelIndexList::const_iterator i;
186 
187  for (i = rows.begin(); i != rows.end(); ++i)
188  {
189  if (!isDefaultRow(*i) && mpMIRIAMInfo->getCreators()[(*i).row()])
190  pCreators.append(mpMIRIAMInfo->getCreators()[(*i).row()]);
191  }
192 
193  bool retVal = false, askEveryItem = true;
194  QMessageBox::StandardButton choice = QMessageBox::NoToAll;
195  QList <CCreator *>::const_iterator j;
196 
197  for (j = pCreators.begin(); j != pCreators.end(); ++j)
198  {
199  CCreator * pCreator = *j;
200 
201  size_t delRow =
202  mpMIRIAMInfo->getCreators().CCopasiVector< CCreator >::getIndex(pCreator);
203 
204  if (delRow != C_INVALID_INDEX)
205  {
206  if (askEveryItem)
207  {
208  QString givenName = data(this->index((int) delRow, COL_GIVEN_NAME), Qt::DisplayRole).toString();
209  QString familyName = data(this->index((int) delRow, COL_FAMILY_NAME), Qt::DisplayRole).toString();
210  QString msg = "Do you want to delete author '";
211 
212  if (!givenName.isNull())
213  {
214  msg.append(givenName);
215  }
216 
217  if (!familyName.isNull())
218  {
219  msg.append(" ");
220  msg.append(familyName);
221  }
222 
223  msg.append("'?");
224 
225  choice = CQMessageBox::question(NULL, tr("Confirm Delete"), msg,
226  QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll,
227  QMessageBox::No);
228  }
229 
230  if (choice == QMessageBox::NoToAll)
231  {return retVal;}
232  else if (choice == QMessageBox::Yes)
233  {retVal = removeRow((int) delRow);}
234  else if (choice == QMessageBox::YesToAll)
235  {
236  askEveryItem = false;
237  retVal = removeRow((int) delRow);
238  }
239  }
240  }
241 
242  return true;
243 }
const CCopasiVector< CCreator > & getCreators() const
#define TOTAL_COLS_CREATORS
Definition: CQCreatorDM.h:30
#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 COL_FAMILY_NAME
Definition: CQCreatorDM.h:26
#define C_INVALID_INDEX
Definition: copasi.h:222
#define C_UNUSED(p)
Definition: copasi.h:220
#define COL_EMAIL
Definition: CQCreatorDM.h:28
CQCreatorDM(CMIRIAMInfo *MIRIAMInfo, QObject *parent=0)
Definition: CQCreatorDM.cpp:25
CMIRIAMInfo * mpMIRIAMInfo
Definition: CQCreatorDM.h:50
bool removeRow(int position)
QVariant data(const QModelIndex &index, int role) const
Definition: CQCreatorDM.cpp:41
CCreator * createCreator(const std::string &objectName)
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
bool removeCreator(int position)
#define COL_GIVEN_NAME
Definition: CQCreatorDM.h:27
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: CQCreatorDM.cpp:79
virtual bool isDefaultRow(const QModelIndex &i) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQCreatorDM.cpp:32
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQCreatorDM.cpp:36
static StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
#define COL_ORG
Definition: CQCreatorDM.h:29
#define COL_ROW_NUMBER
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())