kexi

validator.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004, 2006 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(QValidator *validator, 
00067     QObject * parent, const char * name)
00068  : Validator(parent, name)
00069 {
00070     addSubvalidator(validator);
00071 }
00072 
00073 
00074 void MultiValidator::addSubvalidator( QValidator* 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     State s;
00086     foreach( QValueList<QValidator*>::ConstIterator, it,  m_subValidators ) {
00087         s = (*it)->validate(input, pos);
00088         if (s==Intermediate || s==Invalid)
00089             return s;
00090     }
00091     return Acceptable;
00092 }
00093 
00094 void MultiValidator::fixup ( QString & input ) const
00095 {
00096     foreach( QValueList<QValidator*>::ConstIterator, it,  m_subValidators )
00097         (*it)->fixup(input);
00098 }
00099 
00100 Validator::Result MultiValidator::internalCheck(
00101     const QString &valueName, const QVariant& v, 
00102     QString &message, QString &details)
00103 {
00104     Result r;
00105     bool warning = false;
00106     foreach( QValueList<QValidator*>::ConstIterator, it,  m_subValidators ) {
00107         if (dynamic_cast<Validator*>(*it))
00108             r = dynamic_cast<Validator*>(*it)->internalCheck(valueName, v, message, details);
00109         else
00110             r = Ok; //ignore
00111         if (r==Error)
00112             return Error;
00113         else if (r==Warning)
00114             warning = true;
00115     }
00116     return warning ? Warning : Ok;
00117 }
00118 
KDE Home | KDE Accessibility Home | Description of Access Keys