File indexing completed on 2025-01-05 03:59:54

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2004-09-19
0007  * Description : a options group to set renaming files
0008  *               operations during camera downloading
0009  *
0010  * SPDX-FileCopyrightText: 2004-2005 by Renchi Raju <renchi dot raju at gmail dot com>
0011  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0012  * SPDX-FileCopyrightText: 2011      by Andi Clemens <andi dot clemens at gmail dot com>
0013  *
0014  * SPDX-License-Identifier: GPL-2.0-or-later
0015  *
0016  * ============================================================ */
0017 
0018 #include "renamecustomizer.h"
0019 
0020 // Qt includes
0021 
0022 #include <QButtonGroup>
0023 #include <QGridLayout>
0024 #include <QHBoxLayout>
0025 #include <QRadioButton>
0026 #include <QTimer>
0027 #include <QApplication>
0028 #include <QStyle>
0029 #include <QComboBox>
0030 #include <QLabel>
0031 
0032 // KDE includes
0033 
0034 #include <kconfiggroup.h>
0035 #include <klocalizedstring.h>
0036 #include <ksharedconfig.h>
0037 
0038 // Local includes
0039 
0040 #include "dcursortracker.h"
0041 #include "parsesettings.h"
0042 #include "parser.h"
0043 #include "advancedrenamewidget.h"
0044 #include "importrenameparser.h"
0045 #include "advancedrenamemanager.h"
0046 
0047 namespace Digikam
0048 {
0049 
0050 class Q_DECL_HIDDEN RenameCustomizer::Private
0051 {
0052 public:
0053 
0054     explicit Private()
0055       : startIndex           (1),
0056         buttonGroup          (nullptr),
0057         renameDefaultCase    (nullptr),
0058         renameDefault        (nullptr),
0059         renameCustom         (nullptr),
0060         changedTimer         (nullptr),
0061         renameDefaultBox     (nullptr),
0062         renameDefaultCaseType(nullptr),
0063         previewRenameLabel   (nullptr),
0064         advancedRenameWidget (nullptr),
0065         advancedRenameManager(nullptr)
0066     {
0067     }
0068 
0069     int                    startIndex;
0070 
0071     QButtonGroup*          buttonGroup;
0072 
0073     QLabel*                renameDefaultCase;
0074 
0075     QRadioButton*          renameDefault;
0076     QRadioButton*          renameCustom;
0077 
0078     QString                cameraTitle;
0079 
0080     QTimer*                changedTimer;
0081 
0082     QWidget*               renameDefaultBox;
0083 
0084     QComboBox*             renameDefaultCaseType;
0085 
0086     QLabel*                previewRenameLabel;
0087 
0088     AdvancedRenameWidget*  advancedRenameWidget;
0089     AdvancedRenameManager* advancedRenameManager;
0090 };
0091 
0092 RenameCustomizer::RenameCustomizer(QWidget* const parent, const QString& cameraTitle)
0093     : QWidget(parent),
0094       d      (new Private)
0095 {
0096     d->changedTimer = new QTimer(this);
0097     d->cameraTitle  = cameraTitle;
0098     d->buttonGroup  = new QButtonGroup(this);
0099     d->buttonGroup->setExclusive(true);
0100 
0101     setAttribute(Qt::WA_DeleteOnClose);
0102 
0103     QGridLayout* mainLayout  = new QGridLayout(this);
0104 
0105     const int spacing = qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0106                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing));
0107 
0108     // ----------------------------------------------------------------
0109 
0110     d->renameDefault         = new QRadioButton(i18n("Camera filenames"), this);
0111     d->buttonGroup->addButton(d->renameDefault, 0);
0112     d->renameDefault->setWhatsThis(i18n("Turn on this option to use the camera "
0113                                         "provided image filenames without modifications."));
0114 
0115     d->renameDefaultBox      = new QWidget(this);
0116     QHBoxLayout* boxLayout1  = new QHBoxLayout(d->renameDefaultBox);
0117 
0118     d->renameDefaultCase     = new QLabel(i18n("Change case to:"), d->renameDefaultBox);
0119     d->renameDefaultCase->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
0120 
0121     d->renameDefaultCaseType = new QComboBox(d->renameDefaultBox);
0122     d->renameDefaultCaseType->insertItem(NONE,  i18nc("Leave filename as it is", "Leave as-is"));
0123     d->renameDefaultCaseType->insertItem(UPPER, i18nc("Filename to uppercase",   "Upper"));
0124     d->renameDefaultCaseType->insertItem(LOWER, i18nc("Filename to lowercase",   "Lower"));
0125     d->renameDefaultCaseType->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
0126     d->renameDefaultCaseType->setWhatsThis(i18n("Set the method to use to change the case "
0127                                                 "of the image filenames."));
0128 
0129     boxLayout1->setContentsMargins(spacing, spacing, spacing, spacing);
0130     boxLayout1->setSpacing(spacing);
0131     boxLayout1->addSpacing(10);
0132     boxLayout1->addWidget(d->renameDefaultCase);
0133     boxLayout1->addWidget(d->renameDefaultCaseType);
0134 
0135     // ----------------------------------------------------------------------
0136 
0137     d->renameCustom          = new QRadioButton(i18nc("Custom Image Renaming", "Customize"), this);
0138 
0139     d->advancedRenameWidget  = new AdvancedRenameWidget(this);
0140     d->advancedRenameManager = new AdvancedRenameManager();
0141     d->advancedRenameManager->setParserType(AdvancedRenameManager::ImportParser);
0142     d->advancedRenameManager->setWidget(d->advancedRenameWidget);
0143 
0144     d->buttonGroup->addButton(d->renameCustom, 2);
0145 
0146     d->previewRenameLabel     = new QLabel(this);
0147     d->previewRenameLabel->setAlignment(Qt::AlignHCenter);
0148     d->previewRenameLabel->setWordWrap(true);
0149 
0150     mainLayout->addWidget(d->renameDefault,        0, 0, 1, 2);
0151     mainLayout->addWidget(d->renameDefaultBox,     1, 0, 1, 2);
0152     mainLayout->addWidget(d->renameCustom,         4, 0, 1, 2);
0153     mainLayout->addWidget(d->advancedRenameWidget, 5, 0, 1, 2);
0154     mainLayout->addWidget(d->previewRenameLabel,   6, 0, 1, 2);
0155     mainLayout->setRowStretch(7, 10);
0156     mainLayout->setContentsMargins(spacing, spacing, spacing, spacing);
0157     mainLayout->setSpacing(spacing);
0158 
0159     // -- setup connections -------------------------------------------------
0160 
0161 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
0162 
0163     connect(d->buttonGroup, SIGNAL(idClicked(int)),
0164             this, SLOT(slotRadioButtonClicked(int)));
0165 
0166     connect(d->renameDefaultCaseType, SIGNAL(activated(int)),
0167             this, SLOT(slotRenameOptionsChanged()));
0168 
0169 #else
0170 
0171     connect(d->buttonGroup, SIGNAL(buttonClicked(int)),
0172             this, SLOT(slotRadioButtonClicked(int)));
0173 
0174     connect(d->renameDefaultCaseType, SIGNAL(activated(QString)),
0175             this, SLOT(slotRenameOptionsChanged()));
0176 
0177 #endif
0178 
0179     connect(d->changedTimer, SIGNAL(timeout()),
0180             this, SIGNAL(signalChanged()));
0181 
0182     connect(d->advancedRenameWidget, SIGNAL(signalTextChanged(QString)),
0183             this, SLOT(slotCustomRenameChanged()));
0184 
0185     // --------------------------------------------------------
0186 
0187     readSettings();
0188 }
0189 
0190 RenameCustomizer::~RenameCustomizer()
0191 {
0192     saveSettings();
0193     delete d->advancedRenameManager;
0194     delete d;
0195 }
0196 
0197 void RenameCustomizer::setUseDefault(bool val)
0198 {
0199     if (val)
0200     {
0201         d->renameDefault->setChecked(true);
0202     }
0203     else
0204     {
0205         d->renameCustom->setChecked(true);
0206     }
0207 }
0208 
0209 bool RenameCustomizer::useDefault() const
0210 {
0211     return d->renameDefault->isChecked();
0212 }
0213 
0214 int RenameCustomizer::startIndex() const
0215 {
0216     return d->startIndex;
0217 }
0218 
0219 void RenameCustomizer::setStartIndex(int startIndex)
0220 {
0221     d->startIndex = startIndex;
0222     d->advancedRenameManager->setStartIndex(startIndex);
0223 }
0224 
0225 void RenameCustomizer::reset()
0226 {
0227     d->advancedRenameManager->reset();
0228 }
0229 
0230 AdvancedRenameManager* RenameCustomizer::renameManager() const
0231 {
0232     return d->advancedRenameManager;
0233 }
0234 
0235 QString RenameCustomizer::newName(const QString& fileName) const
0236 {
0237     QString result = fileName.trimmed();
0238 
0239     if (d->renameDefault->isChecked())
0240     {
0241         switch (changeCase())
0242         {
0243             case UPPER:
0244                 return result.toUpper();
0245                 break;
0246 
0247             case LOWER:
0248                 return result.toLower();
0249                 break;
0250 
0251             default:
0252                 return result;
0253         }
0254     }
0255 
0256     return d->advancedRenameManager->newName(result);
0257 }
0258 
0259 RenameCustomizer::Case RenameCustomizer::changeCase() const
0260 {
0261     return static_cast<Case>(d->renameDefaultCaseType->currentIndex());
0262 }
0263 
0264 void RenameCustomizer::setChangeCase(RenameCustomizer::Case val)
0265 {
0266     d->renameDefaultCaseType->setCurrentIndex(val);
0267 }
0268 
0269 void RenameCustomizer::slotRadioButtonClicked(int id)
0270 {
0271     QRadioButton* btn = dynamic_cast<QRadioButton*>(d->buttonGroup->button(id));
0272 
0273     if (!btn)
0274     {
0275         return;
0276     }
0277 
0278     btn->setChecked(true);
0279     d->renameDefaultBox->setEnabled(btn == d->renameDefault);
0280     d->advancedRenameWidget->setEnabled(btn == d->renameCustom);
0281     slotRenameOptionsChanged();
0282 }
0283 
0284 void RenameCustomizer::slotRenameOptionsChanged()
0285 {
0286     d->changedTimer->setSingleShot(true);
0287     d->changedTimer->start(500);
0288 }
0289 
0290 void RenameCustomizer::slotCustomRenameChanged()
0291 {
0292     d->advancedRenameManager->parseFiles();
0293     slotRenameOptionsChanged();
0294 }
0295 
0296 void RenameCustomizer::setPreviewText(const QString& txt)
0297 {
0298     d->previewRenameLabel->setText(txt);
0299 }
0300 
0301 void RenameCustomizer::readSettings()
0302 {
0303     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0304 
0305     KConfigGroup group   = config->group(QLatin1String("Camera Settings"));
0306     int def              = group.readEntry("Rename Method",        0);
0307     int chcaseT          = group.readEntry("Case Type",            (int)NONE);
0308     QString manualRename = group.readEntry("Manual Rename String", QString());
0309 
0310     slotRadioButtonClicked(def);
0311     d->renameDefaultCaseType->setCurrentIndex(chcaseT);
0312     d->advancedRenameWidget->setParseString(manualRename);
0313 }
0314 
0315 void RenameCustomizer::saveSettings()
0316 {
0317     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0318 
0319     KConfigGroup group = config->group(QLatin1String("Camera Settings"));
0320     group.writeEntry("Rename Method",        d->buttonGroup->checkedId());
0321     group.writeEntry("Case Type",            d->renameDefaultCaseType->currentIndex());
0322     group.writeEntry("Manual Rename String", d->advancedRenameWidget->parseString());
0323     config->sync();
0324 }
0325 
0326 } // namespace Digikam
0327 
0328 #include "moc_renamecustomizer.cpp"