Merge lp:~osomon/unity-2d/DropItem into lp:unity-2d/3.0

Proposed by Olivier Tilloy
Status: Merged
Approved by: Florian Boucault
Approved revision: 399
Merged at revision: 421
Proposed branch: lp:~osomon/unity-2d/DropItem
Merge into: lp:unity-2d/3.0
Diff against target: 450 lines (+133/-122)
11 files modified
launcher/Launcher.qml (+4/-1)
launcher/LauncherItem.qml (+1/-4)
launcher/LauncherList.qml (+2/-2)
launcher/app/launcherview.cpp (+16/-91)
launcher/app/launcherview.h (+6/-20)
libunity-2d-private/Unity2d/CMakeLists.txt (+2/-0)
libunity-2d-private/Unity2d/dropitem.cpp (+47/-0)
libunity-2d-private/Unity2d/dropitem.h (+46/-0)
libunity-2d-private/Unity2d/plugin.cpp (+2/-0)
libunity-2d-private/src/dragdropevent.cpp (+3/-1)
libunity-2d-private/src/dragdropevent.h (+4/-3)
To merge this branch: bzr merge lp:~osomon/unity-2d/DropItem
Reviewer Review Type Date Requested Status
Florian Boucault Pending
Review via email: mp+51915@code.launchpad.net

Commit message

[launcher] Better implementation of drag’n’drop in QML.

The DropItem element has the following advantages:
 - less code
 - more re-usable

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'launcher/Launcher.qml'
2--- launcher/Launcher.qml 2011-03-01 16:40:00 +0000
3+++ launcher/Launcher.qml 2011-03-02 16:04:16 +0000
4@@ -2,7 +2,7 @@
5 import UnityApplications 1.0
6 import Unity2d 1.0 /* required for drag’n’drop handling */
7
8-Item {
9+DropItem {
10 id: launcher
11
12 Image {
13@@ -13,6 +13,9 @@
14 source: "artwork/background.png"
15 }
16
17+ onDragEnter: launcherView.onDragEnter(event)
18+ onDrop: launcherView.onDrop(event)
19+
20 LauncherList {
21 id: main
22 anchors.top: parent.top
23
24=== modified file 'launcher/LauncherItem.qml'
25--- launcher/LauncherItem.qml 2011-03-02 10:34:42 +0000
26+++ launcher/LauncherItem.qml 2011-03-02 16:04:16 +0000
27@@ -27,11 +27,8 @@
28 Additionally, when the tile is marked as 'urgent' it will start an animation where the
29 rotation is changed so that it appears to be "shaking".
30 */
31-Item {
32+DropItem {
33 id: item
34- /* The object name is used by the launcher view to find the current
35- launcher item under the mouse cursor during a drag’n’drop event. */
36- objectName: "launcherItem"
37
38 anchors.horizontalCenter: parent.horizontalCenter
39 /* Manually add some padding to compensate for the spacing
40
41=== modified file 'launcher/LauncherList.qml'
42--- launcher/LauncherList.qml 2011-03-01 13:05:38 +0000
43+++ launcher/LauncherList.qml 2011-03-02 16:04:16 +0000
44@@ -68,8 +68,8 @@
45 Binding { target: item.menu; property: "title"; value: item.name }
46
47 /* Drag’n’drop handling */
48- function dragEnterEvent(event) { item.onDragEnter(event) }
49- function dropEvent(event) { item.onDrop(event) }
50+ onDragEnter: item.onDragEnter(event)
51+ onDrop: item.onDrop(event)
52
53 function showMenu() {
54 /* Prevent the simultaneous display of multiple menus */
55
56=== modified file 'launcher/app/launcherview.cpp'
57--- launcher/app/launcherview.cpp 2011-03-02 08:31:11 +0000
58+++ launcher/app/launcherview.cpp 2011-03-02 16:04:16 +0000
59@@ -28,24 +28,18 @@
60 #include <QDeclarativeEngine>
61 #include <QDeclarativeContext>
62 #include <QDeclarativeImageProvider>
63-#include <QGraphicsObject>
64-#include <QMetaObject>
65
66 #include <X11/Xlib.h>
67 #include <X11/Xatom.h>
68
69-#include "dragdropevent.h"
70 #include <keyboardmodifiersmonitor.h>
71 #include <hotkey.h>
72 #include <hotkeymonitor.h>
73+#include <dragdropevent.h>
74
75 LauncherView::LauncherView() :
76- QDeclarativeView(),
77- m_dndCurrentLauncherItem(NULL), m_dndCurrentLauncherItemAccepted(false),
78- m_dndAccepted(false), m_superKeyPressed(false)
79+ QDeclarativeView(), m_superKeyPressed(false)
80 {
81- setAcceptDrops(true);
82-
83 m_enableSuperKey.setKey("/desktop/unity/launcher/super_key_enable");
84 QObject::connect(&m_enableSuperKey, SIGNAL(valueChanged()),
85 this, SLOT(updateSuperKeyMonitoring()));
86@@ -126,45 +120,8 @@
87 }
88 }
89
90-QGraphicsObject*
91-LauncherView::launcherItemAt(const QPoint& pos) const
92-{
93- QGraphicsItem* item = itemAt(pos);
94- while(item != NULL) {
95- QGraphicsObject* object = qgraphicsitem_cast<QGraphicsObject*>(item);
96- if (object->objectName() == "launcherItem") {
97- return object;
98- }
99- item = item->parentItem();
100- }
101- return NULL;
102-}
103-
104-void
105-LauncherView::delegateDragEventHandlingToItem(QDropEvent* event, QGraphicsObject* item)
106-{
107- if (item == NULL) {
108- return;
109- }
110- DeclarativeDragDropEvent dde(event, this);
111- QMetaObject::invokeMethod(item, "dragEnterEvent",
112- Q_ARG(QVariant, QVariant::fromValue(&dde)));
113-}
114-
115-bool
116-LauncherView::acceptDndEvent(QDragEnterEvent* event)
117-{
118- Q_FOREACH(QUrl url, getEventUrls(event)) {
119- if ((url.scheme() == "file" && url.path().endsWith(".desktop")) ||
120- url.scheme().startsWith("http")) {
121- return true;
122- }
123- }
124- return false;
125-}
126-
127 QList<QUrl>
128-LauncherView::getEventUrls(QDropEvent* event)
129+LauncherView::getEventUrls(DeclarativeDragDropEvent* event)
130 {
131 const QMimeData* mimeData = event->mimeData();
132 if (mimeData->hasUrls()) {
133@@ -191,57 +148,25 @@
134 return QList<QUrl>();
135 }
136
137-void
138-LauncherView::dragEnterEvent(QDragEnterEvent* event)
139-{
140- m_dndCurrentLauncherItem = NULL;
141- m_dndCurrentLauncherItemAccepted = false;
142- /* Compute whether the launcher itself accepts the event only once for this
143- given event. */
144- m_dndAccepted = acceptDndEvent(event);
145- /* Always accept the event so that subsequent move events are received. */
146- event->setAccepted(true);
147-}
148-
149-void
150-LauncherView::dragMoveEvent(QDragMoveEvent* event)
151-{
152- QGraphicsObject* launcherItem = launcherItemAt(event->pos());
153- if (launcherItem == m_dndCurrentLauncherItem) {
154- if (m_dndCurrentLauncherItemAccepted || m_dndAccepted) {
155+void LauncherView::onDragEnter(DeclarativeDragDropEvent* event)
156+{
157+ Q_FOREACH(QUrl url, getEventUrls(event)) {
158+ if ((url.scheme() == "file" && url.path().endsWith(".desktop")) ||
159+ url.scheme().startsWith("http")) {
160 event->setAccepted(true);
161- }
162- } else {
163- m_dndCurrentLauncherItem = launcherItem;
164- m_dndCurrentLauncherItemAccepted = false;
165-
166- if (m_dndCurrentLauncherItem != NULL) {
167- delegateDragEventHandlingToItem(event, m_dndCurrentLauncherItem);
168- if (event->isAccepted()) {
169- m_dndCurrentLauncherItemAccepted = true;
170- }
171- } else {
172- if (m_dndAccepted) {
173- event->setAccepted(true);
174- }
175+ return;
176 }
177 }
178 }
179
180-void
181-LauncherView::dropEvent(QDropEvent* event)
182+void LauncherView::onDrop(DeclarativeDragDropEvent* event)
183 {
184- if (m_dndCurrentLauncherItemAccepted) {
185- DeclarativeDragDropEvent dde(event, this);
186- QMetaObject::invokeMethod(m_dndCurrentLauncherItem, "dropEvent",
187- Q_ARG(QVariant, QVariant::fromValue(&dde)));
188- } else if (m_dndAccepted) {
189- Q_FOREACH(QUrl url, getEventUrls(event)) {
190- if (url.scheme() == "file" && url.path().endsWith(".desktop")) {
191- emit desktopFileDropped(url.path());
192- } else if (url.scheme().startsWith("http")) {
193- emit webpageUrlDropped(url);
194- }
195+ foreach (QUrl url, getEventUrls(event)) {
196+ if (url.scheme() == "file" && url.path().endsWith(".desktop")) {
197+ emit desktopFileDropped(url.path());
198+ }
199+ else if (url.scheme().startsWith("http")) {
200+ emit webpageUrlDropped(url);
201 }
202 }
203 }
204
205=== modified file 'launcher/app/launcherview.h'
206--- launcher/app/launcherview.h 2011-03-02 08:31:11 +0000
207+++ launcher/app/launcherview.h 2011-03-02 16:04:16 +0000
208@@ -21,12 +21,12 @@
209 #define LAUNCHERVIEW
210
211 #include <QDeclarativeView>
212+#include <QList>
213 #include <QUrl>
214-#include <QList>
215-#include <QDragEnterEvent>
216+
217 #include "gconfitem-qml-wrapper.h"
218
219-class QGraphicsObject;
220+class DeclarativeDragDropEvent;
221
222 class LauncherView : public QDeclarativeView
223 {
224@@ -37,6 +37,8 @@
225 public:
226 explicit LauncherView();
227 Q_INVOKABLE QList<QVariant> getColorsFromIcon(QUrl source, QSize size) const;
228+ Q_INVOKABLE void onDragEnter(DeclarativeDragDropEvent* event);
229+ Q_INVOKABLE void onDrop(DeclarativeDragDropEvent* event);
230
231 bool superKeyPressed() const { return m_superKeyPressed; }
232
233@@ -52,25 +54,9 @@
234 void updateSuperKeyMonitoring();
235
236 private:
237- QList<QUrl> getEventUrls(QDropEvent*);
238+ QList<QUrl> getEventUrls(DeclarativeDragDropEvent* event);
239 void changeKeyboardShortcutsState(bool enabled);
240
241- /* Custom drag’n’drop handling */
242- void dragEnterEvent(QDragEnterEvent*);
243- void dragMoveEvent(QDragMoveEvent*);
244- void dropEvent(QDropEvent*);
245-
246- QGraphicsObject* launcherItemAt(const QPoint&) const;
247- void delegateDragEventHandlingToItem(QDropEvent*, QGraphicsObject*);
248- bool acceptDndEvent(QDragEnterEvent*);
249-
250- /* The launcher item currently under the mouse cursor during a dnd event */
251- QGraphicsObject* m_dndCurrentLauncherItem;
252- /* Whether it accepted the event */
253- bool m_dndCurrentLauncherItemAccepted;
254- /* Whether the launcher itself handles the current dnd event */
255- bool m_dndAccepted;
256-
257 GConfItemQmlWrapper m_enableSuperKey;
258 bool m_superKeyPressed;
259 };
260
261=== modified file 'libunity-2d-private/Unity2d/CMakeLists.txt'
262--- libunity-2d-private/Unity2d/CMakeLists.txt 2011-02-14 19:59:09 +0000
263+++ libunity-2d-private/Unity2d/CMakeLists.txt 2011-03-02 16:04:16 +0000
264@@ -15,6 +15,7 @@
265 cacheeffect.cpp
266 workspacesinfo.cpp
267 signalwaiter.cpp
268+ dropitem.cpp
269 )
270
271 set(unity-2d-private-qml_MOC_HDRS
272@@ -26,6 +27,7 @@
273 cacheeffect.h
274 workspacesinfo.h
275 signalwaiter.h
276+ dropitem.h
277 )
278
279 qt4_wrap_cpp(unity-2d-private-qml_MOC_SRCS ${unity-2d-private-qml_MOC_HDRS})
280
281=== added file 'libunity-2d-private/Unity2d/dropitem.cpp'
282--- libunity-2d-private/Unity2d/dropitem.cpp 1970-01-01 00:00:00 +0000
283+++ libunity-2d-private/Unity2d/dropitem.cpp 2011-03-02 16:04:16 +0000
284@@ -0,0 +1,47 @@
285+/*
286+ * Copyright (C) 2011 Canonical, Ltd.
287+ *
288+ * Authors:
289+ * Florian Boucault <florian.boucault@canonical.com>
290+ *
291+ * This program is free software; you can redistribute it and/or modify
292+ * it under the terms of the GNU General Public License as published by
293+ * the Free Software Foundation; version 3.
294+ *
295+ * This program is distributed in the hope that it will be useful,
296+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
297+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
298+ * GNU General Public License for more details.
299+ *
300+ * You should have received a copy of the GNU General Public License
301+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
302+ */
303+
304+#include "dropitem.h"
305+
306+#include <QGraphicsSceneDragDropEvent>
307+
308+#include "dragdropevent.h"
309+
310+DeclarativeDropItem::DeclarativeDropItem(QDeclarativeItem *parent) : QDeclarativeItem(parent)
311+{
312+ setAcceptDrops(true);
313+}
314+
315+void DeclarativeDropItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
316+{
317+ DeclarativeDragDropEvent dragDropEvent(event, this);
318+ Q_EMIT dragEnter(&dragDropEvent);
319+}
320+
321+void DeclarativeDropItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
322+{
323+ DeclarativeDragDropEvent dragDropEvent(event, this);
324+ Q_EMIT dragLeave(&dragDropEvent);
325+}
326+
327+void DeclarativeDropItem::dropEvent(QGraphicsSceneDragDropEvent *event)
328+{
329+ DeclarativeDragDropEvent dragDropEvent(event, this);
330+ Q_EMIT drop(&dragDropEvent);
331+}
332
333=== added file 'libunity-2d-private/Unity2d/dropitem.h'
334--- libunity-2d-private/Unity2d/dropitem.h 1970-01-01 00:00:00 +0000
335+++ libunity-2d-private/Unity2d/dropitem.h 2011-03-02 16:04:16 +0000
336@@ -0,0 +1,46 @@
337+/*
338+ * Copyright (C) 2011 Canonical, Ltd.
339+ *
340+ * Authors:
341+ * Florian Boucault <florian.boucault@canonical.com>
342+ *
343+ * This program is free software; you can redistribute it and/or modify
344+ * it under the terms of the GNU General Public License as published by
345+ * the Free Software Foundation; version 3.
346+ *
347+ * This program is distributed in the hope that it will be useful,
348+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
349+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
350+ * GNU General Public License for more details.
351+ *
352+ * You should have received a copy of the GNU General Public License
353+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
354+ */
355+
356+#ifndef DROPITEM_H
357+#define DROPITEM_H
358+
359+#include <QDeclarativeItem>
360+
361+class QGraphicsSceneDragDropEvent;
362+class DeclarativeDragDropEvent;
363+
364+class DeclarativeDropItem : public QDeclarativeItem
365+{
366+ Q_OBJECT
367+
368+public:
369+ DeclarativeDropItem(QDeclarativeItem *parent=0);
370+
371+Q_SIGNALS:
372+ void dragEnter(DeclarativeDragDropEvent* event);
373+ void dragLeave(DeclarativeDragDropEvent* event);
374+ void drop(DeclarativeDragDropEvent* event);
375+
376+protected:
377+ void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
378+ void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
379+ void dropEvent(QGraphicsSceneDragDropEvent *event);
380+};
381+
382+#endif // DROPITEM_H
383
384=== modified file 'libunity-2d-private/Unity2d/plugin.cpp'
385--- libunity-2d-private/Unity2d/plugin.cpp 2011-02-21 02:37:20 +0000
386+++ libunity-2d-private/Unity2d/plugin.cpp 2011-03-02 16:04:16 +0000
387@@ -34,6 +34,7 @@
388
389 #include "mimedata.h"
390 #include "dragdropevent.h"
391+#include "dropitem.h"
392
393 #include <QtDeclarative/qdeclarative.h>
394 #include <QDeclarativeEngine>
395@@ -75,6 +76,7 @@
396 qmlRegisterType<QGraphicsDropShadowEffect>("Effects", 1, 0, "DropShadow");
397
398 /* Custom drag’n’drop implementation in QML */
399+ qmlRegisterType<DeclarativeDropItem>(uri, 0, 1, "DropItem");
400 qmlRegisterType<DeclarativeMimeData>();
401 qmlRegisterType<DeclarativeDragDropEvent>();
402 }
403
404=== modified file 'libunity-2d-private/src/dragdropevent.cpp'
405--- libunity-2d-private/src/dragdropevent.cpp 2011-02-15 10:28:23 +0000
406+++ libunity-2d-private/src/dragdropevent.cpp 2011-03-02 16:04:16 +0000
407@@ -19,7 +19,9 @@
408
409 #include "dragdropevent.h"
410
411-DeclarativeDragDropEvent::DeclarativeDragDropEvent(QDropEvent* event, QObject* parent)
412+#include <QGraphicsSceneDragDropEvent>
413+
414+DeclarativeDragDropEvent::DeclarativeDragDropEvent(QGraphicsSceneDragDropEvent* event, QObject* parent)
415 : QObject(parent)
416 , m_event(event)
417 , m_mimeData(event->mimeData())
418
419=== modified file 'libunity-2d-private/src/dragdropevent.h'
420--- libunity-2d-private/src/dragdropevent.h 2011-02-15 10:28:23 +0000
421+++ libunity-2d-private/src/dragdropevent.h 2011-03-02 16:04:16 +0000
422@@ -25,9 +25,10 @@
423
424 #include "mimedata.h"
425
426-#include <QtGui/QDropEvent>
427 #include <QtCore/Qt>
428
429+class QGraphicsSceneDragDropEvent;
430+
431 class DeclarativeDragDropEvent : public QObject
432 {
433 Q_OBJECT
434@@ -37,7 +38,7 @@
435 Q_PROPERTY(Qt::DropAction dropAction READ dropAction WRITE setDropAction)
436
437 public:
438- DeclarativeDragDropEvent(QDropEvent* event, QObject* parent=0);
439+ DeclarativeDragDropEvent(QGraphicsSceneDragDropEvent* event, QObject* parent=0);
440
441 /* getters */
442 DeclarativeMimeData* mimeData() { return &m_mimeData; }
443@@ -49,7 +50,7 @@
444 void setDropAction(Qt::DropAction action);
445
446 private:
447- QDropEvent* m_event;
448+ QGraphicsSceneDragDropEvent* m_event;
449 DeclarativeMimeData m_mimeData;
450 };
451

Subscribers

People subscribed via source and target branches