Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "qxtstringvalidator.h"
00027 #include "qxtstringvalidator_p.h"
00028
00029 #include <QDebug>
00030 #include <QAbstractItemModel>
00031 #include <QStringListModel>
00032 #include <QFlags>
00033
00034 QxtStringValidatorPrivate::QxtStringValidatorPrivate() : isUserModel(false)
00035 , model(0)
00036 , cs(Qt::CaseSensitive)
00037 , lookupRole(Qt::EditRole)
00038 , userFlags(Qt::MatchWrap)
00039 , lookupStartModelIndex(QModelIndex())
00040 {}
00041
00042 QModelIndex QxtStringValidatorPrivate::lookupPartialMatch(const QString &value) const
00043 {
00044
00045 if (value.isEmpty())
00046 return QModelIndex();
00047
00048 Qt::MatchFlags matchFlags = Qt::MatchStartsWith | userFlags;
00049 if (cs == Qt::CaseSensitive)
00050 matchFlags |= Qt::MatchCaseSensitive;
00051
00052 return lookup(value, matchFlags);
00053 }
00054
00055 QModelIndex QxtStringValidatorPrivate::lookupExactMatch(const QString &value) const
00056 {
00057 Qt::MatchFlags matchFlags = Qt::MatchFixedString | userFlags;
00058 if (cs == Qt::CaseSensitive)
00059 matchFlags |= Qt::MatchCaseSensitive;
00060
00061 return lookup(value, matchFlags);
00062 }
00063
00064 QModelIndex QxtStringValidatorPrivate::lookup(const QString &value, const Qt::MatchFlags &matchFlags) const
00065 {
00066 QModelIndex startIndex = lookupStartModelIndex.isValid() ? lookupStartModelIndex : model->index(0, 0);
00067
00068 QModelIndexList list = model->match(startIndex, lookupRole, value, 1, matchFlags);
00069
00070 if (list.size() > 0)
00071 return list[0];
00072 return QModelIndex();
00073 }
00074
00075
00107 QxtStringValidator::QxtStringValidator(QObject * parent) : QValidator(parent)
00108 {
00109 QXT_INIT_PRIVATE(QxtStringValidator);
00110 }
00111
00112 QxtStringValidator::~QxtStringValidator(void)
00113 {}
00114
00119 void QxtStringValidator::fixup(QString & input) const
00120 {
00121 qDebug() << "Fixup called";
00122
00123
00124
00125 QValidator::fixup(input);
00126 }
00127
00132 void QxtStringValidator::setStringList(const QStringList &stringList)
00133 {
00134
00135 if (qxt_d().model && !qxt_d().isUserModel)
00136 {
00137 delete qxt_d().model;
00138 qxt_d().model = 0;
00139 }
00140
00141 qxt_d().isUserModel = false;
00142 qxt_d().lookupStartModelIndex = QModelIndex();
00143 qxt_d().lookupRole = Qt::EditRole;
00144 qxt_d().model = new QStringListModel(stringList, this);
00145 }
00146
00155 QValidator::State QxtStringValidator::validate(QString & input, int & pos) const
00156 {
00157 Q_UNUSED(pos);
00158
00159
00160 if (!qxt_d().model)
00161 return QValidator::Acceptable;
00162
00163 if (qxt_d().model->rowCount() == 0)
00164 return QValidator::Acceptable;
00165
00166 if (input.isEmpty())
00167 return QValidator::Intermediate;
00168
00169 if (qxt_d().lookupExactMatch(input).isValid())
00170 {
00171 qDebug() << input << " is QValidator::Acceptable";
00172 return QValidator::Acceptable;
00173 }
00174
00175 if (qxt_d().lookupPartialMatch(input).isValid())
00176 {
00177 qDebug() << input << " is QValidator::Intermediate";
00178 return QValidator::Intermediate;
00179 }
00180
00181 qDebug() << input << " is QValidator::Invalid";
00182 return QValidator::Invalid;
00183 }
00184
00185
00191 QModelIndex QxtStringValidator::startModelIndex() const
00192 {
00193 if (qxt_d().isUserModel && qxt_d().model)
00194 {
00195 if (qxt_d().lookupStartModelIndex.isValid())
00196 return qxt_d().lookupStartModelIndex;
00197 else
00198 return qxt_d().model->index(0, 0);
00199 }
00200 return QModelIndex();
00201 }
00202
00207 bool QxtStringValidator::recursiveLookup() const
00208 {
00209 if (qxt_d().userFlags & Qt::MatchRecursive)
00210 return true;
00211 return false;
00212 }
00213
00218 bool QxtStringValidator::wrappingLookup() const
00219 {
00220 if (qxt_d().userFlags & Qt::MatchWrap)
00221 return true;
00222 return false;
00223 }
00224
00229 QAbstractItemModel * QxtStringValidator::lookupModel() const
00230 {
00231 if (qxt_d().isUserModel)
00232 return qxt_d().model;
00233 return 0;
00234 }
00235
00240 Qt::CaseSensitivity QxtStringValidator::caseSensitivity() const
00241 {
00242 return qxt_d().cs;
00243 }
00244
00251 void QxtStringValidator::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
00252 {
00253 qxt_d().cs = caseSensitivity;
00254 }
00255
00264 void QxtStringValidator::setStartModelIndex(const QModelIndex &index)
00265 {
00266 if (index.model() == qxt_d().model)
00267 qxt_d().lookupStartModelIndex = index;
00268 else
00269 qWarning() << "ModelIndex from different model. Ignoring.";
00270 }
00271
00277 void QxtStringValidator::setRecursiveLookup(bool enable)
00278 {
00279 if (enable)
00280 qxt_d().userFlags |= Qt::MatchRecursive;
00281 else
00282 qxt_d().userFlags &= ~Qt::MatchRecursive;
00283
00284 }
00285
00292 void QxtStringValidator::setWrappingLookup(bool enable)
00293 {
00294 if (enable)
00295 qxt_d().userFlags |= Qt::MatchWrap;
00296 else
00297 qxt_d().userFlags &= ~Qt::MatchWrap;
00298 }
00299
00305 void QxtStringValidator::setLookupModel(QAbstractItemModel *model)
00306 {
00307 if (!qxt_d().isUserModel && qxt_d().model)
00308 {
00309 delete qxt_d().model;
00310 qxt_d().model = 0;
00311 }
00312
00313 qxt_d().lookupRole = Qt::EditRole;
00314 qxt_d().isUserModel = true;
00315 qxt_d().lookupStartModelIndex = QModelIndex();
00316 qxt_d().model = QPointer<QAbstractItemModel>(model);
00317 }
00318
00325 void QxtStringValidator::setLookupRole(const int role)
00326 {
00327 if (qxt_d().isUserModel)
00328 qxt_d().lookupRole = role;
00329 }
00330
00335 int QxtStringValidator::lookupRole() const
00336 {
00337 return qxt_d().lookupRole;
00338 }