From 9ab920bd802e2d248f8977e9d8a7879579214fa5 Mon Sep 17 00:00:00 2001
From: Marek Kochanowicz <sirherrbatka@gmail.com>
Date: Mon, 10 Mar 2014 09:37:53 +0100
Subject: [PATCH] display id and other not editable data

---
 apps/opencs/view/world/enumdelegate.cpp |  4 ++++
 apps/opencs/view/world/util.cpp         | 32 ++++++++++++++++++++++++-
 apps/opencs/view/world/util.hpp         |  3 +++
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/apps/opencs/view/world/enumdelegate.cpp b/apps/opencs/view/world/enumdelegate.cpp
index b882e20e6..7f40b966d 100644
--- a/apps/opencs/view/world/enumdelegate.cpp
+++ b/apps/opencs/view/world/enumdelegate.cpp
@@ -65,6 +65,10 @@ void CSVWorld::EnumDelegate::setEditorData (QWidget *editor, const QModelIndex&
         if (!data.isValid())
         {
             data = index.data (Qt::DisplayRole);
+            if (!data.isValid())
+            {
+                return;
+            }
         }
 
         int value = data.toInt();
diff --git a/apps/opencs/view/world/util.cpp b/apps/opencs/view/world/util.cpp
index 97af3b99c..fd35ef3ef 100644
--- a/apps/opencs/view/world/util.cpp
+++ b/apps/opencs/view/world/util.cpp
@@ -4,6 +4,8 @@
 #include <stdexcept>
 
 #include <QUndoStack>
+#include <QMetaProperty>
+#include <QStyledItemDelegate>
 
 #include "../../model/world/commands.hpp"
 
@@ -119,7 +121,7 @@ void CSVWorld::CommandDelegate::setModelData (QWidget *editor, QAbstractItemMode
 QWidget *CSVWorld::CommandDelegate::createEditor (QWidget *parent, const QStyleOptionViewItem& option,
     const QModelIndex& index) const
 {
-    if (!index.data().isValid())
+    if (!(index.data(Qt::EditRole).isValid() or index.data(Qt::DisplayRole).isValid()))
         return 0;
 
     return QStyledItemDelegate::createEditor (parent, option, index);
@@ -140,4 +142,32 @@ bool CSVWorld::CommandDelegate::updateEditorSetting (const QString &settingName,
     const QString &settingValue)
 {
     return false;
+}
+
+void CSVWorld::CommandDelegate::setEditorData (QWidget *editor, const QModelIndex& index) const
+{
+    QVariant v = index.data(Qt::EditRole);
+    if (!v.isValid())
+    {
+        v = index.data(Qt::DisplayRole);
+        if (!v.isValid())
+        {
+            return;
+        }
+    }
+
+    QByteArray n = editor->metaObject()->userProperty().name();
+
+    if (n == "dateTime") {
+        if (editor->inherits("QTimeEdit"))
+            n = "time";
+        else if (editor->inherits("QDateEdit"))
+            n = "date";
+    }
+
+    if (!n.isEmpty()) {
+        if (!v.isValid())
+            v = QVariant(editor->property(n).userType(), (const void *)0);
+        editor->setProperty(n, v);
+    }
 }
\ No newline at end of file
diff --git a/apps/opencs/view/world/util.hpp b/apps/opencs/view/world/util.hpp
index 87f118cd7..0d04dda4e 100644
--- a/apps/opencs/view/world/util.hpp
+++ b/apps/opencs/view/world/util.hpp
@@ -111,6 +111,9 @@ namespace CSVWorld
             virtual bool updateEditorSetting (const QString &settingName, const QString &settingValue);
             ///< \return Does column require update?
 
+            virtual void setEditorData (QWidget *editor, const QModelIndex& index) const;
+
+
         private slots:
 
             virtual void slotUpdateEditorSetting (const QString &settingName, const QString &settingValue) {}