From cd4b2830b1ca08df11697e2a8f2299ba6fc4fe8b Mon Sep 17 00:00:00 2001
From: "Robert C. Helling" <[email protected]>
Date: Fri, 18 Sep 2015 17:11:08 +0200
Subject: [PATCH] Drag and Drop images

Now that we have the possibility to add images without meaningful
time stamps to a dive, we should let the user provide that time
offset manually. This patch allowed pictures to be dragged from
the image list to the profile.

Signed-off-by: Robert C. Helling <[email protected]>
---
 qt-ui/divepicturewidget.cpp      | 30 ++++++++++++++++++
 qt-ui/divepicturewidget.h        |  3 ++
 qt-ui/profile/profilewidget2.cpp | 66 ++++++++++++++++++++++++++++++++++++++++
 qt-ui/profile/profilewidget2.h   |  4 +++
 4 files changed, 103 insertions(+)

diff --git a/qt-ui/divepicturewidget.cpp b/qt-ui/divepicturewidget.cpp
index bed3d3b..daf62c6 100644
--- a/qt-ui/divepicturewidget.cpp
+++ b/qt-ui/divepicturewidget.cpp
@@ -14,6 +14,7 @@
 #include <mainwindow.h>
 #include <qthelper.h>
 #include <QStandardPaths>
+#include <QtWidgets>
 
 void loadPicture(struct picture *picture)
 {
@@ -98,3 +99,32 @@ void DivePictureWidget::doubleClicked(const QModelIndex 
&index)
        QString filePath = model()->data(index, 
Qt::DisplayPropertyRole).toString();
        emit photoDoubleClicked(localFilePath(filePath));
 }
+
+
+void DivePictureWidget::mousePressEvent(QMouseEvent *event)
+{
+
+       QPixmap pixmap = model()->data(indexAt(event->pos()), 
Qt::DecorationRole).value<QPixmap>();
+
+       QString filename = model()->data(indexAt(event->pos()), 
Qt::DisplayPropertyRole).toString();
+
+       QByteArray itemData;
+       QDataStream dataStream(&itemData, QIODevice::WriteOnly);
+       dataStream << filename << event->pos();
+
+       QMimeData *mimeData = new QMimeData;
+       mimeData->setData("application/x-subsurfaceimagedrop", itemData);
+
+       QDrag *drag = new QDrag(this);
+       drag->setMimeData(mimeData);
+       drag->setPixmap(pixmap);
+       drag->setHotSpot(event->pos() - 
rectForIndex(indexAt(event->pos())).topLeft());
+
+       QPixmap tempPixmap = pixmap;
+       QPainter painter;
+       painter.begin(&tempPixmap);
+       painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
+       painter.end();
+
+       drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
+}
diff --git a/qt-ui/divepicturewidget.h b/qt-ui/divepicturewidget.h
index 54f5bb8..f02d6b5 100644
--- a/qt-ui/divepicturewidget.h
+++ b/qt-ui/divepicturewidget.h
@@ -23,6 +23,9 @@ class DivePictureWidget : public QListView {
        Q_OBJECT
 public:
        DivePictureWidget(QWidget *parent);
+protected:
+       void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
+
 signals:
        void photoDoubleClicked(const QString filePath);
 private
diff --git a/qt-ui/profile/profilewidget2.cpp b/qt-ui/profile/profilewidget2.cpp
index 514edcf..d85a0e3 100644
--- a/qt-ui/profile/profilewidget2.cpp
+++ b/qt-ui/profile/profilewidget2.cpp
@@ -16,6 +16,7 @@
 #include "divepicturemodel.h"
 #include "maintab.h"
 #include "diveplanner.h"
+#include "divelist.h"
 
 #include <libdivecomputer/parser.h>
 #include <QScrollBar>
@@ -30,6 +31,7 @@
 #endif
 #include "mainwindow.h"
 #include <preferences.h>
+#include <QtWidgets>
 
 /* This is the global 'Item position' variable.
  * it should tell you where to position things up
@@ -120,6 +122,8 @@ ProfileWidget2::ProfileWidget2(QWidget *parent) : 
QGraphicsView(parent),
        scene()->installEventFilter(this);
        connect(PreferencesDialog::instance(), SIGNAL(settingsChanged()), this, 
SLOT(settingsChanged()));
        QAction *action = NULL;
+       setAcceptDrops(true);
+
 #define ADD_ACTION(SHORTCUT, Slot)                                  \
        action = new QAction(this);                                 \
        action->setShortcut(SHORTCUT);                              \
@@ -1815,3 +1819,65 @@ void ProfileWidget2::plotPictures()
                pictures.push_back(item);
        }
 }
+
+void ProfileWidget2::dropEvent(QDropEvent *event)
+{
+       if (event->mimeData()->hasFormat("application/x-subsurfaceimagedrop")) {
+               QByteArray itemData = 
event->mimeData()->data("application/x-subsurfaceimagedrop");
+               QDataStream dataStream(&itemData, QIODevice::ReadOnly);
+
+               QString filename;
+               QPoint offset;
+               dataStream >> filename >> offset;
+
+               QPointF mappedPos = mapToScene(event->pos());
+
+               FOR_EACH_PICTURE(current_dive) {
+                       if (QString(picture->filename) == filename) {
+                               picture->offset.seconds = 
timeAxis->valueAt(mappedPos);
+                               mark_divelist_changed(true);
+                               break;
+                       }
+               }
+               copy_dive(current_dive, &displayed_dive);
+               DivePictureModel::instance()->updateDivePictures();
+
+
+               if (event->source() == this) {
+                       event->setDropAction(Qt::MoveAction);
+                       event->accept();
+               } else {
+                       event->acceptProposedAction();
+               }
+       } else {
+               event->ignore();
+       }
+}
+
+void ProfileWidget2::dragEnterEvent(QDragEnterEvent *event)
+{
+       if (event->mimeData()->hasFormat("application/x-subsurfaceimagedrop")) {
+               if (event->source() == this) {
+                       event->setDropAction(Qt::MoveAction);
+                       event->accept();
+               } else {
+                       event->acceptProposedAction();
+               }
+       } else {
+               event->ignore();
+       }
+}
+
+void ProfileWidget2::dragMoveEvent(QDragMoveEvent *event)
+{
+       if (event->mimeData()->hasFormat("application/x-subsurfaceimagedrop")) {
+               if (event->source() == this) {
+                       event->setDropAction(Qt::MoveAction);
+                       event->accept();
+               } else {
+                       event->acceptProposedAction();
+               }
+       } else {
+               event->ignore();
+       }
+}
diff --git a/qt-ui/profile/profilewidget2.h b/qt-ui/profile/profilewidget2.h
index 1127b84..533a1a2 100644
--- a/qt-ui/profile/profilewidget2.h
+++ b/qt-ui/profile/profilewidget2.h
@@ -134,6 +134,10 @@ protected:
        virtual void mouseDoubleClickEvent(QMouseEvent *event);
        virtual void mousePressEvent(QMouseEvent *event);
        virtual void mouseReleaseEvent(QMouseEvent *event);
+       void dropEvent(QDropEvent *event) Q_DECL_OVERRIDE;
+       void dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE;
+       void dragMoveEvent(QDragMoveEvent *event) Q_DECL_OVERRIDE;
+
 
 private: /*methods*/
        void fixBackgroundPos();
-- 
1.9.5 (Apple Git-50.3)

Hi,

Dirk I don’t know if you want to take this at this point or wait till after the 
release (although I am pretty sure not much harm can be caused by this…).

We have a checkbox to ignore image time stamps when adding pictures. Those 
pictures end up at time 0. With this patch, the user can drag those images form 
the list of images to the profile and a drop sets the time offset accordingly.

Best
Robert

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to