COPASI API  4.16.103
CQFunctionDM.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 
16 #include "function/CFunctionDB.h"
17 
18 #include "CQMessageBox.h"
19 #include "CQFunctionDM.h"
20 #include "qtUtilities.h"
21 
23  : CQBaseDataModel(parent)
24 
25 {
26 }
27 
28 int CQFunctionDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
29 {
31 }
32 int CQFunctionDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
33 {
34  return TOTAL_COLS_FUNCTIONS;
35 }
36 
37 Qt::ItemFlags CQFunctionDM::flags(const QModelIndex &index) const
38 {
39  if (!index.isValid())
40  return Qt::ItemIsEnabled;
41 
42  if (!isDefaultRow(index) && isFunctionReadOnly(index))
43  return QAbstractItemModel::flags(index);
44 
45  if (index.column() == COL_NAME_FUNCTIONS)
46  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
47  else
48  return QAbstractItemModel::flags(index);
49 }
50 
51 bool CQFunctionDM::isFunctionReadOnly(const QModelIndex &index) const
52 {
54 
55  switch (pFunc->getType())
56  {
59  return true;
60  break;
65  return false;
66  break;
67  }
68 
69  return true;
70 }
71 
72 QVariant CQFunctionDM::data(const QModelIndex &index, int role) const
73 {
74  if (!index.isValid())
75  return QVariant();
76 
77  if (index.row() >= rowCount())
78  return QVariant();
79 
80  if (index.column() > 0 && role == Qt::ForegroundRole && !(flags(index) & Qt::ItemIsEditable))
81  return QColor(Qt::darkGray);
82 
83  if (role == Qt::DisplayRole || role == Qt::EditRole)
84  {
85  if (isDefaultRow(index))
86  {
87  switch (index.column())
88  {
89  case COL_ROW_NUMBER:
90  return QVariant(QString(""));
91  case COL_NAME_FUNCTIONS:
92  return QVariant(QString("New Function"));
93  case COL_TYPE_FUNCTIONS:
94  return QVariant(QString(FROM_UTF8(CEvaluationTree::TypeName[4])));
95  default:
96  return QVariant(QString(""));
97  }
98  }
99  else
100  {
102 
103  if (pFunc == NULL)
104  return QVariant();
105 
106  switch (index.column())
107  {
108  case COL_ROW_NUMBER:
109  return QVariant(index.row() + 1);
110 
111  case COL_NAME_FUNCTIONS:
112  return QVariant(QString(FROM_UTF8(pFunc->getObjectName())));
113 
114  case COL_TYPE_FUNCTIONS:
115  return QVariant(QString(FROM_UTF8(CEvaluationTree::TypeName[pFunc->getType()])));
116 
118  return QVariant(QString(FROM_UTF8(pFunc->getInfix())));
119 
121  return QVariant();
122  }
123  }
124  }
125 
126  return QVariant();
127 }
128 
129 QVariant CQFunctionDM::headerData(int section, Qt::Orientation orientation,
130  int role) const
131 {
132  if (role != Qt::DisplayRole)
133  return QVariant();
134 
135  if (orientation == Qt::Horizontal)
136  {
137  switch (section)
138  {
139  case COL_ROW_NUMBER:
140  return QVariant(QString("#"));
141  case COL_NAME_FUNCTIONS:
142  return QVariant(QString("Name"));
143  case COL_TYPE_FUNCTIONS:
144  return QVariant(QString("Type"));
146  return QVariant(QString("Mathematical Description"));
148  return QVariant(QString("SBML ID"));
149  default:
150  return QVariant();
151  }
152  }
153  else
154  return QString("%1").arg(section + 1);
155 }
156 
157 bool CQFunctionDM::setData(const QModelIndex &index, const QVariant &value,
158  int role)
159 {
160  if (index.isValid() && role == Qt::EditRole)
161  {
162  bool defaultRow = isDefaultRow(index);
163 
164  if (defaultRow)
165  {
166  if (index.data() != value)
167  insertRow();
168  else
169  return false;
170  }
171 
173 
174  if (pFunc == NULL)
175  return false;
176 
177  if (index.column() == COL_NAME_FUNCTIONS)
178  pFunc->setObjectName(TO_UTF8(value.toString()));
179  else if (index.column() == COL_TYPE_FUNCTIONS)
180  {
181  if (index.data() != value)
182  {
183  QString msg;
184  msg = "Type must not be changed for '" + FROM_UTF8(pFunc->getObjectName()) + "'.\n";
185 
187  "Unable to change Function Type",
188  msg,
189  QMessageBox::Ok, QMessageBox::Ok);
190  }
191  }
192  else if (index.column() == COL_MATH_DESC_FUNCTIONS)
193  {
194  if (index.data() != value)
195  {
196  if (!pFunc->setInfix(TO_UTF8(value.toString())))
197  {
198  QString msg;
199  msg = "Incorrect mathematical description'" + FROM_UTF8(pFunc->getObjectName()) + "'.\n";
200 
202  "Unable to change mathematical description",
203  msg,
204  QMessageBox::Ok, QMessageBox::Ok);
205  }
206  }
207  }
208 
209  if (defaultRow && this->index(index.row(), COL_NAME_FUNCTIONS).data().toString() == "function")
211 
212  emit dataChanged(index, index);
214  }
215 
216  return true;
217 }
218 
219 bool CQFunctionDM::insertRows(int position, int rows, const QModelIndex&)
220 {
221  beginInsertRows(QModelIndex(), position, position + rows - 1);
222 
223  for (int row = 0; row < rows; ++row)
224  {
225  CFunction *pFunc;
228  }
229 
230  endInsertRows();
231 
232  return true;
233 }
234 
235 bool CQFunctionDM::removeRows(int position, int rows, const QModelIndex&)
236 {
237  if (rows <= 0)
238  return true;
239 
240  std::vector< std::string > DeletedKeys;
241  DeletedKeys.resize(rows);
242 
243  std::vector< std::string >::iterator itDeletedKey;
244  std::vector< std::string >::iterator endDeletedKey = DeletedKeys.end();
245 
248  int row = 0;
249 
250  for (itDeletedKey = DeletedKeys.begin(), row = 0; itDeletedKey != endDeletedKey; ++itDeletedKey, ++itRow, ++row)
251  {
252  if (isFunctionReadOnly(this->index(position + row, 0)))
253  {
254  *itDeletedKey = "";
255  }
256  else
257  {
258  *itDeletedKey = (*itRow)->getKey();
259  }
260  }
261 
262  for (itDeletedKey = DeletedKeys.begin(), row = 0; itDeletedKey != endDeletedKey; ++itDeletedKey, ++row)
263  {
264  if (*itDeletedKey != "")
265  {
266  beginRemoveRows(QModelIndex(), position + row, position + row);
267 
269  emit notifyGUI(ListViews::FUNCTION, ListViews::DELETE, *itDeletedKey);
270  emit notifyGUI(ListViews::FUNCTION, ListViews::DELETE, ""); //Refresh all as there may be dependencies.
271 
272  endRemoveRows();
273  }
274  }
275 
276  return true;
277 }
278 
279 bool CQFunctionDM::removeRows(QModelIndexList rows, const QModelIndex&)
280 {
281  if (rows.isEmpty())
282  return false;
283 
284  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
286  assert(pDataModel != NULL);
287  CModel * pModel = pDataModel->getModel();
288 
289  if (pModel == NULL)
290  return false;
291 
292 //Build the list of pointers to items to be deleted
293 //before actually deleting any item.
294  QList <CEvaluationTree *> pFunctions;
295  QModelIndexList::const_iterator i;
296 
297  for (i = rows.begin(); i != rows.end(); ++i)
298  {
300  pFunctions.append(CCopasiRootContainer::getFunctionList()->loadedFunctions()[(*i).row()]);
301  }
302 
303  QList <CEvaluationTree *>::const_iterator j;
304 
305  for (j = pFunctions.begin(); j != pFunctions.end(); ++j)
306  {
307  CEvaluationTree * pFunction = *j;
308 
309  size_t delRow =
310  CCopasiRootContainer::getFunctionList()->loadedFunctions().CCopasiVector< CFunction >::getIndex(pFunction);
311 
312  if (delRow != C_INVALID_INDEX)
313  {
314  QMessageBox::StandardButton choice =
315  CQMessageBox::confirmDelete(NULL, "function",
316  FROM_UTF8(pFunction->getObjectName()),
317  pFunction->getDeletedObjects());
318 
319  if (choice == QMessageBox::Ok)
320  removeRow((int) delRow);
321  }
322  }
323 
324  return true;
325 }
CQFunctionDM(QObject *parent=0)
CCopasiVectorN< CFunction > & loadedFunctions()
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
void notifyGUI(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
const std::string & getObjectName() const
virtual size_t size() const
iterator begin()
const CEvaluationTree::Type & getType() const
static StandardButton information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
#define C_INVALID_INDEX
Definition: copasi.h:222
#define C_UNUSED(p)
Definition: copasi.h:220
bool isFunctionReadOnly(const QModelIndex &index) const
const std::string & getKey() const
Definition: CFunction.cpp:58
static const std::string TypeName[]
bool removeRow(int position)
virtual const std::string & getKey() const
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
std::vector< CType * >::const_iterator const_iterator
Definition: CCopasiVector.h:57
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
static StandardButton confirmDelete(QWidget *parent, const QString &objectType, const QString &objects, const std::set< const CCopasiObject * > &deletedObjects)
QVariant data(const QModelIndex &index, int role) const
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
CCopasiObject::DataObjectSet getDeletedObjects() const
QString createNewName(const QString name, const int nameCol)
static CFunctionDB * getFunctionList()
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
bool removeFunction(size_t index)
#define COL_SBML_ID_FUNCTIONS
Definition: CQFunctionDM.h:29
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
#define COL_MATH_DESC_FUNCTIONS
Definition: CQFunctionDM.h:28
The class for handling a chemical kinetic function.
Definition: CFunction.h:29
#define COL_TYPE_FUNCTIONS
Definition: CQFunctionDM.h:27
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
#define TOTAL_COLS_FUNCTIONS
Definition: CQFunctionDM.h:30
Definition: CModel.h:50
#define COL_NAME_FUNCTIONS
Definition: CQFunctionDM.h:26
virtual bool isDefaultRow(const QModelIndex &i) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
bool setObjectName(const std::string &name)
Qt::ItemFlags flags(const QModelIndex &index) const
const std::string & getInfix() const
virtual bool setInfix(const std::string &infix)
#define COL_ROW_NUMBER
bool add(CFunction *pFunction, const bool &adopt)