COPASI API  4.16.103
CQParameterSetsDM.cpp
Go to the documentation of this file.
1 // Copyright (C) 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 #include <QtCore/QString>
7 
8 #include "CQMessageBox.h"
9 #include "CQParameterSetsDM.h"
10 #include "qtUtilities.h"
11 
14 #include "model/CModel.h"
16 
18  CQBaseDataModel(parent),
19  mpListOfParameterSets(NULL)
20 {}
21 
23 {}
24 
25 int CQParameterSetsDM::rowCount(const QModelIndex & /* parent */) const
26 {
27  if (mpListOfParameterSets == NULL) return 0;
28 
29  return (int) mpListOfParameterSets->size();
30 }
31 
33 {
34  return removeRows(0, rowCount());
35 }
36 
37 int CQParameterSetsDM::columnCount(const QModelIndex & /* parent */) const
38 {
39  return TOTAL_COLS;
40 }
41 
42 Qt::ItemFlags CQParameterSetsDM::flags(const QModelIndex &index) const
43 {
44  if (!index.isValid())
45  return QAbstractItemModel::flags(index);
46 
47  switch (index.column())
48  {
49  case COL_ROW_NUMBER:
50  return QAbstractItemModel::flags(index) & ~Qt::ItemIsEditable;
51  break;
52 
53  case COL_NAME:
54  return QAbstractItemModel::flags(index) & ~Qt::ItemIsEditable;
55  break;
56  }
57 
58  return QAbstractItemModel::flags(index);
59 }
60 
61 QVariant CQParameterSetsDM::data(const QModelIndex &index, int role) const
62 {
63  if (mpListOfParameterSets == NULL) return QVariant();
64 
65  if (!index.isValid()) return QVariant();
66 
67  if (index.row() >= rowCount()) return QVariant();
68 
69  if (role == Qt::DisplayRole || role == Qt::EditRole)
70  {
71  switch (index.column())
72  {
73  case COL_ROW_NUMBER:
74  return QVariant(index.row() + 1);
75  break;
76 
77  case COL_NAME:
78  return QVariant(FROM_UTF8((*mpListOfParameterSets)[index.row()]->getObjectName()));
79  break;
80  }
81  }
82 
83  return QVariant();
84 }
85 
86 QVariant CQParameterSetsDM::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  break;
99 
100  case COL_NAME:
101  return QVariant(QString("Name"));
102  break;
103 
104  default:
105  return QVariant();
106  break;
107  }
108  }
109 
110  return QString("%1").arg(section + 1);
111 }
112 
113 bool CQParameterSetsDM::setData(const QModelIndex &index, const QVariant & /* value */, int role)
114 {
115  if (mpListOfParameterSets == NULL) return false;
116 
117  if (!index.isValid()) return false;
118 
119  if (index.row() >= rowCount()) return false;
120 
121  if (index.isValid() && role == Qt::EditRole)
122  {
123  switch (index.column())
124  {
125  case COL_ROW_NUMBER:
126  return false;
127  break;
128 
129  case COL_NAME:
130  return false;
131  break;
132 
133  default:
134  return false;
135  break;
136  }
137  }
138 
139  return true;
140 }
141 
142 // virtual
143 bool CQParameterSetsDM::isDefaultRow(const QModelIndex & /* index */) const
144 {
145  return false;
146 }
147 
148 bool CQParameterSetsDM::insertRows(int position, int rows, const QModelIndex&)
149 {
150  if (mpListOfParameterSets == NULL) return false;
151 
152  if (position + rows > (int) mpListOfParameterSets->size()) return false;
153 
154  beginInsertRows(QModelIndex(), position, position + rows - 1);
155 
156  for (int row = 0; row < rows; ++row)
157  {
158  emit notifyGUI(ListViews::LAYOUT, ListViews::ADD, (*mpListOfParameterSets)[position + row]->getKey());
159  }
160 
161  endInsertRows();
162 
163  return true;
164 }
165 
166 bool CQParameterSetsDM::removeRows(int position, int rows, const QModelIndex&)
167 {
168  if (rows <= 0) return true;
169 
170  if (mpListOfParameterSets == NULL) return false;
171 
172  beginRemoveRows(QModelIndex(), position, position + rows - 1);
173 
174  std::vector< CModelParameterSet * > DeletedModelParameterSets;
175  DeletedModelParameterSets.resize(rows);
176 
177  std::vector< CModelParameterSet * >::iterator itDeletedModelParameterSet;
178  std::vector< CModelParameterSet * >::iterator endDeletedModelParameterSet = DeletedModelParameterSets.end();
179 
181 
182  for (itDeletedModelParameterSet = DeletedModelParameterSets.begin(); itDeletedModelParameterSet != endDeletedModelParameterSet; ++itDeletedModelParameterSet, ++itRow)
183  {
184  *itDeletedModelParameterSet = *itRow;
185  }
186 
187  for (itDeletedModelParameterSet = DeletedModelParameterSets.begin(); itDeletedModelParameterSet != endDeletedModelParameterSet; ++itDeletedModelParameterSet)
188  {
189  std::string Key = (*itDeletedModelParameterSet)->getKey();
190 
191  pdelete(*itDeletedModelParameterSet);
192 
194  }
195 
196  endRemoveRows();
197 
198  return true;
199 }
200 
201 bool CQParameterSetsDM::removeRows(QModelIndexList rows, const QModelIndex & /* index */)
202 {
203  if (rows.isEmpty()) return false;
204 
205  if (mpListOfParameterSets == NULL) return false;
206 
207  // Build the list of pointers to items to be deleted
208  // before actually deleting any item.
209  QList< CModelParameterSet * > ModelParameterSets;
210  QModelIndexList::const_iterator i;
211 
212  for (i = rows.begin(); i != rows.end(); ++i)
213  {
214  ModelParameterSets.append((*mpListOfParameterSets)[(*i).row()]);
215  }
216 
217  QList< CModelParameterSet * >::const_iterator j;
218 
219  for (j = ModelParameterSets.begin(); j != ModelParameterSets.end(); ++j)
220  {
221  CModelParameterSet * pModelParameterSet = *j;
222 
223  size_t delRow = mpListOfParameterSets->CCopasiVector< CModelParameterSet >::getIndex(pModelParameterSet);
224 
225  if (delRow != C_INVALID_INDEX)
226  {
227  QMessageBox::StandardButton choice =
228  CQMessageBox::confirmDelete(NULL, "layout",
229  FROM_UTF8(pModelParameterSet->getObjectName()),
230  std::set< const CCopasiObject * >());
231 
232  if (choice == QMessageBox::Ok)
233  removeRow((int) delRow);
234  }
235  }
236 
237  return true;
238 }
239 
241 
242 {
243  if (mpListOfParameterSets != pListOfModelParameterSets)
244  {
245  mpListOfParameterSets = pListOfModelParameterSets;
246  reset();
247  }
248 }
virtual QVariant data(const QModelIndex &index, int role) const
#define pdelete(p)
Definition: copasi.h:215
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
virtual bool isDefaultRow(const QModelIndex &i) const
void setListOfModelParameterSets(CCopasiVectorN< CModelParameterSet > *pListOfModelParameterSets)
void notifyGUI(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
const std::string & getObjectName() const
virtual size_t size() const
CCopasiVectorN< CModelParameterSet > * mpListOfParameterSets
iterator begin()
virtual Qt::ItemFlags flags(const QModelIndex &index) const
virtual bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
virtual bool clear()
#define C_INVALID_INDEX
Definition: copasi.h:222
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
CQParameterSetsDM(QObject *parent=0)
bool removeRow(int position)
virtual const std::string & getKey() const
static StandardButton confirmDelete(QWidget *parent, const QString &objectType, const QString &objects, const std::set< const CCopasiObject * > &deletedObjects)
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
#define COL_NAME
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
#define TOTAL_COLS
Definition: CQLayoutsDM.h:22
#define COL_ROW_NUMBER