From 32c697abc65986b18f32690f89e9687ac2d53b51 Mon Sep 17 00:00:00 2001
From: Marc Zinnschlag <marc@zpages.de>
Date: Sun, 28 Jul 2013 14:40:11 +0200
Subject: [PATCH] validate IDs entered by the user

---
 apps/opencs/CMakeLists.txt                |  2 +-
 apps/opencs/view/world/genericcreator.cpp |  3 +++
 apps/opencs/view/world/idvalidator.cpp    | 26 +++++++++++++++++++++++
 apps/opencs/view/world/idvalidator.hpp    | 23 ++++++++++++++++++++
 4 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 apps/opencs/view/world/idvalidator.cpp
 create mode 100644 apps/opencs/view/world/idvalidator.hpp

diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt
index bca37755c5..233c3d0fe9 100644
--- a/apps/opencs/CMakeLists.txt
+++ b/apps/opencs/CMakeLists.txt
@@ -63,7 +63,7 @@ opencs_units (view/world
 opencs_units_noqt (view/world
     dialoguesubview subviews
     enumdelegate vartypedelegate recordstatusdelegate refidtypedelegate datadisplaydelegate
-    scripthighlighter
+    scripthighlighter idvalidator
     )
 
 
diff --git a/apps/opencs/view/world/genericcreator.cpp b/apps/opencs/view/world/genericcreator.cpp
index f4dd53207d..f30ada0923 100644
--- a/apps/opencs/view/world/genericcreator.cpp
+++ b/apps/opencs/view/world/genericcreator.cpp
@@ -10,6 +10,8 @@
 #include "../../model/world/data.hpp"
 #include "../../model/world/idtable.hpp"
 
+#include "idvalidator.hpp"
+
 void CSVWorld::GenericCreator::update()
 {
     mErrors = getErrors();
@@ -27,6 +29,7 @@ CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undo
     layout->setContentsMargins (0, 0, 0, 0);
 
     mId = new QLineEdit;
+    mId->setValidator (new IdValidator (this));
     layout->addWidget (mId, 1);
 
     mCreate = new QPushButton ("Create");
diff --git a/apps/opencs/view/world/idvalidator.cpp b/apps/opencs/view/world/idvalidator.cpp
new file mode 100644
index 0000000000..cf6e5d77ba
--- /dev/null
+++ b/apps/opencs/view/world/idvalidator.cpp
@@ -0,0 +1,26 @@
+
+#include "idvalidator.hpp"
+
+bool CSVWorld::IdValidator::isValid (const QChar& c, bool first) const
+{
+    if (c.isLetter() || c=='_')
+        return true;
+
+    if (!first && (c.isDigit()  || c.isSpace()))
+        return true;
+
+    return false;
+}
+
+CSVWorld::IdValidator::IdValidator (QObject *parent) : QValidator (parent) {}
+
+QValidator::State CSVWorld::IdValidator::validate (QString& input, int& pos) const
+{
+    bool first = true;
+
+    for (QString::const_iterator iter (input.begin()); iter!=input.end(); ++iter, first = false)
+        if (!isValid (*iter, first))
+            return QValidator::Invalid;
+
+    return QValidator::Acceptable;
+}
\ No newline at end of file
diff --git a/apps/opencs/view/world/idvalidator.hpp b/apps/opencs/view/world/idvalidator.hpp
new file mode 100644
index 0000000000..db0ecb27a7
--- /dev/null
+++ b/apps/opencs/view/world/idvalidator.hpp
@@ -0,0 +1,23 @@
+#ifndef CSV_WORLD_IDVALIDATOR_H
+#define CSV_WORLD_IDVALIDATOR_H
+
+#include <QValidator>
+
+namespace CSVWorld
+{
+    class IdValidator : public QValidator
+    {
+        private:
+
+            bool isValid (const QChar& c, bool first) const;
+
+        public:
+
+            IdValidator (QObject *parent = 0);
+
+            virtual State validate (QString& input, int& pos) const;
+
+    };
+}
+
+#endif