RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
qvariantdelegate.cpp
Go to the documentation of this file.
1 // *************************************************************************************************
2 //
3 // QPropertyEditor v 0.3
4 //
5 // --------------------------------------
6 // Copyright (C) 2007 Volker Wiendl
7 // Acknowledgements to Roman alias banal from qt-apps.org for the Enum enhancement
8 //
9 //
10 // The QPropertyEditor Library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation version 3 of the License
13 //
14 // The Horde3D Scene Editor is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 //
22 // *************************************************************************************************
23 
24 #include "QVariantDelegate.h"
25 
26 #include "Property.h"
27 
28 #include <QtWidgets/QAbstractItemView>
29 #include <QtCore/QSignalMapper>
30 
31 #include "Editor/EditorWindow.h"
32 
33 
34 QVariantDelegate::QVariantDelegate(QObject* parent) : QItemDelegate(parent)
35 {
36  m_finishedMapper = new QSignalMapper(this);
37  connect(m_finishedMapper, SIGNAL(mapped(QWidget*)), this, SIGNAL(commitData(QWidget*)));
38  connect(m_finishedMapper, SIGNAL(mapped(QWidget*)), this, SIGNAL(closeEditor(QWidget*)));
39  connect(this, SIGNAL(beginPropertyEdit()),
40  EditorWindow::currentInstance, SLOT(onEditPropertyBegin()));
41  connect(this, SIGNAL(finishPropertyEdit()),
42  EditorWindow::currentInstance, SLOT(onEditPropertyEnd()));
43 }
44 
45 
47 {
48 }
49 
50 QWidget *QVariantDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option , const QModelIndex & index ) const
51 {
52  QWidget* editor = 0;
53  Property* p = static_cast<Property*>(index.internalPointer());
54  switch(p->value().type())
55  {
56  case QVariant::Color:
57  case QVariant::Int:
58  case QMetaType::Float:
59  case QVariant::Double:
60  case QVariant::UserType:
61  editor = p->createEditor(parent, option);
62  if (editor)
63  {
64  if (editor->metaObject()->indexOfSignal("editFinished()") != -1)
65  {
66  connect(editor, SIGNAL(editFinished()), m_finishedMapper, SLOT(map()));
67  m_finishedMapper->setMapping(editor, editor);
68  }
69  break; // if no editor could be created take default case
70  }
71  default:
72  editor = QItemDelegate::createEditor(parent, option, index);
73  }
74  parseEditorHints(editor, p->editorHints());
75  // fix needed for boolean Properties
76  if (editor->metaObject()->indexOfSignal("currentIndexChanged(int)") != -1)
77  {
78  connect(editor, SIGNAL(currentIndexChanged(int)), m_finishedMapper, SLOT(map()));
79  m_finishedMapper->setMapping(editor, editor);
80  }
81  // --------
82  connect(editor, SIGNAL(destroyed(QObject*)),
83  this, SLOT(editorDestroyed(QObject*)));
84  emit beginPropertyEdit();
85  return editor;
86 }
87 
88 void QVariantDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
89 {
90  m_finishedMapper->blockSignals(true);
91  QVariant data = index.model()->data(index, Qt::EditRole);
92 
93  switch(data.type())
94  {
95  case QVariant::Color:
96  case QMetaType::Double:
97  case QMetaType::Float:
98  case QVariant::UserType:
99  case QVariant::Int:
100  if (static_cast<Property*>(index.internalPointer())->setEditorData(editor, data)) // if editor couldn't be recognized use default
101  break;
102  default:
103  QItemDelegate::setEditorData(editor, index);
104  break;
105  }
106  m_finishedMapper->blockSignals(false);
107 }
108 
109 void QVariantDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
110 {
111  QVariant data = index.model()->data(index, Qt::EditRole);
112  switch(data.type())
113  {
114  case QVariant::Color:
115  case QMetaType::Double:
116  case QMetaType::Float:
117  case QVariant::UserType:
118  case QVariant::Int:
119  {
120  QVariant data = static_cast<Property*>(index.internalPointer())->editorData(editor);
121  if (data.isValid())
122  {
123  model->setData(index, data , Qt::EditRole);
124  break;
125  }
126  }
127  default:
128  QItemDelegate::setModelData(editor, model, index);
129  break;
130  }
131 }
132 
133 void QVariantDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index ) const
134 {
135  return QItemDelegate::updateEditorGeometry(editor, option, index);
136 }
137 
138 void QVariantDelegate::parseEditorHints(QWidget* editor, const QString& editorHints) const
139 {
140  if (editor && !editorHints.isEmpty())
141  {
142  editor->blockSignals(true);
143  // Parse for property values
144  QRegExp rx("(.*)(=\\s*)(.*)(;{1})");
145  rx.setMinimal(true);
146  int pos = 0;
147  while ((pos = rx.indexIn(editorHints, pos)) != -1)
148  {
149  //qDebug("Setting %s to %s", qPrintable(rx.cap(1)), qPrintable(rx.cap(3)));
150  editor->setProperty(qPrintable(rx.cap(1).trimmed()), rx.cap(3).trimmed());
151  pos += rx.matchedLength();
152  }
153  editor->blockSignals(false);
154  }
155 }
156 
157 void QVariantDelegate::editorDestroyed(QObject*)
158 {
159  emit finishPropertyEdit();
160 }
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
void finishPropertyEdit() const
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option)
Definition: property.cpp:71
QVariantDelegate(QObject *parent=0)
virtual ~QVariantDelegate()
Destructor.
QString editorHints()
Definition: property.h:90
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
QItemDelegate implementation.
static EditorWindow * currentInstance
Definition: editorwindow.h:69
void beginPropertyEdit() const
virtual QVariant value(int role=Qt::UserRole) const
Definition: property.cpp:43
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const