COPASI API  4.16.103
CQPushButtonDelegate.cpp
Go to the documentation of this file.
1 // Copyright (C) 2011 - 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 #include <QtGui/QPushButton>
7 #include <QtGui/QToolButton>
8 #include <QtGui/QSortFilterProxyModel>
9 
10 #include "CQPushButtonDelegate.h"
11 
12 #include <iostream>
13 
15  QObject *parent):
16  QStyledItemDelegate(parent),
17  mIcon(),
18  mText(),
19  mButtonType(buttonType),
20  mEditorToIndex()
21 {}
22 
24  const QString & text,
25  const ButtonType & buttonType,
26  QObject *parent):
27  QStyledItemDelegate(parent),
28  mIcon(icon),
29  mText(text),
30  mButtonType(buttonType),
31  mEditorToIndex()
32 {}
33 
35 {}
36 
37 QWidget * CQPushButtonDelegate::createEditor(QWidget * parent,
38  const QStyleOptionViewItem & /* option */,
39  const QModelIndex & index) const
40 {
41  QModelIndex SourceIndex = index;
42  const QAbstractItemModel *pModel = index.model();
43 
44  while (pModel->inherits("QSortFilterProxyModel"))
45  {
46  SourceIndex = static_cast< const QSortFilterProxyModel *>(pModel)->mapToSource(index);
47  pModel = SourceIndex.model();
48  }
49 
50  QAbstractButton * pEditor;
51  QVariant Data;
52 
53  Data = SourceIndex.model()->data(SourceIndex, Qt::UserRole);
54 
55  if (Data.type() == QVariant::Icon)
56  {
57  mIcon = Data.value< QIcon >();
58  }
59 
60  switch (mButtonType)
61  {
62  case PushButton:
63  pEditor = new QPushButton(mIcon, mText, parent);
64  break;
65 
66  case ToolButton:
67  pEditor = new QToolButton(parent);
68 
69  if (mIcon.isNull())
70  {
71  Data = SourceIndex.model()->data(SourceIndex, Qt::DisplayRole);
72 
73  if (Data.type() == QVariant::String)
74  {
75  mText = Data.toString();
76  }
77 
78  pEditor->setText(mText);
79  }
80  else
81  {
82  pEditor->setIcon(mIcon);
83  }
84 
85  break;
86  }
87 
88  mEditorToIndex[pEditor] = SourceIndex;
89 
90  connect(pEditor, SIGNAL(clicked(bool)), this, SLOT(slotButtonClicked()));
91  connect(pEditor, SIGNAL(destroyed(QObject *)), this, SLOT(slotEditorDeleted(QObject *)));
92 
93  return pEditor;
94 }
95 
96 void CQPushButtonDelegate::setEditorData(QWidget * pEditor, const QModelIndex & index) const
97 {
98  QString value = index.model()->data(index, Qt::EditRole).toString();
99  QPushButton * pButton = static_cast< QPushButton * >(pEditor);
100 
101  if (pButton->icon().isNull())
102  {
103  pButton->setText(value);
104  }
105 }
106 
107 // We do not do anything since the signal clicked of the editor is reemited with the correct row index.
108 void CQPushButtonDelegate::setModelData(QWidget * /* pEditor */,
109  QAbstractItemModel * /* pModel */,
110  const QModelIndex & /* index */) const
111 {}
112 
114  const QStyleOptionViewItem & option,
115  const QModelIndex & /* index */) const
116 {
117  pEditor->setGeometry(option.rect);
118 }
119 
121 {
122  QAbstractButton * pEditor = dynamic_cast< QAbstractButton *>(sender());
123 
124  if (pEditor)
125  {
126  QMap< QAbstractButton *, QModelIndex >::const_iterator found = mEditorToIndex.find(pEditor);
127 
128  if (found != mEditorToIndex.end())
129  {
130  emit clicked(found.value());
131  }
132  }
133 }
134 
135 void CQPushButtonDelegate::slotEditorDeleted(QObject * /* pObject */)
136 {
137  QAbstractButton * pEditor = dynamic_cast< QAbstractButton *>(sender());
138 
139  if (pEditor)
140  {
141  mEditorToIndex.remove(pEditor);
142  }
143 }
virtual void updateEditorGeometry(QWidget *pEditor, const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
void slotEditorDeleted(QObject *pObject)
CQPushButtonDelegate(const ButtonType &buttonType, QObject *parent=0)
virtual void setEditorData(QWidget *pEditor, const QModelIndex &index) const
QMap< QAbstractButton *, QModelIndex > mEditorToIndex
virtual void setModelData(QWidget *pEditor, QAbstractItemModel *pModel, const QModelIndex &index) const
void clicked(const QModelIndex &)