summaryrefslogtreecommitdiff
path: root/utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.cpp')
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.cpp b/utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.cpp
new file mode 100644
index 0000000000..ebda9b2c31
--- /dev/null
+++ b/utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.cpp
@@ -0,0 +1,105 @@
1// *************************************************************************************************
2//
3// QPropertyEditor v 0.1
4//
5// --------------------------------------
6// Copyright (C) 2007 Volker Wiendl
7//
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or any later version.
13//
14// This library 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 GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// *************************************************************************************************
24
25#include "QVariantDelegate.h"
26
27#include "Property.h"
28
29#include <Qt/qabstractitemview.h>
30
31
32QVariantDelegate::QVariantDelegate(QObject* parent) : QItemDelegate(parent) {}
33
34
35QVariantDelegate::~QVariantDelegate() {}
36
37QWidget *QVariantDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option , const QModelIndex & index ) const {
38 QWidget* editor = 0;
39 Property* p = static_cast<Property*>(index.internalPointer());
40 switch (p->value().type()) {
41 case QVariant::Color:
42 case QVariant::Int:
43 case QMetaType::Float:
44 case QVariant::Double:
45 case QVariant::UserType:
46 editor = p->createEditor(parent, option);
47 if (editor) break; // if no editor could be created take default case
48 default:
49 editor = QItemDelegate::createEditor(parent, option, index);
50 }
51 parseEditorHints(editor, p->editorHints());
52 return editor;
53}
54
55void QVariantDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
56 QVariant data = index.model()->data(index, Qt::EditRole);
57 switch (data.type()) {
58 case QVariant::Color:
59 case QMetaType::Double:
60 case QMetaType::Float:
61 case QVariant::UserType:
62 if (static_cast<Property*>(index.internalPointer())->setEditorData(editor, data)) // if editor couldn't be recognized use default
63 break;
64 default:
65 QItemDelegate::setEditorData(editor, index);
66 break;
67 }
68}
69
70void QVariantDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
71 QVariant data = index.model()->data(index, Qt::EditRole);
72 switch (data.type()) {
73 case QVariant::Color:
74 case QMetaType::Double:
75 case QMetaType::Float:
76 case QVariant::UserType: {
77 QVariant data = static_cast<Property*>(index.internalPointer())->editorData(editor);
78 if (data.isValid()) {
79 model->setData(index, data , Qt::EditRole);
80 break;
81 }
82 }
83 default:
84 QItemDelegate::setModelData(editor, model, index);
85 break;
86 }
87}
88
89void QVariantDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index ) const {
90 return QItemDelegate::updateEditorGeometry(editor, option, index);
91}
92
93void QVariantDelegate::parseEditorHints(QWidget* editor, const QString& editorHints) const {
94 if (editor && !editorHints.isEmpty()) {
95 // Parse for property values
96 QRegExp rx("(.*)(=\\s*)(.*)(;{1})");
97 rx.setMinimal(true);
98 int pos = 0;
99 while ((pos = rx.indexIn(editorHints, pos)) != -1) {
100 qDebug("Setting %s to %s", qPrintable(rx.cap(1)), qPrintable(rx.cap(3)));
101 editor->setProperty(qPrintable(rx.cap(1).trimmed()), rx.cap(3).trimmed());
102 pos += rx.matchedLength();
103 }
104 }
105}