From 2b9395333ade426cef18a1f70fc5be6ab8c76418 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Fri, 15 Aug 2014 13:11:55 +0200 Subject: [PATCH] fixed error detection and improved error reporting in IdValidator --- apps/opencs/view/world/genericcreator.cpp | 12 +++--------- apps/opencs/view/world/idvalidator.cpp | 24 ++++++++++++++++++++++- apps/opencs/view/world/idvalidator.hpp | 7 +++++++ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/apps/opencs/view/world/genericcreator.cpp b/apps/opencs/view/world/genericcreator.cpp index 7ad5305e3..e905cfef6 100644 --- a/apps/opencs/view/world/genericcreator.cpp +++ b/apps/opencs/view/world/genericcreator.cpp @@ -154,16 +154,10 @@ std::string CSVWorld::GenericCreator::getErrors() const { std::string errors; - std::string id = getId(); - - if (id.empty()) - { - errors = "Missing ID"; - } - else if (mData.hasId (id)) - { + if (!mId->hasAcceptableInput()) + errors = mValidator->getError(); + else if (mData.hasId (getId())) errors = "ID is already in use"; - } return errors; } diff --git a/apps/opencs/view/world/idvalidator.cpp b/apps/opencs/view/world/idvalidator.cpp index 5f4cd2194..7caa20f9b 100644 --- a/apps/opencs/view/world/idvalidator.cpp +++ b/apps/opencs/view/world/idvalidator.cpp @@ -20,6 +20,8 @@ CSVWorld::IdValidator::IdValidator (bool relaxed, QObject *parent) QValidator::State CSVWorld::IdValidator::validate (QString& input, int& pos) const { + mError.clear(); + if (mRelaxed) { if (input.indexOf ('"')!=-1 || input.indexOf ("::")!=-1 || input.indexOf ("#")!=-1) @@ -27,6 +29,12 @@ QValidator::State CSVWorld::IdValidator::validate (QString& input, int& pos) con } else { + if (input.isEmpty()) + { + mError = "Missing ID"; + return QValidator::Intermediate; + } + bool first = true; bool scope = false; bool prevScope = false; @@ -88,8 +96,17 @@ QValidator::State CSVWorld::IdValidator::validate (QString& input, int& pos) con } } + if (scope) + { + mError = "ID ending with incomplete scope operator"; + return QValidator::Intermediate; + } + if (prevScope) - return QValidator::Intermediate; // ending with scope operator + { + mError = "ID ending with scope operator"; + return QValidator::Intermediate; + } } return QValidator::Acceptable; @@ -98,4 +115,9 @@ QValidator::State CSVWorld::IdValidator::validate (QString& input, int& pos) con void CSVWorld::IdValidator::setNamespace (const std::string& namespace_) { mNamespace = Misc::StringUtils::lowerCase (namespace_); +} + +std::string CSVWorld::IdValidator::getError() const +{ + return mError; } \ No newline at end of file diff --git a/apps/opencs/view/world/idvalidator.hpp b/apps/opencs/view/world/idvalidator.hpp index 4b3e6cf6a..a9df9580a 100644 --- a/apps/opencs/view/world/idvalidator.hpp +++ b/apps/opencs/view/world/idvalidator.hpp @@ -11,6 +11,7 @@ namespace CSVWorld { bool mRelaxed; std::string mNamespace; + mutable std::string mError; private: @@ -25,6 +26,12 @@ namespace CSVWorld void setNamespace (const std::string& namespace_); + /// Return a description of the error that resulted in the last call of validate + /// returning QValidator::Intermediate. If the last call to validate returned + /// a different value (or if there was no such call yet), an empty string is + /// returned. + std::string getError() const; + }; }