COPASI API  4.16.103
CQPlotDM.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 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 // Copyright (C) 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 #include <QtCore/QString>
12 #include <QtCore/QList>
13 
18 
19 #include "CQPlotDM.h"
20 #include "qtUtilities.h"
21 
22 CQPlotDM::CQPlotDM(QObject *parent)
23  : CQBaseDataModel(parent)
24 
25 {
26 }
27 
28 int CQPlotDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
29 {
30  return (int)(*CCopasiRootContainer::getDatamodelList())[0]->getPlotDefinitionList()->size() + 1;
31 }
32 int CQPlotDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
33 {
34  return TOTAL_COLS_PLOTS;
35 }
36 
37 Qt::ItemFlags CQPlotDM::flags(const QModelIndex &index) const
38 {
39  if (!index.isValid())
40  return Qt::ItemIsEnabled;
41 
42  if (index.column() == COL_NAME_PLOTS)
43  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
44  else if (index.column() == COL_ACTIVE_PLOTS)
45  return QAbstractItemModel::flags(index) | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
46  else
47  return QAbstractItemModel::flags(index);
48 }
49 
50 QVariant CQPlotDM::data(const QModelIndex &index, int role) const
51 {
52  if (!index.isValid())
53  return QVariant();
54 
55  if (index.row() >= rowCount())
56  return QVariant();
57 
58  if (index.column() > 0 && role == Qt::ForegroundRole && !(flags(index) & Qt::ItemIsEditable))
59  return QColor(Qt::darkGray);
60 
61  if (role == Qt::DisplayRole || role == Qt::EditRole)
62  {
63  if (isDefaultRow(index))
64  {
65  switch (index.column())
66  {
67  case COL_ROW_NUMBER:
68  return QVariant(QString(""));
69  case COL_NAME_PLOTS:
70  return QVariant(QString("New Plot"));
71  case COL_ACTIVE_PLOTS:
72 
73  if (role == Qt::DisplayRole)
74  return QVariant();
75  else
76  return QVariant(true);
77 
78  default:
79  return QVariant(QString(""));
80  }
81  }
82  else
83  {
84  const CPlotSpecification *pPS = dynamic_cast<const CPlotSpecification*>((*CCopasiRootContainer::getDatamodelList())[0]->getPlotDefinitionList()->operator[](index.row()));
85 
86  switch (index.column())
87  {
88  case COL_ROW_NUMBER:
89  return QVariant(index.row() + 1);
90 
91  case COL_NAME_PLOTS:
92  return QVariant(QString(FROM_UTF8(pPS->getObjectName())));
93 
94  case COL_CURVES_PLOTS:
95  return QVariant((unsigned int)pPS->getItems().size());
96 
97  case COL_ACTIVE_PLOTS:
98 
99  if (role == Qt::DisplayRole)
100  return QVariant();
101  else
102  return QVariant(pPS->isActive());
103  }
104  }
105  }
106  else if (role == Qt::CheckStateRole && index.column() == COL_ACTIVE_PLOTS)
107  return index.data(Qt::EditRole).toBool() ? QVariant(Qt::Checked) : QVariant(Qt::Unchecked);
108 
109  return QVariant();
110 }
111 
112 QVariant CQPlotDM::headerData(int section, Qt::Orientation orientation,
113  int role) const
114 {
115  if (role != Qt::DisplayRole)
116  return QVariant();
117 
118  if (orientation == Qt::Horizontal)
119  {
120  switch (section)
121  {
122  case COL_ROW_NUMBER:
123  return QVariant(QString("#"));
124  case COL_NAME_PLOTS:
125  return QVariant(QString("Name"));
126  case COL_CURVES_PLOTS:
127  return QVariant(QString("Nr. of Curves"));
128  case COL_ACTIVE_PLOTS:
129  return QVariant(QString("Active"));
130  default:
131  return QVariant();
132  }
133  }
134  else
135  return QString("%1").arg(section + 1);
136 }
137 
138 bool CQPlotDM::setData(const QModelIndex &index, const QVariant &value,
139  int role)
140 {
141  if (index.isValid() && role == Qt::EditRole)
142  {
143  bool changed = false;
144  bool defaultRow = isDefaultRow(index);
145 
146  if (defaultRow)
147  {
148  if (index.data() != value)
149  {
150  insertRow();
151  changed = true;
152  }
153  else
154  return false;
155  }
156 
157  CPlotSpecification *pPS = (CPlotSpecification*)(*CCopasiRootContainer::getDatamodelList())[0]->getPlotDefinitionList()->operator[](index.row());
158 
159  switch (index.column())
160  {
161  case COL_NAME_PLOTS:
162 
163  if (pPS->getObjectName() != TO_UTF8(value.toString()))
164  {
165  pPS->setObjectName(TO_UTF8(value.toString()));
166  changed = true;
167  }
168 
169  break;
170 
171  case COL_ACTIVE_PLOTS:
172 
173  if (pPS->isActive() != value.toBool())
174  {
175  pPS->setActive(value.toBool());
176  changed = true;
177  }
178 
179  break;
180 
181  default:
182  break;
183  }
184 
185  if (defaultRow && this->index(index.row(), COL_NAME_PLOTS).data().toString() == "plot")
186  {
188  changed = true;
189  }
190 
191  if (changed)
192  {
193  emit dataChanged(index, index);
194  emit notifyGUI(ListViews::PLOT, ListViews::CHANGE, pPS->CCopasiParameter::getKey());
195  }
196  }
197  else if (role == Qt::CheckStateRole && index.column() == COL_ACTIVE_PLOTS)
198  {
199  QVariant data = value.toInt() == Qt::Checked ? QVariant(true) : QVariant(false);
200  return setData(index, data, Qt::EditRole);
201  }
202 
203  return true;
204 }
205 
206 bool CQPlotDM::insertRows(int position, int rows, const QModelIndex&)
207 {
208  beginInsertRows(QModelIndex(), position, position + rows - 1);
209 
210  for (int row = 0; row < rows; ++row)
211  {
212  CPlotSpecification *pPS = (*CCopasiRootContainer::getDatamodelList())[0]->getPlotDefinitionList()->createPlotSpec(TO_UTF8(createNewName("plot", COL_NAME_PLOTS)), CPlotItem::plot2d);
213  emit notifyGUI(ListViews::PLOT, ListViews::ADD, pPS->CCopasiParameter::getKey());
214  }
215 
216  endInsertRows();
217 
218  return true;
219 }
220 
221 bool CQPlotDM::removeRows(int position, int rows, const QModelIndex&)
222 {
223  if (rows <= 0)
224  return true;
225 
226  beginRemoveRows(QModelIndex(), position, position + rows - 1);
227 
228  for (int row = 0; row < rows; ++row)
229  {
230  CPlotSpecification* pPS =
231  (CPlotSpecification*)(*CCopasiRootContainer::getDatamodelList())[0]->getPlotDefinitionList()->operator[](position);
232  std::string deletedKey = pPS->CCopasiParameter::getKey();
233  (*CCopasiRootContainer::getDatamodelList())[0]->getPlotDefinitionList()->CCopasiVector< CPlotSpecification >::remove(position);
234  emit notifyGUI(ListViews::PLOT, ListViews::DELETE, deletedKey);
235  }
236 
237  endRemoveRows();
238 
239  return true;
240 }
241 
242 bool CQPlotDM::removeRows(QModelIndexList rows, const QModelIndex&)
243 {
244  if (rows.isEmpty())
245  return false;
246 
248  assert(pDataModel != NULL);
249 
250  if (!pDataModel->getModel())
251  return false;
252 
253 //Build the list of pointers to items to be deleted
254 //before actually deleting any item.
255  QList <CPlotSpecification *> pPSs;
256  QModelIndexList::const_iterator i;
257  CPlotSpecification * pPS = NULL;
258 
259  for (i = rows.begin(); i != rows.end(); ++i)
260  {
261  if (!isDefaultRow(*i) && pDataModel->getPlotDefinitionList()->operator[]((*i).row()))
262  {
263  pPS = pDataModel->getPlotDefinitionList()->operator[]((*i).row());
264  pPSs.append(pPS);
265  }
266  }
267 
268  QList <CPlotSpecification *>::const_iterator j;
269 
270  for (j = pPSs.begin(); j != pPSs.end(); ++j)
271  {
272  pPS = *j;
273 
274  size_t delRow =
275  pDataModel->getPlotDefinitionList()->CCopasiVector< CPlotSpecification >::getIndex(pPS);
276  removeRow((int) delRow);
277  }
278 
279  return true;
280 }
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQPlotDM.cpp:28
#define COL_NAME_PLOTS
Definition: CQPlotDM.h:25
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
#define COL_CURVES_PLOTS
Definition: CQPlotDM.h:26
void notifyGUI(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
const std::string & getObjectName() const
virtual size_t size() const
#define TOTAL_COLS_PLOTS
Definition: CQPlotDM.h:28
void setActive(const bool &active)
const bool & isActive() const
#define C_UNUSED(p)
Definition: copasi.h:220
CQPlotDM(QObject *parent=0)
Definition: CQPlotDM.cpp:22
QVariant data(const QModelIndex &index, int role) const
Definition: CQPlotDM.cpp:50
bool removeRow(int position)
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: CQPlotDM.cpp:37
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
Definition: CQPlotDM.cpp:242
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQPlotDM.cpp:32
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
QString createNewName(const QString name, const int nameCol)
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
Definition: CQPlotDM.cpp:206
const COutputDefinitionVector * getPlotDefinitionList() const
const CCopasiVector< CPlotItem > & getItems() const
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: CQPlotDM.cpp:138
#define COL_ACTIVE_PLOTS
Definition: CQPlotDM.h:27
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: CQPlotDM.cpp:112
virtual bool isDefaultRow(const QModelIndex &i) const
bool setObjectName(const std::string &name)
#define COL_ROW_NUMBER