1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtGui module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
25 #include "qxtstringvalidator.h"
26 #include "qxtstringvalidator_p.h"
29 #include <QAbstractItemModel>
30 #include <QStringListModel>
33 QxtStringValidatorPrivate::QxtStringValidatorPrivate() : isUserModel(false)
35 , cs(Qt::CaseSensitive)
36 , lookupRole(Qt::EditRole)
37 , userFlags(Qt::MatchWrap)
38 , lookupStartModelIndex(QModelIndex())
41 QModelIndex QxtStringValidatorPrivate::lookupPartialMatch(const QString &value) const
43 Qt::MatchFlags matchFlags = Qt::MatchStartsWith| userFlags;
44 if (cs == Qt::CaseSensitive)
45 matchFlags |= Qt::MatchCaseSensitive;
47 return lookup(value,matchFlags);
50 QModelIndex QxtStringValidatorPrivate::lookupExactMatch(const QString &value) const
52 Qt::MatchFlags matchFlags = Qt::MatchFixedString | userFlags;
53 if (cs == Qt::CaseSensitive)
54 matchFlags |= Qt::MatchCaseSensitive;
56 return lookup(value,matchFlags);
59 QModelIndex QxtStringValidatorPrivate::lookup(const QString &value,const Qt::MatchFlags &matchFlags) const
61 QModelIndex startIndex = lookupStartModelIndex.isValid() ? lookupStartModelIndex : model->index(0,0);
63 QModelIndexList list = model->match(startIndex,lookupRole,value,1,matchFlags);
72 \class QxtStringValidator QxtStringValidator
74 \brief The QxtStringValidator class provides validation on a QStringList or a QAbstractItemModel
76 It provides a String based validation in a stringlist or a custom model.
77 QxtStringValidator uses QAbstractItemModel::match() to validate the input.
78 For a partial match it returns QValidator::Intermediate and for a full match QValidator::Acceptable.
83 QLineEdit * testLineEdit = new QLineEdit();
86 testList << "StringTestString" << "sTrInGCaSe"<< "StringTest"<< "String"<< "Foobar"<< "BarFoo"<< "QxtLib";
88 QxtStringValidator *validator = new QxtStringValidator(ui.lineEdit);
89 validator->setStringList(testList);
91 //change lookup case sensitivity
92 validator->setCaseSensitivity(Qt::CaseInsensitive);
94 testLineEdit->setValidator(validator);
100 Constructs a validator object with a parent object that accepts any string in the stringlist.
102 QxtStringValidator::QxtStringValidator(QObject * parent) : QValidator(parent)
104 QXT_INIT_PRIVATE(QxtStringValidator);
107 QxtStringValidator::~QxtStringValidator(void)
111 Fixes up the string input if there is no exact match in the stringlist/model.
112 The first match in the stringlist/model is used to fix the input.
114 void QxtStringValidator::fixup ( QString & input ) const
116 qDebug()<<"Fixup called";
121 if (qxt_d().lookupExactMatch(input).isValid())
124 QModelIndex partialMatch = qxt_d().lookupPartialMatch(input);
125 if (partialMatch.isValid())
126 input = partialMatch.data(qxt_d().lookupRole).toString();
131 uses stringlist as new validation list
132 if a model was set before it is not deleted
134 void QxtStringValidator::setStringList(const QStringList &stringList)
136 //delete model only if it is a model created by us
137 if (qxt_d().model && !qxt_d().isUserModel)
139 delete qxt_d().model;
143 qxt_d().isUserModel = false;
144 qxt_d().lookupStartModelIndex = QModelIndex();
145 qxt_d().lookupRole = Qt::EditRole;
146 qxt_d().model = new QStringListModel(stringList,this);
150 Returns Acceptable if the string input matches a item in the stringlist.
151 Returns Intermediate if the string input matches a item in the stringlist partial or if input is empty.
152 Returns Invalid otherwise.
154 Note: A partial match means the beginning of the strings are matching:
155 qxtL matches qxtLib but not testqxtLib
157 QValidator::State QxtStringValidator::validate ( QString & input, int & pos ) const
161 // no model or a empty model has only Acceptable values (like no validator was set)
163 return QValidator::Acceptable;
165 if (qxt_d().model->rowCount() == 0)
166 return QValidator::Acceptable;
169 return QValidator::Intermediate;
171 if (qxt_d().lookupExactMatch(input).isValid())
173 qDebug()<<input<<" is QValidator::Acceptable";
174 return QValidator::Acceptable;
177 if (qxt_d().lookupPartialMatch(input).isValid())
179 qDebug()<<input<<" is QValidator::Intermediate";
180 return QValidator::Intermediate;
183 qDebug()<<input<<" is QValidator::Invalid";
184 return QValidator::Invalid;
189 Returns the startModelIndex.
190 Note: The return value will only we valid if the user has set the model with setLookupModel().
191 \sa setStartModelIndex()
193 QModelIndex QxtStringValidator::startModelIndex() const
195 if (qxt_d().isUserModel && qxt_d().model)
197 if (qxt_d().lookupStartModelIndex.isValid())
198 return qxt_d().lookupStartModelIndex;
200 return qxt_d().model->index(0,0);
202 return QModelIndex();
206 Returns if recursive lookup is enabled.
207 \sa setRecursiveLookup()
209 bool QxtStringValidator::recursiveLookup() const
211 if (qxt_d().userFlags & Qt::MatchRecursive)
217 Returns if wrapping lookup is enabled.
218 \sa setWrappingLookup()
220 bool QxtStringValidator::wrappingLookup() const
222 if (qxt_d().userFlags & Qt::MatchWrap)
228 Returns the used model if it was set by the user.
231 QAbstractItemModel * QxtStringValidator::lookupModel() const
233 if (qxt_d().isUserModel)
234 return qxt_d().model;
239 Returns Qt::CaseSensitive if the QxtStringValidator is matched case sensitively; otherwise returns Qt::CaseInsensitive.
240 \sa setCaseSensitivity().
242 Qt::CaseSensitivity QxtStringValidator::caseSensitivity () const
248 Sets case sensitive matching to cs.
249 If cs is Qt::CaseSensitive, inp matches input but not INPUT.
250 The default is Qt::CaseSensitive.
251 \sa caseSensitivity().
253 void QxtStringValidator::setCaseSensitivity ( Qt::CaseSensitivity caseSensitivity )
255 qxt_d().cs = caseSensitivity;
259 Sets the index the search should start at
260 The default is QModelIndex(0,0).
261 Note: this is set to default when the model changes.
262 Changing the startModelIndex is only possible if the validator uses a userdefined model
263 and the modelindex comes from the used model
264 \sa startModelIndex()
266 void QxtStringValidator::setStartModelIndex(const QModelIndex &index)
268 if (index.model() == qxt_d().model)
269 qxt_d().lookupStartModelIndex = index;
271 qWarning()<<"ModelIndex from different model. Ignoring.";
275 If enabled QxtStringValidator searches the entire hierarchy of the model.
276 This is disabled by default.
277 \sa recursiveLookup()
279 void QxtStringValidator::setRecursiveLookup(bool enable)
282 qxt_d().userFlags |= Qt::MatchRecursive;
284 qxt_d().userFlags &= ~Qt::MatchRecursive;
289 If enabled QxtStringValidator performs a search that wraps around,
290 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.
291 This is set by default.
294 void QxtStringValidator::setWrappingLookup(bool enable)
297 qxt_d().userFlags |= Qt::MatchWrap;
299 qxt_d().userFlags &= ~Qt::MatchWrap;
303 Uses model as new validation model.
304 Note: The validator does not take ownership of the model.
307 void QxtStringValidator::setLookupModel(QAbstractItemModel *model)
309 if (!qxt_d().isUserModel && qxt_d().model)
311 delete qxt_d().model;
315 qxt_d().lookupRole = Qt::EditRole;
316 qxt_d().isUserModel = true;
317 qxt_d().lookupStartModelIndex = QModelIndex();
318 qxt_d().model = QPointer<QAbstractItemModel>(model);
322 Sets the item role to be used to query the contents of items for validation.
323 Note: this is only possible if the model was set with setLookupModel()
324 This is set to default Value Qt::EditRole when the model changes
327 void QxtStringValidator::setLookupRole(const int role)
329 if (qxt_d().isUserModel)
330 qxt_d().lookupRole = role;
334 Returns the item role to be used to query the contents of items for validation.
337 int QxtStringValidator::lookupRole() const
339 return qxt_d().lookupRole;