diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-12-19 12:32:32 +0100 |
---|---|---|
committer | bstoeger <32835590+bstoeger@users.noreply.github.com> | 2020-12-19 20:19:51 +0100 |
commit | fceb3691d9b4ccccf1ebb76f83fb7d58067251d7 (patch) | |
tree | d6a145f13bf4fee7676d6874c0bc3e092194fe52 | |
parent | 636c932dfe202c44b6f8d13433dd8fef1c302277 (diff) | |
download | subsurface-fceb3691d9b4ccccf1ebb76f83fb7d58067251d7.tar.gz |
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>
-rw-r--r-- | profile-widget/divepixmapitem.cpp | 18 | ||||
-rw-r--r-- | profile-widget/divepixmapitem.h | 7 | ||||
-rw-r--r-- | profile-widget/profilewidget2.cpp | 17 | ||||
-rw-r--r-- | profile-widget/profilewidget2.h | 3 |
4 files changed, 23 insertions, 22 deletions
diff --git a/profile-widget/divepixmapitem.cpp b/profile-widget/divepixmapitem.cpp index 0e46519b6..91b7ac469 100644 --- a/profile-widget/divepixmapitem.cpp +++ b/profile-widget/divepixmapitem.cpp @@ -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 -} diff --git a/profile-widget/divepixmapitem.h b/profile-widget/divepixmapitem.h index 545ecb2ae..44c5dd88e 100644 --- a/profile-widget/divepixmapitem.h +++ b/profile-widget/divepixmapitem.h @@ -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); diff --git a/profile-widget/profilewidget2.cpp b/profile-widget/profilewidget2.cpp index 09d1dc135..19819eae9 100644 --- a/profile-widget/profilewidget2.cpp +++ b/profile-widget/profilewidget2.cpp @@ -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(); } diff --git a/profile-widget/profilewidget2.h b/profile-widget/profilewidget2.h index fe54da3d9..272398095 100644 --- a/profile-widget/profilewidget2.h +++ b/profile-widget/profilewidget2.h @@ -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); |