Warning, file /office/skrooge/skgbasegui/kdatevalidator.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002   This file is part of libkdepim.
0003 
0004   Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
0005   Copyright (c) 2002 David Jarvie <software@astrojar.org.uk>
0006   Copyright (c) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
0007   Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
0008 
0009   This library is free software; you can redistribute it and/or
0010   modify it under the terms of the GNU Library General Public
0011   License as published by the Free Software Foundation; either
0012   version 2 of the License, or (at your option) any later version.
0013 
0014   This library is distributed in the hope that it will be useful,
0015   but WITHOUT ANY WARRANTY; without even the implied warranty of
0016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0017   Library General Public License for more details.
0018 
0019   You should have received a copy of the GNU Library General Public License
0020   along with this library; see the file COPYING.LIB.  If not, write to
0021   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0022   Boston, MA 02110-1301, USA.
0023 */
0024 
0025 // krazy:excludeall=qclasses as we want to subclass from QComboBox, not KComboBox
0026 
0027 #include "kdatevalidator.h"
0028 #include <skgservices.h>
0029 
0030 #include <qdatetime.h>
0031 #include <qstringlist.h>
0032 
0033 namespace KPIM
0034 {
0035 
0036 class KDateValidatorPrivate
0037 {
0038 public:
0039     KDateValidatorPrivate() = default;
0040 
0041     QStringList keywords;
0042     KDateValidator::FixupBehavior behavior{KDateValidator::FixupCurrent};
0043     QString mAlternativeDateFormatToUse;
0044 };
0045 
0046 } // namespace KPIM
0047 
0048 using namespace KPIM;
0049 
0050 KDateValidator::KDateValidator(QObject* iParent)
0051     : QValidator(iParent), d(new KDateValidatorPrivate)
0052 {
0053     // Check if we can use the QLocale::ShortFormat
0054     d->mAlternativeDateFormatToUse = QLocale().dateFormat(QLocale::ShortFormat);
0055     if (!d->mAlternativeDateFormatToUse.contains(QStringLiteral("yyyy"))) {
0056         d->mAlternativeDateFormatToUse = d->mAlternativeDateFormatToUse.replace(QStringLiteral("yy"), QStringLiteral("yyyy"));
0057     }
0058 }
0059 
0060 KDateValidator::~KDateValidator()
0061 {
0062     delete d;
0063 }
0064 
0065 QValidator::State KDateValidator::validate(QString& str, int& /*pos*/) const
0066 {
0067     int length = str.length();
0068 
0069     // empty string is intermediate so one can clear the edit line and start from scratch
0070     if (length <= 0) {
0071         return Intermediate;
0072     }
0073 
0074     if (d->keywords.contains(str.toLower())) {
0075         return Acceptable;
0076     }
0077 
0078     QDate date = QLocale().toDate(str, d->mAlternativeDateFormatToUse);
0079     if (date.isValid()) {
0080         return Acceptable;
0081     }
0082     return Intermediate;
0083 
0084 }
0085 
0086 void KDateValidator::fixup(QString& input) const
0087 {
0088     if (d->behavior == NoFixup) {
0089         return;
0090     }
0091 
0092     QDate result = SKGServices::partialStringToDate(input, d->behavior == FixupBackward);
0093     if (result.isValid()) {
0094         input = QLocale().toString(result, d->mAlternativeDateFormatToUse);
0095     }
0096 }
0097 
0098 void KDateValidator::setKeywords(const QStringList& iKeywords)
0099 {
0100     d->keywords = iKeywords;
0101 }
0102 
0103 QStringList KDateValidator::keywords() const
0104 {
0105     return d->keywords;
0106 }
0107 
0108 void KDateValidator::setFixupBehavior(FixupBehavior behavior)
0109 {
0110     d->behavior = behavior;
0111 }
0112 
0113 KDateValidator::FixupBehavior KDateValidator::fixupBehavior() const
0114 {
0115     return d->behavior;
0116 }
0117 
0118