This is a missing functionality on the 4.2 that I'v talked to dirk and we
decided that it was better to implement it now before waiting for 4.3
because things can get messy if a user adds the wrong pictures on the dive.

So, possibility to remove pictures on the dive now.
Now that I'v mastered 'git rebase squash', I'll have so much less commits
=p , 9 became 3.
From 10c818b5318bc72351cb8a73471a11a59fa207a1 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <tomaz.canabr...@intel.com>
Date: Tue, 29 Jul 2014 21:56:45 -0300
Subject: [PATCH 1/3] Added a button to hide a picture from the dive.

This patch hides a picture from the dive, it should actually
remove it, but because I didn't found a quick way to remove
a picture from the dive yet, it just hides it.

To remove a picture from the dive, the DivePictureItem has to
remember the QUrl of the original file, to remove that from the
model, and currently it only has the QPixmap.

this can be for 4.2.1 or we can postpone 4.2 a tiny bit since this
is a important feature.

Signed-off-by: Tomaz Canabrava <tomaz.canabr...@intel.com>
---
 qt-ui/profile/divepixmapitem.cpp | 62 ++++++++++++++++++++++++++++++++++++++++
 qt-ui/profile/divepixmapitem.h   | 20 +++++++++++++
 2 files changed, 82 insertions(+)

diff --git a/qt-ui/profile/divepixmapitem.cpp b/qt-ui/profile/divepixmapitem.cpp
index dd002e5..372cbd1 100644
--- a/qt-ui/profile/divepixmapitem.cpp
+++ b/qt-ui/profile/divepixmapitem.cpp
@@ -7,12 +7,44 @@
 #include <QBrush>
 #include <QGraphicsDropShadowEffect>
 #include <QDesktopServices>
+#include <QGraphicsScene>
+#include <QGraphicsSceneMouseEvent>
+#include <QGraphicsView>
 #include <QUrl>
 
 DivePixmapItem::DivePixmapItem(QObject *parent) : QObject(parent), QGraphicsPixmapItem()
 {
 }
 
+DiveButtonItem::DiveButtonItem(QObject *parent): DivePixmapItem(parent)
+{
+}
+
+void DiveButtonItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
+{
+	QGraphicsItem::mousePressEvent(event);
+	emit clicked();
+}
+
+// If we have many many pictures on screen, maybe a shared-pixmap would be better to
+// paint on screen, but for now, this.
+CloseButtonItem::CloseButtonItem(QObject *parent): DiveButtonItem(parent)
+{
+	static QPixmap p = QPixmap(":trash");
+	setPixmap(p);
+	setFlag(ItemIgnoresTransformations);
+}
+
+void CloseButtonItem::hide()
+{
+	DiveButtonItem::hide();
+}
+
+void CloseButtonItem::show()
+{
+	DiveButtonItem::show();
+}
+
 DivePictureItem::DivePictureItem(int row, QObject *parent): DivePixmapItem(parent)
 {
 	setFlag(ItemIgnoresTransformations);
@@ -50,20 +82,50 @@ void DivePictureItem::setPixmap(const QPixmap &pix)
 	shadow->setZValue(-2);
 }
 
+CloseButtonItem *button = NULL;
 void DivePictureItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
 {
 	Animations::scaleTo(this, 1.0);
 	setZValue(5);
+
+	if(!button) {
+		button = new CloseButtonItem();
+		button->setScale(0.2);
+		button->setZValue(7);
+		scene()->addItem(button);
+	}
+	button->setPos(mapToScene(0,0));
+	button->show();
+	button->disconnect();
+	connect(button, SIGNAL(clicked()), this, SLOT(removePicture()));
 }
 
 void DivePictureItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
 {
 	Animations::scaleTo(this, 0.2);
 	setZValue(0);
+	if(button)
+		button->hide();
 }
 
 void DivePictureItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
 {
+	QGraphicsView *view = scene()->views().first();
+	QList<QGraphicsItem*> items = view->items(view->mapFromScene(event->scenePos()));
+	Q_FOREACH(QGraphicsItem *item, items){
+		if (dynamic_cast<CloseButtonItem*>(item)){
+			return;
+		}
+	}
 	QString filePath = DivePictureModel::instance()->index(rowOnModel,0).data(Qt::ToolTipRole).toString();
 	QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
 }
+
+void DivePictureItem::removePicture()
+{
+	/* this is a WIP, it doesn't really *removes* anything, merely hides it.
+	 * good workaround, I still need to figure out how to activelly remove
+	 * it from the model. */
+	button->hide();
+	hide();
+}
diff --git a/qt-ui/profile/divepixmapitem.h b/qt-ui/profile/divepixmapitem.h
index 950b211..b5bed37 100644
--- a/qt-ui/profile/divepixmapitem.h
+++ b/qt-ui/profile/divepixmapitem.h
@@ -22,6 +22,7 @@ public:
 	void setPixmap(const QPixmap& pix);
 public slots:
 	void settingsChanged();
+	void removePicture();
 protected:
 	void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
 	void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
@@ -30,4 +31,23 @@ private:
 	int rowOnModel;
 };
 
+class DiveButtonItem : public DivePixmapItem {
+	Q_OBJECT
+public:
+	DiveButtonItem(QObject *parent = 0);
+protected:
+	virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
+signals:
+	void clicked();
+};
+
+class CloseButtonItem : public DiveButtonItem {
+	Q_OBJECT
+public:
+	CloseButtonItem(QObject *parent = 0);
+public slots:
+	void hide();
+	void show();
+};
+
 #endif // DIVEPIXMAPITEM_H
-- 
2.0.3

From 5156b528d57233cf3175f77055ab1f8de1da5dab Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <tomaz.canabr...@intel.com>
Date: Tue, 29 Jul 2014 22:13:14 -0300
Subject: [PATCH 2/3] Instead of holding the index, hold the URL.

Since the idea is to remove the picture in the future, we need
to not hold the row on the model, as when we delete one, the
other pictures will change the row. but the QUrl is unique.

Signed-off-by: Tomaz Canabrava <tomaz.canabr...@intel.com>
---
 qt-ui/profile/divepixmapitem.cpp | 18 +++++++-----------
 qt-ui/profile/divepixmapitem.h   |  5 +++--
 qt-ui/profile/profilewidget2.cpp |  3 ++-
 3 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/qt-ui/profile/divepixmapitem.cpp b/qt-ui/profile/divepixmapitem.cpp
index 372cbd1..3fb06ad 100644
--- a/qt-ui/profile/divepixmapitem.cpp
+++ b/qt-ui/profile/divepixmapitem.cpp
@@ -45,7 +45,7 @@ void CloseButtonItem::show()
 	DiveButtonItem::show();
 }
 
-DivePictureItem::DivePictureItem(int row, QObject *parent): DivePixmapItem(parent)
+DivePictureItem::DivePictureItem(QObject *parent): DivePixmapItem(parent)
 {
 	setFlag(ItemIgnoresTransformations);
 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
@@ -53,7 +53,6 @@ DivePictureItem::DivePictureItem(int row, QObject *parent): DivePixmapItem(paren
 #else
 	setAcceptHoverEvents(true);
 #endif
-	rowOnModel = row;
 	setScale(0.2);
 	connect(PreferencesDialog::instance(), SIGNAL(settingsChanged()), this, SLOT(settingsChanged()));
 	setVisible(prefs.show_pictures_in_profile);
@@ -100,6 +99,11 @@ void DivePictureItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
 	connect(button, SIGNAL(clicked()), this, SLOT(removePicture()));
 }
 
+void DivePictureItem::setFileUrl(const QString &s)
+{
+	fileUrl = s;
+}
+
 void DivePictureItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
 {
 	Animations::scaleTo(this, 0.2);
@@ -110,15 +114,7 @@ void DivePictureItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
 
 void DivePictureItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
 {
-	QGraphicsView *view = scene()->views().first();
-	QList<QGraphicsItem*> items = view->items(view->mapFromScene(event->scenePos()));
-	Q_FOREACH(QGraphicsItem *item, items){
-		if (dynamic_cast<CloseButtonItem*>(item)){
-			return;
-		}
-	}
-	QString filePath = DivePictureModel::instance()->index(rowOnModel,0).data(Qt::ToolTipRole).toString();
-	QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
+	QDesktopServices::openUrl(QUrl::fromLocalFile(fileUrl));
 }
 
 void DivePictureItem::removePicture()
diff --git a/qt-ui/profile/divepixmapitem.h b/qt-ui/profile/divepixmapitem.h
index b5bed37..d39f26d 100644
--- a/qt-ui/profile/divepixmapitem.h
+++ b/qt-ui/profile/divepixmapitem.h
@@ -18,17 +18,18 @@ class DivePictureItem : public DivePixmapItem {
 	Q_OBJECT
 	Q_PROPERTY(qreal scale WRITE setScale READ scale)
 public:
-	DivePictureItem(int row, QObject *parent = 0);
+	DivePictureItem(QObject *parent = 0);
 	void setPixmap(const QPixmap& pix);
 public slots:
 	void settingsChanged();
 	void removePicture();
+	void setFileUrl(const QString& s);
 protected:
 	void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
 	void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
 	void mousePressEvent(QGraphicsSceneMouseEvent *event);
 private:
-	int rowOnModel;
+	QString fileUrl;
 };
 
 class DiveButtonItem : public DivePixmapItem {
diff --git a/qt-ui/profile/profilewidget2.cpp b/qt-ui/profile/profilewidget2.cpp
index b601af3..21f0cb4 100644
--- a/qt-ui/profile/profilewidget2.cpp
+++ b/qt-ui/profile/profilewidget2.cpp
@@ -1366,8 +1366,9 @@ void ProfileWidget2::plotPictures()
 		// information area.
 		if (!pic->offset.seconds)
 			continue;
-		DivePictureItem *item = new DivePictureItem(i);
+		DivePictureItem *item = new DivePictureItem();
 		item->setPixmap(m->index(i,0).data(Qt::DecorationRole).value<QPixmap>());
+		item->setFileUrl(m->index(i,0).data(Qt::DisplayPropertyRole).toString());
 		// let's put the picture at the correct time, but at a fixed "depth" on the profile
 		// not sure this is ideal, but it seems to look right.
 		x = timeAxis->posAtValue(pic->offset.seconds);
-- 
2.0.3

From bade1a06c5e94305460497aff4922f296689bd69 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <tomaz.canabr...@intel.com>
Date: Tue, 29 Jul 2014 23:03:32 -0300
Subject: [PATCH 3/3] Implement the functionality of remove_picture.

Added the remove_picture functionality, with code
shamelessy stolen from remove_event, and hoock it
up with the interface.

Signed-off-by: Tomaz Canabrava <tomaz.canabr...@intel.com>
---
 dive.c                           | 20 +++++++++++++++++++-
 dive.h                           |  2 +-
 qt-ui/divepicturewidget.cpp      | 10 ++++++++++
 qt-ui/divepicturewidget.h        |  1 +
 qt-ui/profile/divepixmapitem.cpp |  1 +
 qt-ui/profile/profilewidget2.cpp |  6 ++++--
 6 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/dive.c b/dive.c
index 139afe2..e74265f 100644
--- a/dive.c
+++ b/dive.c
@@ -2489,9 +2489,27 @@ void dive_set_geodata_from_picture(struct dive *d, struct picture *pic)
 	}
 }
 
-void dive_remove_picture(struct dive *d, struct picture *p)
+static void picture_free( struct picture *p){
+	if (!p)
+		return;
+	free( p->filename );
+	free( p );
+}
+void dive_remove_picture(struct picture *p)
 {
+	printf("Dive %s picture %s \n",  current_dive->picture_list->filename, p->filename);
+	fflush(stdout);
 
+	struct picture **ep = &current_dive->picture_list;
+	while (ep && !same_string((*ep)->filename, p->filename))
+		ep = &(*ep)->next;
+	if (ep) {
+		struct picture *temp = (*ep)->next;
+		picture_free(*ep);
+		*ep = temp;
+		printf("Dive picture apagado. \n");
+		fflush(stdout);
+	}
 }
 
 /* this always acts on the current divecomputer of the current dive */
diff --git a/dive.h b/dive.h
index 204cb2f..0bffeb6 100644
--- a/dive.h
+++ b/dive.h
@@ -311,7 +311,7 @@ struct picture {
 extern struct picture *alloc_picture();
 extern void dive_create_picture(struct dive *d, char *filename, int shift_time);
 extern void dive_add_picture(struct dive *d, struct picture *newpic);
-extern void dive_remove_picture(struct dive *d, struct picture *pic);
+extern void dive_remove_picture(struct picture *pic);
 extern unsigned int dive_get_picture_count(struct dive *d);
 extern void picture_load_exif_data(struct picture *p, timestamp_t *timestamp);
 extern void dive_set_geodata_from_picture(struct dive *d, struct picture *pic);
diff --git a/qt-ui/divepicturewidget.cpp b/qt-ui/divepicturewidget.cpp
index fd64f61..76d3e7c 100644
--- a/qt-ui/divepicturewidget.cpp
+++ b/qt-ui/divepicturewidget.cpp
@@ -89,11 +89,21 @@ QVariant DivePictureModel::data(const QModelIndex &index, int role) const
 		switch (role) {
 		case Qt::UserRole:
 			ret = QVariant::fromValue((void *)stringPixmapCache[key].picture);
+		break;
+		case Qt::DisplayRole:
+			ret = key;
 		}
 	}
 	return ret;
 }
 
+void DivePictureModel::removePicture(const QString &fileUrl)
+{
+	dive_remove_picture(stringPixmapCache[fileUrl].picture);
+	copy_dive(current_dive, &displayed_dive);
+	updateDivePictures();
+}
+
 int DivePictureModel::rowCount(const QModelIndex &parent) const
 {
 	return numberOfPictures;
diff --git a/qt-ui/divepicturewidget.h b/qt-ui/divepicturewidget.h
index a0fd269..6f53bee 100644
--- a/qt-ui/divepicturewidget.h
+++ b/qt-ui/divepicturewidget.h
@@ -19,6 +19,7 @@ public:
 	virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
 	virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
 	void updateDivePictures();
+	void removePicture(const QString& fileUrl);
 
 private:
 	DivePictureModel();
diff --git a/qt-ui/profile/divepixmapitem.cpp b/qt-ui/profile/divepixmapitem.cpp
index 3fb06ad..c73557f 100644
--- a/qt-ui/profile/divepixmapitem.cpp
+++ b/qt-ui/profile/divepixmapitem.cpp
@@ -124,4 +124,5 @@ void DivePictureItem::removePicture()
 	 * it from the model. */
 	button->hide();
 	hide();
+	DivePictureModel::instance()->removePicture(fileUrl);
 }
diff --git a/qt-ui/profile/profilewidget2.cpp b/qt-ui/profile/profilewidget2.cpp
index 21f0cb4..8f0d158 100644
--- a/qt-ui/profile/profilewidget2.cpp
+++ b/qt-ui/profile/profilewidget2.cpp
@@ -1352,7 +1352,9 @@ void ProfileWidget2::keyEscAction()
 
 void ProfileWidget2::plotPictures()
 {
-	qDeleteAll(pictures);
+	Q_FOREACH(DivePictureItem *item, pictures){
+		item->deleteLater();
+	}
 	pictures.clear();
 
 	if (printMode)
@@ -1368,7 +1370,7 @@ void ProfileWidget2::plotPictures()
 			continue;
 		DivePictureItem *item = new DivePictureItem();
 		item->setPixmap(m->index(i,0).data(Qt::DecorationRole).value<QPixmap>());
-		item->setFileUrl(m->index(i,0).data(Qt::DisplayPropertyRole).toString());
+		item->setFileUrl(m->index(i,1).data().toString());
 		// let's put the picture at the correct time, but at a fixed "depth" on the profile
 		// not sure this is ideal, but it seems to look right.
 		x = timeAxis->posAtValue(pic->offset.seconds);
-- 
2.0.3

_______________________________________________
subsurface mailing list
subsurface@hohndel.org
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to