1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2011-08-11
7 * Description : a widget to customize album name created by
8 * camera interface.
9 *
10 * Copyright (C) 2011-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
11 *
12 * This program is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General
14 * Public License as published by the Free Software Foundation;
15 * either version 2, or (at your option)
16 * any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * ============================================================ */
24
25 #include "albumcustomizer.h"
26
27 // Qt includes
28
29 #include <QVBoxLayout>
30 #include <QLabel>
31 #include <QCheckBox>
32 #include <QToolButton>
33 #include <QApplication>
34 #include <QStyle>
35 #include <QComboBox>
36 #include <QLineEdit>
37 #include <QIcon>
38
39 // KDE includes
40
41 #include <klocalizedstring.h>
42 #include <kconfiggroup.h>
43
44 // Local includes
45
46 #include "dlayoutbox.h"
47 #include "tooltipdialog.h"
48 #include "digikam_debug.h"
49 #include "dexpanderbox.h"
50
51 namespace Digikam
52 {
53
54 class Q_DECL_HIDDEN AlbumCustomizer::Private
55 {
56 public:
57
Private()58 explicit Private()
59 : autoAlbumDateCheck (nullptr),
60 autoAlbumExtCheck (nullptr),
61 folderDateLabel (nullptr),
62 customizer (nullptr),
63 tooltipToggleButton(nullptr),
64 customExample (nullptr),
65 folderDateFormat (nullptr),
66 tooltipDialog (nullptr)
67 {
68 }
69
70 QCheckBox* autoAlbumDateCheck;
71 QCheckBox* autoAlbumExtCheck;
72
73 QLabel* folderDateLabel;
74
75 QLineEdit* customizer;
76
77 QToolButton* tooltipToggleButton;
78
79 DAdjustableLabel* customExample;
80
81 QComboBox* folderDateFormat;
82
83 TooltipDialog* tooltipDialog;
84 };
85
AlbumCustomizer(QWidget * const parent)86 AlbumCustomizer::AlbumCustomizer(QWidget* const parent)
87 : QWidget(parent),
88 d (new Private)
89 {
90 const int spacing = QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
91
92 d->tooltipDialog = new TooltipDialog(this);
93 d->tooltipDialog->setTooltip(i18nc("@info",
94 "These expressions may be used to customize date format:\n"
95 "\"d\": The day as a number without a leading zero (1 to 31)\n"
96 "\"dd\": The day as a number with a leading zero (01 to 31)\n"
97 "\"ddd\": The abbreviated localized day name (e.g. 'Mon' to 'Sun')\n"
98 "\"dddd\": The long localized day name (e.g. 'Monday' to 'Sunday').\n"
99 "\"M\": The month as a number without a leading zero (1 to 12)\n"
100 "\"MM\": The month as a number with a leading zero (01 to 12)\n"
101 "\"MMM\": The abbreviated localized month name (e.g. 'Jan' to 'Dec')\n"
102 "\"MMMM\": The long localized month name (e.g. 'January' to 'December')\n"
103 "\"yy\": The year as two digit number (eg. 00 to 99)\n"
104 "\"yyyy\": The year as four digit number (eg. 2012)\n"
105 "All other input characters will be treated as text. Any sequence of characters "
106 "that are enclosed in singlequotes will be treated as text and not be used as an "
107 "expression. Examples, if date is 20 July 1969:\n"
108 "\"dd.MM.yyyy\" : 20.07.1969\n"
109 "\"ddd MMMM d yy\" : Sun July 20 69\n"
110 "\"'Photo shot on ' dddd\" : Photo shot on Sunday\n"
111 ));
112 d->tooltipDialog->resize(650, 530);
113
114 QVBoxLayout* const albumVlay = new QVBoxLayout(this);
115 d->autoAlbumExtCheck = new QCheckBox(i18nc("@option:check", "Extension-based sub-albums"), this);
116 d->autoAlbumDateCheck = new QCheckBox(i18nc("@option:check", "Date-based sub-albums"), this);
117 DHBox* const hbox1 = new DHBox(this);
118 d->folderDateLabel = new QLabel(i18nc("@label:listbox", "Date format:"), hbox1);
119 d->folderDateFormat = new QComboBox(hbox1);
120 d->folderDateFormat->insertItem(IsoDateFormat, i18nc("@item:inlistbox folder date format", "ISO"));
121 d->folderDateFormat->insertItem(TextDateFormat, i18nc("@item:inlistbox folder date format", "Full Text"));
122 d->folderDateFormat->insertItem(LocalDateFormat, i18nc("@item:inlistbox folder date format", "Local Settings"));
123 d->folderDateFormat->insertItem(CustomDateFormat, i18nc("@item:inlistbox folder date format", "Custom"));
124
125 DHBox* const hbox2 = new DHBox(this);
126 d->customizer = new QLineEdit(hbox2);
127 d->tooltipToggleButton = new QToolButton(hbox2);
128 d->tooltipToggleButton->setIcon(QIcon::fromTheme(QLatin1String("dialog-information")));
129 d->tooltipToggleButton->setToolTip(i18nc("@info", "Show a list of all available options"));
130
131 d->customExample = new DAdjustableLabel(this);
132
133 albumVlay->addWidget(d->autoAlbumExtCheck);
134 albumVlay->addWidget(d->autoAlbumDateCheck);
135 albumVlay->addWidget(hbox1);
136 albumVlay->addWidget(hbox2);
137 albumVlay->addWidget(d->customExample);
138 albumVlay->addStretch();
139 albumVlay->setContentsMargins(spacing, spacing, spacing, spacing);
140 albumVlay->setSpacing(spacing);
141
142 setWhatsThis(i18nc("@info", "Set how digiKam creates albums automatically when downloading."));
143 d->autoAlbumExtCheck->setWhatsThis(i18nc("@info", "Enable this option if you want to download your "
144 "pictures into automatically created file extension-based sub-albums of the destination "
145 "album. This way, you can separate JPEG and RAW files as they are downloaded from your camera."));
146 d->autoAlbumDateCheck->setWhatsThis(i18nc("@info", "Enable this option if you want to "
147 "download your pictures into automatically created file date-based sub-albums "
148 "of the destination album."));
149 d->folderDateFormat->setWhatsThis(i18nc("@info", "Select your preferred date format used to\n"
150 "create new albums. The options available are:\n"
151 "\"ISO\": the date format is in accordance with ISO 8601\n"
152 "(YYYY-MM-DD). E.g.: 2006-08-24\n"
153 "\"Full Text\": the date format is in a user-readable string.\n"
154 "E.g.: Thu Aug 24 2006\n"
155 "\"Local Settings\": the date format depending on KDE control panel settings.\n"
156 "\"Custom\": use a customized format for date."));
157 d->customExample->setWhatsThis(i18nc("@info", "Show the result of converted date 1968-12-26 using your customized format."));
158
159 // --------------------------------------------------------------------------------------
160
161 connect(d->autoAlbumDateCheck, SIGNAL(toggled(bool)),
162 d->folderDateFormat, SLOT(setEnabled(bool)));
163
164 connect(d->autoAlbumDateCheck, SIGNAL(toggled(bool)),
165 d->folderDateLabel, SLOT(setEnabled(bool)));
166
167 connect(d->tooltipToggleButton, SIGNAL(clicked(bool)),
168 this, SLOT(slotToolTipButtonToggled(bool)));
169
170 connect(d->folderDateFormat, SIGNAL(activated(int)),
171 this, SLOT(slotFolderDateFormatChanged(int)));
172
173 connect(d->customizer, SIGNAL(textChanged(QString)),
174 this, SLOT(slotCustomizerChanged()));
175 }
176
~AlbumCustomizer()177 AlbumCustomizer::~AlbumCustomizer()
178 {
179 delete d;
180 }
181
readSettings(KConfigGroup & group)182 void AlbumCustomizer::readSettings(KConfigGroup& group)
183 {
184 d->autoAlbumDateCheck->setChecked(group.readEntry("AutoAlbumDate", false));
185 d->autoAlbumExtCheck->setChecked(group.readEntry("AutoAlbumExt", false));
186 d->folderDateFormat->setCurrentIndex(group.readEntry("FolderDateFormat", (int)IsoDateFormat));
187 d->customizer->setText(group.readEntry("CustomDateFormat", QString()));
188
189 d->folderDateFormat->setEnabled(d->autoAlbumDateCheck->isChecked());
190 d->folderDateLabel->setEnabled(d->autoAlbumDateCheck->isChecked());
191 slotFolderDateFormatChanged(d->folderDateFormat->currentIndex());
192 }
193
saveSettings(KConfigGroup & group)194 void AlbumCustomizer::saveSettings(KConfigGroup& group)
195 {
196 group.writeEntry("AutoAlbumDate", d->autoAlbumDateCheck->isChecked());
197 group.writeEntry("AutoAlbumExt", d->autoAlbumExtCheck->isChecked());
198 group.writeEntry("FolderDateFormat", d->folderDateFormat->currentIndex());
199 group.writeEntry("CustomDateFormat", d->customizer->text());
200 }
201
autoAlbumDateEnabled() const202 bool AlbumCustomizer::autoAlbumDateEnabled() const
203 {
204 return d->autoAlbumDateCheck->isChecked();
205 }
206
autoAlbumExtEnabled() const207 bool AlbumCustomizer::autoAlbumExtEnabled() const
208 {
209 return d->autoAlbumExtCheck->isChecked();
210 }
211
folderDateFormat() const212 int AlbumCustomizer::folderDateFormat() const
213 {
214 return d->folderDateFormat->currentIndex();
215 }
216
customDateFormat() const217 QString AlbumCustomizer::customDateFormat() const
218 {
219 return d->customizer->text();
220 }
221
customDateFormatIsValid() const222 bool AlbumCustomizer::customDateFormatIsValid() const
223 {
224 QDate date(1968, 12, 26);
225
226 return !date.toString(customDateFormat()).isEmpty();
227 }
228
slotToolTipButtonToggled(bool)229 void AlbumCustomizer::slotToolTipButtonToggled(bool /*checked*/)
230 {
231 if (!d->tooltipDialog->isVisible())
232 {
233 d->tooltipDialog->show();
234 }
235
236 d->tooltipDialog->raise();
237 }
238
slotFolderDateFormatChanged(int index)239 void AlbumCustomizer::slotFolderDateFormatChanged(int index)
240 {
241 bool b = (index == CustomDateFormat);
242 d->customizer->setEnabled(b);
243 d->tooltipToggleButton->setEnabled(b);
244 d->customExample->setEnabled(b);
245 slotCustomizerChanged();
246 }
247
slotCustomizerChanged()248 void AlbumCustomizer::slotCustomizerChanged()
249 {
250 if (folderDateFormat() == CustomDateFormat)
251 {
252 QDate date(1968, 12, 26);
253
254 if (customDateFormatIsValid())
255 {
256 d->customExample->setAdjustedText(i18nc("@info Example of custom date format for album naming", "Ex.: %1", date.toString(customDateFormat())));
257 }
258 else
259 {
260 d->customExample->setAdjustedText(i18nc("@info Custom date format", "Format is not valid..."));
261 }
262 }
263 else
264 {
265 d->customExample->setAdjustedText();
266 }
267 }
268
269 } // namespace Digikam
270