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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2011-08-11
0007  * Description : a widget to customize album name created by
0008  *               camera interface.
0009  *
0010  * SPDX-FileCopyrightText: 2011-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "albumcustomizer.h"
0017 
0018 // Qt includes
0019 
0020 #include <QVBoxLayout>
0021 #include <QLabel>
0022 #include <QCheckBox>
0023 #include <QToolButton>
0024 #include <QApplication>
0025 #include <QStyle>
0026 #include <QComboBox>
0027 #include <QLineEdit>
0028 #include <QIcon>
0029 
0030 // KDE includes
0031 
0032 #include <klocalizedstring.h>
0033 #include <kconfiggroup.h>
0034 
0035 // Local includes
0036 
0037 #include "dlayoutbox.h"
0038 #include "tooltipdialog.h"
0039 #include "digikam_debug.h"
0040 #include "dexpanderbox.h"
0041 
0042 namespace Digikam
0043 {
0044 
0045 class Q_DECL_HIDDEN AlbumCustomizer::Private
0046 {
0047 public:
0048 
0049     explicit Private()
0050         : autoAlbumDateCheck (nullptr),
0051           autoAlbumExtCheck  (nullptr),
0052           folderDateLabel    (nullptr),
0053           customizer         (nullptr),
0054           tooltipToggleButton(nullptr),
0055           customExample      (nullptr),
0056           folderDateFormat   (nullptr),
0057           tooltipDialog      (nullptr)
0058     {
0059     }
0060 
0061     QCheckBox*          autoAlbumDateCheck;
0062     QCheckBox*          autoAlbumExtCheck;
0063 
0064     QLabel*             folderDateLabel;
0065 
0066     QLineEdit*          customizer;
0067 
0068     QToolButton*        tooltipToggleButton;
0069 
0070     DAdjustableLabel*   customExample;
0071 
0072     QComboBox*          folderDateFormat;
0073 
0074     TooltipDialog*      tooltipDialog;
0075 };
0076 
0077 AlbumCustomizer::AlbumCustomizer(QWidget* const parent)
0078     : QWidget(parent),
0079       d      (new Private)
0080 {
0081     const int spacing = qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0082                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing));
0083 
0084     d->tooltipDialog = new TooltipDialog(this);
0085     d->tooltipDialog->setTooltip(i18nc("@info",
0086                                        "<p>These expressions may be used to customize date format:</p>"
0087                                        "<p><b>d</b>: The day as a number without a leading zero (1 to 31)</p>"
0088                                        "<p><b>dd</b>: The day as a number with a leading zero (01 to 31)</p>"
0089                                        "<p><b>ddd</b>: The abbreviated localized day name (e.g. 'Mon' to 'Sun')</p>"
0090                                        "<p><b>dddd</b>: The long localized day name (e.g. 'Monday' to 'Sunday').</p>"
0091                                        "<p><b>M</b>: The month as a number without a leading zero (1 to 12)</p>"
0092                                        "<p><b>MM</b>: The month as a number with a leading zero (01 to 12)</p>"
0093                                        "<p><b>MMM</b>: The abbreviated localized month name (e.g. 'Jan' to 'Dec')</p>"
0094                                        "<p><b>MMMM</b>: The long localized month name (e.g. 'January' to 'December')</p>"
0095                                        "<p><b>yy</b>: The year as two digit number (eg. 00 to 99)</p>"
0096                                        "<p><b>yyyy</b>: The year as four digit number (eg. 2012)</p>"
0097                                        "<p>All other input characters will be treated as text. Any sequence of characters "
0098                                        "that are enclosed in singlequotes will be treated as text and not be used as an "
0099                                        "expression. Examples, if date is 20 July 1969:</p>"
0100                                        "<p><b>dd.MM.yyyy</b> : 20.07.1969</p>"
0101                                        "<p><b>ddd MMMM d yy</b> : Sun July 20 69</p>"
0102                                        "<p><b>'Photo shot on ' dddd</b> : Photo shot on Sunday</p>"
0103                                      ));
0104     d->tooltipDialog->resize(650, 530);
0105 
0106     QVBoxLayout* const albumVlay = new QVBoxLayout(this);
0107     d->autoAlbumExtCheck         = new QCheckBox(i18nc("@option:check", "Extension-based sub-albums"), this);
0108     d->autoAlbumDateCheck        = new QCheckBox(i18nc("@option:check", "Date-based sub-albums"), this);
0109     DHBox* const hbox1           = new DHBox(this);
0110     d->folderDateLabel           = new QLabel(i18nc("@label:listbox", "Date format:"), hbox1);
0111     d->folderDateFormat          = new QComboBox(hbox1);
0112     d->folderDateFormat->insertItem(IsoDateFormat,    i18nc("@item:inlistbox folder date format", "ISO"));
0113     d->folderDateFormat->insertItem(TextDateFormat,   i18nc("@item:inlistbox folder date format", "Full Text"));
0114     d->folderDateFormat->insertItem(LocalDateFormat,  i18nc("@item:inlistbox folder date format", "Local Settings"));
0115     d->folderDateFormat->insertItem(CustomDateFormat, i18nc("@item:inlistbox folder date format", "Custom"));
0116 
0117     DHBox* const hbox2     = new DHBox(this);
0118     d->customizer          = new QLineEdit(hbox2);
0119     d->tooltipToggleButton = new QToolButton(hbox2);
0120     d->tooltipToggleButton->setIcon(QIcon::fromTheme(QLatin1String("dialog-information")));
0121     d->tooltipToggleButton->setToolTip(i18nc("@info", "Show a list of all available options"));
0122 
0123     d->customExample       = new DAdjustableLabel(this);
0124 
0125     albumVlay->addWidget(d->autoAlbumExtCheck);
0126     albumVlay->addWidget(d->autoAlbumDateCheck);
0127     albumVlay->addWidget(hbox1);
0128     albumVlay->addWidget(hbox2);
0129     albumVlay->addWidget(d->customExample);
0130     albumVlay->addStretch();
0131     albumVlay->setContentsMargins(spacing, spacing, spacing, spacing);
0132     albumVlay->setSpacing(spacing);
0133 
0134     setWhatsThis(i18nc("@info", "Set how digiKam creates albums automatically when downloading."));
0135     d->autoAlbumExtCheck->setWhatsThis(i18nc("@info", "Enable this option if you want to download your "
0136                                             "pictures into automatically created file extension-based sub-albums of the destination "
0137                                             "album. This way, you can separate JPEG and RAW files as they are downloaded from your camera."));
0138     d->autoAlbumDateCheck->setWhatsThis(i18nc("@info", "Enable this option if you want to "
0139                                              "download your pictures into automatically created file date-based sub-albums "
0140                                              "of the destination album."));
0141     d->folderDateFormat->setWhatsThis(i18nc("@info", "Select your preferred date format used to\n"
0142                                            "create new albums. The options available are:\n"
0143                                            "\"ISO\": the date format is in accordance with ISO 8601\n"
0144                                            "(YYYY-MM-DD). E.g.: 2006-08-24\n"
0145                                            "\"Full Text\": the date format is in a user-readable string.\n"
0146                                            "E.g.: Thu Aug 24 2006\n"
0147                                            "\"Local Settings\": the date format depending on KDE control panel settings.\n"
0148                                            "\"Custom\": use a customized format for date."));
0149     d->customExample->setWhatsThis(i18nc("@info", "Show the result of converted date 1968-12-26 using your customized format."));
0150 
0151     // --------------------------------------------------------------------------------------
0152 
0153     connect(d->autoAlbumDateCheck, SIGNAL(toggled(bool)),
0154             d->folderDateFormat, SLOT(setEnabled(bool)));
0155 
0156     connect(d->autoAlbumDateCheck, SIGNAL(toggled(bool)),
0157             d->folderDateLabel, SLOT(setEnabled(bool)));
0158 
0159     connect(d->tooltipToggleButton, SIGNAL(clicked(bool)),
0160             this, SLOT(slotToolTipButtonToggled(bool)));
0161 
0162     connect(d->folderDateFormat, SIGNAL(activated(int)),
0163             this, SLOT(slotFolderDateFormatChanged(int)));
0164 
0165     connect(d->customizer, SIGNAL(textChanged(QString)),
0166             this, SLOT(slotCustomizerChanged()));
0167 }
0168 
0169 AlbumCustomizer::~AlbumCustomizer()
0170 {
0171     delete d;
0172 }
0173 
0174 void AlbumCustomizer::readSettings(KConfigGroup& group)
0175 {
0176     d->autoAlbumDateCheck->setChecked(group.readEntry("AutoAlbumDate",       false));
0177     d->autoAlbumExtCheck->setChecked(group.readEntry("AutoAlbumExt",         false));
0178     d->folderDateFormat->setCurrentIndex(group.readEntry("FolderDateFormat", (int)IsoDateFormat));
0179     d->customizer->setText(group.readEntry("CustomDateFormat",               QString()));
0180 
0181     d->folderDateFormat->setEnabled(d->autoAlbumDateCheck->isChecked());
0182     d->folderDateLabel->setEnabled(d->autoAlbumDateCheck->isChecked());
0183     slotFolderDateFormatChanged(d->folderDateFormat->currentIndex());
0184 }
0185 
0186 void AlbumCustomizer::saveSettings(KConfigGroup& group)
0187 {
0188     group.writeEntry("AutoAlbumDate",    d->autoAlbumDateCheck->isChecked());
0189     group.writeEntry("AutoAlbumExt",     d->autoAlbumExtCheck->isChecked());
0190     group.writeEntry("FolderDateFormat", d->folderDateFormat->currentIndex());
0191     group.writeEntry("CustomDateFormat", d->customizer->text());
0192 }
0193 
0194 bool AlbumCustomizer::autoAlbumDateEnabled() const
0195 {
0196     return d->autoAlbumDateCheck->isChecked();
0197 }
0198 
0199 bool AlbumCustomizer::autoAlbumExtEnabled() const
0200 {
0201     return d->autoAlbumExtCheck->isChecked();
0202 }
0203 
0204 int AlbumCustomizer::folderDateFormat() const
0205 {
0206     return d->folderDateFormat->currentIndex();
0207 }
0208 
0209 QString AlbumCustomizer::customDateFormat() const
0210 {
0211     return d->customizer->text();
0212 }
0213 
0214 bool AlbumCustomizer::customDateFormatIsValid() const
0215 {
0216     QDate date(1968, 12, 26);
0217 
0218     return !date.toString(customDateFormat()).isEmpty();
0219 }
0220 
0221 void AlbumCustomizer::slotToolTipButtonToggled(bool /*checked*/)
0222 {
0223     if (!d->tooltipDialog->isVisible())
0224     {
0225         d->tooltipDialog->show();
0226     }
0227 
0228     d->tooltipDialog->raise();
0229 }
0230 
0231 void AlbumCustomizer::slotFolderDateFormatChanged(int index)
0232 {
0233     bool b = (index == CustomDateFormat);
0234     d->customizer->setEnabled(b);
0235     d->tooltipToggleButton->setEnabled(b);
0236     d->customExample->setEnabled(b);
0237     slotCustomizerChanged();
0238 }
0239 
0240 void AlbumCustomizer::slotCustomizerChanged()
0241 {
0242     if (folderDateFormat() == CustomDateFormat)
0243     {
0244         QDate date(1968, 12, 26);
0245 
0246         if (customDateFormatIsValid())
0247         {
0248             d->customExample->setAdjustedText(i18nc("@info Example of custom date format for album naming", "Ex.: %1", date.toString(customDateFormat())));
0249         }
0250         else
0251         {
0252             d->customExample->setAdjustedText(i18nc("@info Custom date format", "Format is not valid..."));
0253         }
0254     }
0255     else
0256     {
0257         d->customExample->setAdjustedText();
0258     }
0259 }
0260 
0261 } // namespace Digikam
0262 
0263 #include "moc_albumcustomizer.cpp"