File indexing completed on 2024-12-01 08:10:07

0001 /*
0002     SPDX-FileCopyrightText: 2009 Andrey Batyiev <batyiev@gmail.com>
0003     SPDX-FileCopyrightText: 2015 Jan Grulich <jgrulich@redhat.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "listvalidator.h"
0009 
0010 #include <QStringList>
0011 
0012 ListValidator::ListValidator(QObject *parent)
0013     : QValidator(parent)
0014 {
0015 }
0016 
0017 ListValidator::~ListValidator() = default;
0018 
0019 QValidator::State ListValidator::validate(QString &text, int &pos) const
0020 {
0021     Q_ASSERT(inner);
0022     Q_UNUSED(pos);
0023 
0024     QStringList strings = text.split(QLatin1Char(','));
0025     int unusedPos;
0026     QValidator::State state = Acceptable;
0027     for (QStringList::iterator i = strings.begin(); i != strings.end(); ++i) {
0028         QString string = i->trimmed();
0029         const int position = i->indexOf(string);
0030         const int size = string.size();
0031         const QValidator::State current = inner->validate(string, unusedPos);
0032         i->replace(position, size, string);
0033         if (current == Invalid) {
0034             state = Invalid;
0035             break;
0036         }
0037         if (current == Intermediate) {
0038             if (state == Intermediate) {
0039                 state = Invalid;
0040                 break;
0041             }
0042             state = Intermediate;
0043         }
0044     }
0045     text = strings.join(QLatin1Char(','));
0046     return state;
0047 }
0048 
0049 void ListValidator::setInnerValidator(QValidator *validator)
0050 {
0051     inner = validator;
0052 }
0053 
0054 #include "moc_listvalidator.cpp"