fixed error detection and improved error reporting in IdValidator

This commit is contained in:
Marc Zinnschlag 2014-08-15 13:11:55 +02:00
parent 3486da0fb0
commit 2b9395333a
3 changed files with 33 additions and 10 deletions

View file

@ -154,16 +154,10 @@ std::string CSVWorld::GenericCreator::getErrors() const
{ {
std::string errors; std::string errors;
std::string id = getId(); if (!mId->hasAcceptableInput())
errors = mValidator->getError();
if (id.empty()) else if (mData.hasId (getId()))
{
errors = "Missing ID";
}
else if (mData.hasId (id))
{
errors = "ID is already in use"; errors = "ID is already in use";
}
return errors; return errors;
} }

View file

@ -20,6 +20,8 @@ CSVWorld::IdValidator::IdValidator (bool relaxed, QObject *parent)
QValidator::State CSVWorld::IdValidator::validate (QString& input, int& pos) const QValidator::State CSVWorld::IdValidator::validate (QString& input, int& pos) const
{ {
mError.clear();
if (mRelaxed) if (mRelaxed)
{ {
if (input.indexOf ('"')!=-1 || input.indexOf ("::")!=-1 || input.indexOf ("#")!=-1) 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 else
{ {
if (input.isEmpty())
{
mError = "Missing ID";
return QValidator::Intermediate;
}
bool first = true; bool first = true;
bool scope = false; bool scope = false;
bool prevScope = 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) if (prevScope)
return QValidator::Intermediate; // ending with scope operator {
mError = "ID ending with scope operator";
return QValidator::Intermediate;
}
} }
return QValidator::Acceptable; 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_) void CSVWorld::IdValidator::setNamespace (const std::string& namespace_)
{ {
mNamespace = Misc::StringUtils::lowerCase (namespace_); mNamespace = Misc::StringUtils::lowerCase (namespace_);
}
std::string CSVWorld::IdValidator::getError() const
{
return mError;
} }

View file

@ -11,6 +11,7 @@ namespace CSVWorld
{ {
bool mRelaxed; bool mRelaxed;
std::string mNamespace; std::string mNamespace;
mutable std::string mError;
private: private:
@ -25,6 +26,12 @@ namespace CSVWorld
void setNamespace (const std::string& namespace_); 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;
}; };
} }