File indexing completed on 2025-03-09 03:57:05

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-09-14
0007  * Description : modifier to change the case of a renaming option
0008  *
0009  * SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "casemodifier.h"
0016 
0017 // Qt includes
0018 
0019 #include <QRegularExpression>
0020 
0021 // KDE includes
0022 
0023 #include <klocalizedstring.h>
0024 
0025 namespace Digikam
0026 {
0027 
0028 CaseModifier::CaseModifier()
0029     : Modifier(i18n("Change Case"), i18n("change the case of a renaming option"))
0030 {
0031     setUseTokenMenu(true);
0032 
0033     addToken(QLatin1String("{upper}"),      i18n("Convert to uppercase"),
0034              i18n("Uppercase"));
0035 
0036     addToken(QLatin1String("{lower}"),      i18n("Convert to lowercase"),
0037              i18n("Lowercase"));
0038 
0039     addToken(QLatin1String("{firstupper}"), i18n("Convert the first letter of each word to uppercase"),
0040              i18n("First Letter of Each Word Uppercase"));
0041 
0042     QRegularExpression reg(QLatin1String("\\{(firstupper|lower|upper)\\}"));
0043     reg.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
0044     setRegExp(reg);
0045 }
0046 
0047 QString CaseModifier::parseOperation(ParseSettings& settings, const QRegularExpressionMatch& match)
0048 {
0049     const QString& token = match.captured(1);
0050 
0051     if      (token == QLatin1String("firstupper"))
0052     {
0053         return firstupper(settings.str2Modify);
0054     }
0055     else if (token == QLatin1String("upper"))
0056     {
0057         return settings.str2Modify.toUpper();
0058     }
0059     else if (token == QLatin1String("lower"))
0060     {
0061         return settings.str2Modify.toLower();
0062     }
0063 
0064     return settings.str2Modify;
0065 }
0066 
0067 QString CaseModifier::firstupper(const QString& str2Modify)
0068 {
0069     if (str2Modify.isNull() || str2Modify.isEmpty())
0070     {
0071         return QString();
0072     }
0073 
0074     QString result = str2Modify.toLower();
0075 
0076     if (result.at(0).isLetter())
0077     {
0078         result[0] = result.at(0).toUpper();
0079     }
0080 
0081     for (int i = 0 ; i < result.length() - 1 ; ++i)
0082     {
0083         if (result.at(i + 1).isLetter() && !result.at(i).isLetter())
0084         {
0085             result[i + 1] = result.at(i + 1).toUpper();
0086         }
0087     }
0088 
0089     return result;
0090 }
0091 
0092 } // namespace Digikam
0093 
0094 #include "moc_casemodifier.cpp"