on 545d96e help sign in
unity8/plugins/Ubuntu/Gestures/DirectionalDragArea.cpp
894 lines 28 kB history raw
1/*
2 * Copyright (C) 2013-2014 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#define ACTIVETOUCHESINFO_DEBUG 0
18#define DIRECTIONALDRAGAREA_DEBUG 0
20#include "DirectionalDragArea.h"
22#include <QQuickWindow>
23#include <QtCore/qmath.h>
24#include <QDebug>
26#pragma GCC diagnostic push
27#pragma GCC diagnostic ignored "-pedantic"
28#include <private/qquickwindow_p.h>
29#pragma GCC diagnostic pop
31// local
32#include "TouchOwnershipEvent.h"
33#include "TouchRegistry.h"
34#include "UnownedTouchEvent.h"
36using namespace UbuntuGestures;
38#if DIRECTIONALDRAGAREA_DEBUG
39#define DDA_DEBUG(msg) qDebug("[DDA] " msg)
40#include "DebugHelpers.h"
42namespace {
43const char *statusToString(DirectionalDragArea::Status status)
45 if (status == DirectionalDragArea::WaitingForTouch) {
46 return "WaitingForTouch";
47 } else if (status == DirectionalDragArea::Undecided) {
48 return "Undecided";
49 } else {
50 return "Recognized";
51 }
54} // namespace {
55#else // DIRECTIONALDRAGAREA_DEBUG
56#define DDA_DEBUG(msg) do{}while(0)
57#endif // DIRECTIONALDRAGAREA_DEBUG
60DirectionalDragArea::DirectionalDragArea(QQuickItem *parent)
61 : QQuickItem(parent)
62 , m_status(WaitingForTouch)
63 , m_sceneDistance(0)
64 , m_touchId(-1)
65 , m_direction(Direction::Rightwards)
66 , m_wideningAngle(0)
67 , m_wideningFactor(0)
68 , m_distanceThreshold(0)
69 , m_distanceThresholdSquared(0.)
70 , m_minSpeed(0)
71 , m_maxSilenceTime(200)
72 , m_silenceTime(0)
73 , m_compositionTime(60)
74 , m_numSamplesOnLastSpeedCheck(0)
75 , m_recognitionTimer(0)
76 , m_velocityCalculator(0)
77 , m_timeSource(new RealTimeSource)
78 , m_activeTouches(m_timeSource)
80 setRecognitionTimer(new Timer(this));
81 m_recognitionTimer->setInterval(60);
82 m_recognitionTimer->setSingleShot(false);
84 m_velocityCalculator = new AxisVelocityCalculator(this);
86 connect(this, &QQuickItem::enabledChanged, this, &DirectionalDragArea::giveUpIfDisabledOrInvisible);
87 connect(this, &QQuickItem::visibleChanged, this, &DirectionalDragArea::giveUpIfDisabledOrInvisible);
90Direction::Type DirectionalDragArea::direction() const
92 return m_direction;
95void DirectionalDragArea::setDirection(Direction::Type direction)
97 if (direction != m_direction) {
98 m_direction = direction;
99 Q_EMIT directionChanged(m_direction);
100 }
103void DirectionalDragArea::setMaxDeviation(qreal value)
105 if (m_dampedScenePos.maxDelta() != value) {
106 m_dampedScenePos.setMaxDelta(value);
107 Q_EMIT maxDeviationChanged(value);
108 }
111qreal DirectionalDragArea::wideningAngle() const
113 return m_wideningAngle;
116void DirectionalDragArea::setWideningAngle(qreal angle)
118 if (angle == m_wideningAngle)
119 return;
121 m_wideningAngle = angle;
123 // wideningFactor = pow(cosine(angle), 2)
124 {
125 qreal angleRadians = angle * M_PI / 180.0;
126 m_wideningFactor = qCos(angleRadians);
127 m_wideningFactor = m_wideningFactor * m_wideningFactor;
128 }
130 Q_EMIT wideningAngleChanged(angle);
133void DirectionalDragArea::setDistanceThreshold(qreal value)
135 if (m_distanceThreshold != value) {
136 m_distanceThreshold = value;
137 m_distanceThresholdSquared = m_distanceThreshold * m_distanceThreshold;
138 Q_EMIT distanceThresholdChanged(value);
139 }
142void DirectionalDragArea::setMinSpeed(qreal value)
144 if (m_minSpeed != value) {
145 m_minSpeed = value;
146 Q_EMIT minSpeedChanged(value);
147 }
150void DirectionalDragArea::setMaxSilenceTime(int value)
152 if (m_maxSilenceTime != value) {
153 m_maxSilenceTime = value;
154 Q_EMIT maxSilenceTimeChanged(value);
155 }
158void DirectionalDragArea::setCompositionTime(int value)
160 if (m_compositionTime != value) {
161 m_compositionTime = value;
162 Q_EMIT compositionTimeChanged(value);
163 }
166void DirectionalDragArea::setRecognitionTimer(UbuntuGestures::AbstractTimer *timer)
168 int interval = 0;
169 bool timerWasRunning = false;
170 bool wasSingleShot = false;
172 // can be null when called from the constructor
173 if (m_recognitionTimer) {
174 interval = m_recognitionTimer->interval();
175 timerWasRunning = m_recognitionTimer->isRunning();
176 if (m_recognitionTimer->parent() == this) {
177 delete m_recognitionTimer;
178 }
179 }
181 m_recognitionTimer = timer;
182 timer->setInterval(interval);
183 timer->setSingleShot(wasSingleShot);
184 connect(timer, &UbuntuGestures::AbstractTimer::timeout,
185 this, &DirectionalDragArea::checkSpeed);
186 if (timerWasRunning) {
187 m_recognitionTimer->start();
188 }
191void DirectionalDragArea::setTimeSource(const SharedTimeSource &timeSource)
193 m_timeSource = timeSource;
194 m_velocityCalculator->setTimeSource(timeSource);
195 m_activeTouches.m_timeSource = timeSource;
198qreal DirectionalDragArea::distance() const
200 if (Direction::isHorizontal(m_direction)) {
201 return m_previousPos.x() - m_startPos.x();
202 } else {
203 return m_previousPos.y() - m_startPos.y();
204 }
207void DirectionalDragArea::updateSceneDistance()
209 QPointF totalMovement = m_previousScenePos - m_startScenePos;
210 m_sceneDistance = projectOntoDirectionVector(totalMovement);
213qreal DirectionalDragArea::sceneDistance() const
215 return m_sceneDistance;
218qreal DirectionalDragArea::touchX() const
220 return m_previousPos.x();
223qreal DirectionalDragArea::touchY() const
225 return m_previousPos.y();
228qreal DirectionalDragArea::touchSceneX() const
230 return m_previousScenePos.x();
233qreal DirectionalDragArea::touchSceneY() const
235 return m_previousScenePos.y();
238bool DirectionalDragArea::event(QEvent *event)
240 if (event->type() == TouchOwnershipEvent::touchOwnershipEventType()) {
241 touchOwnershipEvent(static_cast<TouchOwnershipEvent *>(event));
242 return true;
243 } else if (event->type() == UnownedTouchEvent::unownedTouchEventType()) {
244 unownedTouchEvent(static_cast<UnownedTouchEvent *>(event));
245 return true;
246 } else {
247 return QQuickItem::event(event);
248 }
251void DirectionalDragArea::touchOwnershipEvent(TouchOwnershipEvent *event)
253 if (event->gained()) {
254 QVector<int> ids;
255 ids.append(event->touchId());
256 DDA_DEBUG("grabbing touch");
257 grabTouchPoints(ids);
259 // Work around for Qt bug. If we grab a touch that is being used for mouse pointer
260 // emulation it will cause the emulation logic to go nuts.
261 // Thus we have to also grab the mouse in this case.
262 // TODO: Report bug to Qt
263 if (window()) {
264 QQuickWindowPrivate *windowPrivate = QQuickWindowPrivate::get(window());
265 if (windowPrivate->touchMouseId == event->touchId() && window()->mouseGrabberItem()) {
266 DDA_DEBUG("removing mouse grabber");
267 window()->mouseGrabberItem()->ungrabMouse();
268 }
269 }
270 } else {
271 // We still wanna know when it ends for keeping the composition time window up-to-date
272 TouchRegistry::instance()->addTouchWatcher(m_touchId, this);
274 setStatus(WaitingForTouch);
275 }
278void DirectionalDragArea::unownedTouchEvent(UnownedTouchEvent *unownedTouchEvent)
280 QTouchEvent *event = unownedTouchEvent->touchEvent();
282 Q_ASSERT(!event->touchPointStates().testFlag(Qt::TouchPointPressed));
284 #if DIRECTIONALDRAGAREA_DEBUG
285 // TODO Consider using qCDebug() when available (Qt 5.2)
286 qDebug() << "[DDA] Unowned" << m_timeSource->msecsSinceReference()
287 << qPrintable(touchEventToString(event));
288 #endif
290 switch (m_status) {
291 case WaitingForTouch:
292 // do nothing
293 break;
294 case Undecided:
295 Q_ASSERT(isEnabled() && isVisible());
296 unownedTouchEvent_undecided(unownedTouchEvent);
297 break;
298 default: // Recognized:
299 // do nothing
300 break;
301 }
303 m_activeTouches.update(event);
306void DirectionalDragArea::unownedTouchEvent_undecided(UnownedTouchEvent *unownedTouchEvent)
308 const QTouchEvent::TouchPoint *touchPoint = fetchTargetTouchPoint(unownedTouchEvent->touchEvent());
309 if (!touchPoint) {
310 qCritical() << "DirectionalDragArea[status=Undecided]: touch " << m_touchId
311 << "missing from UnownedTouchEvent without first reaching state Qt::TouchPointReleased. "
312 "Considering it as released.";
314 TouchRegistry::instance()->removeCandidateOwnerForTouch(m_touchId, this);
315 setStatus(WaitingForTouch);
316 return;
317 }
319 const QPointF &touchScenePos = touchPoint->scenePos();
321 if (touchPoint->state() == Qt::TouchPointReleased) {
322 // touch has ended before recognition concluded
323 DDA_DEBUG("Touch has ended before recognition concluded");
324 TouchRegistry::instance()->removeCandidateOwnerForTouch(m_touchId, this);
325 emitSignalIfTapped();
326 setStatus(WaitingForTouch);
327 return;
328 }
330 m_previousDampedScenePos.setX(m_dampedScenePos.x());
331 m_previousDampedScenePos.setY(m_dampedScenePos.y());
332 m_dampedScenePos.update(touchScenePos);
333 updateVelocityCalculator(touchScenePos);
335 if (!pointInsideAllowedArea()) {
336 DDA_DEBUG("Rejecting gesture because touch point is outside allowed area.");
337 TouchRegistry::instance()->removeCandidateOwnerForTouch(m_touchId, this);
338 // We still wanna know when it ends for keeping the composition time window up-to-date
339 TouchRegistry::instance()->addTouchWatcher(m_touchId, this);
340 setStatus(WaitingForTouch);
341 return;
342 }
344 if (!movingInRightDirection()) {
345 DDA_DEBUG("Rejecting gesture because touch point is moving in the wrong direction.");
346 TouchRegistry::instance()->removeCandidateOwnerForTouch(m_touchId, this);
347 // We still wanna know when it ends for keeping the composition time window up-to-date
348 TouchRegistry::instance()->addTouchWatcher(m_touchId, this);
349 setStatus(WaitingForTouch);
350 return;
351 }
353 setPreviousPos(touchPoint->pos());
354 setPreviousScenePos(touchScenePos);
356 if (isWithinTouchCompositionWindow()) {
357 // There's still time for some new touch to appear and ruin our party as it would be combined
358 // with our m_touchId one and therefore deny the possibility of a single-finger gesture.
359 DDA_DEBUG("Sill within composition window. Let's wait more.");
360 return;
361 }
363 if (movedFarEnough(touchScenePos)) {
364 TouchRegistry::instance()->requestTouchOwnership(m_touchId, this);
365 setStatus(Recognized);
366 } else {
367 DDA_DEBUG("Didn't move far enough yet. Let's wait more.");
368 }
371void DirectionalDragArea::touchEvent(QTouchEvent *event)
373 // TODO: Consider when more than one touch starts in the same event (although it's not possible
374 // with Mir's android-input). Have to track them all. Consider it a plus/bonus.
376 #if DIRECTIONALDRAGAREA_DEBUG
377 // TODO Consider using qCDebug() when available (Qt 5.2)
378 qDebug() << "[DDA]" << m_timeSource->msecsSinceReference()
379 << qPrintable(touchEventToString(event));
380 #endif
382 if (!isEnabled() || !isVisible()) {
383 QQuickItem::touchEvent(event);
384 return;
385 }
387 switch (m_status) {
388 case WaitingForTouch:
389 touchEvent_absent(event);
390 break;
391 case Undecided:
392 touchEvent_undecided(event);
393 break;
394 default: // Recognized:
395 touchEvent_recognized(event);
396 break;
397 }
399 m_activeTouches.update(event);
402void DirectionalDragArea::touchEvent_absent(QTouchEvent *event)
404 // TODO: accept/reject is for the whole event, not per touch id. See how that affects us.
406 if (!event->touchPointStates().testFlag(Qt::TouchPointPressed)) {
407 // Nothing to see here. No touch starting in this event.
408 return;
409 }
411 // to be proven wrong, if that's the case
412 bool allGood = true;
414 if (isWithinTouchCompositionWindow()) {
415 // too close to the last touch start. So we consider them as starting roughly at the same time.
416 // Can't be a single-touch gesture.
417 #if DIRECTIONALDRAGAREA_DEBUG
418 qDebug("[DDA] A new touch point came in but we're still within time composition window. Ignoring it.");
419 #endif
420 allGood = false;
421 }
423 const QList<QTouchEvent::TouchPoint> &touchPoints = event->touchPoints();
425 const QTouchEvent::TouchPoint *newTouchPoint = nullptr;
426 for (int i = 0; i < touchPoints.count() && allGood; ++i) {
427 const QTouchEvent::TouchPoint &touchPoint = touchPoints.at(i);
428 if (touchPoint.state() == Qt::TouchPointPressed) {
429 if (newTouchPoint) {
430 // more than one touch starting in this QTouchEvent. Can't be a single-touch gesture
431 allGood = false;
432 } else {
433 // that's our candidate
434 m_touchId = touchPoint.id();
435 newTouchPoint = &touchPoint;
436 }
437 }
438 }
440 if (allGood) {
441 Q_ASSERT(newTouchPoint);
443 m_startPos = newTouchPoint->pos();
444 m_startScenePos = newTouchPoint->scenePos();
445 m_touchId = newTouchPoint->id();
446 m_dampedScenePos.reset(m_startScenePos);
447 m_velocityCalculator->setTrackedPosition(0.);
448 m_velocityCalculator->reset();
449 m_numSamplesOnLastSpeedCheck = 0;
450 m_silenceTime = 0;
451 setPreviousPos(m_startPos);
452 setPreviousScenePos(m_startScenePos);
453 updateSceneDirectionVector();
455 if (recognitionIsDisabled()) {
456 // Behave like a dumb TouchArea
457 TouchRegistry::instance()->requestTouchOwnership(m_touchId, this);
458 setStatus(Recognized);
459 event->accept();
460 } else {
461 // just monitor the touch points for now.
462 TouchRegistry::instance()->addCandidateOwnerForTouch(m_touchId, this);
464 setStatus(Undecided);
465 // Let the item below have it. We will monitor it and grab it later if a gesture
466 // gets recognized.
467 event->ignore();
468 }
469 } else {
470 watchPressedTouchPoints(touchPoints);
471 event->ignore();
472 }
475void DirectionalDragArea::touchEvent_undecided(QTouchEvent *event)
477 Q_ASSERT(event->type() == QEvent::TouchBegin);
478 Q_ASSERT(fetchTargetTouchPoint(event) == nullptr);
480 // We're not interested in new touch points. We already have our candidate (m_touchId).
481 // But we do want to know when those new touches end for keeping the composition time
482 // window up-to-date
483 event->ignore();
484 watchPressedTouchPoints(event->touchPoints());
486 if (event->touchPointStates().testFlag(Qt::TouchPointPressed) && isWithinTouchCompositionWindow()) {
487 // multi-finger drags are not accepted
488 DDA_DEBUG("Multi-finger drags are not accepted");
490 TouchRegistry::instance()->removeCandidateOwnerForTouch(m_touchId, this);
491 // We still wanna know when it ends for keeping the composition time window up-to-date
492 TouchRegistry::instance()->addTouchWatcher(m_touchId, this);
494 setStatus(WaitingForTouch);
495 }
498void DirectionalDragArea::touchEvent_recognized(QTouchEvent *event)
500 const QTouchEvent::TouchPoint *touchPoint = fetchTargetTouchPoint(event);
502 if (!touchPoint) {
503 qCritical() << "DirectionalDragArea[status=Recognized]: touch " << m_touchId
504 << "missing from QTouchEvent without first reaching state Qt::TouchPointReleased. "
505 "Considering it as released.";
506 setStatus(WaitingForTouch);
507 } else {
508 setPreviousPos(touchPoint->pos());
509 setPreviousScenePos(touchPoint->scenePos());
511 if (touchPoint->state() == Qt::TouchPointReleased) {
512 emitSignalIfTapped();
513 setStatus(WaitingForTouch);
514 }
515 }
518void DirectionalDragArea::watchPressedTouchPoints(const QList<QTouchEvent::TouchPoint> &touchPoints)
520 for (int i = 0; i < touchPoints.count(); ++i) {
521 const QTouchEvent::TouchPoint &touchPoint = touchPoints.at(i);
522 if (touchPoint.state() == Qt::TouchPointPressed) {
523 TouchRegistry::instance()->addTouchWatcher(touchPoint.id(), this);
524 }
525 }
528bool DirectionalDragArea::recognitionIsDisabled() const
530 return distanceThreshold() <= 0 && compositionTime() <= 0;
533void DirectionalDragArea::emitSignalIfTapped()
535 qint64 touchDuration = m_timeSource->msecsSinceReference() - m_activeTouches.touchStartTime(m_touchId);
536 if (touchDuration <= maxTapDuration()) {
537 Q_EMIT tapped();
538 }
541const QTouchEvent::TouchPoint *DirectionalDragArea::fetchTargetTouchPoint(QTouchEvent *event)
543 const QList<QTouchEvent::TouchPoint> &touchPoints = event->touchPoints();
544 const QTouchEvent::TouchPoint *touchPoint = 0;
545 for (int i = 0; i < touchPoints.size(); ++i) {
546 if (touchPoints.at(i).id() == m_touchId) {
547 touchPoint = &touchPoints.at(i);
548 break;
549 }
550 }
551 return touchPoint;
554bool DirectionalDragArea::pointInsideAllowedArea() const
556 // NB: Using squared values to avoid computing the square root to find
557 // the length totalMovement
559 QPointF totalMovement(m_dampedScenePos.x() - m_startScenePos.x(),
560 m_dampedScenePos.y() - m_startScenePos.y());
562 qreal squaredTotalMovSize = totalMovement.x() * totalMovement.x() +
563 totalMovement.y() * totalMovement.y();
565 if (squaredTotalMovSize == 0.) {
566 // didn't move
567 return true;
568 }
570 qreal projectedMovement = projectOntoDirectionVector(totalMovement);
573 qreal cosineAngleSquared = (projectedMovement * projectedMovement) / squaredTotalMovSize;
575 // Same as:
576 // angle_between_movement_vector_and_gesture_direction_vector <= widening_angle
577 return cosineAngleSquared >= m_wideningFactor;
580bool DirectionalDragArea::movingInRightDirection() const
582 QPointF movementVector(m_dampedScenePos.x() - m_previousDampedScenePos.x(),
583 m_dampedScenePos.y() - m_previousDampedScenePos.y());
585 qreal scalarProjection = projectOntoDirectionVector(movementVector);
587 return scalarProjection >= 0.;
590bool DirectionalDragArea::movedFarEnough(const QPointF &point) const
592 if (m_distanceThreshold <= 0.) {
593 // distance threshold check is disabled
594 return true;
595 } else {
596 QPointF totalMovement(point.x() - m_startScenePos.x(),
597 point.y() - m_startScenePos.y());
599 qreal squaredTotalMovSize = totalMovement.x() * totalMovement.x() +
600 totalMovement.y() * totalMovement.y();
602 return squaredTotalMovSize > m_distanceThresholdSquared;
603 }
606void DirectionalDragArea::checkSpeed()
608 Q_ASSERT(m_status == Undecided);
610 if (m_velocityCalculator->numSamples() >= AxisVelocityCalculator::MIN_SAMPLES_NEEDED) {
611 qreal speed = qFabs(m_velocityCalculator->calculate());
612 qreal minSpeedMsecs = m_minSpeed / 1000.0;
614 if (speed < minSpeedMsecs) {
615 DDA_DEBUG("Rejecting gesture because it's below minimum speed.");
616 TouchRegistry::instance()->removeCandidateOwnerForTouch(m_touchId, this);
617 TouchRegistry::instance()->addTouchWatcher(m_touchId, this);
618 setStatus(WaitingForTouch);
619 }
620 }
622 if (m_velocityCalculator->numSamples() == m_numSamplesOnLastSpeedCheck) {
623 m_silenceTime += m_recognitionTimer->interval();
625 if (m_silenceTime > m_maxSilenceTime) {
626 DDA_DEBUG("Rejecting gesture because its silence time has been exceeded.");
627 TouchRegistry::instance()->removeCandidateOwnerForTouch(m_touchId, this);
628 TouchRegistry::instance()->addTouchWatcher(m_touchId, this);
629 setStatus(WaitingForTouch);
630 }
631 } else {
632 m_silenceTime = 0;
633 }
634 m_numSamplesOnLastSpeedCheck = m_velocityCalculator->numSamples();
637void DirectionalDragArea::giveUpIfDisabledOrInvisible()
639 if (!isEnabled() || !isVisible()) {
640 if (m_status == Undecided) {
641 TouchRegistry::instance()->removeCandidateOwnerForTouch(m_touchId, this);
642 // We still wanna know when it ends for keeping the composition time window up-to-date
643 TouchRegistry::instance()->addTouchWatcher(m_touchId, this);
644 }
646 if (m_status != WaitingForTouch) {
647 DDA_DEBUG("Resetting status because got disabled or made invisible");
648 setStatus(WaitingForTouch);
649 }
650 }
653void DirectionalDragArea::setStatus(DirectionalDragArea::Status newStatus)
655 if (newStatus == m_status)
656 return;
658 DirectionalDragArea::Status oldStatus = m_status;
660 if (oldStatus == Undecided) {
661 m_recognitionTimer->stop();
662 }
664 m_status = newStatus;
665 Q_EMIT statusChanged(m_status);
667 #if DIRECTIONALDRAGAREA_DEBUG
668 qDebug() << "[DDA]" << statusToString(oldStatus) << "->" << statusToString(newStatus);
669 #endif
671 switch (newStatus) {
672 case WaitingForTouch:
673 Q_EMIT draggingChanged(false);
674 break;
675 case Undecided:
676 m_recognitionTimer->start();
677 Q_EMIT draggingChanged(true);
678 break;
679 case Recognized:
680 if (oldStatus == WaitingForTouch)
681 Q_EMIT draggingChanged(true);
682 break;
683 default:
684 // no-op
685 break;
686 }
689void DirectionalDragArea::setPreviousPos(const QPointF &point)
691 bool xChanged = m_previousPos.x() != point.x();
692 bool yChanged = m_previousPos.y() != point.y();
694 m_previousPos = point;
696 if (xChanged) {
697 Q_EMIT touchXChanged(point.x());
698 if (Direction::isHorizontal(m_direction))
699 Q_EMIT distanceChanged(distance());
700 }
702 if (yChanged) {
703 Q_EMIT touchYChanged(point.y());
704 if (Direction::isVertical(m_direction))
705 Q_EMIT distanceChanged(distance());
706 }
709void DirectionalDragArea::setPreviousScenePos(const QPointF &point)
711 bool xChanged = m_previousScenePos.x() != point.x();
712 bool yChanged = m_previousScenePos.y() != point.y();
714 if (!xChanged && !yChanged)
715 return;
717 qreal oldSceneDistance = sceneDistance();
718 m_previousScenePos = point;
719 updateSceneDistance();
721 if (oldSceneDistance != sceneDistance()) {
722 Q_EMIT sceneDistanceChanged(sceneDistance());
723 }
725 if (xChanged) {
726 Q_EMIT touchSceneXChanged(point.x());
727 }
729 if (yChanged) {
730 Q_EMIT touchSceneYChanged(point.y());
731 }
734void DirectionalDragArea::updateVelocityCalculator(const QPointF &scenePos)
736 QPointF totalSceneMovement = scenePos - m_startScenePos;
738 qreal scalarProjection = projectOntoDirectionVector(totalSceneMovement);
740 m_velocityCalculator->setTrackedPosition(scalarProjection);
743bool DirectionalDragArea::isWithinTouchCompositionWindow()
745 return
746 compositionTime() > 0 &&
747 !m_activeTouches.isEmpty() &&
748 m_timeSource->msecsSinceReference() <=
749 m_activeTouches.mostRecentStartTime() + (qint64)compositionTime();
752//************************** ActiveTouchesInfo **************************
754DirectionalDragArea::ActiveTouchesInfo::ActiveTouchesInfo(const SharedTimeSource &timeSource)
755 : m_timeSource(timeSource)
759void DirectionalDragArea::ActiveTouchesInfo::update(QTouchEvent *event)
761 if (!(event->touchPointStates() & (Qt::TouchPointPressed | Qt::TouchPointReleased))) {
762 // nothing to update
763 #if ACTIVETOUCHESINFO_DEBUG
764 qDebug("[DDA::ActiveTouchesInfo] Nothing to Update");
765 #endif
766 return;
767 }
769 const QList<QTouchEvent::TouchPoint> &touchPoints = event->touchPoints();
770 for (int i = 0; i < touchPoints.count(); ++i) {
771 const QTouchEvent::TouchPoint &touchPoint = touchPoints.at(i);
772 if (touchPoint.state() == Qt::TouchPointPressed) {
773 addTouchPoint(touchPoint.id());
774 } else if (touchPoint.state() == Qt::TouchPointReleased) {
775 removeTouchPoint(touchPoint.id());
776 }
777 }
780#if ACTIVETOUCHESINFO_DEBUG
781QString DirectionalDragArea::ActiveTouchesInfo::toString()
783 QString string = "(";
785 {
786 QTextStream stream(&string);
787 m_touchInfoPool.forEach([&](Pool<ActiveTouchInfo>::Iterator &touchInfo) {
788 stream << "(id=" << touchInfo->id << ",startTime=" << touchInfo->startTime << ")";
789 return true;
790 });
791 }
793 string.append(")");
795 return string;
797#endif // ACTIVETOUCHESINFO_DEBUG
799void DirectionalDragArea::ActiveTouchesInfo::addTouchPoint(int touchId)
801 ActiveTouchInfo &activeTouchInfo = m_touchInfoPool.getEmptySlot();
802 activeTouchInfo.id = touchId;
803 activeTouchInfo.startTime = m_timeSource->msecsSinceReference();
805 #if ACTIVETOUCHESINFO_DEBUG
806 qDebug() << "[DDA::ActiveTouchesInfo]" << qPrintable(toString());
807 #endif
810qint64 DirectionalDragArea::ActiveTouchesInfo::touchStartTime(int touchId)
812 qint64 result = -1;
814 m_touchInfoPool.forEach([&](Pool<ActiveTouchInfo>::Iterator &touchInfo) {
815 if (touchId == touchInfo->id) {
816 result = touchInfo->startTime;
817 return false;
818 } else {
819 return true;
820 }
821 });
823 Q_ASSERT(result != -1);
824 return result;
827void DirectionalDragArea::ActiveTouchesInfo::removeTouchPoint(int touchId)
829 m_touchInfoPool.forEach([&](Pool<ActiveTouchInfo>::Iterator &touchInfo) {
830 if (touchId == touchInfo->id) {
831 m_touchInfoPool.freeSlot(touchInfo);
832 return false;
833 } else {
834 return true;
835 }
836 });
838 #if ACTIVETOUCHESINFO_DEBUG
839 qDebug() << "[DDA::ActiveTouchesInfo]" << qPrintable(toString());
840 #endif
843qint64 DirectionalDragArea::ActiveTouchesInfo::mostRecentStartTime()
845 Q_ASSERT(!m_touchInfoPool.isEmpty());
847 qint64 highestStartTime = -1;
849 m_touchInfoPool.forEach([&](Pool<ActiveTouchInfo>::Iterator &activeTouchInfo) {
850 if (activeTouchInfo->startTime > highestStartTime) {
851 highestStartTime = activeTouchInfo->startTime;
852 }
853 return true;
854 });
856 return highestStartTime;
859void DirectionalDragArea::updateSceneDirectionVector()
861 QPointF localOrigin(0., 0.);
862 QPointF localDirection;
863 switch (m_direction) {
864 case Direction::Upwards:
865 localDirection.rx() = 0.;
866 localDirection.ry() = -1.;
867 break;
868 case Direction::Downwards:
869 localDirection.rx() = 0.;
870 localDirection.ry() = 1;
871 break;
872 case Direction::Leftwards:
873 localDirection.rx() = -1.;
874 localDirection.ry() = 0.;
875 break;
876 default: // Direction::Rightwards:
877 localDirection.rx() = 1.;
878 localDirection.ry() = 0.;
879 break;
880 }
881 QPointF sceneOrigin = mapToScene(localOrigin);
882 QPointF sceneDirection = mapToScene(localDirection);
883 m_sceneDirectionVector = sceneDirection - sceneOrigin;
886qreal DirectionalDragArea::projectOntoDirectionVector(const QPointF &sceneVector) const
888 // same as dot product as m_sceneDirectionVector is a unit vector
889 return sceneVector.x() * m_sceneDirectionVector.x() +
890 sceneVector.y() * m_sceneDirectionVector.y();
893// Because we are defining a new QObject-based class (RecognitionTimer) here.
894#include "DirectionalDragArea.moc"