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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2007-04-11
* Description : light table thumbs bar
*
* SPDX-FileCopyrightText: 2007-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "lighttablethumbbar.h"
// Qt includes
#include <QAction>
#include <QList>
#include <QPixmap>
#include <QPainter>
#include <QContextMenuEvent>
#include <QApplication>
#include <QMenu>
#include <QIcon>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "digikam_debug.h"
#include "coredb.h"
#include "applicationsettings.h"
#include "contextmenuhelper.h"
#include "itemfiltermodel.h"
#include "itemdragdrop.h"
#include "fileactionmngr.h"
#include "thumbnailloadthread.h"
namespace Digikam
{
template <typename T, class Container>
void removeAnyInInterval(Container& list, const T& begin, const T& end)
{
typename Container::iterator it;
for (it = list.begin() ; it != list.end() ; )
{
if (((*it) >= begin) && ((*it) <= end))
{
it = list.erase(it);
}
else
{
++it;
}
}
}
class Q_DECL_HIDDEN LightTableItemListModel : public ItemListModel
{
Q_OBJECT
public:
explicit LightTableItemListModel(QWidget* const parent)
: ItemListModel(parent)
{
}
void clearLightTableState()
{
m_leftIndexes.clear();
m_rightIndexes.clear();
}
void setExclusiveLightTableState(bool exclusive)
{
m_exclusive = exclusive;
}
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override
{
if (role == LTLeftPanelRole)
{
return m_leftIndexes.contains(index.row());
}
else if (role == LTRightPanelRole)
{
return m_rightIndexes.contains(index.row());
}
return ItemListModel::data(index, role);
}
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::DisplayRole) override
{
if (!index.isValid())
{
return false;
}
if (role == LTLeftPanelRole)
{
if (m_exclusive)
{
m_leftIndexes.clear();
}
m_leftIndexes << index.row();
return true;
}
else if (role == LTRightPanelRole)
{
if (m_exclusive)
{
m_rightIndexes.clear();
}
m_rightIndexes << index.row();
return true;
}
return ItemListModel::setData(index, value, role);
}
void imageInfosAboutToBeRemoved(int begin, int end) override
{
removeAnyInInterval(m_leftIndexes, begin, end);
removeAnyInInterval(m_rightIndexes, begin, end);
}
void imageInfosCleared() override
{
clearLightTableState();
}
protected:
QSet<int> m_leftIndexes;
QSet<int> m_rightIndexes;
bool m_exclusive = false;
};
class Q_DECL_HIDDEN LightTableThumbBar::Private
{
public:
Private() = default;
bool navigateByPair = false;
LightTableItemListModel* imageInfoModel = nullptr;
ItemFilterModel* imageFilterModel = nullptr;
ItemDragDropHandler* dragDropHandler = nullptr;
};
LightTableThumbBar::LightTableThumbBar(QWidget* const parent)
: ItemThumbnailBar(parent),
d (new Private)
{
d->imageInfoModel = new LightTableItemListModel(this);
// only one is left, only one is right at a time
d->imageInfoModel->setExclusiveLightTableState(true);
d->imageFilterModel = new ItemFilterModel(this);
d->imageFilterModel->setSourceItemModel(d->imageInfoModel);
d->imageInfoModel->setWatchFlags(d->imageFilterModel->suggestedWatchFlags());
d->imageInfoModel->setThumbnailLoadThread(ThumbnailLoadThread::defaultIconViewThread());
ApplicationSettings* const settings = ApplicationSettings::instance();
d->imageFilterModel->setCategorizationMode(ItemSortSettings::NoCategories);
d->imageFilterModel->setStringTypeNatural(settings->isStringTypeNatural());
d->imageFilterModel->setSortRole((ItemSortSettings::SortRole)settings->getImageSortOrder());
d->imageFilterModel->setSortOrder((ItemSortSettings::SortOrder)settings->getImageSorting());
d->imageFilterModel->setAllGroupsOpen(true); // disable filtering out by group, see bug #308948
d->imageFilterModel->sort(0); // an initial sorting is necessary
d->dragDropHandler = new ItemDragDropHandler(d->imageInfoModel);
d->dragDropHandler->setReadOnlyDrop(true);
d->imageInfoModel->setDragDropHandler(d->dragDropHandler);
setModels(d->imageInfoModel, d->imageFilterModel);
setSelectionMode(QAbstractItemView::SingleSelection);
connect(d->dragDropHandler, SIGNAL(itemInfosDropped(QList<ItemInfo>)),
this, SIGNAL(signalDroppedItems(QList<ItemInfo>)));
connect(d->imageInfoModel, SIGNAL(imageInfosAdded(QList<ItemInfo>)),
this, SIGNAL(signalContentChanged()));
connect(d->imageInfoModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SIGNAL(signalContentChanged()));
connect(settings, SIGNAL(setupChanged()),
this, SLOT(slotSetupChanged()));
}
LightTableThumbBar::~LightTableThumbBar()
{
delete d;
}
void LightTableThumbBar::setItems(const ItemInfoList& list)
{
for (const ItemInfo& info : std::as_const(list))
{
if (!d->imageInfoModel->hasImage(info))
{
d->imageInfoModel->addItemInfo(info);
}
}
}
void LightTableThumbBar::slotDockLocationChanged(Qt::DockWidgetArea area)<--- Derived function 'LightTableThumbBar::slotDockLocationChanged'
{
if ((area == Qt::LeftDockWidgetArea) || (area == Qt::RightDockWidgetArea))
{
if (flow() != TopToBottom)
{
setFlow(TopToBottom);
}
}
else
{
if (flow() != LeftToRight)
{
setFlow(LeftToRight);
}
}
scrollTo(currentIndex());
}
void LightTableThumbBar::clear()
{
d->imageInfoModel->clearItemInfos();
Q_EMIT signalContentChanged();
}
void LightTableThumbBar::setNavigateByPair(bool b)
{
d->navigateByPair = b;
}
void LightTableThumbBar::showContextMenuOnInfo(QContextMenuEvent* e, const ItemInfo& info)
{
// temporary actions ----------------------------------
QAction* const leftPanelAction = new QAction(QIcon::fromTheme(QLatin1String("arrow-left")), i18n("Show on left panel"), this);
QAction* const rightPanelAction = new QAction(QIcon::fromTheme(QLatin1String("arrow-right")), i18n("Show on right panel"), this);
QAction* const editAction = new QAction(QIcon::fromTheme(QLatin1String("document-edit")), i18n("Edit"), this);
QAction* const removeAction = new QAction(QIcon::fromTheme(QLatin1String("window-close")), i18n("Remove item"), this);
QAction* const clearAllAction = new QAction(QIcon::fromTheme(QLatin1String("edit-delete")), i18n("Clear all"), this);
leftPanelAction->setEnabled(d->navigateByPair ? false : true);
rightPanelAction->setEnabled(d->navigateByPair ? false : true);
clearAllAction->setEnabled(countItems() ? true : false);
// ----------------------------------------------------
QMenu popmenu(this);
ContextMenuHelper cmhelper(&popmenu);
cmhelper.addAction(leftPanelAction, true);
cmhelper.addAction(rightPanelAction, true);
cmhelper.addSeparator();
cmhelper.addAction(editAction);
cmhelper.addServicesMenu(QList<QUrl>() << info.fileUrl());
cmhelper.addSeparator();
cmhelper.addLabelsAction();
cmhelper.addSeparator();
cmhelper.addAction(removeAction);
cmhelper.addAction(clearAllAction, true);
// special action handling --------------------------------
connect(&cmhelper, SIGNAL(signalAssignPickLabel(int)),
this, SLOT(slotAssignPickLabel(int)));
connect(&cmhelper, SIGNAL(signalAssignColorLabel(int)),
this, SLOT(slotAssignColorLabel(int)));
connect(&cmhelper, SIGNAL(signalAssignRating(int)),
this, SLOT(slotAssignRating(int)));
QAction* const choice = cmhelper.exec(e->globalPos());
if (choice)
{
if (choice == leftPanelAction)
{
Q_EMIT signalSetItemOnLeftPanel(info);
}
else if (choice == rightPanelAction)
{
Q_EMIT signalSetItemOnRightPanel(info);
}
else if (choice == editAction)
{
Q_EMIT signalEditItem(info);
}
else if (choice == removeAction)
{
Q_EMIT signalRemoveItem(info);
}
else if (choice == clearAllAction)
{
Q_EMIT signalClearAll();
}
}
}
void LightTableThumbBar::slotColorLabelChanged(const QUrl& url, int color)
{
assignColorLabel(ItemInfo::fromUrl(url), color);
}
void LightTableThumbBar::slotPickLabelChanged(const QUrl& url, int pick)
{
assignPickLabel(ItemInfo::fromUrl(url), pick);
}
void LightTableThumbBar::slotAssignPickLabel(int pickId)
{
assignPickLabel(currentInfo(), pickId);
}
void LightTableThumbBar::slotAssignColorLabel(int colorId)
{
assignColorLabel(currentInfo(), colorId);
}
void LightTableThumbBar::slotRatingChanged(const QUrl& url, int rating)
{
assignRating(ItemInfo::fromUrl(url), rating);
}
void LightTableThumbBar::slotAssignRating(int rating)
{
assignRating(currentInfo(), rating);
}
void LightTableThumbBar::assignPickLabel(const ItemInfo& info, int pickId)
{
FileActionMngr::instance()->assignPickLabel(info, pickId);
}
void LightTableThumbBar::assignRating(const ItemInfo& info, int rating)
{
rating = qMin(RatingMax, qMax(RatingMin, rating));
FileActionMngr::instance()->assignRating(info, rating);
}
void LightTableThumbBar::assignColorLabel(const ItemInfo& info, int colorId)
{
FileActionMngr::instance()->assignColorLabel(info, colorId);
}
void LightTableThumbBar::slotToggleTag(const QUrl& url, int tagID)
{
toggleTag(ItemInfo::fromUrl(url), tagID);
}
void LightTableThumbBar::toggleTag(int tagID)
{
toggleTag(currentInfo(), tagID);
}
void LightTableThumbBar::toggleTag(const ItemInfo& info, int tagID)
{
if (!info.isNull())
{
if (!info.tagIds().contains(tagID))
{
FileActionMngr::instance()->assignTag(info, tagID);
}
else
{
FileActionMngr::instance()->removeTag(info, tagID);
}
}
}
void LightTableThumbBar::setOnLeftPanel(const ItemInfo& info)
{
QModelIndex index = d->imageInfoModel->indexForItemInfo(info);
// model has exclusiveLightTableState, so any previous index will be reset
d->imageInfoModel->setData(index, true, ItemModel::LTLeftPanelRole);
viewport()->update();
}
void LightTableThumbBar::setOnRightPanel(const ItemInfo& info)
{
QModelIndex index = d->imageInfoModel->indexForItemInfo(info);
// model has exclusiveLightTableState, so any previous index will be reset
d->imageInfoModel->setData(index, true, ItemModel::LTRightPanelRole);
viewport()->update();
}
bool LightTableThumbBar::isOnLeftPanel(const ItemInfo& info) const
{
return d->imageInfoModel->indexForItemInfo(info).data(ItemModel::LTLeftPanelRole).toBool();
}
bool LightTableThumbBar::isOnRightPanel(const ItemInfo& info) const
{
return d->imageInfoModel->indexForItemInfo(info).data(ItemModel::LTRightPanelRole).toBool();
}
QModelIndex LightTableThumbBar::findItemByInfo(const ItemInfo& info) const
{
if (!info.isNull())
{
return d->imageInfoModel->indexForItemInfo(info);
}
return QModelIndex();
}
ItemInfo LightTableThumbBar::findItemByIndex(const QModelIndex& index) const
{
if (index.isValid())
{
return d->imageInfoModel->imageInfo(index);
}
return ItemInfo();
}
void LightTableThumbBar::removeItemByInfo(const ItemInfo& info)
{
if (info.isNull())
{
return;
}
d->imageInfoModel->removeItemInfo(info);
}
int LightTableThumbBar::countItems() const
{
return d->imageInfoModel->rowCount();
}
void LightTableThumbBar::paintEvent(QPaintEvent* e)
{
if (!countItems())
{
QPainter p(viewport());
p.setPen(QPen(qApp->palette().color(QPalette::Text)));
p.drawText(0, 0, width(), height(),
Qt::AlignCenter | Qt::TextWordWrap,
i18n("Drag and drop images here"));
p.end();
return;
}
ItemThumbnailBar::paintEvent(e);
}
void LightTableThumbBar::slotSetupChanged()
{
ApplicationSettings* const settings = ApplicationSettings::instance();
d->imageFilterModel->setStringTypeNatural(settings->isStringTypeNatural());
d->imageFilterModel->setSortRole((ItemSortSettings::SortRole)settings->getImageSortOrder());
d->imageFilterModel->setSortOrder((ItemSortSettings::SortOrder)settings->getImageSorting());
}
} // namespace Digikam
#include "lighttablethumbbar.moc"
#include "moc_lighttablethumbbar.cpp"
|