2017-04-27 18:25:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "qt-models/divepicturemodel.h"
|
|
|
|
#include "core/dive.h"
|
|
|
|
#include "core/metrics.h"
|
|
|
|
#include "core/divelist.h"
|
|
|
|
#include "core/imagedownloader.h"
|
2018-04-29 20:07:05 +00:00
|
|
|
#include "core/qthelper.h"
|
|
|
|
#include "core/metadata.h"
|
2015-05-29 17:42:57 +00:00
|
|
|
|
|
|
|
#include <QtConcurrent>
|
|
|
|
|
2017-12-17 15:17:38 +00:00
|
|
|
static const int maxZoom = 3; // Maximum zoom: thrice of standard size
|
2015-11-09 15:48:12 +00:00
|
|
|
|
2017-12-17 15:17:38 +00:00
|
|
|
static QImage getThumbnailFromCache(const PictureEntry &entry)
|
2015-05-29 17:42:57 +00:00
|
|
|
{
|
2018-04-29 20:07:05 +00:00
|
|
|
// First, check if we know a hash for this filename
|
|
|
|
QString filename = thumbnailFileName(entry.filename);
|
|
|
|
if (filename.isEmpty())
|
|
|
|
return QImage();
|
|
|
|
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
return QImage();
|
|
|
|
QDataStream stream(&file);
|
|
|
|
|
|
|
|
// Each thumbnail file is composed of a media-type and an image file.
|
|
|
|
// Currently, the type is ignored. This will be used to mark videos.
|
|
|
|
quint32 type;
|
|
|
|
QImage res;
|
|
|
|
stream >> type;
|
|
|
|
stream >> res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addThumbnailToCache(const QImage &thumbnail, const PictureEntry &entry)
|
|
|
|
{
|
|
|
|
if (thumbnail.isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString filename = thumbnailFileName(entry.filename);
|
|
|
|
|
|
|
|
// If we got a thumbnail, we are guaranteed to have its hash and therefore
|
|
|
|
// thumbnailFileName() should return a filename.
|
|
|
|
if (filename.isEmpty()) {
|
|
|
|
qWarning() << "Internal error: can't get filename of recently created thumbnail";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSaveFile file(filename);
|
|
|
|
if (!file.open(QIODevice::WriteOnly))
|
|
|
|
return;
|
|
|
|
QDataStream stream(&file);
|
|
|
|
|
|
|
|
// For format of the file, see comments in getThumnailForCache
|
|
|
|
quint32 type = MEDIATYPE_PICTURE;
|
|
|
|
stream << type;
|
|
|
|
stream << thumbnail;
|
|
|
|
file.commit();
|
2017-12-17 15:17:38 +00:00
|
|
|
}
|
2017-12-09 23:17:57 +00:00
|
|
|
|
2018-04-11 04:56:46 +00:00
|
|
|
static void scaleImages(PictureEntry &entry, int maxSize)
|
2017-12-17 15:17:38 +00:00
|
|
|
{
|
|
|
|
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)) {
|
2018-03-25 14:46:16 +00:00
|
|
|
qDebug() << "No thumbnail in cache for" << entry.filename;
|
2018-03-07 15:37:31 +00:00
|
|
|
thumbnail = getHashedImage(QString(entry.picture->filename));
|
2018-04-29 20:07:05 +00:00
|
|
|
addThumbnailToCache(thumbnail, entry);
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
2017-12-17 15:17:38 +00:00
|
|
|
|
2018-04-11 04:56:46 +00:00
|
|
|
entry.image = thumbnail;
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DivePictureModel *DivePictureModel::instance()
|
|
|
|
{
|
|
|
|
static DivePictureModel *self = new DivePictureModel();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-12-17 15:17:38 +00:00
|
|
|
DivePictureModel::DivePictureModel() : rowDDStart(0),
|
|
|
|
rowDDEnd(0),
|
2018-04-11 04:56:46 +00:00
|
|
|
zoomLevel(0.0),
|
|
|
|
defaultSize(defaultIconMetrics().sz_pic)
|
2015-05-29 17:42:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-09 21:59:21 +00:00
|
|
|
void DivePictureModel::updateDivePicturesWhenDone(QList<QFuture<void>> futures)
|
2015-05-29 17:42:57 +00:00
|
|
|
{
|
|
|
|
Q_FOREACH (QFuture<void> f, futures) {
|
|
|
|
f.waitForFinished();
|
|
|
|
}
|
|
|
|
updateDivePictures();
|
|
|
|
}
|
|
|
|
|
2017-12-17 15:17:38 +00:00
|
|
|
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;
|
2018-04-11 04:56:46 +00:00
|
|
|
updateZoom();
|
2017-12-17 15:17:38 +00:00
|
|
|
layoutChanged();
|
|
|
|
}
|
|
|
|
|
2018-04-11 04:56:46 +00:00
|
|
|
void DivePictureModel::updateZoom()
|
2017-12-17 15:17:38 +00:00
|
|
|
{
|
|
|
|
// 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.
|
2018-04-11 04:56:46 +00:00
|
|
|
size = static_cast<int>(round(defaultSize * pow(maxZoom, zoomLevel)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePictureModel::updateThumbnails()
|
|
|
|
{
|
2017-12-17 15:17:38 +00:00
|
|
|
int maxSize = defaultSize * maxZoom;
|
2018-04-11 04:56:46 +00:00
|
|
|
updateZoom();
|
|
|
|
QtConcurrent::blockingMap(pictures, [maxSize](PictureEntry &entry){scaleImages(entry, maxSize);});
|
2017-12-17 15:17:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 17:42:57 +00:00
|
|
|
void DivePictureModel::updateDivePictures()
|
|
|
|
{
|
2017-12-09 23:07:46 +00:00
|
|
|
if (!pictures.isEmpty()) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, pictures.count() - 1);
|
|
|
|
pictures.clear();
|
2015-05-29 17:42:57 +00:00
|
|
|
endRemoveRows();
|
2017-12-13 18:32:29 +00:00
|
|
|
rowDDStart = rowDDEnd = 0;
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 08:19:26 +00:00
|
|
|
// if the dive_table is empty, quit
|
|
|
|
if (dive_table.nr == 0)
|
2015-05-29 17:42:57 +00:00
|
|
|
return;
|
|
|
|
|
2017-12-03 08:19:26 +00:00
|
|
|
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)
|
2018-04-11 04:56:46 +00:00
|
|
|
pictures.push_back({picture, picture->filename, {}, picture->offset.seconds});
|
2017-12-03 08:19:26 +00:00
|
|
|
if (dive->id == displayed_dive.id)
|
2017-12-13 18:32:29 +00:00
|
|
|
rowDDEnd = pictures.count();
|
2017-12-03 08:19:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-17 15:17:38 +00:00
|
|
|
|
|
|
|
updateThumbnails();
|
2015-05-29 17:42:57 +00:00
|
|
|
|
2017-12-24 13:28:04 +00:00
|
|
|
if (!pictures.isEmpty()) {
|
|
|
|
beginInsertRows(QModelIndex(), 0, pictures.count() - 1);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DivePictureModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2016-03-08 05:26:23 +00:00
|
|
|
Q_UNUSED(parent);
|
2015-05-29 17:42:57 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DivePictureModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
QVariant ret;
|
|
|
|
if (!index.isValid())
|
|
|
|
return ret;
|
|
|
|
|
2017-12-09 23:07:46 +00:00
|
|
|
const PictureEntry &entry = pictures.at(index.row());
|
2015-05-29 17:42:57 +00:00
|
|
|
if (index.column() == 0) {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::ToolTipRole:
|
2017-12-09 23:07:46 +00:00
|
|
|
ret = entry.filename;
|
2015-05-29 17:42:57 +00:00
|
|
|
break;
|
|
|
|
case Qt::DecorationRole:
|
2018-04-11 04:56:46 +00:00
|
|
|
ret = entry.image.scaled(size, size, Qt::KeepAspectRatio);
|
2015-05-29 17:42:57 +00:00
|
|
|
break;
|
2017-12-17 15:17:38 +00:00
|
|
|
case Qt::UserRole: // Used by profile widget to access bigger thumbnails
|
2018-04-11 04:56:46 +00:00
|
|
|
ret = entry.image.scaled(defaultSize, defaultSize, Qt::KeepAspectRatio);
|
2017-12-17 15:17:38 +00:00
|
|
|
break;
|
2015-05-29 17:42:57 +00:00
|
|
|
case Qt::DisplayRole:
|
2017-12-09 23:07:46 +00:00
|
|
|
ret = QFileInfo(entry.filename).fileName();
|
2015-05-29 17:42:57 +00:00
|
|
|
break;
|
|
|
|
case Qt::DisplayPropertyRole:
|
2017-12-09 23:07:46 +00:00
|
|
|
ret = QFileInfo(entry.filename).filePath();
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
} else if (index.column() == 1) {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::UserRole:
|
2017-12-09 23:07:46 +00:00
|
|
|
ret = QVariant::fromValue(entry.offsetSeconds);
|
2017-12-09 21:59:21 +00:00
|
|
|
break;
|
2015-05-29 17:42:57 +00:00
|
|
|
case Qt::DisplayRole:
|
2017-12-09 23:07:46 +00:00
|
|
|
ret = entry.filename;
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-20 19:03:53 +00:00
|
|
|
void DivePictureModel::removePicture(const QString &fileUrl, bool last)
|
2015-05-29 17:42:57 +00:00
|
|
|
{
|
2017-12-11 20:40:06 +00:00
|
|
|
int i;
|
|
|
|
struct dive *dive;
|
|
|
|
for_each_dive (i, dive) {
|
2018-02-25 12:51:41 +00:00
|
|
|
if (dive->selected && dive_remove_picture(dive, qPrintable(fileUrl)))
|
2017-12-11 20:40:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-10-20 19:03:53 +00:00
|
|
|
if (last) {
|
|
|
|
copy_dive(current_dive, &displayed_dive);
|
|
|
|
updateDivePictures();
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
}
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DivePictureModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2016-03-08 05:26:23 +00:00
|
|
|
Q_UNUSED(parent);
|
2017-12-09 23:07:46 +00:00
|
|
|
return pictures.count();
|
2015-08-06 13:14:18 +00:00
|
|
|
}
|