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
|
// SPDX-License-Identifier: GPL-2.0
#include "qt-models/divepicturemodel.h"
#include "core/dive.h"
#include "core/metrics.h"
#include "core/divelist.h"
#include "core/imagedownloader.h"
#include <QtConcurrent>
extern QHash <QString, QImage> thumbnailCache;
static QMutex thumbnailMutex;
static const int maxZoom = 3; // Maximum zoom: thrice of standard size
static QImage getThumbnailFromCache(const PictureEntry &entry)
{
QMutexLocker l(&thumbnailMutex);
return thumbnailCache.value(entry.filename);
}
static void scaleImages(PictureEntry &entry, int size, int maxSize)
{
QImage thumbnail = getThumbnailFromCache(entry);
// If thumbnails were written by an earlier version, they might be smaller than needed.
// Rescale in such a case to avoid resizing artifacts.
if (thumbnail.isNull() || (thumbnail.size().width() < maxSize && thumbnail.size().height() < maxSize)) {
thumbnail = SHashedImage(entry.picture).scaled(maxSize, maxSize, Qt::KeepAspectRatio);
QMutexLocker l(&thumbnailMutex);
thumbnailCache.insert(entry.filename, thumbnail);
}
entry.imageProfile = thumbnail.scaled(maxSize / maxZoom, maxSize / maxZoom, Qt::KeepAspectRatio);
entry.image = size == maxSize ? thumbnail
: thumbnail.scaled(size, size, Qt::KeepAspectRatio);
}
DivePictureModel *DivePictureModel::instance()
{
static DivePictureModel *self = new DivePictureModel();
return self;
}
DivePictureModel::DivePictureModel() : rowDDStart(0),
rowDDEnd(0),
zoomLevel(0.0)
{
}
void DivePictureModel::updateDivePicturesWhenDone(QList<QFuture<void>> futures)
{
Q_FOREACH (QFuture<void> f, futures) {
f.waitForFinished();
}
updateDivePictures();
}
void DivePictureModel::setZoomLevel(int level)
{
zoomLevel = level / 10.0;
// zoomLevel is bound by [-1.0 1.0], see comment below.
if (zoomLevel < -1.0)
zoomLevel = -1.0;
if (zoomLevel > 1.0)
zoomLevel = 1.0;
updateThumbnails();
layoutChanged();
}
void DivePictureModel::updateThumbnails()
{
// Calculate size of thumbnails. The standard size is defaultIconMetrics().sz_pic.
// We use exponential scaling so that the central point is the standard
// size and the minimum and maximum extreme points are a third respectively
// three times the standard size.
// Naturally, these three zoom levels are then represented by
// -1.0 (minimum), 0 (standard) and 1.0 (maximum). The actual size is
// calculated as standard_size*3.0^zoomLevel.
int defaultSize = defaultIconMetrics().sz_pic;
int maxSize = defaultSize * maxZoom;
int size = static_cast<int>(round(defaultSize * pow(maxZoom, zoomLevel)));
QtConcurrent::blockingMap(pictures, [size, maxSize](PictureEntry &entry){scaleImages(entry, size, maxSize);});
}
void DivePictureModel::updateDivePictures()
{
if (!pictures.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, pictures.count() - 1);
pictures.clear();
endRemoveRows();
rowDDStart = rowDDEnd = 0;
}
// if the dive_table is empty, quit
if (dive_table.nr == 0)
return;
int i;
struct dive *dive;
for_each_dive (i, dive) {
if (dive->selected) {
if (dive->id == displayed_dive.id)
rowDDStart = pictures.count();
FOR_EACH_PICTURE(dive)
pictures.push_back({picture, picture->filename, {}, {}, picture->offset.seconds});
if (dive->id == displayed_dive.id)
rowDDEnd = pictures.count();
}
}
updateThumbnails();
if (!pictures.isEmpty()) {
beginInsertRows(QModelIndex(), 0, pictures.count() - 1);
endInsertRows();
}
}
int DivePictureModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;
}
QVariant DivePictureModel::data(const QModelIndex &index, int role) const
{
QVariant ret;
if (!index.isValid())
return ret;
const PictureEntry &entry = pictures.at(index.row());
if (index.column() == 0) {
switch (role) {
case Qt::ToolTipRole:
ret = entry.filename;
break;
case Qt::DecorationRole:
ret = entry.image;
break;
case Qt::UserRole: // Used by profile widget to access bigger thumbnails
ret = entry.imageProfile;
break;
case Qt::DisplayRole:
ret = QFileInfo(entry.filename).fileName();
break;
case Qt::DisplayPropertyRole:
ret = QFileInfo(entry.filename).filePath();
}
} else if (index.column() == 1) {
switch (role) {
case Qt::UserRole:
ret = QVariant::fromValue(entry.offsetSeconds);
break;
case Qt::DisplayRole:
ret = entry.filename;
}
}
return ret;
}
void DivePictureModel::removePicture(const QString &fileUrl, bool last)
{
int i;
struct dive *dive;
for_each_dive (i, dive) {
if (dive->selected && dive_remove_picture(dive, fileUrl.toUtf8().data()))
break;
}
if (last) {
copy_dive(current_dive, &displayed_dive);
updateDivePictures();
mark_divelist_changed(true);
}
}
int DivePictureModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return pictures.count();
}
|