Debian Codegraph @

core/libs/album/widgets/albumpropsedit.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2003-03-09
7 * Description : Album properties dialog.
8 *
9 * SPDX-FileCopyrightText: 2003-2004 by Renchi Raju <renchi dot raju at gmail dot com>
10 * SPDX-FileCopyrightText: 2005 by Tom Albers <tomalbers at kde dot nl>
11 * SPDX-FileCopyrightText: 2006-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
12 *
13 * SPDX-License-Identifier: GPL-2.0-or-later
14 *
15 * ============================================================ */
16
17 #include "albumpropsedit.h"
18
19 // Qt includes
20
21 #include <QCheckBox>
22 #include <QDateTime>
23 #include <QGridLayout>
24 #include <QLabel>
25 #include <QPointer>
26 #include <QApplication>
27 #include <QStyle>
28 #include <QComboBox>
29 #include <QLineEdit>
30 #include <QStandardPaths>
31 #include <QMessageBox>
32 #include <QDialogButtonBox>
33 #include <QVBoxLayout>
34 #include <QPushButton>
35
36 // KDE includes
37
38 #include <klocalizedstring.h>
39
40 // Local includes
41
42 #include "digikam_globals.h"
43 #include "dlayoutbox.h"
44 #include "coredb.h"
45 #include "album.h"
46 #include "albummanager.h"
47 #include "applicationsettings.h"
48 #include "collectionmanager.h"
49 #include "coredbaccess.h"
50 #include "dexpanderbox.h"
51 #include "ddatepicker.h"
52 #include "dtextedit.h"
53
54 namespace Digikam
55 {
56
57 class Q_DECL_HIDDEN AlbumDatePicker : public DDatePicker
58 {
59 Q_OBJECT
60
61 public:
62
63 explicit AlbumDatePicker(QWidget* const widget)
64 : DDatePicker(widget)
65 {
66 }
67
68 ~AlbumDatePicker() override = default;
69
70 void dateLineEnterPressed()
71 {
72 lineEnterPressed();
73 }
74 };
75
76 // --------------------------------------------------------------------------------
77
78 class Q_DECL_HIDDEN AlbumPropsEdit::Private
79 {
80
81 public:
82
83 Private() = default;
84
85 public:
86
87 QDialogButtonBox* buttons = nullptr;
88
89 QLabel* topLabel = nullptr;
90 QComboBox* categoryCombo = nullptr;
91 QComboBox* parentCombo = nullptr;
92 DTextEdit* titleEdit = nullptr;
93 DPlainTextEdit* commentsEdit = nullptr;
94
95 AlbumDatePicker* datePicker = nullptr;
96
97 PAlbum* album = nullptr;
98 };
99
100 AlbumPropsEdit::AlbumPropsEdit(PAlbum* const album, bool create)
101 : QDialog(nullptr),
102 d (new Private)
103 {
104 setWindowTitle(create ? i18nc("@title:window, album properties", "New Album")
105 : i18nc("@title:window, album properties", "Edit Album"));
106
107 d->buttons = new QDialogButtonBox(QDialogButtonBox::Help |
108 QDialogButtonBox::Ok |
109 QDialogButtonBox::Cancel, this);
110 d->buttons->button(QDialogButtonBox::Ok)->setDefault(true);
111
112 d->album = album;
113 QWidget* const page = new QWidget(this);
114 QLabel* const logo = new QLabel(page);
115
116 logo->setPixmap(QIcon::fromTheme(QLatin1String("digikam")).pixmap(QSize(48,48)));
117
118 d->topLabel = new QLabel(page);
119 d->topLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
120 d->topLabel->setWordWrap(false);
121
122 if (create)
123 {
124 slotNewAlbumTextChanged(0);
125 }
126 else
127 {
128 d->topLabel->setText(i18nc("@label: album properties", "\"%1\"\nAlbum Properties", album->title()));
129 }
130
131 // --------------------------------------------------------
132
133 DLineWidget* const topLine = new DLineWidget(Qt::Horizontal);
134
135 QLabel* const titleLabel = new QLabel(page);
136 titleLabel->setText(i18nc("@label: album properties", "&Title:"));
137
138 d->titleEdit = new DTextEdit(page);
139 d->titleEdit->setLinesVisible(1);
140 d->titleEdit->setPlaceholderText(i18nc("@label: album properties", "Set here the new album title"));
141 d->titleEdit->setIgnoredCharacters(QLatin1String("/:"));
142 titleLabel->setBuddy(d->titleEdit);
143
144 QLabel* const categoryLabel = new QLabel(page);
145 categoryLabel->setText(i18nc("@label: album properties", "Ca&tegory:"));
146
147 d->categoryCombo = new QComboBox(page);
148 d->categoryCombo->setEditable(true);
149 d->categoryCombo->lineEdit()->setClearButtonEnabled(true);
150 categoryLabel->setBuddy(d->categoryCombo);
151
152 QLabel* const parentLabel = new QLabel(page);
153 parentLabel->setText(i18nc("@label: album properties", "Ch&ild Of:"));
154
155 d->parentCombo = new QComboBox(page);
156 parentLabel->setBuddy(d->parentCombo);
157
158 QLabel* const commentsLabel = new QLabel(page);
159 commentsLabel->setText(i18nc("@label: album properties", "Ca&ption:"));
160
161 d->commentsEdit = new DPlainTextEdit(page);
162 commentsLabel->setBuddy(d->commentsEdit);
163 d->commentsEdit->setWordWrapMode(QTextOption::WordWrap);
164 d->commentsEdit->setPlaceholderText(i18nc("@label: album properties", "Enter album caption here..."));
165
166 QLabel* const dateLabel = new QLabel(page);
167 dateLabel->setText(i18nc("@label: album properties", "Album &date:"));
168
169 d->datePicker = new AlbumDatePicker(page);
170 dateLabel->setBuddy(d->datePicker);
171
172 DHBox* const buttonRow = new DHBox(page);
173 QPushButton* const dateLowButton = new QPushButton(i18nc("@action: Selects the date of the oldest image", "&Oldest"), buttonRow);
174 dateLowButton->setWhatsThis(i18nc("@info", "Use this button to select the date of the oldest image from album."));
175 QPushButton* const dateAvgButton = new QPushButton(i18nc("@action: Calculates the average date", "&Average"), buttonRow);
176 dateAvgButton->setWhatsThis(i18nc("@info", "Use this button to calculate the average date of images from album."));
177 QPushButton* const dateHighButton = new QPushButton(i18nc("@action: Selects the date of the newest image", "Newest"), buttonRow);
178 dateHighButton->setWhatsThis(i18nc("@info", "Use this button to select the date of the newest image from album."));
179
180 if (create)
181 {
182 setTabOrder(d->titleEdit, d->categoryCombo);
183 setTabOrder(d->categoryCombo, d->parentCombo);
184 setTabOrder(d->parentCombo, d->commentsEdit);
185 setTabOrder(d->commentsEdit, d->datePicker);
186 dateHighButton->hide();
187 dateAvgButton->hide();
188 dateLowButton->hide();
189 }
190 else
191 {
192 setTabOrder(d->titleEdit, d->categoryCombo);
193 setTabOrder(d->categoryCombo, d->commentsEdit);
194 setTabOrder(d->commentsEdit, d->datePicker);
195 d->parentCombo->hide();
196 parentLabel->hide();
197 }
198
199 d->commentsEdit->setTabChangesFocus(true);
200
201 // --------------------------------------------------------
202
203 QGridLayout* const grid = new QGridLayout();
204 grid->addWidget(logo, 0, 0, 1, 1);
205 grid->addWidget(d->topLabel, 0, 1, 1, 1);
206 grid->addWidget(topLine, 1, 0, 1, 2);
207 grid->addWidget(titleLabel, 2, 0, 1, 1);
208 grid->addWidget(d->titleEdit, 2, 1, 1, 1);
209 grid->addWidget(categoryLabel, 3, 0, 1, 1);
210 grid->addWidget(d->categoryCombo, 3, 1, 1, 1);
211
212 if (create)
213 {
214 grid->addWidget(parentLabel, 4, 0, 1, 1);
215 grid->addWidget(d->parentCombo, 4, 1, 1, 1);
216 grid->addWidget(commentsLabel, 5, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
217 grid->addWidget(d->commentsEdit, 5, 1, 1, 1);
218 grid->addWidget(dateLabel, 6, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
219 grid->addWidget(d->datePicker, 6, 1, 1, 1);
220 grid->addWidget(buttonRow, 7, 1, 1, 1);
221 }
222 else
223 {
224 grid->addWidget(commentsLabel, 4, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
225 grid->addWidget(d->commentsEdit, 4, 1, 1, 1);
226 grid->addWidget(dateLabel, 5, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
227 grid->addWidget(d->datePicker, 5, 1, 1, 1);
228 grid->addWidget(buttonRow, 6, 1, 1, 1);
229 }
230
231 grid->setSpacing(layoutSpacing());
232
233 grid->setContentsMargins(QMargins());
234 page->setLayout(grid);
235
236 QVBoxLayout* const vbx = new QVBoxLayout(this);
237 vbx->addWidget(page);
238 vbx->addWidget(d->buttons);
239 setLayout(vbx);
240
241 // Initialize ---------------------------------------------
242
243 const ApplicationSettings* const settings = ApplicationSettings::instance();
244
245 if (settings)
246 {
247 QStringList Categories = settings->getAlbumCategoryNames();
248 d->categoryCombo->addItems(Categories);
249 int categoryIndex = Categories.indexOf(album->category());
250
251 if (categoryIndex != -1)
252 {
253 d->categoryCombo->setCurrentIndex(categoryIndex);
254 }
255 else
256 {
257 d->categoryCombo->lineEdit()->clear();
258 }
259 }
260
261 if (create)
262 {
263 d->titleEdit->setText(i18nc("@label: album properties", "New Album"));
264 d->datePicker->setDate(QDate::currentDate());
265 d->parentCombo->addItem(i18nc("@item: album properties", "Selected Album (Default)"));
266 d->parentCombo->addItem(i18nc("@item: top level folder of album","Root of current collection"));
267 }
268 else
269 {
270 d->titleEdit->setText(album->title());
271 d->commentsEdit->setPlainText(album->caption());
272 d->datePicker->setDate(album->date());
273 }
274
275 // -- slots connections -------------------------------------------
276
277 connect(d->titleEdit, SIGNAL(textChanged()),
278 this, SLOT(slotTitleChanged()));
279
280 connect(dateLowButton, SIGNAL(clicked()),
281 this, SLOT(slotDateLowButtonClicked()));
282
283 connect(dateAvgButton, SIGNAL(clicked()),
284 this, SLOT(slotDateAverageButtonClicked()));
285
286 connect(dateHighButton, SIGNAL(clicked()),
287 this, SLOT(slotDateHighButtonClicked()));
288
289 connect(d->parentCombo, SIGNAL(activated(int)),
290 this, SLOT(slotNewAlbumTextChanged(int)));
291
292 connect(d->buttons->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
293 this, SLOT(accept()));
294
295 connect(d->buttons->button(QDialogButtonBox::Cancel), SIGNAL(clicked()),
296 this, SLOT(reject()));
297
298 connect(d->buttons->button(QDialogButtonBox::Help), SIGNAL(clicked()),
299 this, SLOT(slotHelp()));
300
301 // ---
302
303 adjustSize();
304 d->titleEdit->selectAll();
305 d->titleEdit->setFocus();
306 }
307
308 AlbumPropsEdit::~AlbumPropsEdit()
309 {
310 delete d;
311 }
312
313 QString AlbumPropsEdit::title() const
314 {
315 return d->titleEdit->text().trimmed();
316 }
317
318 QString AlbumPropsEdit::comments() const
319 {
320 return d->commentsEdit->toPlainText();
321 }
322
323 QDate AlbumPropsEdit::date() const
324 {
325 // See bug #267944 : update calendar view if user enter a date in text field.
326
327 d->datePicker->dateLineEnterPressed();
328
329 return d->datePicker->date();
330 }
331
332 int AlbumPropsEdit::parent() const
333 {
334 return d->parentCombo->currentIndex();
335 }
336
337 QString AlbumPropsEdit::category() const
338 {
339 QString name = d->categoryCombo->currentText();
340
341 if (name.isEmpty())
342 {
343 name = i18nc("@info: album properties", "Uncategorized Album");
344 }
345
346 return name;
347 }
348
349 QStringList AlbumPropsEdit::albumCategories() const
350 {
351 QStringList Categories;
352 const ApplicationSettings* const settings = ApplicationSettings::instance();
353
354 if (settings)
355 {
356 Categories = settings->getAlbumCategoryNames();
357 }
358
359 QString currentCategory = d->categoryCombo->currentText();
360
361 if (!currentCategory.isEmpty())
362 {
363 if (Categories.indexOf(currentCategory) == -1)
364 {
365 Categories.append(currentCategory);
366 }
367 }
368
369 Categories.sort();
370
371 return Categories;
372 }
373
374 bool AlbumPropsEdit::editProps(PAlbum* const album, QString& title,
375 QString& comments, QDate& date, QString& category,
376 QStringList& albumCategories)
377 {
378 QPointer<AlbumPropsEdit> dlg = new AlbumPropsEdit(album);
379
380 bool ok = (dialogExec(dlg) == QDialog::Accepted);
381
382 title = dlg->title();
383 comments = dlg->comments();
384 date = dlg->date();
385 category = dlg->category();
386 albumCategories = dlg->albumCategories();
387
388 delete dlg;
389 return ok;
390 }
391
392 bool AlbumPropsEdit::createNew(PAlbum* const parent, QString& title, QString& comments,
393 QDate& date, QString& category, QStringList& albumCategories,
394 int& parentSelector)
395 {
396 QPointer<AlbumPropsEdit> dlg = new AlbumPropsEdit(parent, true);
397
398 bool ok = (dialogExec(dlg) == QDialog::Accepted);
399
400 title = dlg->title();
401 comments = dlg->comments();
402 date = dlg->date();
403 category = dlg->category();
404 albumCategories = dlg->albumCategories();
405 parentSelector = dlg->parent();
406
407 delete dlg;
408 return ok;
409 }
410
411 void AlbumPropsEdit::slotTitleChanged()
412 {
413 QString newtitle = d->titleEdit->text();
414 QRegularExpression emptyTitle(QRegularExpression::anchoredPattern(QLatin1String("^\\s*$")));
415 bool enable = (!emptyTitle.match(newtitle).hasMatch() && !newtitle.isEmpty());
416 d->buttons->button(QDialogButtonBox::Ok)->setEnabled(enable);
417 }
418
419 void AlbumPropsEdit::slotNewAlbumTextChanged(int index)
420 {
421 QString atitle;
422
423 if (index == 0)
424 {
425 atitle = d->album->title();
426 }
427 else
428 {
429 atitle = CollectionManager::instance()->albumRootLabel(d->album->albumRootId());
430 }
431
432 d->topLabel->setText(i18nc("@label: album properties", "Create new Album in\n\"%1\"", atitle));
433 }
434
435 void AlbumPropsEdit::slotDateLowButtonClicked()
436 {
437 setCursor(Qt::WaitCursor);
438
439 QDate lowDate = CoreDbAccess().db()->getAlbumLowestDate(d->album->id());
440
441 if (lowDate.isValid())
442 {
443 d->datePicker->setDate(lowDate);
444 }
445
446 setCursor(Qt::ArrowCursor);
447 }
448
449 void AlbumPropsEdit::slotDateHighButtonClicked()
450 {
451 setCursor(Qt::WaitCursor);
452
453 QDate highDate = CoreDbAccess().db()->getAlbumHighestDate(d->album->id());
454
455 if (highDate.isValid())
456 {
457 d->datePicker->setDate(highDate);
458 }
459
460 setCursor(Qt::ArrowCursor);
461 }
462
463 void AlbumPropsEdit::slotDateAverageButtonClicked()
464 {
465 setCursor(Qt::WaitCursor);
466
467 QDate avDate = CoreDbAccess().db()->getAlbumAverageDate(d->album->id());
468
469 setCursor(Qt::ArrowCursor);
470
471 if (avDate.isValid())
472 {
473 d->datePicker->setDate(avDate);
474 }
475 else
476 {
477 QMessageBox::critical(this, i18nc("@title:window, album properties", "Could not Calculate Average"),
478 i18nc("@info: album properties", "Could not calculate date average for this album."));
479 }
480 }
481
482 void AlbumPropsEdit::slotHelp()
483 {
484 openOnlineDocumentation(QLatin1String("left_sidebar"),
485 QLatin1String("albums_view"),
486 QLatin1String("managing-albums"));
487 }
488
489 } // namespace Digikam
490
491 #include "albumpropsedit.moc"
492
493 #include "moc_albumpropsedit.cpp"