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"
|
2015-05-29 17:42:57 +00:00
|
|
|
|
|
|
|
#include <QtConcurrent>
|
|
|
|
|
2017-12-09 21:59:21 +00:00
|
|
|
extern QHash <QString, QImage> thumbnailCache;
|
2017-12-09 23:17:57 +00:00
|
|
|
static QMutex thumbnailMutex;
|
2015-11-09 15:48:12 +00:00
|
|
|
|
2017-12-17 13:48:04 +00:00
|
|
|
static void scaleImages(PictureEntry &entry)
|
2015-05-29 17:42:57 +00:00
|
|
|
{
|
2017-12-09 23:17:57 +00:00
|
|
|
QMutexLocker l(&thumbnailMutex);
|
2017-12-09 23:07:46 +00:00
|
|
|
if (thumbnailCache.contains(entry.filename) && !thumbnailCache.value(entry.filename).isNull()) {
|
|
|
|
entry.image = thumbnailCache.value(entry.filename);
|
2017-12-09 23:17:57 +00:00
|
|
|
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))
|
2017-12-09 23:07:46 +00:00
|
|
|
thumbnailCache.insert(entry.filename, p);
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
2017-12-09 23:17:57 +00:00
|
|
|
entry.image = p;
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DivePictureModel *DivePictureModel::instance()
|
|
|
|
{
|
|
|
|
static DivePictureModel *self = new DivePictureModel();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-12-03 08:19:26 +00:00
|
|
|
DivePictureModel::DivePictureModel() : rowDDStart(0), rowDDEnd(0)
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
pictures.push_back({picture, picture->filename, QImage(), picture->offset.seconds});
|
|
|
|
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-09 23:07:46 +00:00
|
|
|
QtConcurrent::blockingMap(pictures, scaleImages);
|
2015-05-29 17:42:57 +00:00
|
|
|
|
2017-12-09 23:07:46 +00:00
|
|
|
beginInsertRows(QModelIndex(), 0, pictures.count() - 1);
|
2015-05-29 17:42:57 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2017-12-09 23:07:46 +00:00
|
|
|
ret = entry.image;
|
2015-05-29 17:42:57 +00:00
|
|
|
break;
|
|
|
|
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) {
|
|
|
|
if (dive->selected && dive_remove_picture(dive, fileUrl.toUtf8().data()))
|
|
|
|
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
|
|
|
}
|