kexi

validator.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "validator.h"
00021 
00022 using namespace KexiUtils;
00023 
00024 Validator::Validator(QObject * parent, const char * name)
00025 : QValidator(parent,name)
00026 , m_acceptsEmptyValue(false)
00027 {
00028 }
00029 
00030 Validator::~Validator()
00031 {
00032 }
00033 
00034 Validator::Result Validator::check(const QString &valueName, const QVariant& v, 
00035     QString &message, QString &details)
00036 {
00037     if (v.isNull() || v.type()==QVariant::String && v.toString().isEmpty()) {
00038         if (!m_acceptsEmptyValue) {
00039             message = Validator::msgColumnNotEmpty().arg(valueName);
00040             return Error;
00041         }
00042         return Ok;
00043     }
00044     return internalCheck(valueName, v, message, details);
00045 }
00046 
00047 Validator::Result Validator::internalCheck(const QString & /*valueName*/, 
00048     const QVariant& /*v*/, QString & /*message*/, QString & /*details*/)
00049 {
00050     return Error;
00051 }
00052 
00053 QValidator::State Validator::validate ( QString & , int & ) const
00054 {
00055     return QValidator::Acceptable;
00056 }
00057 
00058 //-----------------------------------------------------------
00059 
00060 MultiValidator::MultiValidator(QObject* parent, const char * name)
00061  : Validator(parent, name)
00062 {
00063     m_ownedSubValidators.setAutoDelete(true);
00064 }
00065 
00066 MultiValidator::MultiValidator(Validator *validator, 
00067     QObject * parent, const char * name)
00068  : Validator(parent, name)
00069 {
00070     addSubvalidator(validator);
00071 }
00072 
00073 
00074 void MultiValidator::addSubvalidator( Validator* validator, bool owned )
00075 {
00076     if (!validator)
00077         return;
00078     m_subValidators.append(validator);
00079     if (owned && !validator->parent())
00080         m_ownedSubValidators.append(validator);
00081 }
00082 
00083 QValidator::State MultiValidator::validate( QString & input, int & pos ) const
00084 {
00085     if (m_subValidators.isEmpty())
00086         return Invalid;
00087     State s;
00088     QValueList<Validator*>::const_iterator it;
00089     for ( it=m_subValidators.constBegin(); it!=m_subValidators.constEnd(); ++it) {
00090         s = (*it)->validate(input, pos);
00091         if (s==Intermediate || s==Invalid)
00092             return s;
00093     }
00094     return Acceptable;
00095 }
00096 
00097 void MultiValidator::fixup ( QString & input ) const
00098 {
00099     QValueList<Validator*>::const_iterator it;
00100     for ( it=m_subValidators.constBegin(); it!=m_subValidators.constEnd(); ++it) {
00101         (*it)->fixup(input);
00102     }
00103 }
00104 
00105 Validator::Result MultiValidator::internalCheck(
00106     const QString &valueName, const QVariant& v, 
00107     QString &message, QString &details)
00108 {
00109     if (m_subValidators.isEmpty())
00110         return Error;
00111     Result r;
00112     bool warning = false;
00113     QValueList<Validator*>::const_iterator it;
00114     for ( it=m_subValidators.begin(); it!=m_subValidators.end(); ++it) {
00115         r = (*it)->internalCheck(valueName, v, message, details);
00116         if (r==Error)
00117             return Error;
00118         if (r==Warning)
00119             warning = true;
00120     }
00121     return warning ? Warning : Ok;
00122 }
00123 
KDE Home | KDE Accessibility Home | Description of Access Keys