@@ -0,0 +1,150 @@
2+ * Copyright (C) 2015 Canonical, Ltd.
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.
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.
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/>.
17+#include "FloatingFlickable.h"
19+#include <private/qquickflickable_p.h>
20+#include "DirectionalDragArea.h"
24+FloatingFlickable::FloatingFlickable(QQuickItem *parent)
26+ , m_mousePressed(false)
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::onDragAreaTouchPosChanged);
34+ connect(m_dragArea, &DirectionalDragArea::touchYChanged,
35+ this, &FloatingFlickable::onDragAreaTouchPosChanged);
36+ connect(m_dragArea, &DirectionalDragArea::draggingChanged,
37+ this, &FloatingFlickable::onDragAreaDraggingChanged);
38+ connect(m_dragArea, &DirectionalDragArea::directionChanged, this, &FloatingFlickable::directionChanged);
40+ m_flickable = new QQuickFlickable(this);
41+ m_flickable->setEnabled(false);
42+ m_flickable->setWidth(width());
43+ m_flickable->setHeight(height());
44+ connect(m_flickable, &QQuickFlickable::contentWidthChanged, this, &FloatingFlickable::contentWidthChanged);
45+ connect(m_flickable, &QQuickFlickable::contentHeightChanged, this, &FloatingFlickable::contentHeightChanged);
46+ connect(m_flickable, &QQuickFlickable::contentXChanged, this, &FloatingFlickable::contentXChanged);
47+ connect(m_flickable, &QQuickFlickable::contentYChanged, this, &FloatingFlickable::contentYChanged);
49+ connect(this, &QQuickItem::widthChanged, this, &FloatingFlickable::updateChildrenWidth);
50+ connect(this, &QQuickItem::heightChanged, this, &FloatingFlickable::updateChildrenHeight);
53+qreal FloatingFlickable::contentWidth() const
55+ return m_flickable->contentWidth();
58+void FloatingFlickable::setContentWidth(qreal contentWidth)
60+ m_flickable->setContentWidth(contentWidth);
63+qreal FloatingFlickable::contentHeight() const
65+ return m_flickable->contentHeight();
68+void FloatingFlickable::setContentHeight(qreal contentHeight)
70+ m_flickable->setContentHeight(contentHeight);
73+qreal FloatingFlickable::contentX() const
75+ return m_flickable->contentX();
78+void FloatingFlickable::setContentX(qreal contentX)
80+ m_flickable->setContentX(contentX);
83+qreal FloatingFlickable::contentY() const
85+ return m_flickable->contentY();
88+void FloatingFlickable::setContentY(qreal contentY)
90+ m_flickable->setContentY(contentY);
93+Direction::Type FloatingFlickable::direction() const
95+ return m_dragArea->direction();
98+void FloatingFlickable::setDirection(Direction::Type direction)
100+ m_dragArea->setDirection(direction);
101+ if (Direction::isHorizontal(direction)) {
102+ m_flickable->setFlickableDirection(QQuickFlickable::HorizontalFlick);
104+ m_flickable->setFlickableDirection(QQuickFlickable::VerticalFlick);
108+void FloatingFlickable::updateChildrenWidth()
110+ m_dragArea->setWidth(width());
111+ m_flickable->setWidth(width());
114+void FloatingFlickable::updateChildrenHeight()
116+ m_dragArea->setHeight(height());
117+ m_flickable->setHeight(height());
120+void FloatingFlickable::onDragAreaTouchPosChanged(qreal)
122+ if (m_mousePressed) {
123+ QMouseEvent mouseEvent(QEvent::MouseMove,
124+ QPointF(m_dragArea->touchX(),m_dragArea->touchY()),
125+ Qt::NoButton, Qt::LeftButton, Qt::NoModifier);
127+ QCoreApplication::sendEvent(m_flickable, &mouseEvent);
132+void FloatingFlickable::onDragAreaDraggingChanged(bool dragging)
134+ if (dragging && !m_mousePressed) {
135+ QMouseEvent mouseEvent(QEvent::MouseButtonPress,
136+ QPointF(m_dragArea->touchX(),m_dragArea->touchY()),
137+ Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
139+ QCoreApplication::sendEvent(m_flickable, &mouseEvent);
140+ m_mousePressed = true;
142+ } else if (!dragging && m_mousePressed) {
143+ QMouseEvent mouseEvent(QEvent::MouseButtonRelease,
144+ QPointF(m_dragArea->touchX(),m_dragArea->touchY()),
145+ Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
147+ QCoreApplication::sendEvent(m_flickable, &mouseEvent);
148+ m_mousePressed = false;