COPASI API  4.16.103
CQComboDelegate.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 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/QComboBox>
12 #include <QtGui/QSortFilterProxyModel>
13 
14 #include "CQComboDelegate.h"
15 
16 #include "copasi.h"
17 
18 CQComboDelegate::CQComboDelegate(const QStringList *pComboItems, QObject *parent, bool commitOnSelect):
19  QItemDelegate(parent),
20  mpComboItems(pComboItems),
21  mEditorToIndex(),
22  mRowToItems(),
23  mCommitOnSelect(commitOnSelect)
24 {}
25 
27 {}
28 
29 QWidget *CQComboDelegate::createEditor(QWidget *parent,
30  const QStyleOptionViewItem & C_UNUSED(option),
31  const QModelIndex & index) const
32 {
33  QModelIndex SourceIndex = index;
34  const QAbstractItemModel *pModel = index.model();
35 
36  while (pModel->inherits("QSortFilterProxyModel"))
37  {
38  SourceIndex = static_cast< const QSortFilterProxyModel *>(pModel)->mapToSource(index);
39  pModel = SourceIndex.model();
40  }
41 
42  QComboBox *pEditor = new QComboBox(parent);
43 
44  if (getItems(SourceIndex.row()) != NULL)
45  {
46  pEditor->addItems(*getItems(SourceIndex.row()));
47  }
48 
49  mEditorToIndex[pEditor] = SourceIndex;
50 
51  connect(pEditor, SIGNAL(currentIndexChanged(int)), this, SLOT(slotCurrentIndexChanged(int)));
52  connect(pEditor, SIGNAL(destroyed(QObject *)), this, SLOT(slotEditorDeleted(QObject *)));
53 
54  return pEditor;
55 }
56 
57 void CQComboDelegate::setEditorData(QWidget *editor,
58  const QModelIndex &index) const
59 {
60  QString value = index.model()->data(index, Qt::DisplayRole).toString();
61  QComboBox *comboBox = static_cast<QComboBox*>(editor);
62 
63  comboBox->blockSignals(true);
64  comboBox->setCurrentIndex(comboBox->findText(value));
65  comboBox->blockSignals(false);
66 }
67 
68 void CQComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
69  const QModelIndex &index) const
70 {
71  QComboBox *comboBox = static_cast<QComboBox*>(editor);
72  QVariant value(comboBox->currentText());
73  model->setData(index, value, Qt::EditRole);
74 }
75 
77  const QStyleOptionViewItem &option, const QModelIndex & C_UNUSED(index)) const
78 {
79  editor->setGeometry(option.rect);
80 }
81 
82 void CQComboDelegate::setItems(int row, const QStringList* pComboItems)
83 {
84  mRowToItems[row] = pComboItems;
85 }
86 
87 const QStringList * CQComboDelegate::getItems(int row) const
88 {
89  if (mpComboItems != NULL)
90  {
91  return mpComboItems;
92  }
93 
94  QMap< int, const QStringList * >::const_iterator found = mRowToItems.find(row);
95 
96  if (found != mRowToItems.end()) // OK to return found.value() = NULL
97  {
98  return found.value();
99  }
100 
101  return NULL;
102 }
103 
105 {
106  QComboBox * pEditor = dynamic_cast< QComboBox * >(sender());
107 
108  if (pEditor)
109  {
110  QMap< QWidget * , QModelIndex >::const_iterator found = mEditorToIndex.find(pEditor);
111 
112  if (found != mEditorToIndex.end())
113  {
114  emit currentIndexChanged(found.value().row(), index);
115 
116  if (mCommitOnSelect)
117  commitData(pEditor);
118  }
119  }
120 }
121 
122 void CQComboDelegate::slotEditorDeleted(QObject * pObject)
123 {
124  mEditorToIndex.remove(static_cast< QWidget * >(pObject));
125 }
126 
127 bool
129 {
130  return mCommitOnSelect;
131 }
132 
133 void
135 {
136  mCommitOnSelect = commitOnSelect;
137 }
138 
139 CQIndexComboDelegate::CQIndexComboDelegate(const QStringList *pComboItems, QObject *parent)
140  : CQComboDelegate(pComboItems, parent)
141 {}
142 
144 {}
145 
146 void CQIndexComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
147  const QModelIndex &index) const
148 {
149  QComboBox *comboBox = static_cast<QComboBox*>(editor);
150  QVariant value(comboBox->currentIndex());
151  model->setData(index, value, Qt::EditRole);
152 }
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
CQComboDelegate(const QStringList *pComboItems, QObject *parent=NULL, bool commitOnSelect=true)
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
const QStringList * mpComboItems
QMap< QWidget *, QModelIndex > mEditorToIndex
bool isCommitOnSelect() const
void currentIndexChanged(int, int)
#define C_UNUSED(p)
Definition: copasi.h:220
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
const QStringList * getItems(int row) const
CQIndexComboDelegate(const QStringList *pComboItems, QObject *parent=NULL)
virtual ~CQComboDelegate()
QMap< int, const QStringList * > mRowToItems
void setCommitOnSelect(bool commitOnSelect)
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
void setItems(int row, const QStringList *pComboItems)
void slotEditorDeleted(QObject *pObject)
void slotCurrentIndexChanged(int index)