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