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"
|
2020-04-17 21:18:58 +00:00
|
|
|
#include "core/divelist.h" // for comp_dives
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/metrics.h"
|
|
|
|
#include "core/imagedownloader.h"
|
2020-04-10 07:42:14 +00:00
|
|
|
#include "core/picture.h"
|
2018-04-29 20:07:05 +00:00
|
|
|
#include "core/qthelper.h"
|
2022-09-24 12:06:56 +00:00
|
|
|
#include "core/range.h"
|
2020-09-29 22:01:24 +00:00
|
|
|
#include "core/selection.h"
|
2020-04-14 20:07:00 +00:00
|
|
|
#include "core/subsurface-qt/divelistnotifier.h"
|
2020-04-17 21:18:58 +00:00
|
|
|
#include "commands/command.h"
|
2015-05-29 17:42:57 +00:00
|
|
|
|
2018-03-10 13:15:50 +00:00
|
|
|
#include <QFileInfo>
|
2018-07-25 15:26:56 +00:00
|
|
|
#include <QPainter>
|
2015-05-29 17:42:57 +00:00
|
|
|
|
2020-04-17 21:18:58 +00:00
|
|
|
PictureEntry::PictureEntry(dive *dIn, const PictureObj &p) : d(dIn),
|
|
|
|
filename(p.filename),
|
|
|
|
offsetSeconds(p.offset.seconds),
|
|
|
|
length({ 0 })
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PictureEntry::PictureEntry(dive *dIn, const picture &p) : d(dIn),
|
|
|
|
filename(p.filename),
|
|
|
|
offsetSeconds(p.offset.seconds),
|
|
|
|
length({ 0 })
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: it is crucial that this uses the same sorting as the core.
|
|
|
|
// Therefore, we use the C strcmp functions [std::string::operator<()
|
|
|
|
// should give the same result].
|
|
|
|
bool PictureEntry::operator<(const PictureEntry &p2) const
|
|
|
|
{
|
|
|
|
if (int cmp = comp_dives(d, p2.d))
|
|
|
|
return cmp < 0;
|
|
|
|
if (offsetSeconds != p2.offsetSeconds)
|
|
|
|
return offsetSeconds < p2.offsetSeconds;
|
|
|
|
return strcmp(filename.c_str(), p2.filename.c_str()) < 0;
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:42:57 +00:00
|
|
|
DivePictureModel *DivePictureModel::instance()
|
|
|
|
{
|
|
|
|
static DivePictureModel *self = new DivePictureModel();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-06-30 09:36:37 +00:00
|
|
|
DivePictureModel::DivePictureModel() : zoomLevel(0.0)
|
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);
|
2020-04-14 20:07:00 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::pictureOffsetChanged,
|
|
|
|
this, &DivePictureModel::pictureOffsetChanged);
|
2020-04-17 21:18:58 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::picturesRemoved,
|
|
|
|
this, &DivePictureModel::picturesRemoved);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::picturesAdded,
|
|
|
|
this, &DivePictureModel::picturesAdded);
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
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)
|
pictures: turn QString into std::string for filenames
For undo of picture manipulation, it will be crucial that the
model and the core have the same order of pictures. The first
sort criterion will be time, the second filename in the case
that two pictures have, for whatever reason, the same timestamp.
However in the core we us C-strings and thus sort byte-wise
using strcmp. In the Qt-part we use QStrings, which sort according
to unicode encoding. To enable consistent sorting, change the
Qt-part to std::string, which uses a C-style 0-terminated string
as its backing store.
One might argue that in general filenames should use system-encoding
and therefore use std::string instead of QString. However, a
broader conversion to std::string turned out to be very painful,
since Qt is (deliberately?) difficult to use with std::string.
Notable all the file-manipulation functions don't take std::string
by default. Thus, this commit only converts the internal data
of DivePictureModel, but continues to use QString for the Qt-facing
interface.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 07:47:55 +00:00
|
|
|
entry.image = Thumbnailer::instance()->fetchThumbnail(QString::fromStdString(entry.filename), false);
|
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();
|
2020-04-17 20:43:43 +00:00
|
|
|
if (!pictures.empty()) {
|
2017-12-09 23:07:46 +00:00
|
|
|
pictures.clear();
|
2018-03-10 13:15:50 +00:00
|
|
|
Thumbnailer::instance()->clearWorkQueue();
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 22:01:24 +00:00
|
|
|
for (struct dive *dive: getDiveSelection()) {
|
|
|
|
size_t first = pictures.size();
|
|
|
|
FOR_EACH_PICTURE(dive)
|
|
|
|
pictures.push_back(PictureEntry(dive, *picture));
|
|
|
|
|
|
|
|
// Sort pictures of this dive by offset.
|
|
|
|
// Thus, the list will be sorted by (dive, offset).
|
|
|
|
std::sort(pictures.begin() + first, pictures.end(),
|
|
|
|
[](const PictureEntry &a, const PictureEntry &b) { return a.offsetSeconds < b.offsetSeconds; });
|
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
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int DivePictureModel::columnCount(const QModelIndex&) const
|
2015-05-29 17:42:57 +00:00
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DivePictureModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
2020-04-10 07:50:57 +00:00
|
|
|
return QVariant();
|
2015-05-29 17:42:57 +00:00
|
|
|
|
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:
|
pictures: turn QString into std::string for filenames
For undo of picture manipulation, it will be crucial that the
model and the core have the same order of pictures. The first
sort criterion will be time, the second filename in the case
that two pictures have, for whatever reason, the same timestamp.
However in the core we us C-strings and thus sort byte-wise
using strcmp. In the Qt-part we use QStrings, which sort according
to unicode encoding. To enable consistent sorting, change the
Qt-part to std::string, which uses a C-style 0-terminated string
as its backing store.
One might argue that in general filenames should use system-encoding
and therefore use std::string instead of QString. However, a
broader conversion to std::string turned out to be very painful,
since Qt is (deliberately?) difficult to use with std::string.
Notable all the file-manipulation functions don't take std::string
by default. Thus, this commit only converts the internal data
of DivePictureModel, but continues to use QString for the Qt-facing
interface.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 07:47:55 +00:00
|
|
|
return QString::fromStdString(entry.filename);
|
2015-05-29 17:42:57 +00:00
|
|
|
case Qt::DecorationRole:
|
2020-04-10 07:50:57 +00:00
|
|
|
return entry.image.scaled(size, size, Qt::KeepAspectRatio);
|
2015-05-29 17:42:57 +00:00
|
|
|
case Qt::DisplayRole:
|
pictures: turn QString into std::string for filenames
For undo of picture manipulation, it will be crucial that the
model and the core have the same order of pictures. The first
sort criterion will be time, the second filename in the case
that two pictures have, for whatever reason, the same timestamp.
However in the core we us C-strings and thus sort byte-wise
using strcmp. In the Qt-part we use QStrings, which sort according
to unicode encoding. To enable consistent sorting, change the
Qt-part to std::string, which uses a C-style 0-terminated string
as its backing store.
One might argue that in general filenames should use system-encoding
and therefore use std::string instead of QString. However, a
broader conversion to std::string turned out to be very painful,
since Qt is (deliberately?) difficult to use with std::string.
Notable all the file-manipulation functions don't take std::string
by default. Thus, this commit only converts the internal data
of DivePictureModel, but continues to use QString for the Qt-facing
interface.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 07:47:55 +00:00
|
|
|
return QFileInfo(QString::fromStdString(entry.filename)).fileName();
|
2015-05-29 17:42:57 +00:00
|
|
|
case Qt::DisplayPropertyRole:
|
pictures: turn QString into std::string for filenames
For undo of picture manipulation, it will be crucial that the
model and the core have the same order of pictures. The first
sort criterion will be time, the second filename in the case
that two pictures have, for whatever reason, the same timestamp.
However in the core we us C-strings and thus sort byte-wise
using strcmp. In the Qt-part we use QStrings, which sort according
to unicode encoding. To enable consistent sorting, change the
Qt-part to std::string, which uses a C-style 0-terminated string
as its backing store.
One might argue that in general filenames should use system-encoding
and therefore use std::string instead of QString. However, a
broader conversion to std::string turned out to be very painful,
since Qt is (deliberately?) difficult to use with std::string.
Notable all the file-manipulation functions don't take std::string
by default. Thus, this commit only converts the internal data
of DivePictureModel, but continues to use QString for the Qt-facing
interface.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 07:47:55 +00:00
|
|
|
return QFileInfo(QString::fromStdString(entry.filename)).filePath();
|
2019-04-14 14:19:23 +00:00
|
|
|
case Qt::UserRole + 1:
|
2020-04-10 07:50:57 +00:00
|
|
|
return entry.offsetSeconds;
|
2019-04-14 14:19:23 +00:00
|
|
|
case Qt::UserRole + 2:
|
2020-04-10 07:50:57 +00:00
|
|
|
return entry.length.seconds;
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
} else if (index.column() == 1) {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
pictures: turn QString into std::string for filenames
For undo of picture manipulation, it will be crucial that the
model and the core have the same order of pictures. The first
sort criterion will be time, the second filename in the case
that two pictures have, for whatever reason, the same timestamp.
However in the core we us C-strings and thus sort byte-wise
using strcmp. In the Qt-part we use QStrings, which sort according
to unicode encoding. To enable consistent sorting, change the
Qt-part to std::string, which uses a C-style 0-terminated string
as its backing store.
One might argue that in general filenames should use system-encoding
and therefore use std::string instead of QString. However, a
broader conversion to std::string turned out to be very painful,
since Qt is (deliberately?) difficult to use with std::string.
Notable all the file-manipulation functions don't take std::string
by default. Thus, this commit only converts the internal data
of DivePictureModel, but continues to use QString for the Qt-facing
interface.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 07:47:55 +00:00
|
|
|
return QString::fromStdString(entry.filename);
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-10 07:50:57 +00:00
|
|
|
return QVariant();
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 21:18:58 +00:00
|
|
|
void DivePictureModel::removePictures(const QModelIndexList &indices)
|
2015-05-29 17:42:57 +00:00
|
|
|
{
|
2020-04-17 21:18:58 +00:00
|
|
|
// Collect pictures to remove by dive
|
|
|
|
std::vector<Command::PictureListForDeletion> pics;
|
|
|
|
for (const QModelIndex &idx: indices) {
|
|
|
|
if (!idx.isValid())
|
|
|
|
continue;
|
|
|
|
const PictureEntry &item = pictures[idx.row()];
|
|
|
|
// Check if we already have pictures for that dive.
|
|
|
|
auto it = find_if(pics.begin(), pics.end(),
|
|
|
|
[&item](const Command::PictureListForDeletion &list)
|
|
|
|
{ return list.d == item.d; });
|
|
|
|
// If not found, add a new list
|
|
|
|
if (it == pics.end())
|
|
|
|
pics.push_back({ item.d, { item.filename }});
|
|
|
|
else
|
|
|
|
it->filenames.push_back(item.filename);
|
2015-10-20 19:03:53 +00:00
|
|
|
}
|
2020-04-17 21:18:58 +00:00
|
|
|
Command::removePictures(pics);
|
2018-05-18 19:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 21:18:58 +00:00
|
|
|
void DivePictureModel::picturesRemoved(dive *d, QVector<QString> filenamesIn)
|
2018-05-18 19:57:18 +00:00
|
|
|
{
|
pictures: turn QString into std::string for filenames
For undo of picture manipulation, it will be crucial that the
model and the core have the same order of pictures. The first
sort criterion will be time, the second filename in the case
that two pictures have, for whatever reason, the same timestamp.
However in the core we us C-strings and thus sort byte-wise
using strcmp. In the Qt-part we use QStrings, which sort according
to unicode encoding. To enable consistent sorting, change the
Qt-part to std::string, which uses a C-style 0-terminated string
as its backing store.
One might argue that in general filenames should use system-encoding
and therefore use std::string instead of QString. However, a
broader conversion to std::string turned out to be very painful,
since Qt is (deliberately?) difficult to use with std::string.
Notable all the file-manipulation functions don't take std::string
by default. Thus, this commit only converts the internal data
of DivePictureModel, but continues to use QString for the Qt-facing
interface.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 07:47:55 +00:00
|
|
|
// Transform vector of QStrings into vector of std::strings
|
2020-04-17 21:18:58 +00:00
|
|
|
std::vector<std::string> filenames;
|
|
|
|
filenames.reserve(filenamesIn.size());
|
|
|
|
std::transform(filenamesIn.begin(), filenamesIn.end(), std::back_inserter(filenames),
|
pictures: turn QString into std::string for filenames
For undo of picture manipulation, it will be crucial that the
model and the core have the same order of pictures. The first
sort criterion will be time, the second filename in the case
that two pictures have, for whatever reason, the same timestamp.
However in the core we us C-strings and thus sort byte-wise
using strcmp. In the Qt-part we use QStrings, which sort according
to unicode encoding. To enable consistent sorting, change the
Qt-part to std::string, which uses a C-style 0-terminated string
as its backing store.
One might argue that in general filenames should use system-encoding
and therefore use std::string instead of QString. However, a
broader conversion to std::string turned out to be very painful,
since Qt is (deliberately?) difficult to use with std::string.
Notable all the file-manipulation functions don't take std::string
by default. Thus, this commit only converts the internal data
of DivePictureModel, but continues to use QString for the Qt-facing
interface.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 07:47:55 +00:00
|
|
|
[] (const QString &s) { return s.toStdString(); });
|
|
|
|
|
2020-04-17 21:18:58 +00:00
|
|
|
// Get range of pictures of the given dive.
|
|
|
|
// Note: we could be more efficient by either using a binary search or a two-level data structure.
|
|
|
|
auto from = std::find_if(pictures.begin(), pictures.end(), [d](const PictureEntry &e) { return e.d == d; });
|
|
|
|
auto to = std::find_if(from, pictures.end(), [d](const PictureEntry &e) { return e.d != d; });
|
|
|
|
if (from == pictures.end())
|
2018-05-18 19:57:18 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-17 21:18:58 +00:00
|
|
|
size_t fromIdx = from - pictures.begin();
|
|
|
|
size_t toIdx = to - pictures.begin();
|
|
|
|
for (size_t i = fromIdx; i < toIdx; ++i) {
|
2018-05-19 19:18:39 +00:00
|
|
|
// Find range [i j) of pictures to remove
|
2020-04-17 21:18:58 +00:00
|
|
|
if (std::find(filenames.begin(), filenames.end(), pictures[i].filename) == filenames.end())
|
2018-05-19 19:18:39 +00:00
|
|
|
continue;
|
2020-04-17 20:43:43 +00:00
|
|
|
size_t j;
|
2020-04-17 21:18:58 +00:00
|
|
|
for (j = i + 1; j < toIdx; ++j) {
|
|
|
|
if (std::find(filenames.begin(), filenames.end(), pictures[j].filename) == filenames.end())
|
2018-05-19 19:18:39 +00:00
|
|
|
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();
|
2020-04-17 21:18:58 +00:00
|
|
|
toIdx -= j - i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assumes that pics is sorted!
|
|
|
|
void DivePictureModel::picturesAdded(dive *d, QVector<PictureObj> picsIn)
|
|
|
|
{
|
|
|
|
// We only display pictures of selected dives
|
|
|
|
if (!d->selected || picsIn.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Convert the picture-data into our own format
|
|
|
|
std::vector<PictureEntry> pics;
|
|
|
|
pics.reserve(picsIn.size());
|
2022-10-30 20:24:13 +00:00
|
|
|
for (const PictureObj &pic: picsIn)
|
|
|
|
pics.push_back(PictureEntry(d, pic));
|
2020-04-17 21:18:58 +00:00
|
|
|
|
|
|
|
// Insert batch-wise to avoid too many reloads
|
|
|
|
pictures.reserve(pictures.size() + pics.size());
|
|
|
|
auto from = pics.begin();
|
|
|
|
int dest = 0;
|
|
|
|
while (from != pics.end()) {
|
|
|
|
// Search for the insertion index. This supposes a lexicographical sort for the [dive, offset, filename] triple.
|
|
|
|
// TODO: currently this works, because all undo commands that manipulate the dive list also reset the selection
|
|
|
|
// and thus the model is rebuilt. However, we might catch the respective signals here and not rely on being
|
|
|
|
// called by the tab-widgets.
|
|
|
|
auto dest_it = std::lower_bound(pictures.begin() + dest, pictures.end(), *from);
|
|
|
|
int dest = dest_it - pictures.begin();
|
|
|
|
auto to = dest_it == pictures.end() ? pics.end() : from + 1; // If at the end - just add the rest
|
|
|
|
while (to != pics.end() && *to < *dest_it)
|
|
|
|
++to;
|
|
|
|
int batch_size = to - from;
|
|
|
|
beginInsertRows(QModelIndex(), dest, dest + batch_size - 1);
|
|
|
|
pictures.insert(pictures.begin() + dest, from, to);
|
|
|
|
// Get thumbnails of inserted pictures
|
|
|
|
for (auto it = pictures.begin() + dest; it < pictures.begin() + dest + batch_size; ++it)
|
|
|
|
it->image = Thumbnailer::instance()->fetchThumbnail(QString::fromStdString(it->filename), false);
|
|
|
|
endInsertRows();
|
|
|
|
from = to;
|
|
|
|
dest += batch_size;
|
2018-05-19 19:18:39 +00:00
|
|
|
}
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int DivePictureModel::rowCount(const QModelIndex&) const
|
2015-05-29 17:42:57 +00:00
|
|
|
{
|
2020-04-17 20:43:43 +00:00
|
|
|
return (int)pictures.size();
|
2015-08-06 13:14:18 +00:00
|
|
|
}
|
2018-03-10 15:36:20 +00:00
|
|
|
|
pictures: turn QString into std::string for filenames
For undo of picture manipulation, it will be crucial that the
model and the core have the same order of pictures. The first
sort criterion will be time, the second filename in the case
that two pictures have, for whatever reason, the same timestamp.
However in the core we us C-strings and thus sort byte-wise
using strcmp. In the Qt-part we use QStrings, which sort according
to unicode encoding. To enable consistent sorting, change the
Qt-part to std::string, which uses a C-style 0-terminated string
as its backing store.
One might argue that in general filenames should use system-encoding
and therefore use std::string instead of QString. However, a
broader conversion to std::string turned out to be very painful,
since Qt is (deliberately?) difficult to use with std::string.
Notable all the file-manipulation functions don't take std::string
by default. Thus, this commit only converts the internal data
of DivePictureModel, but continues to use QString for the Qt-facing
interface.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 07:47:55 +00:00
|
|
|
int DivePictureModel::findPictureId(const std::string &filename)
|
2018-05-01 10:35:18 +00:00
|
|
|
{
|
2022-10-31 18:35:15 +00:00
|
|
|
return index_of_if(pictures, [&filename](const PictureEntry &p)
|
|
|
|
{ return p.filename == filename; });
|
2018-05-01 10:35:18 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:26:56 +00:00
|
|
|
static void addDurationToThumbnail(QImage &img, duration_t duration)
|
|
|
|
{
|
|
|
|
int seconds = duration.seconds;
|
|
|
|
if (seconds < 0)
|
|
|
|
return;
|
|
|
|
QString s = seconds >= 3600 ?
|
|
|
|
QStringLiteral("%1:%2:%3").arg(seconds / 3600, 2, 10, QChar('0'))
|
|
|
|
.arg((seconds % 3600) / 60, 2, 10, QChar('0'))
|
|
|
|
.arg(seconds % 60, 2, 10, QChar('0')) :
|
|
|
|
QStringLiteral("%1:%2").arg(seconds / 60, 2, 10, QChar('0'))
|
|
|
|
.arg(seconds % 60, 2, 10, QChar('0'));
|
|
|
|
|
|
|
|
QFont font(system_divelist_default_font, 30);
|
|
|
|
QFontMetrics metrics(font);
|
|
|
|
QSize size = metrics.size(Qt::TextSingleLine, s);
|
|
|
|
QSize imgSize = img.size();
|
|
|
|
int x = imgSize.width() - size.width();
|
|
|
|
int y = imgSize.height() - size.height() + metrics.descent();
|
|
|
|
QPainter painter(&img);
|
|
|
|
painter.setBrush(Qt::white);
|
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
painter.drawRect(x, y, size.width(), size.height() - metrics.descent());
|
|
|
|
painter.setFont(font);
|
|
|
|
painter.setPen(Qt::black);
|
|
|
|
painter.drawText(x, imgSize.height(), s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePictureModel::updateThumbnail(QString filename, QImage thumbnail, duration_t duration)
|
2018-03-10 15:36:20 +00:00
|
|
|
{
|
pictures: turn QString into std::string for filenames
For undo of picture manipulation, it will be crucial that the
model and the core have the same order of pictures. The first
sort criterion will be time, the second filename in the case
that two pictures have, for whatever reason, the same timestamp.
However in the core we us C-strings and thus sort byte-wise
using strcmp. In the Qt-part we use QStrings, which sort according
to unicode encoding. To enable consistent sorting, change the
Qt-part to std::string, which uses a C-style 0-terminated string
as its backing store.
One might argue that in general filenames should use system-encoding
and therefore use std::string instead of QString. However, a
broader conversion to std::string turned out to be very painful,
since Qt is (deliberately?) difficult to use with std::string.
Notable all the file-manipulation functions don't take std::string
by default. Thus, this commit only converts the internal data
of DivePictureModel, but continues to use QString for the Qt-facing
interface.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 07:47:55 +00:00
|
|
|
int i = findPictureId(filename.toStdString());
|
2018-05-01 10:35:18 +00:00
|
|
|
if (i >= 0) {
|
2019-04-14 14:19:23 +00:00
|
|
|
if (duration.seconds > 0) {
|
2018-07-25 15:26:56 +00:00
|
|
|
addDurationToThumbnail(thumbnail, duration); // If we know the duration paint it on top of the thumbnail
|
2019-04-14 14:19:23 +00:00
|
|
|
pictures[i].length = duration;
|
|
|
|
}
|
2024-01-16 16:39:19 +00:00
|
|
|
pictures[i].image = std::move(thumbnail);
|
2018-03-10 15:36:20 +00:00
|
|
|
emit dataChanged(createIndex(i, 0), createIndex(i, 1));
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 10:35:18 +00:00
|
|
|
|
2020-04-14 20:07:00 +00:00
|
|
|
void DivePictureModel::pictureOffsetChanged(dive *d, const QString filenameIn, offset_t offset)
|
2018-05-01 10:35:18 +00:00
|
|
|
{
|
pictures: turn QString into std::string for filenames
For undo of picture manipulation, it will be crucial that the
model and the core have the same order of pictures. The first
sort criterion will be time, the second filename in the case
that two pictures have, for whatever reason, the same timestamp.
However in the core we us C-strings and thus sort byte-wise
using strcmp. In the Qt-part we use QStrings, which sort according
to unicode encoding. To enable consistent sorting, change the
Qt-part to std::string, which uses a C-style 0-terminated string
as its backing store.
One might argue that in general filenames should use system-encoding
and therefore use std::string instead of QString. However, a
broader conversion to std::string turned out to be very painful,
since Qt is (deliberately?) difficult to use with std::string.
Notable all the file-manipulation functions don't take std::string
by default. Thus, this commit only converts the internal data
of DivePictureModel, but continues to use QString for the Qt-facing
interface.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 07:47:55 +00:00
|
|
|
std::string filename = filenameIn.toStdString();
|
|
|
|
|
2018-06-30 19:32:14 +00:00
|
|
|
// Find the pictures of the given dive.
|
2020-04-14 20:14:23 +00:00
|
|
|
auto from = std::find_if(pictures.begin(), pictures.end(), [d](const PictureEntry &e) { return e.d == d; });
|
|
|
|
auto to = std::find_if(from, pictures.end(), [d](const PictureEntry &e) { return e.d != d; });
|
2018-06-30 19:32:14 +00:00
|
|
|
|
|
|
|
// Find picture with the given filename
|
2024-01-16 16:39:19 +00:00
|
|
|
auto oldPos = std::find_if(from, to, [&filename](const PictureEntry &e) { return e.filename == filename; });
|
2018-06-30 19:32:14 +00:00
|
|
|
if (oldPos == to)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find new position
|
2020-04-14 20:07:00 +00:00
|
|
|
auto newPos = std::find_if(from, to, [offset](const PictureEntry &e) { return e.offsetSeconds > offset.seconds; });
|
2018-06-30 19:32:14 +00:00
|
|
|
|
|
|
|
// Update the offset here and in the backend
|
2020-04-14 20:07:00 +00:00
|
|
|
oldPos->offsetSeconds = offset.seconds;
|
2018-06-30 19:32:14 +00:00
|
|
|
|
|
|
|
// Henceforth we will work with indices instead of iterators
|
|
|
|
int oldIndex = oldPos - pictures.begin();
|
|
|
|
int newIndex = newPos - pictures.begin();
|
|
|
|
if (oldIndex == newIndex || oldIndex + 1 == newIndex)
|
|
|
|
return;
|
|
|
|
beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex);
|
2022-09-24 12:06:56 +00:00
|
|
|
move_in_range(pictures, oldIndex, oldIndex + 1, newIndex);
|
2018-06-30 19:32:14 +00:00
|
|
|
endMoveRows();
|
2018-05-01 10:35:18 +00:00
|
|
|
}
|