1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2004-07-01
* Description : dialog to edit and create digiKam Tags
*
* SPDX-FileCopyrightText: 2004-2005 by Renchi Raju <renchi dot raju at gmail dot com>
* SPDX-FileCopyrightText: 2006-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "tageditdlg.h"
// Qt includes
#include <QGridLayout>
#include <QLabel>
#include <QPointer>
#include <QTreeWidget>
#include <QApplication>
#include <QStyle>
#include <QStandardPaths>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QPushButton>
#include <QRegularExpression>
// KDE includes
#include <klocalizedstring.h>
#include <kkeysequencewidget.h>
#ifdef HAVE_KICONTHEMES
# include <kicondialog.h>
#endif
// Local includes
#include "digikam_debug.h"
#include "digikam_globals.h"
#include "album.h"
#include "syncjob.h"
#include "searchtextbar.h"
#include "tagsactionmngr.h"
#include "coredbconstants.h"
#include "dxmlguiwindow.h"
#include "dexpanderbox.h"
#include "dlayoutbox.h"
namespace Digikam
{
class Q_DECL_HIDDEN TagsListCreationErrorDialog : public QDialog
{
Q_OBJECT
public:
TagsListCreationErrorDialog(QWidget* const parent, const QMap<QString, QString>& errMap);
~TagsListCreationErrorDialog() override {};
};
// ------------------------------------------------------------------------------
class Q_DECL_HIDDEN TagEditDlg::Private
{
public:
Private() = default;
bool create = false;
QLabel* topLabel = nullptr;
QString icon;
QPushButton* iconButton = nullptr;
QPushButton* resetIconButton = nullptr;
QDialogButtonBox* buttons = nullptr;
KKeySequenceWidget* keySeqWidget = nullptr;
TAlbum* mainRootAlbum = nullptr;
SearchTextBar* titleEdit = nullptr;
};
TagEditDlg::TagEditDlg(QWidget* const parent, TAlbum* const album, bool create)
: QDialog(parent),
d (new Private)
{
setModal(true);
d->buttons = new QDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
d->buttons->button(QDialogButtonBox::Ok)->setDefault(true);
if (create)
{
setWindowTitle(i18nc("@title:window", "New Tag"));
}
else
{
setWindowTitle(i18nc("@title:window", "Edit Tag"));
}
d->mainRootAlbum = album;
d->create = create;
QWidget* const page = new QWidget(this);
// --------------------------------------------------------
QGridLayout* const grid = new QGridLayout(page);
QLabel* const logo = new QLabel(page);
logo->setPixmap(QIcon::fromTheme(QLatin1String("digikam")).pixmap(QSize(48,48)));
d->topLabel = new QLabel(page);
d->topLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
d->topLabel->setWordWrap(false);
DLineWidget* const line = new DLineWidget(Qt::Horizontal, page);
// --------------------------------------------------------
QLabel* const titleLabel = new QLabel(page);
titleLabel->setText(i18nc("@label: tag properties", "&Title:"));
d->titleEdit = new SearchTextBar(page, QLatin1String("TagEditDlgTitleEdit"), i18n("Enter tag name here..."));
d->titleEdit->setCaseSensitive(false);
titleLabel->setBuddy(d->titleEdit);
QLabel* const tipLabel = new QLabel(page);
tipLabel->setTextFormat(Qt::RichText);
tipLabel->setWordWrap(true);
tipLabel->setText(i18n("<p>To create new tags, you can use the following rules:</p>"
"<p><ul><li>'/' can be used to create a tags hierarchy.<br/>"
"Ex.: <i>\"Country/City/Paris\"</i></li>"
"<li>',' can be used to create more than one tags hierarchy at the same time.<br/>"
"Ex.: <i>\"City/Paris, Monument/Notre-Dame\"</i></li>"
"<li>If a tag hierarchy starts with '/', root tag album is used as parent.</li></ul></p>"
));
if (d->create)
{
QStringList tagPaths;
AlbumList tList = AlbumManager::instance()->allTAlbums();
for (AlbumList::const_iterator it = tList.constBegin() ; it != tList.constEnd() ; ++it)
{
TAlbum* const tag = static_cast<TAlbum*>(*it);<--- Variable 'tag' can be declared as pointer to const
if (tag && !tag->isInternalTag())
{
tagPaths << tag->tagPath();
}
}
d->titleEdit->completerModel()->setList(tagPaths);
}
else
{
d->titleEdit->setText(d->mainRootAlbum->title());
tipLabel->hide();
}
// --------------------------------------------------------
QLabel* const iconTextLabel = new QLabel(page);
iconTextLabel->setText(i18n("&Icon:"));
d->iconButton = new QPushButton(page);
d->iconButton->setFixedSize(40, 40);
d->iconButton->setIcon(SyncJob::getTagThumbnail(album));
iconTextLabel->setBuddy(d->iconButton);
// In create mode, by default assign the icon of the parent (if not root) to this new tag.
d->icon = album->icon();
d->resetIconButton = new QPushButton(QIcon::fromTheme(QLatin1String("view-refresh")), i18n("Reset"), page);
#ifndef HAVE_KICONTHEMES
iconTextLabel->hide();
d->iconButton->hide();
d->resetIconButton->hide();
#endif
// --------------------------------------------------------
QLabel* const kscTextLabel = new QLabel(page);
kscTextLabel->setText(i18n("&Shortcut:"));
d->keySeqWidget = new KKeySequenceWidget(page);
kscTextLabel->setBuddy(d->keySeqWidget);
if (!create)
{
QString Seq = album->property(TagPropertyName::tagKeyboardShortcut());
d->keySeqWidget->setKeySequence(Seq);
}
else
{
// Do not inherit tag shortcut, it creates a conflict shortcut, see bug 309558.
d->keySeqWidget->setCheckActionCollections(TagsActionMngr::defaultManager()->actionCollections());
}
QLabel* const tipLabel2 = new QLabel(page);
tipLabel2->setTextFormat(Qt::RichText);
tipLabel2->setWordWrap(true);
tipLabel2->setText(i18n("<p><b>Note</b>: this shortcut can be used to assign or unassign tag to items.</p>"));
// --------------------------------------------------------
const int spacing = layoutSpacing();
const int cmargin = layoutMargin();
grid->addWidget(logo, 0, 0, 1, 1);
grid->addWidget(d->topLabel, 0, 1, 1, 4);
grid->addWidget(line, 1, 0, 1, 4);
grid->addWidget(tipLabel, 2, 0, 1, 4);
grid->addWidget(titleLabel, 3, 0, 1, 1);
grid->addWidget(d->titleEdit, 3, 1, 1, 3);
grid->addWidget(iconTextLabel, 4, 0, 1, 1);
grid->addWidget(d->iconButton, 4, 1, 1, 1);
grid->addWidget(d->resetIconButton, 4, 2, 1, 1);
grid->addWidget(kscTextLabel, 5, 0, 1, 1);
grid->addWidget(d->keySeqWidget, 5, 1, 1, 3);
grid->addWidget(tipLabel2, 6, 0, 1, 4);
grid->setRowStretch(7, 10);
grid->setColumnStretch(3, 10);
grid->setContentsMargins(cmargin, cmargin, cmargin, cmargin);
grid->setSpacing(spacing);
QVBoxLayout* const vbx = new QVBoxLayout(this);
vbx->addWidget(page);
vbx->addWidget(d->buttons);
setLayout(vbx);
// --------------------------------------------------------
connect(d->iconButton, SIGNAL(clicked()),
this, SLOT(slotIconChanged()));
connect(d->resetIconButton, SIGNAL(clicked()),
this, SLOT(slotIconResetClicked()));
connect(d->titleEdit, SIGNAL(textChanged(QString)),
this, SLOT(slotTitleChanged(QString)));
connect(d->buttons->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
this, SLOT(accept()));
connect(d->buttons->button(QDialogButtonBox::Cancel), SIGNAL(clicked()),
this, SLOT(reject()));
connect(d->buttons->button(QDialogButtonBox::Help), SIGNAL(clicked()),
this, SLOT(slotHelp()));
// --------------------------------------------------------
slotTitleChanged(d->titleEdit->text());
d->titleEdit->setFocus();
adjustSize();
}
TagEditDlg::~TagEditDlg()
{
delete d;
}
QString TagEditDlg::title() const
{
return d->titleEdit->text();
}
QString TagEditDlg::icon() const
{
return d->icon;
}
QKeySequence TagEditDlg::shortcut() const
{
return d->keySeqWidget->keySequence();
}
void TagEditDlg::slotIconResetClicked()
{
d->icon = d->mainRootAlbum->standardIconName();
d->iconButton->setIcon(QIcon::fromTheme(d->icon));
}
void TagEditDlg::slotIconChanged()
{
#ifdef HAVE_KICONTHEMES
QPointer<KIconDialog> dlg = new KIconDialog(this);
dlg->setup(KIconLoader::NoGroup, KIconLoader::Application, false, 20, false, false, false);
QString icon = dlg->openDialog();
delete dlg;
if (icon.isEmpty() || (icon == d->icon))
{
return;
}
d->icon = icon;
d->iconButton->setIcon(QIcon::fromTheme(d->icon));
#endif
}
void TagEditDlg::slotTitleChanged(const QString& newtitle)
{
QString tagName = d->mainRootAlbum->tagPath();
if (tagName.endsWith(QLatin1Char('/')) && !d->mainRootAlbum->isRoot())
{
tagName.truncate(tagName.length()-1);
}
if (d->create)
{
if (d->titleEdit->text().startsWith(QLatin1Char('/')))
{
d->topLabel->setText(i18n("<b>Create New Tag</b>"));
}
else
{
d->topLabel->setText(i18n("<b>Create New Tag in<br/>"
"\"%1\"</b>", tagName));
}
}
else
{
d->topLabel->setText(i18n("<b>Properties of Tag<br/>"
"\"%1\"</b>", tagName));
}
QRegularExpression emptyTitle(QRegularExpression::anchoredPattern(QLatin1String("^\\s*$")));
bool enable = (!emptyTitle.match(newtitle).hasMatch() && !newtitle.isEmpty());
d->buttons->button(QDialogButtonBox::Ok)->setEnabled(enable);
}
bool TagEditDlg::tagEdit(QWidget* const parent, TAlbum* const album, QString& title, QString& icon, QKeySequence& ks)
{
QPointer<TagEditDlg> dlg = new TagEditDlg(parent, album);
bool valRet = dlg->exec();
if (valRet == QDialog::Accepted)
{
title = dlg->title();
icon = dlg->icon();
ks = dlg->shortcut();
}
delete dlg;
return valRet;
}
bool TagEditDlg::tagCreate(QWidget* const parent, TAlbum* const album, QString& title, QString& icon, QKeySequence& ks)
{
QPointer<TagEditDlg> dlg = new TagEditDlg(parent, album, true);
bool valRet = dlg->exec();
if (valRet == QDialog::Accepted)
{
title = dlg->title();
icon = dlg->icon();
ks = dlg->shortcut();
}
delete dlg;
return valRet;
}
AlbumList TagEditDlg::createTAlbum(TAlbum* const mainRootAlbum, const QString& tagStr, const QString& icon,
const QKeySequence& ks, QMap<QString, QString>& errMap)
{
errMap.clear();
AlbumList createdTagsList;
TAlbum* root = nullptr;
// Check if new tags are include in a list of tags hierarchy separated by ','.
// Ex: /Country/France/people,/City/France/Paris
const QStringList tagsHierarchies = tagStr.split(QLatin1Char(','), Qt::SkipEmptyParts);
if (tagsHierarchies.isEmpty())
{
return createdTagsList;
}
for (QStringList::const_iterator it = tagsHierarchies.constBegin() ;
it != tagsHierarchies.constEnd() ; ++it)
{
QString hierarchy = (*it).trimmed();
if (!hierarchy.isEmpty())
{
// Check if new tags is a hierarchy of tags separated by '/'.
root = nullptr;
if (hierarchy.startsWith(QLatin1Char('/')) || !mainRootAlbum)
{
root = AlbumManager::instance()->findTAlbum(0);
}
else
{
root = mainRootAlbum;
}
QStringList tagsList = hierarchy.split(QLatin1Char('/'), Qt::SkipEmptyParts);
qCDebug(DIGIKAM_GENERAL_LOG) << tagsList;
if (!tagsList.isEmpty())
{
for (QStringList::const_iterator it2 = tagsList.constBegin() ;
it2 != tagsList.constEnd() ; ++it2)
{
QString tagPath, errMsg;
QString tag = (*it2).trimmed();
if (root->isRoot())
{
tagPath = QString::fromUtf8("/%1").arg(tag);
}
else
{
tagPath = QString::fromUtf8("%1/%2").arg(root->tagPath()).arg(tag);
}
qCDebug(DIGIKAM_GENERAL_LOG) << tag << " :: " << tagPath;
if (!tag.isEmpty())
{
// Tag already exist ?
TAlbum* const album = AlbumManager::instance()->findTAlbum(tagPath);
if (!album)
{
root = AlbumManager::instance()->createTAlbum(root, tag, icon, errMsg);
}
else
{
root = album;
if (*it2 == tagsList.last())
{
errMap.insert(tagPath, i18n("Tag name already exists"));
}
}
if (root)
{
createdTagsList.append(root);
}
}
// Sanity check if tag creation failed.
if (!root)
{
errMap.insert(tagPath, errMsg);
break;
}
}
}
}
}
// Assign the keyboard shortcut to the last tag created from the hierarchy.
if (root && !ks.isEmpty())
{
TagsActionMngr::defaultManager()->updateTagShortcut(root->id(), ks);
}
return createdTagsList;
}
void TagEditDlg::showtagsListCreationError(QWidget* const parent, const QMap<QString, QString>& errMap)
{
if (!errMap.isEmpty())
{
QPointer<TagsListCreationErrorDialog> dlg = new TagsListCreationErrorDialog(parent, errMap);
dlg->exec();
delete dlg;
}
}
void TagEditDlg::slotHelp()
{
openOnlineDocumentation(QLatin1String("main_window"), QLatin1String("tags_view"), QLatin1String("managing-tags"));
}
// ------------------------------------------------------------------------------
TagsListCreationErrorDialog::TagsListCreationErrorDialog(QWidget* const parent, const QMap<QString, QString>& errMap)
: QDialog(parent)
{
setModal(true);
setWindowTitle(i18nc("@title:window", "Tag Creation Error"));
const int spacing = layoutSpacing();
const int cmargin = qMin(style()->pixelMetric(QStyle::PM_LayoutLeftMargin),
qMin(style()->pixelMetric(QStyle::PM_LayoutTopMargin),
qMin(style()->pixelMetric(QStyle::PM_LayoutRightMargin),
style()->pixelMetric(QStyle::PM_LayoutBottomMargin))));
QDialogButtonBox* const buttons = new QDialogButtonBox(QDialogButtonBox::Ok, this);
buttons->button(QDialogButtonBox::Ok)->setDefault(true);
QWidget* const page = new QWidget(this);
QVBoxLayout* const vLay = new QVBoxLayout(page);
QLabel* const label = new QLabel(i18n("An error occurred during tag creation:"), page);<--- Variable 'label' can be declared as pointer to const
QTreeWidget* const listView = new QTreeWidget(page);
listView->setHeaderLabels(QStringList() << i18n("Tag Path") << i18n("Error"));
listView->setRootIsDecorated(false);
listView->setSelectionMode(QAbstractItemView::SingleSelection);
listView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
vLay->addWidget(label);
vLay->addWidget(listView);
vLay->setContentsMargins(cmargin, cmargin, cmargin, cmargin);
vLay->setSpacing(spacing);
for (QMap<QString, QString>::const_iterator it = errMap.constBegin() ;
it != errMap.constEnd() ; ++it)
{
new QTreeWidgetItem(listView, QStringList() << it.key() << it.value());
}
QVBoxLayout* const vbx = new QVBoxLayout(this);
vbx->addWidget(page);
vbx->addWidget(buttons);
setLayout(vbx);
connect(buttons->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
this, SLOT(accept()));
adjustSize();
}
} // namespace Digikam
#include "tageditdlg.moc"
#include "moc_tageditdlg.cpp"
|