subsurface/qt-models/divepicturemodel.cpp
Stefan Fuchs 5b7e4c57f7 Dive pictures show pictures of all selected dives
In the dive picture tab show pictures of all selected dive.
But at the same moment take care that in the profile only
pictures from displayed_dive are displayed.

Signed-off-by: Stefan Fuchs <sfuchs@gmx.de>
2017-12-13 05:48:20 -08:00

134 lines
3 KiB
C++

// 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() : rowDDStart(0), rowDDEnd(0)
{
}
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, 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, QImage(), picture->offset.seconds});
if (dive->id == displayed_dive.id)
rowDDEnd = pictures.count() - 1;
}
}
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();
}