on xenial help sign in
Introducing FloatingFlickable
9f2119aDaniel d'Andrada
plugins/Ubuntu/Gestures/CMakeLists.txt+1
@@ -7,6 +7,7 @@ set(UbuntuGesturesQml_SOURCES
7 Damper.cpp
7 Damper.cpp
8 Direction.cpp
8 Direction.cpp
9 DirectionalDragArea.cpp
9 DirectionalDragArea.cpp
10+ FloatingFlickable.cpp
10 PressedOutsideNotifier.cpp
11 PressedOutsideNotifier.cpp
11 TouchDispatcher.cpp
12 TouchDispatcher.cpp
12 TouchGate.cpp
13 TouchGate.cpp
plugins/Ubuntu/Gestures/FloatingFlickable.cppnew file+112
@@ -0,0 +1,112 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include "FloatingFlickable.h"
18+
19+#include <private/qquickflickable_p.h>
20+#include "DirectionalDragArea.h"
21+
22+#include <QDebug>
23+
24+FloatingFlickable::FloatingFlickable(QQuickItem *parent)
25+ : QQuickItem(parent)
26+ , m_mousePressed(false)
27+{
28+ m_dragArea = new DirectionalDragArea(this);
29+ m_dragArea->setWidth(width());
30+ m_dragArea->setHeight(height());
31+ m_dragArea->setDirection(Direction::Horizontal);
32+ connect(m_dragArea, &DirectionalDragArea::touchXChanged,
33+ this, &FloatingFlickable::onDragAreaTouchXChanged);
34+ connect(m_dragArea, &DirectionalDragArea::draggingChanged,
35+ this, &FloatingFlickable::onDragAreaDraggingChanged);
36+
37+ m_flickable = new QQuickFlickable(this);
38+ m_flickable->setEnabled(false);
39+ m_flickable->setWidth(width());
40+ m_flickable->setHeight(height());
41+ connect(m_flickable, &QQuickFlickable::contentWidthChanged,
42+ this, &FloatingFlickable::contentWidthChanged);
43+ connect(m_flickable, &QQuickFlickable::contentXChanged,
44+ this, &FloatingFlickable::contentXChanged);
45+
46+ connect(this, &QQuickItem::widthChanged, this, &FloatingFlickable::updateChildrenWidth);
47+ connect(this, &QQuickItem::heightChanged, this, &FloatingFlickable::updateChildrenHeight);
48+}
49+
50+qreal FloatingFlickable::contentWidth() const
51+{
52+ return m_flickable->contentWidth();
53+}
54+
55+void FloatingFlickable::setContentWidth(qreal contentWidth)
56+{
57+ m_flickable->setContentWidth(contentWidth);
58+}
59+
60+qreal FloatingFlickable::contentX() const
61+{
62+ return m_flickable->contentX();
63+}
64+
65+void FloatingFlickable::setContentX(qreal contentX)
66+{
67+ m_flickable->setContentX(contentX);
68+}
69+
70+void FloatingFlickable::updateChildrenWidth()
71+{
72+ m_dragArea->setWidth(width());
73+ m_flickable->setWidth(width());
74+}
75+
76+void FloatingFlickable::updateChildrenHeight()
77+{
78+ m_dragArea->setHeight(height());
79+ m_flickable->setHeight(height());
80+}
81+
82+void FloatingFlickable::onDragAreaTouchXChanged(qreal /*touchX*/)
83+{
84+ if (m_mousePressed) {
85+ QMouseEvent mouseEvent(QEvent::MouseMove,
86+ QPointF(m_dragArea->touchX(),m_dragArea->touchY()),
87+ Qt::NoButton, Qt::LeftButton, Qt::NoModifier);
88+
89+ QCoreApplication::sendEvent(m_flickable, &mouseEvent);
90+
91+ }
92+}
93+
94+void FloatingFlickable::onDragAreaDraggingChanged(bool dragging)
95+{
96+ if (dragging && !m_mousePressed) {
97+ QMouseEvent mouseEvent(QEvent::MouseButtonPress,
98+ QPointF(m_dragArea->touchX(),m_dragArea->touchY()),
99+ Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
100+
101+ QCoreApplication::sendEvent(m_flickable, &mouseEvent);
102+ m_mousePressed = true;
103+
104+ } else if (!dragging && m_mousePressed) {
105+ QMouseEvent mouseEvent(QEvent::MouseButtonRelease,
106+ QPointF(m_dragArea->touchX(),m_dragArea->touchY()),
107+ Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
108+
109+ QCoreApplication::sendEvent(m_flickable, &mouseEvent);
110+ m_mousePressed = false;
111+ }
112+}
plugins/Ubuntu/Gestures/FloatingFlickable.hnew file+60
@@ -0,0 +1,60 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#ifndef FLOATING_FLICKABLE_H
18+#define FLOATING_FLICKABLE_H
19+
20+#include <QQuickItem>
21+#include "UbuntuGesturesQmlGlobal.h"
22+
23+class DirectionalDragArea;
24+class QQuickFlickable;
25+
26+/*
27+ A Flickable that doesn't require the item being flicked to be its child.
28+ */
29+class UBUNTUGESTURESQML_EXPORT FloatingFlickable : public QQuickItem {
30+ Q_OBJECT
31+
32+ Q_PROPERTY(qreal contentWidth READ contentWidth WRITE setContentWidth NOTIFY contentWidthChanged)
33+ Q_PROPERTY(qreal contentX READ contentX WRITE setContentX NOTIFY contentXChanged)
34+
35+Q_SIGNALS:
36+ void contentWidthChanged();
37+ void contentXChanged();
38+
39+public:
40+ FloatingFlickable(QQuickItem *parent = nullptr);
41+
42+ qreal contentWidth() const;
43+ void setContentWidth(qreal contentWidth);
44+
45+ qreal contentX() const;
46+ void setContentX(qreal contentX);
47+
48+private Q_SLOTS:
49+ void updateChildrenWidth();
50+ void updateChildrenHeight();
51+ void onDragAreaTouchXChanged(qreal touchX);
52+ void onDragAreaDraggingChanged(bool value);
53+
54+private:
55+ DirectionalDragArea *m_dragArea;
56+ QQuickFlickable *m_flickable;
57+ bool m_mousePressed;
58+};
59+
60+#endif // FLOATING_FLICKABLE_H
plugins/Ubuntu/Gestures/plugin.cpp+3−1
@@ -1,5 +1,5 @@
1/*
1/*
2− * Copyright (C) 2013 Canonical, Ltd.
2+ * Copyright (C) 2013,2015 Canonical, Ltd.
3 *
3 *
4 * This program is free software; you can redistribute it and/or modify
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
5 * it under the terms of the GNU General Public License as published by
@@ -18,6 +18,7 @@
18#include "AxisVelocityCalculator.h"
18#include "AxisVelocityCalculator.h"
19#include "Direction.h"
19#include "Direction.h"
20#include "DirectionalDragArea.h"
20#include "DirectionalDragArea.h"
21+#include "FloatingFlickable.h"
21#include "PressedOutsideNotifier.h"
22#include "PressedOutsideNotifier.h"
22#include "TouchGate.h"
23#include "TouchGate.h"
23
24
@@ -34,6 +35,7 @@ void UbuntuGesturesQmlPlugin::registerTypes(const char *uri)
34 qmlRegisterSingletonType<Direction>(uri, 0, 1, "Direction", directionSingleton);
35 qmlRegisterSingletonType<Direction>(uri, 0, 1, "Direction", directionSingleton);
35 qmlRegisterType<DirectionalDragArea>(uri, 0, 1, "DirectionalDragArea");
36 qmlRegisterType<DirectionalDragArea>(uri, 0, 1, "DirectionalDragArea");
36 qmlRegisterType<AxisVelocityCalculator>(uri, 0, 1, "AxisVelocityCalculator");
37 qmlRegisterType<AxisVelocityCalculator>(uri, 0, 1, "AxisVelocityCalculator");
38+ qmlRegisterType<FloatingFlickable>(uri, 0, 1, "FloatingFlickable");
37 qmlRegisterType<PressedOutsideNotifier>(uri, 0, 1, "PressedOutsideNotifier");
39 qmlRegisterType<PressedOutsideNotifier>(uri, 0, 1, "PressedOutsideNotifier");
38 qmlRegisterType<TouchGate>(uri, 0, 1, "TouchGate");
40 qmlRegisterType<TouchGate>(uri, 0, 1, "TouchGate");
39}
41}
tests/plugins/Ubuntu/Gestures/CMakeLists.txt+1
@@ -46,6 +46,7 @@ macro(add_gesture_test CLASSNAME)
46endmacro(add_gesture_test)
46endmacro(add_gesture_test)
47
47
48add_gesture_ui_test(DirectionalDragArea)
48add_gesture_ui_test(DirectionalDragArea)
49+add_gesture_ui_test(FloatingFlickable)
49add_gesture_ui_test(PressedOutsideNotifier)
50add_gesture_ui_test(PressedOutsideNotifier)
50add_gesture_ui_test(TouchDispatcher)
51add_gesture_ui_test(TouchDispatcher)
51add_gesture_ui_test(TouchGate)
52add_gesture_ui_test(TouchGate)
tests/plugins/Ubuntu/Gestures/tst_FloatingFlickable.cppnew file+63
@@ -0,0 +1,63 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include <QtTest>
18+#include <QQuickView>
19+
20+#include "FloatingFlickable.h"
21+
22+#include "GestureTest.h"
23+
24+using namespace UbuntuGestures;
25+
26+class tst_FloatingFlickable: public GestureTest
27+{
28+ Q_OBJECT
29+public:
30+ tst_FloatingFlickable();
31+private Q_SLOTS:
32+ void init(); // called right before each and every test function is executed
33+
34+ void foo();
35+
36+};
37+
38+tst_FloatingFlickable::tst_FloatingFlickable()
39+ : GestureTest(QStringLiteral("tst_FloatingFlickable.qml"))
40+{
41+}
42+
43+void tst_FloatingFlickable::init()
44+{
45+ GestureTest::init();
46+
47+ // We shouldn't need the three lines below, but a compiz/unity7
48+ // regression means we don't pass the test without them because
49+ // the window doesn't have the proper size. Can be removed in the
50+ // future if the regression is fixed and tests pass again
51+ m_view->resize(m_view->rootObject()->width(), m_view->rootObject()->height());
52+ QTRY_COMPARE(m_view->width(), (int)m_view->rootObject()->width());
53+ QTRY_COMPARE(m_view->height(), (int)m_view->rootObject()->height());
54+}
55+
56+void tst_FloatingFlickable::foo()
57+{
58+}
59+
60+
61+QTEST_MAIN(tst_FloatingFlickable)
62+
63+#include "tst_FloatingFlickable.moc"
tests/plugins/Ubuntu/Gestures/tst_FloatingFlickable.qmlnew file+58
@@ -0,0 +1,58 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+import QtQuick 2.0
18+import Ubuntu.Components 0.1
19+import Ubuntu.Gestures 0.1
20+import Unity.Test 0.1
21+
22+Rectangle {
23+ id: root
24+ width: units.gu(60)
25+ height: units.gu(60)
26+ color: "white"
27+
28+ Binding {
29+ target: MouseTouchAdaptor
30+ property: "enabled"
31+ value: true
32+ }
33+
34+ Grid {
35+ x: -floatingFlickable.contentX
36+ rows: 1
37+ Repeater {
38+ model: 100
39+ Rectangle {
40+ width: 100
41+ height: root.height
42+ color: mouseArea.pressed ? "red"
43+ : Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
44+ MouseArea {
45+ id: mouseArea
46+ anchors.fill: parent
47+ }
48+ }
49+ }
50+ }
51+
52+ FloatingFlickable {
53+ id: floatingFlickable
54+ anchors.fill: parent
55+ contentWidth: 100 * 100
56+ onContentXChanged: console.log(contentX);
57+ }
58+}