COPASI API  4.16.103
CQSpinBoxDelegate.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 <QtGui/QSpinBox>
12 #include <QtGui/QSortFilterProxyModel>
13 
14 #include "../copasi.h"
15 #include "CQSpinBoxDelegate.h"
16 #include "CQBaseDataModel.h"
17 
19  : QItemDelegate(parent)
20 {}
21 
22 QWidget *CQSpinBoxDelegate::createEditor(QWidget *parent,
23  const QStyleOptionViewItem & C_UNUSED(option),
24  const QModelIndex & index) const
25 {
26  QSpinBox *editor = new QSpinBox(parent);
27  editor->setMinimum(1);
28  unsigned C_INT32 max = 1;
29 
30  const QSortFilterProxyModel *pProxyDM = dynamic_cast<const QSortFilterProxyModel *>(index.model());
31  QModelIndex BDMIndex;
32  const CQBaseDataModel *pBDM;
33 
34  if (pProxyDM)
35  {
36  pBDM = dynamic_cast<const CQBaseDataModel *>(pProxyDM->sourceModel());
37  BDMIndex = pProxyDM->mapToSource(index);
38  }
39  else
40  {
41  pBDM = dynamic_cast<const CQBaseDataModel *>(index.model());
42  BDMIndex = index;
43  }
44 
45  if (pBDM)
46  {
47  if (pBDM->isDefaultRow(BDMIndex))
48  max = pBDM->rowCount();
49  else
50  max = pBDM->rowCount() - 1;
51  }
52 
53  editor->setMaximum(max);
54  return editor;
55 }
56 
57 void CQSpinBoxDelegate::setEditorData(QWidget *editor,
58  const QModelIndex &index) const
59 {
60  QSpinBox *pSpinBox = static_cast<QSpinBox*>(editor);
61  unsigned C_INT32 value = index.model()->data(index, Qt::EditRole).toUInt();
62  pSpinBox->setValue(value);
63 }
64 
65 void CQSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
66  const QModelIndex &index) const
67 {
68  QSpinBox *pSpinBox = static_cast<QSpinBox*>(editor);
69  pSpinBox->interpretText();
70  unsigned int value = pSpinBox->value();
71 
72  model->setData(index, value, Qt::EditRole);
73 }
74 
76  const QStyleOptionViewItem &option, const QModelIndex & C_UNUSED(index)) const
77 {
78  editor->setGeometry(option.rect);
79 }
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
#define C_UNUSED(p)
Definition: copasi.h:220
#define C_INT32
Definition: copasi.h:90
CQSpinBoxDelegate(QObject *parent=0)
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
void setEditorData(QWidget *editor, const QModelIndex &index) const
virtual bool isDefaultRow(const QModelIndex &i) const
#define max(a, b)
Definition: f2c.h:176