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"
|
2015-05-29 17:42:57 +00:00
|
|
|
|
2018-03-10 13:15:50 +00:00
|
|
|
#include <QFileInfo>
|
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),
|
2018-03-11 09:19:08 +00:00
|
|
|
defaultSize(Thumbnailer::defaultThumbnailSize())
|
2015-05-29 17:42:57 +00:00
|
|
|
{
|
2018-03-10 15:36:20 +00:00
|
|
|
connect(Thumbnailer::instance(), &Thumbnailer::thumbnailChanged,
|
|
|
|
this, &DivePictureModel::updateThumbnail, Qt::QueuedConnection);
|
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
|
|
|
{
|
2018-03-11 09:19:08 +00:00
|
|
|
size = Thumbnailer::thumbnailSize(zoomLevel);
|
2018-04-11 04:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePictureModel::updateThumbnails()
|
|
|
|
{
|
|
|
|
updateZoom();
|
2018-03-10 13:15:50 +00:00
|
|
|
for (PictureEntry &entry: pictures)
|
2018-03-11 09:19:08 +00:00
|
|
|
entry.image = Thumbnailer::instance()->fetchThumbnail(entry);
|
2017-12-17 15:17:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 17:42:57 +00:00
|
|
|
void DivePictureModel::updateDivePictures()
|
|
|
|
{
|
2018-05-18 18:33:01 +00:00
|
|
|
beginResetModel();
|
2017-12-09 23:07:46 +00:00
|
|
|
if (!pictures.isEmpty()) {
|
|
|
|
pictures.clear();
|
2017-12-13 18:32:29 +00:00
|
|
|
rowDDStart = rowDDEnd = 0;
|
2018-03-10 13:15:50 +00:00
|
|
|
Thumbnailer::instance()->clearWorkQueue();
|
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();
|
2018-05-18 18:33:01 +00:00
|
|
|
endResetModel();
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-18 19:57:18 +00:00
|
|
|
// Return true if we actually removed a picture
|
|
|
|
static bool removePictureFromSelectedDive(const char *fileUrl)
|
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-05-18 19:57:18 +00:00
|
|
|
if (dive->selected && dive_remove_picture(dive, fileUrl))
|
|
|
|
return true;
|
2015-10-20 19:03:53 +00:00
|
|
|
}
|
2018-05-18 19:57:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePictureModel::removePictures(const QVector<QString> &fileUrls)
|
|
|
|
{
|
|
|
|
bool removed = false;
|
|
|
|
for (const QString &fileUrl: fileUrls)
|
|
|
|
removed |= removePictureFromSelectedDive(qPrintable(fileUrl));
|
|
|
|
if (!removed)
|
|
|
|
return;
|
|
|
|
copy_dive(current_dive, &displayed_dive);
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
|
2018-05-19 19:18:39 +00:00
|
|
|
for (int i = 0; i < pictures.size(); ++i) {
|
|
|
|
// Find range [i j) of pictures to remove
|
|
|
|
if (std::find(fileUrls.begin(), fileUrls.end(), pictures[i].filename) == fileUrls.end())
|
|
|
|
continue;
|
|
|
|
int j;
|
|
|
|
for (j = i + 1; j < pictures.size(); ++j) {
|
|
|
|
if (std::find(fileUrls.begin(), fileUrls.end(), pictures[j].filename) == fileUrls.end())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Qt's model-interface is surprisingly idiosyncratic: you don't pass [first last), but [first last] ranges.
|
|
|
|
// For example, an empty list would be [0 -1].
|
|
|
|
beginRemoveRows(QModelIndex(), i, j - 1);
|
|
|
|
pictures.erase(pictures.begin() + i, pictures.begin() + j);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
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
|
|
|
}
|
2018-03-10 15:36:20 +00:00
|
|
|
|
2018-05-01 10:35:18 +00:00
|
|
|
int DivePictureModel::findPictureId(const QString &filename)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < pictures.size(); ++i)
|
|
|
|
if (pictures[i].filename == filename)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-10 15:36:20 +00:00
|
|
|
void DivePictureModel::updateThumbnail(QString filename, QImage thumbnail)
|
|
|
|
{
|
2018-05-01 10:35:18 +00:00
|
|
|
int i = findPictureId(filename);
|
|
|
|
if (i >= 0) {
|
2018-03-10 15:36:20 +00:00
|
|
|
pictures[i].image = thumbnail;
|
|
|
|
emit dataChanged(createIndex(i, 0), createIndex(i, 1));
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 10:35:18 +00:00
|
|
|
|
|
|
|
void DivePictureModel::updateDivePictureOffset(const QString &filename, int offsetSeconds)
|
|
|
|
{
|
|
|
|
int i = findPictureId(filename);
|
|
|
|
if (i >= 0) {
|
|
|
|
pictures[i].offsetSeconds = offsetSeconds;
|
|
|
|
emit dataChanged(createIndex(i, 0), createIndex(i, 1));
|
|
|
|
}
|
|
|
|
}
|