The QxtStringValidator class provides validation on a QStringList or a QAbstractItemModel. More...
#include <qxtstringvalidator.h>
Public Member Functions | |
QxtStringValidator (QObject *parent) | |
~QxtStringValidator () | |
virtual void | fixup (QString &input) const |
virtual QValidator::State | validate (QString &input, int &pos) const |
Qt::CaseSensitivity | caseSensitivity () const |
void | setCaseSensitivity (Qt::CaseSensitivity caseSensitivity) |
void | setStartModelIndex (const QModelIndex &index) |
void | setStringList (const QStringList &stringList) |
void | setRecursiveLookup (bool enable) |
void | setWrappingLookup (bool enable) |
void | setLookupModel (QAbstractItemModel *model) |
void | setLookupRole (const int role) |
QModelIndex | startModelIndex () const |
bool | recursiveLookup () const |
bool | wrappingLookup () const |
QAbstractItemModel * | lookupModel () const |
int | lookupRole () const |
Friends | |
class | QxtStringValidatorPrivate |
The QxtStringValidator class provides validation on a QStringList or a QAbstractItemModel.
It provides a String based validation in a stringlist or a custom model. QxtStringValidator uses QAbstractItemModel::match() to validate the input. For a partial match it returns QValidator::Intermediate and for a full match QValidator::Acceptable.
Example usage:
QLineEdit * testLineEdit = new QLineEdit(); QStringList testList; testList << "StringTestString" << "sTrInGCaSe"<< "StringTest"<< "String"<< "Foobar"<< "BarFoo"<< "QxtLib"; QxtStringValidator *validator = new QxtStringValidator(ui.lineEdit); validator->setStringList(testList); //change lookup case sensitivity validator->setCaseSensitivity(Qt::CaseInsensitive); testLineEdit->setValidator(validator);
Definition at line 38 of file qxtstringvalidator.h.
QxtStringValidator::QxtStringValidator | ( | QObject * | parent ) |
Constructs a validator object with a parent object that accepts any string in the stringlist.
Definition at line 107 of file qxtstringvalidator.cpp.
References QXT_INIT_PRIVATE.
: QValidator(parent) { QXT_INIT_PRIVATE(QxtStringValidator); }
QxtStringValidator::~QxtStringValidator | ( | void | ) |
Definition at line 112 of file qxtstringvalidator.cpp.
{}
Qt::CaseSensitivity QxtStringValidator::caseSensitivity | ( | ) | const |
Returns Qt::CaseSensitive if the QxtStringValidator is matched case sensitively; otherwise returns Qt::CaseInsensitive.
Definition at line 240 of file qxtstringvalidator.cpp.
Referenced by setCaseSensitivity().
{
return qxt_d().cs;
}
void QxtStringValidator::fixup | ( | QString & | input ) | const [virtual] |
Fixes up the string input if there is no exact match in the stringlist/model. The default implementation does nothing
Definition at line 119 of file qxtstringvalidator.cpp.
{ qDebug() << "Fixup called"; /*we can not choose whats the correct fixup, if a user needs a fixup he has to do it himself using the first match in the model is just wrong, because thats what QCompleter should do */ QValidator::fixup(input); }
QAbstractItemModel * QxtStringValidator::lookupModel | ( | ) | const |
Returns the used model if it was set by the user.
Definition at line 229 of file qxtstringvalidator.cpp.
{ if (qxt_d().isUserModel) return qxt_d().model; return 0; }
int QxtStringValidator::lookupRole | ( | ) | const |
Returns the item role to be used to query the contents of items for validation.
Definition at line 335 of file qxtstringvalidator.cpp.
{
return qxt_d().lookupRole;
}
bool QxtStringValidator::recursiveLookup | ( | ) | const |
Returns if recursive lookup is enabled.
Definition at line 207 of file qxtstringvalidator.cpp.
{ if (qxt_d().userFlags & Qt::MatchRecursive) return true; return false; }
void QxtStringValidator::setCaseSensitivity | ( | Qt::CaseSensitivity | caseSensitivity ) |
Sets case sensitive matching to cs. If cs is Qt::CaseSensitive, inp matches input but not INPUT. The default is Qt::CaseSensitive.
Definition at line 251 of file qxtstringvalidator.cpp.
References caseSensitivity().
{ qxt_d().cs = caseSensitivity; }
void QxtStringValidator::setLookupModel | ( | QAbstractItemModel * | model ) |
Uses model as new validation model. Note: The validator does not take ownership of the model. sa\ lookupModel()
Definition at line 305 of file qxtstringvalidator.cpp.
{ if (!qxt_d().isUserModel && qxt_d().model) { delete qxt_d().model; qxt_d().model = 0; } qxt_d().lookupRole = Qt::EditRole; qxt_d().isUserModel = true; qxt_d().lookupStartModelIndex = QModelIndex(); qxt_d().model = QPointer<QAbstractItemModel>(model); }
void QxtStringValidator::setLookupRole | ( | const int | role ) |
Sets the item role to be used to query the contents of items for validation. Note: this is only possible if the model was set with setLookupModel() This is set to default Value Qt::EditRole when the model changes
Definition at line 325 of file qxtstringvalidator.cpp.
{
if (qxt_d().isUserModel)
qxt_d().lookupRole = role;
}
void QxtStringValidator::setRecursiveLookup | ( | bool | enable ) |
If enabled QxtStringValidator searches the entire hierarchy of the model. This is disabled by default.
Definition at line 277 of file qxtstringvalidator.cpp.
{ if (enable) qxt_d().userFlags |= Qt::MatchRecursive; else qxt_d().userFlags &= ~Qt::MatchRecursive; }
void QxtStringValidator::setStartModelIndex | ( | const QModelIndex & | index ) |
Sets the index the search should start at The default is QModelIndex(0,0). Note: this is set to default when the model changes. Changing the startModelIndex is only possible if the validator uses a userdefined model and the modelindex comes from the used model
Definition at line 264 of file qxtstringvalidator.cpp.
{ if (index.model() == qxt_d().model) qxt_d().lookupStartModelIndex = index; else qWarning() << "ModelIndex from different model. Ignoring."; }
void QxtStringValidator::setStringList | ( | const QStringList & | stringList ) |
uses stringlist as new validation list if a model was set before it is not deleted
Definition at line 132 of file qxtstringvalidator.cpp.
{ //delete model only if it is a model created by us if (qxt_d().model && !qxt_d().isUserModel) { delete qxt_d().model; qxt_d().model = 0; } qxt_d().isUserModel = false; qxt_d().lookupStartModelIndex = QModelIndex(); qxt_d().lookupRole = Qt::EditRole; qxt_d().model = new QStringListModel(stringList, this); }
void QxtStringValidator::setWrappingLookup | ( | bool | enable ) |
If enabled QxtStringValidator performs a search that wraps around, so that when the search reaches the last item in the model, it begins again at the first item and continues until all items have been examined. This is set by default.
Definition at line 292 of file qxtstringvalidator.cpp.
{ if (enable) qxt_d().userFlags |= Qt::MatchWrap; else qxt_d().userFlags &= ~Qt::MatchWrap; }
QModelIndex QxtStringValidator::startModelIndex | ( | ) | const |
Returns the startModelIndex. Note: The return value will only we valid if the user has set the model with setLookupModel().
Definition at line 191 of file qxtstringvalidator.cpp.
{ if (qxt_d().isUserModel && qxt_d().model) { if (qxt_d().lookupStartModelIndex.isValid()) return qxt_d().lookupStartModelIndex; else return qxt_d().model->index(0, 0); } return QModelIndex(); }
QValidator::State QxtStringValidator::validate | ( | QString & | input, |
int & | pos | ||
) | const [virtual] |
Returns Acceptable if the string input matches a item in the stringlist. Returns Intermediate if the string input matches a item in the stringlist partial or if input is empty. Returns Invalid otherwise.
Note: A partial match means the beginning of the strings are matching: qxtL matches qxtLib but not testqxtLib
Definition at line 155 of file qxtstringvalidator.cpp.
{ Q_UNUSED(pos); // no model or a empty model has only Acceptable values (like no validator was set) if (!qxt_d().model) return QValidator::Acceptable; if (qxt_d().model->rowCount() == 0) return QValidator::Acceptable; if (input.isEmpty()) return QValidator::Intermediate; if (qxt_d().lookupExactMatch(input).isValid()) { qDebug() << input << " is QValidator::Acceptable"; return QValidator::Acceptable; } if (qxt_d().lookupPartialMatch(input).isValid()) { qDebug() << input << " is QValidator::Intermediate"; return QValidator::Intermediate; } qDebug() << input << " is QValidator::Invalid"; return QValidator::Invalid; }
bool QxtStringValidator::wrappingLookup | ( | ) | const |
Returns if wrapping lookup is enabled.
Definition at line 218 of file qxtstringvalidator.cpp.
{ if (qxt_d().userFlags & Qt::MatchWrap) return true; return false; }
friend class QxtStringValidatorPrivate [friend] |
Definition at line 65 of file qxtstringvalidator.h.