2 * Copyright (C) 2013-2014 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#define ACTIVETOUCHESINFO_DEBUG 0
18#define DIRECTIONALDRAGAREA_DEBUG 0
20#include "DirectionalDragArea.h"
22#include <QQuickWindow>
23#include <QtCore/qmath.h>
26#pragma GCC diagnostic push
27#pragma GCC diagnostic ignored "-pedantic"
28#include <private/qquickwindow_p.h>
29#pragma GCC diagnostic pop
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"
43const char *statusToString(DirectionalDragArea::Status status)
45 if (status == DirectionalDragArea::WaitingForTouch) {
46 return "WaitingForTouch";
47 } else if (status == DirectionalDragArea::Undecided) {
55#else // DIRECTIONALDRAGAREA_DEBUG
56#define DDA_DEBUG(msg) do{}while(0)
57#endif // DIRECTIONALDRAGAREA_DEBUG
60DirectionalDragArea::DirectionalDragArea(QQuickItem *parent)
62 , m_status(WaitingForTouch)
65 , m_direction(Direction::Rightwards)
68 , m_distanceThreshold(0)
69 , m_distanceThresholdSquared(0.)
71 , m_maxSilenceTime(200)
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
95void DirectionalDragArea::setDirection(Direction::Type direction)
97 if (direction != m_direction) {
98 m_direction = direction;
99 Q_EMIT directionChanged(m_direction);
103void DirectionalDragArea::setMaxDeviation(qreal value)
105 if (m_dampedScenePos.maxDelta() != value) {
106 m_dampedScenePos.setMaxDelta(value);
107 Q_EMIT maxDeviationChanged(value);
111qreal DirectionalDragArea::wideningAngle() const
113 return m_wideningAngle;
116void DirectionalDragArea::setWideningAngle(qreal angle)
118 if (angle == m_wideningAngle)
121 m_wideningAngle = angle;
123 // wideningFactor = pow(cosine(angle), 2)
125 qreal angleRadians = angle * M_PI / 180.0;
126 m_wideningFactor = qCos(angleRadians);
127 m_wideningFactor = m_wideningFactor * m_wideningFactor;
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);
142void DirectionalDragArea::setMinSpeed(qreal value)
144 if (m_minSpeed != value) {
146 Q_EMIT minSpeedChanged(value);
150void DirectionalDragArea::setMaxSilenceTime(int value)
152 if (m_maxSilenceTime != value) {
153 m_maxSilenceTime = value;
154 Q_EMIT maxSilenceTimeChanged(value);
158void DirectionalDragArea::setCompositionTime(int value)
160 if (m_compositionTime != value) {
161 m_compositionTime = value;
162 Q_EMIT compositionTimeChanged(value);
166void DirectionalDragArea::setRecognitionTimer(UbuntuGestures::AbstractTimer *timer)
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;
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();
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();
203 return m_previousPos.y() - m_startPos.y();
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));
243 } else if (event->type() == UnownedTouchEvent::unownedTouchEventType()) {
244 unownedTouchEvent(static_cast<UnownedTouchEvent *>(event));
247 return QQuickItem::event(event);
251void DirectionalDragArea::touchOwnershipEvent(TouchOwnershipEvent *event)
253 if (event->gained()) {
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
264 QQuickWindowPrivate *windowPrivate = QQuickWindowPrivate::get(window());
265 if (windowPrivate->touchMouseId == event->touchId() && window()->mouseGrabberItem()) {
266 DDA_DEBUG("removing mouse grabber");
267 window()->mouseGrabberItem()->ungrabMouse();
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);
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));
291 case WaitingForTouch:
295 Q_ASSERT(isEnabled() && isVisible());
296 unownedTouchEvent_undecided(unownedTouchEvent);
298 default: // Recognized:
303 m_activeTouches.update(event);
306void DirectionalDragArea::unownedTouchEvent_undecided(UnownedTouchEvent *unownedTouchEvent)
308 const QTouchEvent::TouchPoint *touchPoint = fetchTargetTouchPoint(unownedTouchEvent->touchEvent());
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);
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);
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);
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);
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.");
363 if (movedFarEnough(touchScenePos)) {
364 TouchRegistry::instance()->requestTouchOwnership(m_touchId, this);
365 setStatus(Recognized);
367 DDA_DEBUG("Didn't move far enough yet. Let's wait more.");
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));
382 if (!isEnabled() || !isVisible()) {
383 QQuickItem::touchEvent(event);
388 case WaitingForTouch:
389 touchEvent_absent(event);
392 touchEvent_undecided(event);
394 default: // Recognized:
395 touchEvent_recognized(event);
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.
411 // to be proven wrong, if that's the case
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.");
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) {
430 // more than one touch starting in this QTouchEvent. Can't be a single-touch gesture
433 // that's our candidate
434 m_touchId = touchPoint.id();
435 newTouchPoint = &touchPoint;
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;
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);
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
470 watchPressedTouchPoints(touchPoints);
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
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);
498void DirectionalDragArea::touchEvent_recognized(QTouchEvent *event)
500 const QTouchEvent::TouchPoint *touchPoint = fetchTargetTouchPoint(event);
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);
508 setPreviousPos(touchPoint->pos());
509 setPreviousScenePos(touchPoint->scenePos());
511 if (touchPoint->state() == Qt::TouchPointReleased) {
512 emitSignalIfTapped();
513 setStatus(WaitingForTouch);
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);
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()) {
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);
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.) {
570 qreal projectedMovement = projectOntoDirectionVector(totalMovement);
573 qreal cosineAngleSquared = (projectedMovement * projectedMovement) / squaredTotalMovSize;
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
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;
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);
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);
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);
646 if (m_status != WaitingForTouch) {
647 DDA_DEBUG("Resetting status because got disabled or made invisible");
648 setStatus(WaitingForTouch);
653void DirectionalDragArea::setStatus(DirectionalDragArea::Status newStatus)
655 if (newStatus == m_status)
658 DirectionalDragArea::Status oldStatus = m_status;
660 if (oldStatus == Undecided) {
661 m_recognitionTimer->stop();
664 m_status = newStatus;
665 Q_EMIT statusChanged(m_status);
667 #if DIRECTIONALDRAGAREA_DEBUG
668 qDebug() << "[DDA]" << statusToString(oldStatus) << "->" << statusToString(newStatus);
672 case WaitingForTouch:
673 Q_EMIT draggingChanged(false);
676 m_recognitionTimer->start();
677 Q_EMIT draggingChanged(true);
680 if (oldStatus == WaitingForTouch)
681 Q_EMIT draggingChanged(true);
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;
697 Q_EMIT touchXChanged(point.x());
698 if (Direction::isHorizontal(m_direction))
699 Q_EMIT distanceChanged(distance());
703 Q_EMIT touchYChanged(point.y());
704 if (Direction::isVertical(m_direction))
705 Q_EMIT distanceChanged(distance());
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)
717 qreal oldSceneDistance = sceneDistance();
718 m_previousScenePos = point;
719 updateSceneDistance();
721 if (oldSceneDistance != sceneDistance()) {
722 Q_EMIT sceneDistanceChanged(sceneDistance());
726 Q_EMIT touchSceneXChanged(point.x());
730 Q_EMIT touchSceneYChanged(point.y());
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()
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))) {
763 #if ACTIVETOUCHESINFO_DEBUG
764 qDebug("[DDA::ActiveTouchesInfo] Nothing to Update");
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());
780#if ACTIVETOUCHESINFO_DEBUG
781QString DirectionalDragArea::ActiveTouchesInfo::toString()
783 QString string = "(";
786 QTextStream stream(&string);
787 m_touchInfoPool.forEach([&](Pool<ActiveTouchInfo>::Iterator &touchInfo) {
788 stream << "(id=" << touchInfo->id << ",startTime=" << touchInfo->startTime << ")";
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());
810qint64 DirectionalDragArea::ActiveTouchesInfo::touchStartTime(int touchId)
814 m_touchInfoPool.forEach([&](Pool<ActiveTouchInfo>::Iterator &touchInfo) {
815 if (touchId == touchInfo->id) {
816 result = touchInfo->startTime;
823 Q_ASSERT(result != -1);
827void DirectionalDragArea::ActiveTouchesInfo::removeTouchPoint(int touchId)
829 m_touchInfoPool.forEach([&](Pool<ActiveTouchInfo>::Iterator &touchInfo) {
830 if (touchId == touchInfo->id) {
831 m_touchInfoPool.freeSlot(touchInfo);
838 #if ACTIVETOUCHESINFO_DEBUG
839 qDebug() << "[DDA::ActiveTouchesInfo]" << qPrintable(toString());
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;
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.;
868 case Direction::Downwards:
869 localDirection.rx() = 0.;
870 localDirection.ry() = 1;
872 case Direction::Leftwards:
873 localDirection.rx() = -1.;
874 localDirection.ry() = 0.;
876 default: // Direction::Rightwards:
877 localDirection.rx() = 1.;
878 localDirection.ry() = 0.;
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"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list