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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2009-11-21
* Description : Qt Model for Tag - drag and drop handling
*
* SPDX-FileCopyrightText: 2009 by Johannes Wienke <languitar at semipol dot de>
* SPDX-FileCopyrightText: 2013 by Veaceslav Munteanu <veaceslav dot munteanu90 at gmail dot com>
* SPDX-FileCopyrightText: 2013-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "tagdragdrop.h"
#include "facetags.h"
#include "digikamitemview_p.h"
// Qt includes
#include <QDropEvent>
#include <QMenu>
#include <QIcon>
#include <QMessageBox>
#include <QApplication>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "digikam_debug.h"
#include "albummanager.h"
#include "ddragobjects.h"
#include "iteminfo.h"
#include "albumtreeview.h"
namespace Digikam
{
TagDragDropHandler::TagDragDropHandler(TagModel* const model)
: AlbumModelDragDropHandler(model)
{
}
TagModel* TagDragDropHandler::model() const
{
return (static_cast<TagModel*>(m_model));
}
bool TagDragDropHandler::dropEvent(QAbstractItemView* view,
const QDropEvent* e,
const QModelIndex& droppedOn)
{
if (accepts(e, droppedOn) == Qt::IgnoreAction)
{
return false;
}
TAlbum* const destAlbum = model()->talbumForIndex(droppedOn);
if (DTagListDrag::canDecode(e->mimeData()))
{
QList<int> tagIDs;
if (!DTagListDrag::decode(e->mimeData(), tagIDs))
{
return false;
}
if (tagIDs.isEmpty())
{
return false;
}
QMenu popMenu(view);
QAction* const gotoAction = popMenu.addAction(QIcon::fromTheme(QLatin1String("go-jump")), i18n("&Move Here"));<--- Variable 'gotoAction' can be declared as pointer to const
QAction* const mergeAction = popMenu.addAction(QIcon::fromTheme(QLatin1String("merge")), i18n("M&erge Here"));<--- Variable 'mergeAction' can be declared as pointer to const
popMenu.addSeparator();
popMenu.addAction(QIcon::fromTheme(QLatin1String("dialog-cancel")), i18n("C&ancel"));
popMenu.setMouseTracking(true);
QAction* const choice = popMenu.exec(QCursor::pos());<--- Variable 'choice' can be declared as pointer to const
for (int index = 0 ; index < tagIDs.count() ; ++index)
{
TAlbum* const talbum = AlbumManager::instance()->findTAlbum(tagIDs.at(index));
if (!talbum)
{
return false;
}
if (destAlbum && (talbum == destAlbum))
{
return false;
}
if (choice == gotoAction)
{
TAlbum* newParentTag = nullptr;
if (!destAlbum)
{
// move dragItem to the root
newParentTag = AlbumManager::instance()->findTAlbum(0);
}
else
{
// move dragItem as child of dropItem
newParentTag = destAlbum;
}
QString errMsg;
if (!AlbumManager::instance()->moveTAlbum(talbum, newParentTag, errMsg))
{
QMessageBox::critical(view, qApp->applicationName(), errMsg);
}
if (view && !view->isVisible())
{
view->setVisible(true);
}
}
else if (choice == mergeAction)
{
if (!destAlbum)
{
return false;
}
QString errMsg;
if (!AlbumManager::instance()->mergeTAlbum(talbum, destAlbum, true, errMsg))
{
QMessageBox::critical(view, qApp->applicationName(), errMsg);
}
if (view && !view->isVisible())
{
view->setVisible(true);
}
}
}
return true;
}
else if (DItemDrag::canDecode(e->mimeData()))
{
QList<QUrl> urls;
QList<int> albumIDs;
QList<qlonglong> imageIDs;
if (!DItemDrag::decode(e->mimeData(), urls, albumIDs, imageIDs))
{
return false;
}
if (urls.isEmpty() || albumIDs.isEmpty() || imageIDs.isEmpty())
{
return false;
}
if (destAlbum->id() == FaceTags::personParentTag())
{
return false;
}
// here the user dragged a "faceitem" to a "person" album
if (destAlbum->hasProperty(TagPropertyName::person()))
{
// use dropped on album
QString targetName = destAlbum->title();
DigikamItemView* const dview = qobject_cast<DigikamItemView*>(e->source());
if (dview)
{
// get selected indexes
QModelIndexList selectedIndexes = dview->selectionModel()->selectedIndexes();
// get the tag ids for each of the selected faces
QList<int> faceIds = dview->getFaceIds(selectedIndexes);
// create list with face names
QStringList faceNames;
for (const int& faceId : std::as_const(faceIds))
{
// check that all selected faces are the same person
if (!faceId || faceId != faceIds.first())
{
return false;
}
faceNames << FaceTags::faceNameForTag(faceId);
}
// here we set a new thumbnail
if ((imageIDs.size() == 1) && (destAlbum->id() == faceIds.first()))
{
bool set = false;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
if (e->modifiers() == Qt::ControlModifier)
#else
if (e->keyboardModifiers() == Qt::ControlModifier)
#endif
{
set = true;
}
else
{
QMenu popMenu(view);
QAction* const setAction = popMenu.addAction(i18n("Set as Tag Thumbnail"));<--- Variable 'setAction' can be declared as pointer to const
popMenu.addSeparator();
popMenu.addAction(QIcon::fromTheme(QLatin1String("dialog-cancel")), i18n("C&ancel"));
popMenu.setMouseTracking(true);
QAction* const choice = popMenu.exec(QCursor::pos());<--- Variable 'choice' can be declared as pointer to const
set = (choice == setAction);
}
if (set)
{
QString errMsg;
AlbumManager::instance()->updateTAlbumIcon(destAlbum, QString(), imageIDs.first(), errMsg);
}
return true;
}
else if (destAlbum->id() != faceIds.first())
{
//here we move assign a new face tag to the selected faces
if (destAlbum->id() == FaceTags::unconfirmedPersonTagId())
{
return false;
}
bool assign = false;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
if (e->modifiers() == Qt::ControlModifier)
#else
if (e->keyboardModifiers() == Qt::ControlModifier)
#endif
{
assign = true;
}
else
{
QMenu popMenu(view);
QAction* const assignAction = popMenu.addAction(QIcon::fromTheme(QLatin1String("tag")),<--- Variable 'assignAction' can be declared as pointer to const
i18np("Change face name from '%2' to '%3'",
"Change face names from '%2' to '%3'",
faceNames.count(),
faceNames.first(),
targetName));
popMenu.addSeparator();
popMenu.addAction(QIcon::fromTheme(QLatin1String("dialog-cancel")), i18n("C&ancel"));
popMenu.setMouseTracking(true);
QAction* const choice = popMenu.exec(QCursor::pos());<--- Variable 'choice' can be declared as pointer to const
assign = (choice == assignAction);
}
if (assign)
{
if (destAlbum->id() == FaceTags::unknownPersonTagId())
{
dview->unknownFaces(selectedIndexes);
}
else if (destAlbum->id() == FaceTags::ignoredPersonTagId())
{
dview->ignoreFaces(selectedIndexes);
}
else
{
int dstId = destAlbum->parent() ? destAlbum->parent()->id() : -1;
int tagId = FaceTags::getOrCreateTagForPerson(targetName, dstId);
dview->confirmFaces(selectedIndexes, tagId);
}
}
return true;
}
return false;
}
}
// If a ctrl key is pressed while dropping the drag object,
// the tag is assigned to the images without showing a
// popup menu.
bool hasSameTopId = false;
bool assign = false;
bool set = false;
// Use selected tags instead of dropped on.
QList<int> tagIdList;
QStringList tagNames;
AbstractAlbumTreeView* const tview = dynamic_cast<AbstractAlbumTreeView*>(view);
if (!tview)
{
return false;
}
if (imageIDs.size() == 1)
{
const int topId = AlbumManager::instance()->findTopId(destAlbum->id());
const auto tids = ItemInfo(imageIDs.first()).tagIds();
for (int tid : tids)
{
if (AlbumManager::instance()->findTopId(tid) == topId)
{<--- Consider using std::any_of algorithm instead of a raw loop.
hasSameTopId = true;
break;
}
}
}
QList<Album*> selTags = tview->selectedItems();
for (int it = 0 ; it < selTags.count() ; ++it)
{
TAlbum* const temp = dynamic_cast<TAlbum*>(selTags.at(it));<--- Variable 'temp' can be declared as pointer to const
if (temp)
{
tagIdList << temp->id();
tagNames << temp->title();
}
}
// If nothing selected, use dropped on tag
if (tagIdList.isEmpty())
{
tagIdList << destAlbum->id();
tagNames << destAlbum->title();
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
if (e->modifiers() == Qt::ControlModifier)
#else
if (e->keyboardModifiers() == Qt::ControlModifier)
#endif
{
assign = true;
}
else
{
QMenu popMenu(view);
QAction* setAction = nullptr;<--- Variable 'setAction' can be declared as pointer to const
QAction* const assignAction = popMenu.addAction(QIcon::fromTheme(QLatin1String("tag")),<--- Variable 'assignAction' can be declared as pointer to const
i18n("Assign Tag(s) '%1' to Items",
tagNames.join(QLatin1String(", "))));
if (hasSameTopId)
{
setAction = popMenu.addAction(i18n("Set as Tag Thumbnail"));
}
popMenu.addSeparator();
popMenu.addAction(QIcon::fromTheme(QLatin1String("dialog-cancel")), i18n("C&ancel"));
popMenu.setMouseTracking(true);
QAction* const choice = popMenu.exec(QCursor::pos());<--- Variable 'choice' can be declared as pointer to const
assign = (choice == assignAction);
set = (setAction && (choice == setAction));
}
if (assign)
{
Q_EMIT assignTags(imageIDs, tagIdList);
}
else if (set)
{
QString errMsg;
AlbumManager::instance()->updateTAlbumIcon(destAlbum, QString(), imageIDs.first(), errMsg);
}
return true;
}
return false;
}
Qt::DropAction TagDragDropHandler::accepts(const QDropEvent* e, const QModelIndex& dropIndex)
{
TAlbum* const destAlbum = model()->talbumForIndex(dropIndex);
if (DTagListDrag::canDecode(e->mimeData()))
{
/*
int droppedId = 0;
*/
QList<int> droppedId;
if (!DTagListDrag::decode(e->mimeData(), droppedId))
{
qCDebug(DIGIKAM_GENERAL_LOG) << "List decode error" << droppedId.isEmpty();
return Qt::IgnoreAction;
}
TAlbum* const droppedAlbum = AlbumManager::instance()->findTAlbum(droppedId.first());<--- Variable 'droppedAlbum' can be declared as pointer to const
if (!droppedAlbum)
{
return Qt::IgnoreAction;
}
// Allow dragging on empty space to move the dragged album under the root albumForIndex
// unless the itemDrag is already at root level
if (!destAlbum)
{
Album* const palbum = droppedAlbum->parent();<--- Variable 'palbum' can be declared as pointer to const
if (!palbum)
{
return Qt::IgnoreAction;
}
if (palbum->isRoot())
{
return Qt::IgnoreAction;
}
else
{
return Qt::MoveAction;
}
}
// Dragging an item on itself makes no sense
if (destAlbum == droppedAlbum)
{
return Qt::IgnoreAction;
}
// Dragging a parent on its child makes no sense
if (droppedAlbum && droppedAlbum->isAncestorOf(destAlbum))
{
return Qt::IgnoreAction;
}
return Qt::MoveAction;
}
else if (DItemDrag::canDecode(e->mimeData()) && destAlbum && destAlbum->parent())
{
// Only other possibility is image items being dropped
// And allow this only if there is a Tag to be dropped
// on and also the Tag is not root.
return Qt::CopyAction;
}
return Qt::IgnoreAction;
}
QStringList TagDragDropHandler::mimeTypes() const
{
QStringList mimeTypes;
mimeTypes << DTagListDrag::mimeTypes()
<< DItemDrag::mimeTypes();
return mimeTypes;
}
QMimeData* TagDragDropHandler::createMimeData(const QList<Album*>& albums)
{
if (albums.isEmpty())
{
qCDebug(DIGIKAM_GENERAL_LOG) << "Cannot drag no tag";
return nullptr;
}
QList<int> ids;
for (Album* const album : std::as_const(albums))<--- Variable 'album' can be declared as pointer to const
{
ids << album->id();
}
return new DTagListDrag(ids);
}
} // namespace Digikam
#include "moc_tagdragdrop.cpp"
|