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-11-11 0007 * Description : a modifier for setting a default value if option parsing failed 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 "defaultvaluemodifier.h" 0016 0017 // Qt includes 0018 0019 #include <QGridLayout> 0020 #include <QLabel> 0021 #include <QPointer> 0022 #include <QLineEdit> 0023 #include <QApplication> 0024 #include <QStyle> 0025 #include <QRegularExpression> 0026 0027 // KDE includes 0028 0029 #include <klocalizedstring.h> 0030 0031 namespace Digikam 0032 { 0033 0034 DefaultValueDialog::DefaultValueDialog(Rule* parent) 0035 : RuleDialog(parent), 0036 valueInput(nullptr) 0037 { 0038 const int spacing = qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing), 0039 QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing)); 0040 0041 QString defaultValueStr = i18n("Default Value"); 0042 0043 QLabel* const srcLabel = new QLabel(defaultValueStr + QLatin1Char(':')); 0044 valueInput = new QLineEdit(this); 0045 valueInput->setToolTip(i18n("<p>Set a default value for empty strings.<br/>" 0046 "When applied to a renaming option, " 0047 "an empty string will be replaced by the value you specify here.</p>")); 0048 0049 QWidget* const mainWidget = new QWidget(this); 0050 QGridLayout* const mainLayout = new QGridLayout(this); 0051 mainLayout->addWidget(srcLabel, 0, 0); 0052 mainLayout->addWidget(valueInput, 0, 1); 0053 mainLayout->setContentsMargins(spacing, spacing, spacing, spacing); 0054 mainLayout->setSpacing(spacing); 0055 mainLayout->setRowStretch(1, 10); 0056 mainWidget->setLayout(mainLayout); 0057 0058 setSettingsWidget(mainWidget); 0059 0060 valueInput->setFocus(); 0061 } 0062 0063 DefaultValueDialog::~DefaultValueDialog() 0064 { 0065 } 0066 0067 // -------------------------------------------------------- 0068 0069 DefaultValueModifier::DefaultValueModifier() 0070 : Modifier(i18nc("default value for empty strings", "Default Value..."), 0071 i18n("Set a default value for empty strings"), 0072 QLatin1String("edit-undo")) 0073 { 0074 addToken(QLatin1String("{default:\"||value||\"}"), description()); 0075 0076 QRegularExpression reg(QLatin1String("\\{default:\"(.+)\"\\}")); 0077 reg.setPatternOptions(QRegularExpression::InvertedGreedinessOption); 0078 setRegExp(reg); 0079 } 0080 0081 void DefaultValueModifier::slotTokenTriggered(const QString& token) 0082 { 0083 Q_UNUSED(token) 0084 0085 QString result; 0086 0087 QPointer<DefaultValueDialog> dlg = new DefaultValueDialog(this); 0088 0089 if (dlg->exec() == QDialog::Accepted) 0090 { 0091 QString valueStr = dlg->valueInput->text(); 0092 0093 if (!valueStr.isEmpty()) 0094 { 0095 result = QString::fromUtf8("{default:\"%1\"}").arg(valueStr); 0096 } 0097 } 0098 0099 delete dlg; 0100 0101 Q_EMIT signalTokenTriggered(result); 0102 } 0103 0104 QString DefaultValueModifier::parseOperation(ParseSettings& settings, const QRegularExpressionMatch& match) 0105 { 0106 if (!settings.str2Modify.isEmpty()) 0107 { 0108 return settings.str2Modify; 0109 } 0110 0111 QString defaultStr = match.captured(1).isEmpty() ? QString() : match.captured(1); 0112 0113 return defaultStr; 0114 } 0115 0116 } // namespace Digikam 0117 0118 #include "moc_defaultvaluemodifier.cpp"