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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-09-18
0007  * Description : a modifier for replacing text in a token result
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 "replacemodifier.h"
0016 
0017 // Qt includes
0018 
0019 #include <QCheckBox>
0020 #include <QPointer>
0021 #include <QRegularExpression>
0022 
0023 // KDE includes
0024 
0025 #include <klocalizedstring.h>
0026 
0027 // Local includes
0028 
0029 #include "ui_replacemodifierdialogwidget.h"
0030 
0031 namespace Digikam
0032 {
0033 
0034 ReplaceDialog::ReplaceDialog(Rule* const parent)
0035     : RuleDialog(parent),
0036       ui        (new Ui::ReplaceModifierDialogWidget())
0037 {
0038     QWidget* const mainWidget = new QWidget(this);
0039     ui->setupUi(mainWidget);
0040     setSettingsWidget(mainWidget);
0041     ui->source->setFocus();
0042 }
0043 
0044 ReplaceDialog::~ReplaceDialog()
0045 {
0046     delete ui;
0047 }
0048 
0049 // --------------------------------------------------------
0050 
0051 ReplaceModifier::ReplaceModifier()
0052     : Modifier(i18nc("Replace text", "Replace..."), i18n("Replace text in a renaming option"),
0053                QLatin1String("document-edit"))
0054 {
0055     addToken(QLatin1String("{replace:\"||old||\", \"||new||\",||options||}"),
0056              i18n("Replace text (||options||: ||r|| = regular expression, ||i|| = ignore case)"));
0057 
0058     QRegularExpression reg(QLatin1String("\\{replace(:\"(.*)\",\"(.*)\"(,(r|ri|ir|i))?)\\}"));
0059     reg.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
0060     setRegExp(reg);
0061 }
0062 
0063 void ReplaceModifier::slotTokenTriggered(const QString& token)
0064 {
0065     Q_UNUSED(token)
0066 
0067     QString result;
0068 
0069     QPointer<ReplaceDialog> dlg = new ReplaceDialog(this);
0070 
0071     if (dlg->exec() == QDialog::Accepted)
0072     {
0073         QString oldStr = dlg->ui->source->text();
0074         QString newStr = dlg->ui->destination->text();
0075 
0076         if (!oldStr.isEmpty())
0077         {
0078             QString options;
0079 
0080             if (dlg->ui->isRegExp->isChecked())
0081             {
0082                 options.append(QLatin1Char('r'));
0083             }
0084 
0085             if (!dlg->ui->caseSensitive->isChecked())
0086             {
0087                 options.append(QLatin1Char('i'));
0088             }
0089 
0090             if (!options.isEmpty())
0091             {
0092                 options.prepend(QLatin1Char(','));
0093             }
0094 
0095             result = QString::fromUtf8("{replace:\"%1\",\"%2\"%3}").arg(oldStr).arg(newStr).arg(options);
0096         }
0097     }
0098 
0099     delete dlg;
0100 
0101     Q_EMIT signalTokenTriggered(result);
0102 }
0103 
0104 QString ReplaceModifier::parseOperation(ParseSettings& settings, const QRegularExpressionMatch& match)
0105 {
0106     QString original               = match.captured(2);
0107     QString replacement            = match.captured(3);
0108     QString result                 = settings.str2Modify;
0109     QString options                = match.captured(5);
0110     Qt::CaseSensitivity caseType = (!options.isEmpty() && options.contains(QLatin1Char('i'))) ? Qt::CaseInsensitive
0111                                                                                               : Qt::CaseSensitive;
0112 
0113     QRegularExpression ro(original);
0114     if (caseType == Qt::CaseInsensitive)
0115     {
0116         ro.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
0117     }
0118 
0119     if (!options.isEmpty() && options.contains(QLatin1Char('r')))
0120     {
0121         result.replace(ro, replacement);
0122     }
0123     else
0124     {
0125         result.replace(original, replacement, caseType);
0126     }
0127 
0128     return result;
0129 }
0130 
0131 } // namespace Digikam
0132 
0133 #include "moc_replacemodifier.cpp"