|
From: Tapio V. <aa...@us...> - 2011-01-07 13:18:34
|
Module: editor
Branch: master
Commit: 3a79562972d234da8ff1c995994efb4780ebca7d
Author: Tapio Vierros <tap...@gm...>
Date: Fri Jan 7 15:18:10 2011 +0200
Remove MIME-type based dragging and implement own.
Now simpler and gives more control.
---
notegraphwidget.cc | 99 ++++++++-------------------------------------------
notegraphwidget.hh | 6 +--
notelabel.cc | 4 ++-
notelabel.hh | 4 ++-
4 files changed, 24 insertions(+), 89 deletions(-)
diff --git a/notegraphwidget.cc b/notegraphwidget.cc
index 74a7290..7832ee4 100644
--- a/notegraphwidget.cc
+++ b/notegraphwidget.cc
@@ -5,7 +5,7 @@
#include "notegraphwidget.hh"
NoteGraphWidget::NoteGraphWidget(QWidget *parent)
- : QWidget(parent), m_resizing()
+ : QWidget(parent), m_resizingNote(), m_movingNote()
{
setAcceptDrops(true);
}
@@ -92,61 +92,6 @@ void NoteGraphWidget::rebuildNoteList()
m_notes.sort(cmpNoteLabelPtr);
}
-void NoteGraphWidget::dragEnterEvent(QDragEnterEvent *event)
-{
- if (event->mimeData()->hasFormat("application/x-notelabel")) {
- if (children().contains(event->source())) {
- event->setDropAction(Qt::MoveAction);
- event->accept();
- } else {
- event->acceptProposedAction();
- }
- } else {
- event->ignore();
- }
-}
-
-void NoteGraphWidget::dragMoveEvent(QDragMoveEvent *event)
-{
- if (event->mimeData()->hasFormat("application/x-notelabel")) {
- if (children().contains(event->source())) {
- event->setDropAction(Qt::MoveAction);
- event->accept();
- } else {
- event->acceptProposedAction();
- }
- // Update note positions on-the-fly
- updateNotes();
- } else {
- event->ignore();
- }
-}
-
-void NoteGraphWidget::dropEvent(QDropEvent *event)
-{
- if (event->mimeData()->hasFormat("application/x-notelabel")) {
- const QMimeData *mime = event->mimeData();
- QByteArray itemData = mime->data("application/x-notelabel");
- QDataStream dataStream(&itemData, QIODevice::ReadOnly);
-
- QString text;
- QPoint offset;
- int w;
- dataStream >> text >> offset >> w;
- new NoteLabel(text, this, event->pos() - offset, QSize(w, 0), false);
- updateNotes();
-
- if (event->source() == this) {
- event->setDropAction(Qt::MoveAction);
- event->accept();
- } else {
- event->acceptProposedAction();
- }
- } else {
- event->ignore();
- }
-}
-
void NoteGraphWidget::mousePressEvent(QMouseEvent *event)
{
NoteLabel *child = static_cast<NoteLabel*>(childAt(event->pos()));
@@ -162,34 +107,15 @@ void NoteGraphWidget::mousePressEvent(QMouseEvent *event)
const int resizeHandleSize = 5;
if (hotSpot.x() < resizeHandleSize || hotSpot.x() > child->width() - resizeHandleSize) {
// Start a resize
- m_resizing = child;
- child->setResizing( (hotSpot.x() < resizeHandleSize) ? -1 : 1 );
- std::cout << "resize" << std::endl;
+ m_resizingNote = child;
+ child->startResizing( (hotSpot.x() < resizeHandleSize) ? -1 : 1 );
} else {
- // Initialize Drag
-
- QByteArray itemData;
- QDataStream dataStream(&itemData, QIODevice::WriteOnly);
- dataStream << child->getText() << QPoint(hotSpot) << child->width();
-
- QMimeData *mimeData = new QMimeData;
- mimeData->setData("application/x-notelabel", itemData);
- mimeData->setText(child->getText());
-
- QDrag *drag = new QDrag(this);
- drag->setMimeData(mimeData);
- drag->setPixmap(*child->pixmap());
- drag->setHotSpot(hotSpot);
-
+ // Start a drag
+ m_movingNote = child;
+ child->startDragging(hotSpot);
child->disableFloating();
- child->createPixmap();
- child->hide();
-
- if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
- child->close();
- else
- child->show();
+ child->createPixmap(child->size());
}
// Right Click
@@ -213,8 +139,15 @@ void NoteGraphWidget::mousePressEvent(QMouseEvent *event)
void NoteGraphWidget::mouseReleaseEvent(QMouseEvent *event)
{
(void)*event;
- m_resizing->setResizing(0);
- m_resizing = NULL;
+ if (m_resizingNote) {
+ m_resizingNote->startResizing(0);
+ m_resizingNote = NULL;
+ }
+ if (m_movingNote) {
+ m_movingNote->startDragging(QPoint());
+ m_movingNote = NULL;
+ }
+ updateNotes();
}
void NoteGraphWidget::wheelEvent(QWheelEvent *event)
diff --git a/notegraphwidget.hh b/notegraphwidget.hh
index a3587b0..ff4e1f0 100644
--- a/notegraphwidget.hh
+++ b/notegraphwidget.hh
@@ -18,9 +18,6 @@ public:
void rebuildNoteList();
protected:
- void dragEnterEvent(QDragEnterEvent *event);
- void dragMoveEvent(QDragMoveEvent *event);
- void dropEvent(QDropEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
@@ -28,7 +25,8 @@ protected:
private:
int m_requiredWidth;
- NoteLabel* m_resizing;
+ NoteLabel* m_resizingNote;
+ NoteLabel* m_movingNote;
NoteLabels m_notes;
};
diff --git a/notelabel.cc b/notelabel.cc
index 3af1271..355588c 100644
--- a/notelabel.cc
+++ b/notelabel.cc
@@ -6,7 +6,7 @@ namespace {
}
NoteLabel::NoteLabel(const QString &text, QWidget *parent, const QPoint &position, const QSize &size, bool floating)
- : QLabel(parent), m_labelText(text), m_floating(floating), m_resizing(0)
+ : QLabel(parent), m_labelText(text), m_floating(floating), m_resizing(0), m_hotspot()
{
createPixmap(size);
if (!position.isNull())
@@ -87,5 +87,7 @@ void NoteLabel::mouseMoveEvent(QMouseEvent *event)
setGeometry(x() + event->pos().x(), y(), width() - event->pos().x(), height());
else
resize(event->pos().x(), height());
+ } else if (!m_hotspot.isNull()) {
+ move(pos() + event->pos() - m_hotspot);
}
}
diff --git a/notelabel.hh b/notelabel.hh
index 4e1bb7f..a677ac0 100644
--- a/notelabel.hh
+++ b/notelabel.hh
@@ -14,7 +14,8 @@ public:
bool isFloating() const { return m_floating; }
void disableFloating() { m_floating = false; }
- void setResizing(int dir) { m_resizing = dir; }
+ void startResizing(int dir) { m_resizing = dir; m_hotspot = QPoint(); }
+ void startDragging(const QPoint& point) { m_hotspot = point; m_resizing = 0; }
void resizeEvent(QResizeEvent *event);
void mouseMoveEvent(QMouseEvent *event);
@@ -25,6 +26,7 @@ private:
QString m_labelText;
bool m_floating;
int m_resizing;
+ QPoint m_hotspot;
};
bool inline cmpNoteLabelPtr(const NoteLabel *lhs, const NoteLabel *rhs)
|