profile: move picture removal from DivePictureItem to ProfileWidget2

On clicking the DivePictureItem "trash" icon, the item would delete
the picture it represents in the currently displayed dive. This needed
an access to the global "displayed_dive" variable, which we want
to get rid of to make the profile more flexible. For example, we
want to render the profile for printing without messing with global
state.

One solution would be to save the dive with every DivePictureItem.
This commit follows a more Qt-ish strategy by handling this via
signals: The close button emits a signal that is recast by the
DivePictureItem and ultimately handled by the ProfileWidget2,
which knows which dive it represents.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-12-19 12:32:32 +01:00 committed by bstoeger
parent 636c932dfe
commit fceb3691d9
4 changed files with 23 additions and 22 deletions

View file

@ -5,13 +5,9 @@
#include "core/qthelper.h"
#include "core/settings/qPrefDisplay.h"
#include "core/subsurface-qt/divelistnotifier.h"
#ifndef SUBSURFACE_MOBILE
#include "core/dive.h" // for displayed_dive
#include "commands/command.h"
#endif
#include <QDesktopServices>
#include <QGraphicsView>
#include <QPen>
#include <QUrl>
#include <QGraphicsSceneMouseEvent>
@ -28,7 +24,7 @@ CloseButtonItem::CloseButtonItem(QGraphicsItem *parent): DivePixmapItem(parent)
void CloseButtonItem::mousePressEvent(QGraphicsSceneMouseEvent *)
{
qgraphicsitem_cast<DivePictureItem*>(parentItem())->removePicture();
emit clicked();
}
DivePictureItem::DivePictureItem(QGraphicsItem *parent): DivePixmapItem(parent),
@ -41,6 +37,7 @@ DivePictureItem::DivePictureItem(QGraphicsItem *parent): DivePixmapItem(parent),
setAcceptHoverEvents(true);
setScale(0.2);
connect(&diveListNotifier, &DiveListNotifier::settingsChanged, this, &DivePictureItem::settingsChanged);
connect(button, &CloseButtonItem::clicked, [this] () { emit removePicture(fileUrl); });
canvas->setPen(Qt::NoPen);
canvas->setBrush(QColor(Qt::white));
@ -108,12 +105,3 @@ void DivePictureItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
if (event->button() == Qt::LeftButton)
QDesktopServices::openUrl(QUrl::fromLocalFile(localFilePath(fileUrl)));
}
void DivePictureItem::removePicture()
{
#ifndef SUBSURFACE_MOBILE
struct dive *d = get_dive_by_uniq_id(displayed_dive.id);
if (d)
Command::removePictures({ { d, { fileUrl.toStdString() } } });
#endif
}

View file

@ -19,6 +19,8 @@ class CloseButtonItem : public DivePixmapItem {
Q_OBJECT
public:
CloseButtonItem(QGraphicsItem *parent = 0);
signals:
void clicked();
private:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
};
@ -30,10 +32,11 @@ public:
DivePictureItem(QGraphicsItem *parent = 0);
void setPixmap(const QPixmap& pix);
void setBaseZValue(double z);
void setFileUrl(const QString& s);
signals:
void removePicture(const QString &fileUrl);
public slots:
void settingsChanged();
void removePicture();
void setFileUrl(const QString& s);
private:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);

View file

@ -2091,17 +2091,19 @@ void ProfileWidget2::updateThumbnail(QString filename, QImage thumbnail, duratio
}
// Create a PictureEntry object and add its thumbnail to the scene if profile pictures are shown.
ProfileWidget2::PictureEntry::PictureEntry(offset_t offsetIn, const QString &filenameIn, QGraphicsScene *scene, bool synchronous) : offset(offsetIn),
ProfileWidget2::PictureEntry::PictureEntry(offset_t offsetIn, const QString &filenameIn, ProfileWidget2 *profile, bool synchronous) : offset(offsetIn),
duration(duration_t {0}),
filename(filenameIn),
thumbnail(new DivePictureItem)
{
QGraphicsScene *scene = profile->scene();
int size = Thumbnailer::defaultThumbnailSize();
scene->addItem(thumbnail.get());
thumbnail->setVisible(prefs.show_pictures_in_profile);
QImage img = Thumbnailer::instance()->fetchThumbnail(filename, synchronous).scaled(size, size, Qt::KeepAspectRatio);
thumbnail->setPixmap(QPixmap::fromImage(img));
thumbnail->setFileUrl(filename);
connect(thumbnail.get(), &DivePictureItem::removePicture, profile, &ProfileWidget2::removePicture);
}
// Define a default sort order for picture-entries: sort lexicographically by timestamp and filename.
@ -2181,7 +2183,7 @@ void ProfileWidget2::plotPicturesInternal(const struct dive *d, bool synchronous
// Note that FOR_EACH_PICTURE handles d being null gracefully.
FOR_EACH_PICTURE(d) {
if (picture->offset.seconds > 0 && picture->offset.seconds <= d->duration.seconds)
pictures.emplace_back(picture->offset, QString(picture->filename), scene(), synchronous);
pictures.emplace_back(picture->offset, QString(picture->filename), this, synchronous);
}
if (pictures.empty())
return;
@ -2220,7 +2222,7 @@ void ProfileWidget2::picturesAdded(dive *d, QVector<PictureObj> pics)
for (const PictureObj &pic: pics) {
if (pic.offset.seconds > 0 && pic.offset.seconds <= d->duration.seconds) {
pictures.emplace_back(pic.offset, QString::fromStdString(pic.filename), scene(), false);
pictures.emplace_back(pic.offset, QString::fromStdString(pic.filename), this, false);
updateThumbnailXPos(pictures.back());
}
}
@ -2232,6 +2234,13 @@ void ProfileWidget2::picturesAdded(dive *d, QVector<PictureObj> pics)
calculatePictureYPositions();
}
void ProfileWidget2::removePicture(const QString &fileUrl)
{
struct dive *d = get_dive_by_uniq_id(displayed_dive.id);
if (d)
Command::removePictures({ { d, { fileUrl.toStdString() } } });
}
void ProfileWidget2::profileChanged(dive *d)
{
if (!d || d->id != displayed_dive.id)
@ -2323,7 +2332,7 @@ void ProfileWidget2::pictureOffsetChanged(dive *d, QString filename, offset_t of
// The parameters are passed directly to the contructor.
// The call returns an iterator to the new element (which might differ from
// the old iterator, since the buffer might have been reallocated).
newPos = pictures.emplace(newPos, offset, filename, scene(), false);
newPos = pictures.emplace(newPos, offset, filename, this, false);
updateThumbnailXPos(*newPos);
calculatePictureYPositions();
}

View file

@ -118,6 +118,7 @@ slots: // Necessary to call from QAction's signals.
void updateThumbnail(QString filename, QImage thumbnail, duration_t duration);
void profileChanged(dive *d);
void pictureOffsetChanged(dive *d, QString filename, offset_t offset);
void removePicture(const QString &fileUrl);
/* this is called for every move on the handlers. maybe we can speed up this a bit? */
void recreatePlannedDive();
@ -243,7 +244,7 @@ private:
std::unique_ptr<DivePictureItem> thumbnail;
// For videos with known duration, we represent the duration of the video by a line
std::unique_ptr<QGraphicsRectItem> durationLine;
PictureEntry (offset_t offsetIn, const QString &filenameIn, QGraphicsScene *scene, bool synchronous);
PictureEntry (offset_t offsetIn, const QString &filenameIn, ProfileWidget2 *profile, bool synchronous);
bool operator< (const PictureEntry &e) const;
};
void updateThumbnailXPos(PictureEntry &e);