on 9aff648 help sign in
unity8/plugins/Ubuntu/Gestures/FloatingFlickable.cpp
150 lines 4.8 kB history raw
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 */
17#include "FloatingFlickable.h"
19#include <private/qquickflickable_p.h>
20#include "DirectionalDragArea.h"
22#include <QDebug>
24FloatingFlickable::FloatingFlickable(QQuickItem *parent)
25 : 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);
53qreal FloatingFlickable::contentWidth() const
55 return m_flickable->contentWidth();
58void FloatingFlickable::setContentWidth(qreal contentWidth)
60 m_flickable->setContentWidth(contentWidth);
63qreal FloatingFlickable::contentHeight() const
65 return m_flickable->contentHeight();
68void FloatingFlickable::setContentHeight(qreal contentHeight)
70 m_flickable->setContentHeight(contentHeight);
73qreal FloatingFlickable::contentX() const
75 return m_flickable->contentX();
78void FloatingFlickable::setContentX(qreal contentX)
80 m_flickable->setContentX(contentX);
83qreal FloatingFlickable::contentY() const
85 return m_flickable->contentY();
88void FloatingFlickable::setContentY(qreal contentY)
90 m_flickable->setContentY(contentY);
93Direction::Type FloatingFlickable::direction() const
95 return m_dragArea->direction();
98void FloatingFlickable::setDirection(Direction::Type direction)
100 m_dragArea->setDirection(direction);
101 if (Direction::isHorizontal(direction)) {
102 m_flickable->setFlickableDirection(QQuickFlickable::HorizontalFlick);
103 } else {
104 m_flickable->setFlickableDirection(QQuickFlickable::VerticalFlick);
105 }
108void FloatingFlickable::updateChildrenWidth()
110 m_dragArea->setWidth(width());
111 m_flickable->setWidth(width());
114void FloatingFlickable::updateChildrenHeight()
116 m_dragArea->setHeight(height());
117 m_flickable->setHeight(height());
120void 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);
129 }
132void 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;
149 }