summaryrefslogtreecommitdiffstats
path: root/qt-models/divepicturemodel.cpp
blob: 41035aba2ef0d4089f51996d1d529b5880b97052 (plain) (blame)
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
// 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;

void scaleImages(PictureEntry &entry)
{
	QMutexLocker l(&thumbnailMutex);
	if (thumbnailCache.contains(entry.filename) && !thumbnailCache.value(entry.filename).isNull()) {
		entry.image = thumbnailCache.value(entry.filename);
		return;
	}
	l.unlock();

	int dim = defaultIconMetrics().sz_pic;
	QImage p = SHashedImage(entry.picture);
	if(!p.isNull()) {
		p = p.scaled(dim, dim, Qt::KeepAspectRatio);
		QMutexLocker l(&thumbnailMutex);
		if (!thumbnailCache.contains(entry.filename))
			thumbnailCache.insert(entry.filename, p);
	}
	entry.image = p;
}

DivePictureModel *DivePictureModel::instance()
{
	static DivePictureModel *self = new DivePictureModel();
	return self;
}

DivePictureModel::DivePictureModel()
{
}

void DivePictureModel::updateDivePicturesWhenDone(QList<QFuture<void>> futures)
{
	Q_FOREACH (QFuture<void> f, futures) {
		f.waitForFinished();
	}
	updateDivePictures();
}

void DivePictureModel::updateDivePictures()
{
	if (!pictures.isEmpty()) {
		beginRemoveRows(QModelIndex(), 0, pictures.count() - 1);
		pictures.clear();
		endRemoveRows();
	}

	// if the dive_table is empty, ignore the displayed_dive
	if (dive_table.nr == 0 || dive_get_picture_count(&displayed_dive) == 0)
		return;

	FOR_EACH_PICTURE_NON_PTR(displayed_dive)
		pictures.push_back({picture, picture->filename, QImage(), picture->offset.seconds});

	QtConcurrent::blockingMap(pictures, scaleImages);

	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::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)
{
	dive_remove_picture(fileUrl.toUtf8().data());
	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();
}