Ref T215, validator can be restricted to a string list

This commit is contained in:
Klaus Basan
2018-01-02 02:25:08 +01:00
parent 77cd49cb0f
commit ee0d69faf2
2 changed files with 20 additions and 7 deletions

View File

@@ -34,6 +34,19 @@ namespace BlackGui
if (m_optionalValue && input.isEmpty()) { return Acceptable; }
if (input.length() > m_maxLength) { return Invalid; }
if (input.length() < m_minLength) { return Intermediate; }
if (!m_restrictions.isEmpty())
{
bool valid = false;
for (const QString &r : m_restrictions)
{
if (r.startsWith(input))
{
valid = true;
break;
}
}
if (!valid) { return Invalid; }
}
return Acceptable;
}

View File

@@ -13,12 +13,9 @@
#define BLACKGUI_UPPERCASEVALIDATOR_H
#include "blackgui/blackguiexport.h"
#include <QStringList>
#include <QValidator>
class QObject;
class QString;
namespace BlackGui
{
//! Forces uppercase
@@ -35,6 +32,9 @@ namespace BlackGui
//! Constructor
CUpperCaseValidator(bool optionalValue, int minLength, int maxLength, QObject *parent = nullptr);
//! Set restrictions
void setRestrictions(const QStringList &restrictions) { m_restrictions = restrictions; }
//! \copydoc QValidator::validate
virtual State validate(QString &input, int &pos) const override;
@@ -42,10 +42,10 @@ namespace BlackGui
virtual void fixup(QString &input) const override;
private:
bool m_optionalValue = false;
bool m_optionalValue = false; //!< allow empty
int m_minLength = 0;
int m_maxLength = 32678; // standard length
int m_maxLength = 32678; //!< standard length
QStringList m_restrictions; //!< list of allowed strings
};
}
#endif // guard