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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2013-02-25
* Description : Table view column helpers: Thumbnail column
*
* SPDX-FileCopyrightText: 2017-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2013 by Michael G. Hansen <mike at mghansen dot de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "tableview_column_thumbnail.h"
// Qt includes
#include <QPainter>
#include <QStyleOptionViewItem>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "itemfiltermodel.h"
#include "tableview.h"
#include "thumbnailloadthread.h"
#include "thumbnailsize.h"
#include "metaengine.h"
namespace
{
const int ThumbnailBorder = 2;
}
namespace Digikam
{
namespace TableViewColumns
{
ColumnThumbnail::ColumnThumbnail(TableViewShared* const tableViewShared,
const TableViewColumnConfiguration& pConfiguration,
QWidget* const parent)
: TableViewColumn(tableViewShared, pConfiguration, parent),
m_thumbnailSize(s->tableView->getThumbnailSize().size()),
m_displayWidget(parent)
{
connect(s->thumbnailLoadThread, SIGNAL(signalThumbnailLoaded(LoadingDescription,QPixmap)),
this, SLOT(slotThumbnailLoaded(LoadingDescription,QPixmap)));
}
bool ColumnThumbnail::CreateFromConfiguration(TableViewShared* const tableViewShared,
const TableViewColumnConfiguration& pConfiguration,
TableViewColumn** const pNewColumn,
QWidget* const parent)
{
if (pConfiguration.columnId != QLatin1String("thumbnail"))
{
return false;
}
*pNewColumn = new ColumnThumbnail(tableViewShared, pConfiguration, parent);
return true;
}
TableViewColumnDescription ColumnThumbnail::getDescription()<--- Derived function 'ColumnThumbnail::getDescription'
{
return TableViewColumnDescription(QLatin1String("thumbnail"), i18n("Thumbnail"));
}
TableViewColumn::ColumnFlags ColumnThumbnail::getColumnFlags() const
{
return ColumnCustomPainting;
}
QString ColumnThumbnail::getTitle() const
{
return i18n("Thumbnail");
}
QVariant ColumnThumbnail::data(TableViewModel::Item* const item, const int role) const
{
Q_UNUSED(item)
Q_UNUSED(role)
// we do not return any data, but paint(...) something
return QVariant();
}
bool ColumnThumbnail::paint(QPainter* const painter, const QStyleOptionViewItem& option, TableViewModel::Item* const item) const
{
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.highlight());
}
const ItemInfo info = s->tableViewModel->infoFromItem(item);
if (!info.isNull())
{
QSize availableSize = option.rect.size() - QSize(ThumbnailBorder, ThumbnailBorder);
QSize imageSize = info.dimensions();
int maxSize = m_thumbnailSize;
// When the image is rotated, swap width and height.
if (info.orientation() > MetaEngine::ORIENTATION_VFLIP)
{
imageSize.transpose();
}
if (imageSize.isValid() && (imageSize.width() > imageSize.height()))
{
// for landscape pictures, try to use all available horizontal space
qreal scaleFactor = qreal(availableSize.height()) / qreal(imageSize.height());
maxSize = imageSize.width() * scaleFactor;
}
// Calculate the maximum thumbnail size
// The idea here is that for landscape images, we adjust the height to
// to be as high as the row height as long as the width can stretch enough
// because the column is wider than the thumbnail size.
maxSize = qMin(maxSize, availableSize.width());
// However, digiKam limits the thumbnail size, so we also do that here
maxSize = qMin(maxSize, (int)ThumbnailSize::maxThumbsSize());
double dpr = m_displayWidget->devicePixelRatio();
maxSize = qRound((double)maxSize * dpr);
QPixmap thumbnail;
if (s->thumbnailLoadThread->find(info.thumbnailIdentifier(), thumbnail, maxSize))
{
thumbnail.setDevicePixelRatio(dpr);
/// @todo Is slotThumbnailLoaded still called when the thumbnail is found right away?
QSize pixmapSize = (QSizeF(thumbnail.size()) / dpr).toSize();
pixmapSize = pixmapSize.boundedTo(availableSize);
QSize alignSize = option.rect.size();
QPoint startPoint((alignSize.width() - pixmapSize.width()) / 2,
(alignSize.height() - pixmapSize.height()) / 2);
startPoint += option.rect.topLeft();
painter->drawPixmap(QRect(startPoint, pixmapSize), thumbnail,
QRect(QPoint(0, 0), thumbnail.size()));
return true;
}
}
// we did not get to paint a thumbnail...
return false;
}
QSize ColumnThumbnail::sizeHint(const QStyleOptionViewItem& option, TableViewModel::Item* const item) const
{
Q_UNUSED(option)
Q_UNUSED(item)
/// @todo On portrait pictures, the borders are too close. There should be a gap. Is this setting okay?
const int thumbnailSizeWithBorder = m_thumbnailSize+ThumbnailBorder;
return QSize(thumbnailSizeWithBorder, thumbnailSizeWithBorder);
}
void ColumnThumbnail::slotThumbnailLoaded(const LoadingDescription& loadingDescription, const QPixmap& thumb)
{
if (thumb.isNull())
{
return;
}
/// @todo Find a way to do this without the ItemFilterModel
const QModelIndex imageModelIndex = s->imageModel->indexForPath(loadingDescription.filePath);
if (!imageModelIndex.isValid())
{
return;
}
const qlonglong imageId = s->imageModel->imageId(imageModelIndex);
Q_EMIT signalDataChanged(imageId);
}
void ColumnThumbnail::updateThumbnailSize()
{
/// @todo Set minimum column width to m_thumbnailSize
m_thumbnailSize = s->tableView->getThumbnailSize().size();
Q_EMIT signalAllDataChanged();
}
} // namespace TableViewColumns
} // namespace Digikam
#include "moc_tableview_column_thumbnail.cpp"
|