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
=== modified file 'launcher/Launcher.qml'
--- launcher/Launcher.qml 2011-03-01 16:40:00 +0000
+++ launcher/Launcher.qml 2011-03-02 16:04:16 +0000
@@ -2,7 +2,7 @@
2import UnityApplications 1.02import UnityApplications 1.0
3import Unity2d 1.0 /* required for drag’n’drop handling */3import Unity2d 1.0 /* required for drag’n’drop handling */
44
5Item {5DropItem {
6 id: launcher6 id: launcher
77
8 Image {8 Image {
@@ -13,6 +13,9 @@
13 source: "artwork/background.png"13 source: "artwork/background.png"
14 }14 }
1515
16 onDragEnter: launcherView.onDragEnter(event)
17 onDrop: launcherView.onDrop(event)
18
16 LauncherList {19 LauncherList {
17 id: main20 id: main
18 anchors.top: parent.top21 anchors.top: parent.top
1922
=== modified file 'launcher/LauncherItem.qml'
--- launcher/LauncherItem.qml 2011-03-02 10:34:42 +0000
+++ launcher/LauncherItem.qml 2011-03-02 16:04:16 +0000
@@ -27,11 +27,8 @@
27 Additionally, when the tile is marked as 'urgent' it will start an animation where the27 Additionally, when the tile is marked as 'urgent' it will start an animation where the
28 rotation is changed so that it appears to be "shaking".28 rotation is changed so that it appears to be "shaking".
29*/29*/
30Item {30DropItem {
31 id: item31 id: item
32 /* The object name is used by the launcher view to find the current
33 launcher item under the mouse cursor during a drag’n’drop event. */
34 objectName: "launcherItem"
3532
36 anchors.horizontalCenter: parent.horizontalCenter33 anchors.horizontalCenter: parent.horizontalCenter
37 /* Manually add some padding to compensate for the spacing34 /* Manually add some padding to compensate for the spacing
3835
=== modified file 'launcher/LauncherList.qml'
--- launcher/LauncherList.qml 2011-03-01 13:05:38 +0000
+++ launcher/LauncherList.qml 2011-03-02 16:04:16 +0000
@@ -68,8 +68,8 @@
68 Binding { target: item.menu; property: "title"; value: item.name }68 Binding { target: item.menu; property: "title"; value: item.name }
6969
70 /* Drag’n’drop handling */70 /* Drag’n’drop handling */
71 function dragEnterEvent(event) { item.onDragEnter(event) }71 onDragEnter: item.onDragEnter(event)
72 function dropEvent(event) { item.onDrop(event) }72 onDrop: item.onDrop(event)
7373
74 function showMenu() {74 function showMenu() {
75 /* Prevent the simultaneous display of multiple menus */75 /* Prevent the simultaneous display of multiple menus */
7676
=== modified file 'launcher/app/launcherview.cpp'
--- launcher/app/launcherview.cpp 2011-03-02 08:31:11 +0000
+++ launcher/app/launcherview.cpp 2011-03-02 16:04:16 +0000
@@ -28,24 +28,18 @@
28#include <QDeclarativeEngine>28#include <QDeclarativeEngine>
29#include <QDeclarativeContext>29#include <QDeclarativeContext>
30#include <QDeclarativeImageProvider>30#include <QDeclarativeImageProvider>
31#include <QGraphicsObject>
32#include <QMetaObject>
3331
34#include <X11/Xlib.h>32#include <X11/Xlib.h>
35#include <X11/Xatom.h>33#include <X11/Xatom.h>
3634
37#include "dragdropevent.h"
38#include <keyboardmodifiersmonitor.h>35#include <keyboardmodifiersmonitor.h>
39#include <hotkey.h>36#include <hotkey.h>
40#include <hotkeymonitor.h>37#include <hotkeymonitor.h>
38#include <dragdropevent.h>
4139
42LauncherView::LauncherView() :40LauncherView::LauncherView() :
43 QDeclarativeView(),41 QDeclarativeView(), m_superKeyPressed(false)
44 m_dndCurrentLauncherItem(NULL), m_dndCurrentLauncherItemAccepted(false),
45 m_dndAccepted(false), m_superKeyPressed(false)
46{42{
47 setAcceptDrops(true);
48
49 m_enableSuperKey.setKey("/desktop/unity/launcher/super_key_enable");43 m_enableSuperKey.setKey("/desktop/unity/launcher/super_key_enable");
50 QObject::connect(&m_enableSuperKey, SIGNAL(valueChanged()),44 QObject::connect(&m_enableSuperKey, SIGNAL(valueChanged()),
51 this, SLOT(updateSuperKeyMonitoring()));45 this, SLOT(updateSuperKeyMonitoring()));
@@ -126,45 +120,8 @@
126 }120 }
127}121}
128122
129QGraphicsObject*
130LauncherView::launcherItemAt(const QPoint& pos) const
131{
132 QGraphicsItem* item = itemAt(pos);
133 while(item != NULL) {
134 QGraphicsObject* object = qgraphicsitem_cast<QGraphicsObject*>(item);
135 if (object->objectName() == "launcherItem") {
136 return object;
137 }
138 item = item->parentItem();
139 }
140 return NULL;
141}
142
143void
144LauncherView::delegateDragEventHandlingToItem(QDropEvent* event, QGraphicsObject* item)
145{
146 if (item == NULL) {
147 return;
148 }
149 DeclarativeDragDropEvent dde(event, this);
150 QMetaObject::invokeMethod(item, "dragEnterEvent",
151 Q_ARG(QVariant, QVariant::fromValue(&dde)));
152}
153
154bool
155LauncherView::acceptDndEvent(QDragEnterEvent* event)
156{
157 Q_FOREACH(QUrl url, getEventUrls(event)) {
158 if ((url.scheme() == "file" && url.path().endsWith(".desktop")) ||
159 url.scheme().startsWith("http")) {
160 return true;
161 }
162 }
163 return false;
164}
165
166QList<QUrl>123QList<QUrl>
167LauncherView::getEventUrls(QDropEvent* event)124LauncherView::getEventUrls(DeclarativeDragDropEvent* event)
168{125{
169 const QMimeData* mimeData = event->mimeData();126 const QMimeData* mimeData = event->mimeData();
170 if (mimeData->hasUrls()) {127 if (mimeData->hasUrls()) {
@@ -191,57 +148,25 @@
191 return QList<QUrl>();148 return QList<QUrl>();
192}149}
193150
194void151void LauncherView::onDragEnter(DeclarativeDragDropEvent* event)
195LauncherView::dragEnterEvent(QDragEnterEvent* event)152{
196{153 Q_FOREACH(QUrl url, getEventUrls(event)) {
197 m_dndCurrentLauncherItem = NULL;154 if ((url.scheme() == "file" && url.path().endsWith(".desktop")) ||
198 m_dndCurrentLauncherItemAccepted = false;155 url.scheme().startsWith("http")) {
199 /* Compute whether the launcher itself accepts the event only once for this
200 given event. */
201 m_dndAccepted = acceptDndEvent(event);
202 /* Always accept the event so that subsequent move events are received. */
203 event->setAccepted(true);
204}
205
206void
207LauncherView::dragMoveEvent(QDragMoveEvent* event)
208{
209 QGraphicsObject* launcherItem = launcherItemAt(event->pos());
210 if (launcherItem == m_dndCurrentLauncherItem) {
211 if (m_dndCurrentLauncherItemAccepted || m_dndAccepted) {
212 event->setAccepted(true);156 event->setAccepted(true);
213 }157 return;
214 } else {
215 m_dndCurrentLauncherItem = launcherItem;
216 m_dndCurrentLauncherItemAccepted = false;
217
218 if (m_dndCurrentLauncherItem != NULL) {
219 delegateDragEventHandlingToItem(event, m_dndCurrentLauncherItem);
220 if (event->isAccepted()) {
221 m_dndCurrentLauncherItemAccepted = true;
222 }
223 } else {
224 if (m_dndAccepted) {
225 event->setAccepted(true);
226 }
227 }158 }
228 }159 }
229}160}
230161
231void162void LauncherView::onDrop(DeclarativeDragDropEvent* event)
232LauncherView::dropEvent(QDropEvent* event)
233{163{
234 if (m_dndCurrentLauncherItemAccepted) {164 foreach (QUrl url, getEventUrls(event)) {
235 DeclarativeDragDropEvent dde(event, this);165 if (url.scheme() == "file" && url.path().endsWith(".desktop")) {
236 QMetaObject::invokeMethod(m_dndCurrentLauncherItem, "dropEvent",166 emit desktopFileDropped(url.path());
237 Q_ARG(QVariant, QVariant::fromValue(&dde)));167 }
238 } else if (m_dndAccepted) {168 else if (url.scheme().startsWith("http")) {
239 Q_FOREACH(QUrl url, getEventUrls(event)) {169 emit webpageUrlDropped(url);
240 if (url.scheme() == "file" && url.path().endsWith(".desktop")) {
241 emit desktopFileDropped(url.path());
242 } else if (url.scheme().startsWith("http")) {
243 emit webpageUrlDropped(url);
244 }
245 }170 }
246 }171 }
247}172}
248173
=== modified file 'launcher/app/launcherview.h'
--- launcher/app/launcherview.h 2011-03-02 08:31:11 +0000
+++ launcher/app/launcherview.h 2011-03-02 16:04:16 +0000
@@ -21,12 +21,12 @@
21#define LAUNCHERVIEW21#define LAUNCHERVIEW
2222
23#include <QDeclarativeView>23#include <QDeclarativeView>
24#include <QList>
24#include <QUrl>25#include <QUrl>
25#include <QList>26
26#include <QDragEnterEvent>
27#include "gconfitem-qml-wrapper.h"27#include "gconfitem-qml-wrapper.h"
2828
29class QGraphicsObject;29class DeclarativeDragDropEvent;
3030
31class LauncherView : public QDeclarativeView31class LauncherView : public QDeclarativeView
32{32{
@@ -37,6 +37,8 @@
37public:37public:
38 explicit LauncherView();38 explicit LauncherView();
39 Q_INVOKABLE QList<QVariant> getColorsFromIcon(QUrl source, QSize size) const;39 Q_INVOKABLE QList<QVariant> getColorsFromIcon(QUrl source, QSize size) const;
40 Q_INVOKABLE void onDragEnter(DeclarativeDragDropEvent* event);
41 Q_INVOKABLE void onDrop(DeclarativeDragDropEvent* event);
4042
41 bool superKeyPressed() const { return m_superKeyPressed; }43 bool superKeyPressed() const { return m_superKeyPressed; }
4244
@@ -52,25 +54,9 @@
52 void updateSuperKeyMonitoring();54 void updateSuperKeyMonitoring();
5355
54private:56private:
55 QList<QUrl> getEventUrls(QDropEvent*);57 QList<QUrl> getEventUrls(DeclarativeDragDropEvent* event);
56 void changeKeyboardShortcutsState(bool enabled);58 void changeKeyboardShortcutsState(bool enabled);
5759
58 /* Custom drag’n’drop handling */
59 void dragEnterEvent(QDragEnterEvent*);
60 void dragMoveEvent(QDragMoveEvent*);
61 void dropEvent(QDropEvent*);
62
63 QGraphicsObject* launcherItemAt(const QPoint&) const;
64 void delegateDragEventHandlingToItem(QDropEvent*, QGraphicsObject*);
65 bool acceptDndEvent(QDragEnterEvent*);
66
67 /* The launcher item currently under the mouse cursor during a dnd event */
68 QGraphicsObject* m_dndCurrentLauncherItem;
69 /* Whether it accepted the event */
70 bool m_dndCurrentLauncherItemAccepted;
71 /* Whether the launcher itself handles the current dnd event */
72 bool m_dndAccepted;
73
74 GConfItemQmlWrapper m_enableSuperKey;60 GConfItemQmlWrapper m_enableSuperKey;
75 bool m_superKeyPressed;61 bool m_superKeyPressed;
76};62};
7763
=== modified file 'libunity-2d-private/Unity2d/CMakeLists.txt'
--- libunity-2d-private/Unity2d/CMakeLists.txt 2011-02-14 19:59:09 +0000
+++ libunity-2d-private/Unity2d/CMakeLists.txt 2011-03-02 16:04:16 +0000
@@ -15,6 +15,7 @@
15 cacheeffect.cpp15 cacheeffect.cpp
16 workspacesinfo.cpp16 workspacesinfo.cpp
17 signalwaiter.cpp17 signalwaiter.cpp
18 dropitem.cpp
18 )19 )
1920
20set(unity-2d-private-qml_MOC_HDRS21set(unity-2d-private-qml_MOC_HDRS
@@ -26,6 +27,7 @@
26 cacheeffect.h27 cacheeffect.h
27 workspacesinfo.h28 workspacesinfo.h
28 signalwaiter.h29 signalwaiter.h
30 dropitem.h
29 )31 )
3032
31qt4_wrap_cpp(unity-2d-private-qml_MOC_SRCS ${unity-2d-private-qml_MOC_HDRS})33qt4_wrap_cpp(unity-2d-private-qml_MOC_SRCS ${unity-2d-private-qml_MOC_HDRS})
3234
=== added file 'libunity-2d-private/Unity2d/dropitem.cpp'
--- libunity-2d-private/Unity2d/dropitem.cpp 1970-01-01 00:00:00 +0000
+++ libunity-2d-private/Unity2d/dropitem.cpp 2011-03-02 16:04:16 +0000
@@ -0,0 +1,47 @@
1/*
2 * Copyright (C) 2011 Canonical, Ltd.
3 *
4 * Authors:
5 * Florian Boucault <florian.boucault@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "dropitem.h"
21
22#include <QGraphicsSceneDragDropEvent>
23
24#include "dragdropevent.h"
25
26DeclarativeDropItem::DeclarativeDropItem(QDeclarativeItem *parent) : QDeclarativeItem(parent)
27{
28 setAcceptDrops(true);
29}
30
31void DeclarativeDropItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
32{
33 DeclarativeDragDropEvent dragDropEvent(event, this);
34 Q_EMIT dragEnter(&dragDropEvent);
35}
36
37void DeclarativeDropItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
38{
39 DeclarativeDragDropEvent dragDropEvent(event, this);
40 Q_EMIT dragLeave(&dragDropEvent);
41}
42
43void DeclarativeDropItem::dropEvent(QGraphicsSceneDragDropEvent *event)
44{
45 DeclarativeDragDropEvent dragDropEvent(event, this);
46 Q_EMIT drop(&dragDropEvent);
47}
048
=== added file 'libunity-2d-private/Unity2d/dropitem.h'
--- libunity-2d-private/Unity2d/dropitem.h 1970-01-01 00:00:00 +0000
+++ libunity-2d-private/Unity2d/dropitem.h 2011-03-02 16:04:16 +0000
@@ -0,0 +1,46 @@
1/*
2 * Copyright (C) 2011 Canonical, Ltd.
3 *
4 * Authors:
5 * Florian Boucault <florian.boucault@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef DROPITEM_H
21#define DROPITEM_H
22
23#include <QDeclarativeItem>
24
25class QGraphicsSceneDragDropEvent;
26class DeclarativeDragDropEvent;
27
28class DeclarativeDropItem : public QDeclarativeItem
29{
30 Q_OBJECT
31
32public:
33 DeclarativeDropItem(QDeclarativeItem *parent=0);
34
35Q_SIGNALS:
36 void dragEnter(DeclarativeDragDropEvent* event);
37 void dragLeave(DeclarativeDragDropEvent* event);
38 void drop(DeclarativeDragDropEvent* event);
39
40protected:
41 void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
42 void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
43 void dropEvent(QGraphicsSceneDragDropEvent *event);
44};
45
46#endif // DROPITEM_H
047
=== modified file 'libunity-2d-private/Unity2d/plugin.cpp'
--- libunity-2d-private/Unity2d/plugin.cpp 2011-02-21 02:37:20 +0000
+++ libunity-2d-private/Unity2d/plugin.cpp 2011-03-02 16:04:16 +0000
@@ -34,6 +34,7 @@
3434
35#include "mimedata.h"35#include "mimedata.h"
36#include "dragdropevent.h"36#include "dragdropevent.h"
37#include "dropitem.h"
3738
38#include <QtDeclarative/qdeclarative.h>39#include <QtDeclarative/qdeclarative.h>
39#include <QDeclarativeEngine>40#include <QDeclarativeEngine>
@@ -75,6 +76,7 @@
75 qmlRegisterType<QGraphicsDropShadowEffect>("Effects", 1, 0, "DropShadow");76 qmlRegisterType<QGraphicsDropShadowEffect>("Effects", 1, 0, "DropShadow");
7677
77 /* Custom drag’n’drop implementation in QML */78 /* Custom drag’n’drop implementation in QML */
79 qmlRegisterType<DeclarativeDropItem>(uri, 0, 1, "DropItem");
78 qmlRegisterType<DeclarativeMimeData>();80 qmlRegisterType<DeclarativeMimeData>();
79 qmlRegisterType<DeclarativeDragDropEvent>();81 qmlRegisterType<DeclarativeDragDropEvent>();
80}82}
8183
=== modified file 'libunity-2d-private/src/dragdropevent.cpp'
--- libunity-2d-private/src/dragdropevent.cpp 2011-02-15 10:28:23 +0000
+++ libunity-2d-private/src/dragdropevent.cpp 2011-03-02 16:04:16 +0000
@@ -19,7 +19,9 @@
1919
20#include "dragdropevent.h"20#include "dragdropevent.h"
2121
22DeclarativeDragDropEvent::DeclarativeDragDropEvent(QDropEvent* event, QObject* parent)22#include <QGraphicsSceneDragDropEvent>
23
24DeclarativeDragDropEvent::DeclarativeDragDropEvent(QGraphicsSceneDragDropEvent* event, QObject* parent)
23 : QObject(parent)25 : QObject(parent)
24 , m_event(event)26 , m_event(event)
25 , m_mimeData(event->mimeData())27 , m_mimeData(event->mimeData())
2628
=== modified file 'libunity-2d-private/src/dragdropevent.h'
--- libunity-2d-private/src/dragdropevent.h 2011-02-15 10:28:23 +0000
+++ libunity-2d-private/src/dragdropevent.h 2011-03-02 16:04:16 +0000
@@ -25,9 +25,10 @@
2525
26#include "mimedata.h"26#include "mimedata.h"
2727
28#include <QtGui/QDropEvent>
29#include <QtCore/Qt>28#include <QtCore/Qt>
3029
30class QGraphicsSceneDragDropEvent;
31
31class DeclarativeDragDropEvent : public QObject32class DeclarativeDragDropEvent : public QObject
32{33{
33 Q_OBJECT34 Q_OBJECT
@@ -37,7 +38,7 @@
37 Q_PROPERTY(Qt::DropAction dropAction READ dropAction WRITE setDropAction)38 Q_PROPERTY(Qt::DropAction dropAction READ dropAction WRITE setDropAction)
3839
39public:40public:
40 DeclarativeDragDropEvent(QDropEvent* event, QObject* parent=0);41 DeclarativeDragDropEvent(QGraphicsSceneDragDropEvent* event, QObject* parent=0);
4142
42 /* getters */43 /* getters */
43 DeclarativeMimeData* mimeData() { return &m_mimeData; }44 DeclarativeMimeData* mimeData() { return &m_mimeData; }
@@ -49,7 +50,7 @@
49 void setDropAction(Qt::DropAction action);50 void setDropAction(Qt::DropAction action);
5051
51private:52private:
52 QDropEvent* m_event;53 QGraphicsSceneDragDropEvent* m_event;
53 DeclarativeMimeData m_mimeData;54 DeclarativeMimeData m_mimeData;
54};55};
5556

Subscribers

People subscribed via source and target branches