2 * Copyright (C) 2013-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#define ACTIVETOUCHESINFO_DEBUG 0
18#define DIRECTIONALDRAGAREA_DEBUG 0
20#include "DirectionalDragArea.h"
22#include <QQuickWindow>
23#include <QtCore/qmath.h>
27#pragma GCC diagnostic push
28#pragma GCC diagnostic ignored "-pedantic"
29#include <private/qquickwindow_p.h>
30#pragma GCC diagnostic pop
33#include "TouchOwnershipEvent.h"
34#include "TouchRegistry.h"
35#include "UnownedTouchEvent.h"
37#include "DirectionalDragArea_p.h"
39using namespace UbuntuGestures;
41#if DIRECTIONALDRAGAREA_DEBUG
42#define ddaDebug(params) qDebug().nospace() << "[DDA(" << qPrintable(objectName()) << ")] " << params
43#include "DebugHelpers.h"
46const char *statusToString(DirectionalDragAreaPrivate::Status status)
48 if (status == DirectionalDragAreaPrivate::WaitingForTouch) {
49 return "WaitingForTouch";
50 } else if (status == DirectionalDragAreaPrivate::Undecided) {
58#else // DIRECTIONALDRAGAREA_DEBUG
59#define ddaDebug(params) ((void)0)
60#endif // DIRECTIONALDRAGAREA_DEBUG
62DirectionalDragArea::DirectionalDragArea(QQuickItem *parent)
64 , d(new DirectionalDragAreaPrivate(this))
66 d->setRecognitionTimer(new Timer(this));
67 d->recognitionTimer->setInterval(d->maxTime);
68 d->recognitionTimer->setSingleShot(true);
70 connect(this, &QQuickItem::enabledChanged, d, &DirectionalDragAreaPrivate::giveUpIfDisabledOrInvisible);
71 connect(this, &QQuickItem::visibleChanged, d, &DirectionalDragAreaPrivate::giveUpIfDisabledOrInvisible);
74Direction::Type DirectionalDragArea::direction() const
79void DirectionalDragArea::setDirection(Direction::Type direction)
81 if (direction != d->direction) {
82 d->direction = direction;
83 Q_EMIT directionChanged(d->direction);
87void DirectionalDragAreaPrivate::setDistanceThreshold(qreal value)
89 if (distanceThreshold != value) {
90 distanceThreshold = value;
91 distanceThresholdSquared = distanceThreshold * distanceThreshold;
95void DirectionalDragAreaPrivate::setMaxTime(int value)
97 if (maxTime != value) {
99 recognitionTimer->setInterval(maxTime);
103void DirectionalDragAreaPrivate::setRecognitionTimer(UbuntuGestures::AbstractTimer *timer)
106 bool timerWasRunning = false;
107 bool wasSingleShot = false;
109 // can be null when called from the constructor
110 if (recognitionTimer) {
111 interval = recognitionTimer->interval();
112 timerWasRunning = recognitionTimer->isRunning();
113 if (recognitionTimer->parent() == this) {
114 delete recognitionTimer;
118 recognitionTimer = timer;
119 timer->setInterval(interval);
120 timer->setSingleShot(wasSingleShot);
121 connect(timer, &UbuntuGestures::AbstractTimer::timeout,
122 this, &DirectionalDragAreaPrivate::rejectGesture);
123 if (timerWasRunning) {
124 recognitionTimer->start();
128void DirectionalDragAreaPrivate::setTimeSource(const SharedTimeSource &timeSource)
130 this->timeSource = timeSource;
131 activeTouches.m_timeSource = timeSource;
134qreal DirectionalDragArea::distance() const
136 if (Direction::isHorizontal(d->direction)) {
137 return d->publicPos.x() - d->startPos.x();
139 return d->publicPos.y() - d->startPos.y();
143void DirectionalDragAreaPrivate::updateSceneDistance()
145 QPointF totalMovement = publicScenePos - startScenePos;
146 sceneDistance = projectOntoDirectionVector(totalMovement);
149qreal DirectionalDragArea::sceneDistance() const
151 return d->sceneDistance;
154qreal DirectionalDragArea::touchX() const
156 return d->publicPos.x();
159qreal DirectionalDragArea::touchY() const
161 return d->publicPos.y();
164qreal DirectionalDragArea::touchSceneX() const
166 return d->publicScenePos.x();
169qreal DirectionalDragArea::touchSceneY() const
171 return d->publicScenePos.y();
174bool DirectionalDragArea::dragging() const
176 return d->status == DirectionalDragAreaPrivate::Recognized;
179bool DirectionalDragArea::pressed() const
181 return d->status != DirectionalDragAreaPrivate::WaitingForTouch;
184bool DirectionalDragArea::immediateRecognition() const
186 return d->immediateRecognition;
189void DirectionalDragArea::setImmediateRecognition(bool enabled)
191 if (d->immediateRecognition != enabled) {
192 d->immediateRecognition = enabled;
193 Q_EMIT immediateRecognitionChanged(enabled);
197bool DirectionalDragArea::monitorOnly() const
199 return d->monitorOnly;
202void DirectionalDragArea::setMonitorOnly(bool monitorOnly)
204 if (d->monitorOnly != monitorOnly) {
205 d->monitorOnly = monitorOnly;
207 if (monitorOnly && d->status == DirectionalDragAreaPrivate::Undecided) {
208 TouchRegistry::instance()->removeCandidateOwnerForTouch(d->touchId, this);
209 // We still wanna know when it ends for keeping the composition time window up-to-date
210 TouchRegistry::instance()->addTouchWatcher(d->touchId, this);
213 Q_EMIT monitorOnlyChanged(monitorOnly);
217void DirectionalDragArea::removeTimeConstraints()
219 d->setMaxTime(60 * 60 * 1000);
220 d->compositionTime = 0;
221 ddaDebug("removed time constraints");
224bool DirectionalDragArea::event(QEvent *event)
226 if (event->type() == TouchOwnershipEvent::touchOwnershipEventType()) {
227 d->touchOwnershipEvent(static_cast<TouchOwnershipEvent *>(event));
229 } else if (event->type() == UnownedTouchEvent::unownedTouchEventType()) {
230 d->unownedTouchEvent(static_cast<UnownedTouchEvent *>(event));
233 return QQuickItem::event(event);
237void DirectionalDragAreaPrivate::touchOwnershipEvent(TouchOwnershipEvent *event)
239 if (event->gained()) {
241 ids.append(event->touchId());
242 ddaDebug("grabbing touch");
243 q->grabTouchPoints(ids);
245 // Work around for Qt bug. If we grab a touch that is being used for mouse pointer
246 // emulation it will cause the emulation logic to go nuts.
247 // Thus we have to also grab the mouse in this case.
249 // The fix for this bug has landed in Qt 5.4 (https://codereview.qt-project.org/96887)
250 // TODO: Remove this workaround once we start using Qt 5.4
252 QQuickWindowPrivate *windowPrivate = QQuickWindowPrivate::get(q->window());
253 if (windowPrivate->touchMouseId == event->touchId() && q->window()->mouseGrabberItem()) {
254 ddaDebug("removing mouse grabber");
255 q->window()->mouseGrabberItem()->ungrabMouse();
259 // We still wanna know when it ends for keeping the composition time window up-to-date
260 TouchRegistry::instance()->addTouchWatcher(touchId, q);
262 setStatus(WaitingForTouch);
266void DirectionalDragAreaPrivate::unownedTouchEvent(UnownedTouchEvent *unownedTouchEvent)
268 QTouchEvent *event = unownedTouchEvent->touchEvent();
270 Q_ASSERT(!event->touchPointStates().testFlag(Qt::TouchPointPressed));
272 ddaDebug("Unowned " << timeSource->msecsSinceReference() << " " << qPrintable(touchEventToString(event)));
275 case WaitingForTouch:
279 Q_ASSERT(q->isEnabled() && q->isVisible());
280 unownedTouchEvent_undecided(unownedTouchEvent);
282 default: // Recognized:
284 // Treat unowned event as if we owned it, but we are really just watching it
285 touchEvent_recognized(event);
290 activeTouches.update(event);
293void DirectionalDragAreaPrivate::unownedTouchEvent_undecided(UnownedTouchEvent *unownedTouchEvent)
295 const QTouchEvent::TouchPoint *touchPoint = fetchTargetTouchPoint(unownedTouchEvent->touchEvent());
297 qCritical() << "DirectionalDragArea[status=Undecided]: touch " << touchId
298 << "missing from UnownedTouchEvent without first reaching state Qt::TouchPointReleased. "
299 "Considering it as released.";
301 TouchRegistry::instance()->removeCandidateOwnerForTouch(touchId, q);
302 setStatus(WaitingForTouch);
306 const QPointF &touchScenePos = touchPoint->scenePos();
308 if (touchPoint->state() == Qt::TouchPointReleased) {
309 // touch has ended before recognition concluded
310 ddaDebug("Touch has ended before recognition concluded");
311 TouchRegistry::instance()->removeCandidateOwnerForTouch(touchId, q);
312 setStatus(WaitingForTouch);
316 previousDampedScenePos.setX(dampedScenePos.x());
317 previousDampedScenePos.setY(dampedScenePos.y());
318 dampedScenePos.update(touchScenePos);
320 if (!movingInRightDirection()) {
321 ddaDebug("Rejecting gesture because touch point is moving in the wrong direction.");
322 TouchRegistry::instance()->removeCandidateOwnerForTouch(touchId, q);
323 // We still wanna know when it ends for keeping the composition time window up-to-date
324 TouchRegistry::instance()->addTouchWatcher(touchId, q);
325 setStatus(WaitingForTouch);
329 if (isWithinTouchCompositionWindow()) {
330 // There's still time for some new touch to appear and ruin our party as it would be combined
331 // with our touchId one and therefore deny the possibility of a single-finger gesture.
332 ddaDebug("Sill within composition window. Let's wait more.");
336 if (movedFarEnoughAlongGestureAxis()) {
338 TouchRegistry::instance()->requestTouchOwnership(touchId, q);
340 setStatus(Recognized);
341 setPublicPos(touchPoint->pos());
342 setPublicScenePos(touchScenePos);
343 } else if (isPastMaxDistance()) {
344 ddaDebug("Rejecting gesture because it went farther than maxDistance without getting recognized.");
345 TouchRegistry::instance()->removeCandidateOwnerForTouch(touchId, q);
346 // We still wanna know when it ends for keeping the composition time window up-to-date
347 TouchRegistry::instance()->addTouchWatcher(touchId, q);
348 setStatus(WaitingForTouch);
350 ddaDebug("Didn't move far enough yet. Let's wait more.");
354void DirectionalDragArea::touchEvent(QTouchEvent *event)
356 // TODO: Consider when more than one touch starts in the same event (although it's not possible
357 // with Mir's android-input). Have to track them all. Consider it a plus/bonus.
359 ddaDebug(d->timeSource->msecsSinceReference() << " " << qPrintable(touchEventToString(event)));
361 if (!isEnabled() || !isVisible()) {
362 QQuickItem::touchEvent(event);
367 case DirectionalDragAreaPrivate::WaitingForTouch:
368 d->touchEvent_absent(event);
370 case DirectionalDragAreaPrivate::Undecided:
371 d->touchEvent_undecided(event);
373 default: // Recognized:
374 d->touchEvent_recognized(event);
378 d->activeTouches.update(event);
381void DirectionalDragAreaPrivate::touchEvent_absent(QTouchEvent *event)
383 // TODO: accept/reject is for the whole event, not per touch id. See how that affects us.
385 if (!event->touchPointStates().testFlag(Qt::TouchPointPressed)) {
386 // Nothing to see here. No touch starting in this event.
390 // to be proven wrong, if that's the case
393 if (isWithinTouchCompositionWindow()) {
394 // too close to the last touch start. So we consider them as starting roughly at the same time.
395 // Can't be a single-touch gesture.
396 ddaDebug("A new touch point came in but we're still within time composition window. Ignoring it.");
400 const QList<QTouchEvent::TouchPoint> &touchPoints = event->touchPoints();
402 const QTouchEvent::TouchPoint *newTouchPoint = nullptr;
403 for (int i = 0; i < touchPoints.count() && allGood; ++i) {
404 const QTouchEvent::TouchPoint &touchPoint = touchPoints.at(i);
405 if (touchPoint.state() == Qt::TouchPointPressed) {
407 // more than one touch starting in this QTouchEvent. Can't be a single-touch gesture
410 // that's our candidate
411 newTouchPoint = &touchPoint;
417 allGood = sanityCheckRecognitionProperties();
419 qWarning("DirectionalDragArea: recognition properties are wrongly set. Gesture recognition"
425 Q_ASSERT(newTouchPoint);
427 startPos = newTouchPoint->pos();
428 startScenePos = newTouchPoint->scenePos();
429 touchId = newTouchPoint->id();
430 dampedScenePos.reset(startScenePos);
431 setPublicPos(startPos);
433 setPublicScenePos(startScenePos);
434 updateSceneDirectionVector();
436 if (recognitionIsDisabled()) {
437 // Behave like a dumb TouchArea
438 ddaDebug("Gesture recognition is disabled. Requesting touch ownership immediately.");
439 setStatus(Recognized);
441 watchPressedTouchPoints(touchPoints);
444 TouchRegistry::instance()->requestTouchOwnership(touchId, q);
448 // just monitor the touch points for now.
450 watchPressedTouchPoints(touchPoints);
452 TouchRegistry::instance()->addCandidateOwnerForTouch(touchId, q);
455 setStatus(Undecided);
456 // Let the item below have it. We will monitor it and grab it later if a gesture
461 watchPressedTouchPoints(touchPoints);
466void DirectionalDragAreaPrivate::touchEvent_undecided(QTouchEvent *event)
468 Q_ASSERT(fetchTargetTouchPoint(event) == nullptr);
470 // We're not interested in new touch points. We already have our candidate (touchId).
471 // But we do want to know when those new touches end for keeping the composition time
474 watchPressedTouchPoints(event->touchPoints());
476 if (event->touchPointStates().testFlag(Qt::TouchPointPressed) && isWithinTouchCompositionWindow()) {
477 // multi-finger drags are not accepted
478 ddaDebug("Multi-finger drags are not accepted");
480 TouchRegistry::instance()->removeCandidateOwnerForTouch(touchId, q);
481 // We still wanna know when it ends for keeping the composition time window up-to-date
482 TouchRegistry::instance()->addTouchWatcher(touchId, q);
484 setStatus(WaitingForTouch);
488void DirectionalDragAreaPrivate::touchEvent_recognized(QTouchEvent *event)
490 const QTouchEvent::TouchPoint *touchPoint = fetchTargetTouchPoint(event);
493 qCritical() << "DirectionalDragArea[status=Recognized]: touch " << touchId
494 << "missing from QTouchEvent without first reaching state Qt::TouchPointReleased. "
495 "Considering it as released.";
496 setStatus(WaitingForTouch);
498 setPublicPos(touchPoint->pos());
499 setPublicScenePos(touchPoint->scenePos());
501 if (touchPoint->state() == Qt::TouchPointReleased) {
502 setStatus(WaitingForTouch);
507void DirectionalDragAreaPrivate::watchPressedTouchPoints(const QList<QTouchEvent::TouchPoint> &touchPoints)
509 for (int i = 0; i < touchPoints.count(); ++i) {
510 const QTouchEvent::TouchPoint &touchPoint = touchPoints.at(i);
511 if (touchPoint.state() == Qt::TouchPointPressed) {
512 TouchRegistry::instance()->addTouchWatcher(touchPoint.id(), q);
517bool DirectionalDragAreaPrivate::recognitionIsDisabled() const
519 return immediateRecognition || (distanceThreshold <= 0 && compositionTime <= 0);
522bool DirectionalDragAreaPrivate::sanityCheckRecognitionProperties()
524 return recognitionIsDisabled()
525 || (distanceThreshold < maxDistance && compositionTime < maxTime);
528const QTouchEvent::TouchPoint *DirectionalDragAreaPrivate::fetchTargetTouchPoint(QTouchEvent *event)
530 const QList<QTouchEvent::TouchPoint> &touchPoints = event->touchPoints();
531 const QTouchEvent::TouchPoint *touchPoint = 0;
532 for (int i = 0; i < touchPoints.size(); ++i) {
533 if (touchPoints.at(i).id() == touchId) {
534 touchPoint = &touchPoints.at(i);
541bool DirectionalDragAreaPrivate::movingInRightDirection() const
543 if (direction == Direction::Horizontal || direction == Direction::Vertical) {
546 QPointF movementVector(dampedScenePos.x() - previousDampedScenePos.x(),
547 dampedScenePos.y() - previousDampedScenePos.y());
549 qreal scalarProjection = projectOntoDirectionVector(movementVector);
551 return scalarProjection >= 0.;
555bool DirectionalDragAreaPrivate::movedFarEnoughAlongGestureAxis() const
557 if (distanceThreshold <= 0.) {
558 // distance threshold check is disabled
561 QPointF totalMovement(dampedScenePos.x() - startScenePos.x(),
562 dampedScenePos.y() - startScenePos.y());
564 qreal scalarProjection = projectOntoDirectionVector(totalMovement);
566 ddaDebug(" movedFarEnoughAlongGestureAxis: scalarProjection=" << scalarProjection
567 << ", distanceThreshold=" << distanceThreshold);
569 if (direction == Direction::Horizontal || direction == Direction::Vertical) {
570 return qAbs(scalarProjection) > distanceThreshold;
572 return scalarProjection > distanceThreshold;
577bool DirectionalDragAreaPrivate::isPastMaxDistance() const
579 QPointF totalMovement(dampedScenePos.x() - startScenePos.x(),
580 dampedScenePos.y() - startScenePos.y());
582 qreal squaredDistance = totalMovement.x()*totalMovement.x() + totalMovement.y()*totalMovement.y();
583 return squaredDistance > maxDistance*maxDistance;
586void DirectionalDragAreaPrivate::giveUpIfDisabledOrInvisible()
588 if (!q->isEnabled() || !q->isVisible()) {
589 if (status == Undecided) {
590 TouchRegistry::instance()->removeCandidateOwnerForTouch(touchId, q);
591 // We still wanna know when it ends for keeping the composition time window up-to-date
592 TouchRegistry::instance()->addTouchWatcher(touchId, q);
595 if (status != WaitingForTouch) {
596 ddaDebug("Resetting status because got disabled or made invisible");
597 setStatus(WaitingForTouch);
602void DirectionalDragAreaPrivate::rejectGesture()
604 if (status == Undecided) {
605 ddaDebug("Rejecting gesture because it's taking too long to drag beyond the threshold.");
607 TouchRegistry::instance()->removeCandidateOwnerForTouch(touchId, q);
608 // We still wanna know when it ends for keeping the composition time window up-to-date
609 TouchRegistry::instance()->addTouchWatcher(touchId, q);
611 setStatus(WaitingForTouch);
615void DirectionalDragAreaPrivate::setStatus(Status newStatus)
617 if (newStatus == status)
620 Status oldStatus = status;
622 if (oldStatus == Undecided) {
623 recognitionTimer->stop();
627 Q_EMIT statusChanged(status);
629 ddaDebug(statusToString(oldStatus) << " -> " << statusToString(newStatus));
632 case WaitingForTouch:
633 if (oldStatus == Recognized) {
634 Q_EMIT q->draggingChanged(false);
636 Q_EMIT q->pressedChanged(false);
639 recognitionTimer->start();
640 Q_EMIT q->pressedChanged(true);
643 Q_EMIT q->draggingChanged(true);
651void DirectionalDragAreaPrivate::setPublicPos(const QPointF &point)
653 bool xChanged = publicPos.x() != point.x();
654 bool yChanged = publicPos.y() != point.y();
656 // Public position should not get updated while the gesture is still being recognized
657 // (ie, Undecided status).
658 Q_ASSERT(status == WaitingForTouch || status == Recognized);
660 if (status == Recognized && !recognitionIsDisabled()) {
661 // When the gesture finally gets recognized, the finger will likely be
662 // reasonably far from the edge. If we made the contentX immediately
663 // follow the finger position it would be visually unpleasant as it
664 // would appear right next to the user's finger out of nowhere (ie,
665 // it would jump). Instead, we make contentX go towards the user's
666 // finger in several steps. ie., in an animated way.
667 QPointF delta = point - publicPos;
668 // the trick is not to go all the way (1.0) as it would cause a sudden jump
669 publicPos.rx() += 0.4 * delta.x();
670 publicPos.ry() += 0.4 * delta.y();
672 // no smoothing when initializing or if gesture recognition was immediate as there will
678 Q_EMIT q->touchXChanged(publicPos.x());
679 if (Direction::isHorizontal(direction))
680 Q_EMIT q->distanceChanged(q->distance());
684 Q_EMIT q->touchYChanged(publicPos.y());
685 if (Direction::isVertical(direction))
686 Q_EMIT q->distanceChanged(q->distance());
690void DirectionalDragAreaPrivate::setPublicScenePos(const QPointF &point)
692 bool xChanged = publicScenePos.x() != point.x();
693 bool yChanged = publicScenePos.y() != point.y();
695 if (!xChanged && !yChanged)
698 // Public position should not get updated while the gesture is still being recognized
699 // (ie, Undecided status).
700 Q_ASSERT(status == WaitingForTouch || status == Recognized);
702 qreal oldSceneDistance = sceneDistance;
704 if (status == Recognized && !recognitionIsDisabled()) {
705 // When the gesture finally gets recognized, the finger will likely be
706 // reasonably far from the edge. If we made the contentX immediately
707 // follow the finger position it would be visually unpleasant as it
708 // would appear right next to the user's finger out of nowhere (ie,
709 // it would jump). Instead, we make contentX go towards the user's
710 // finger in several steps. ie., in an animated way.
711 QPointF delta = point - publicScenePos;
712 // the trick is not to go all the way (1.0) as it would cause a sudden jump
713 publicScenePos.rx() += 0.4 * delta.x();
714 publicScenePos.ry() += 0.4 * delta.y();
716 // no smoothing when initializing or if gesture recognition was immediate as there will
718 publicScenePos = point;
721 updateSceneDistance();
723 if (oldSceneDistance != sceneDistance) {
724 Q_EMIT q->sceneDistanceChanged(sceneDistance);
728 Q_EMIT q->touchSceneXChanged(publicScenePos.x());
732 Q_EMIT q->touchSceneYChanged(publicScenePos.y());
736bool DirectionalDragAreaPrivate::isWithinTouchCompositionWindow()
739 compositionTime > 0 &&
740 !activeTouches.isEmpty() &&
741 timeSource->msecsSinceReference() <=
742 activeTouches.mostRecentStartTime() + (qint64)compositionTime;
745void DirectionalDragArea::itemChange(ItemChange change, const ItemChangeData &value)
747 if (change == QQuickItem::ItemSceneChange) {
748 if (value.window != nullptr) {
749 value.window->installEventFilter(TouchRegistry::instance());
751 // TODO: Handle window->screen() changes (ie window changing screens)
752 qreal pixelsPerMm = value.window->screen()->physicalDotsPerInch() / 25.4;
753 d->setPixelsPerMm(pixelsPerMm);
758void DirectionalDragAreaPrivate::setPixelsPerMm(qreal pixelsPerMm)
760 dampedScenePos.setMaxDelta(1. * pixelsPerMm);
761 setDistanceThreshold(4. * pixelsPerMm);
762 maxDistance = 10. * pixelsPerMm;
765//************************** ActiveTouchesInfo **************************
767ActiveTouchesInfo::ActiveTouchesInfo(const SharedTimeSource &timeSource)
768 : m_timeSource(timeSource)
772void ActiveTouchesInfo::update(QTouchEvent *event)
774 if (!(event->touchPointStates() & (Qt::TouchPointPressed | Qt::TouchPointReleased))) {
776 #if ACTIVETOUCHESINFO_DEBUG
777 qDebug("[DDA::ActiveTouchesInfo] Nothing to Update");
782 const QList<QTouchEvent::TouchPoint> &touchPoints = event->touchPoints();
783 for (int i = 0; i < touchPoints.count(); ++i) {
784 const QTouchEvent::TouchPoint &touchPoint = touchPoints.at(i);
785 if (touchPoint.state() == Qt::TouchPointPressed) {
786 addTouchPoint(touchPoint.id());
787 } else if (touchPoint.state() == Qt::TouchPointReleased) {
788 removeTouchPoint(touchPoint.id());
793#if ACTIVETOUCHESINFO_DEBUG
794QString ActiveTouchesInfo::toString()
796 QString string = "(";
799 QTextStream stream(&string);
800 m_touchInfoPool.forEach([&](Pool<ActiveTouchInfo>::Iterator &touchInfo) {
801 stream << "(id=" << touchInfo->id << ",startTime=" << touchInfo->startTime << ")";
810#endif // ACTIVETOUCHESINFO_DEBUG
812void ActiveTouchesInfo::addTouchPoint(int touchId)
814 ActiveTouchInfo &activeTouchInfo = m_touchInfoPool.getEmptySlot();
815 activeTouchInfo.id = touchId;
816 activeTouchInfo.startTime = m_timeSource->msecsSinceReference();
818 #if ACTIVETOUCHESINFO_DEBUG
819 qDebug() << "[DDA::ActiveTouchesInfo]" << qPrintable(toString());
823qint64 ActiveTouchesInfo::touchStartTime(int touchId)
827 m_touchInfoPool.forEach([&](Pool<ActiveTouchInfo>::Iterator &touchInfo) {
828 if (touchId == touchInfo->id) {
829 result = touchInfo->startTime;
836 Q_ASSERT(result != -1);
840void ActiveTouchesInfo::removeTouchPoint(int touchId)
842 m_touchInfoPool.forEach([&](Pool<ActiveTouchInfo>::Iterator &touchInfo) {
843 if (touchId == touchInfo->id) {
844 m_touchInfoPool.freeSlot(touchInfo);
851 #if ACTIVETOUCHESINFO_DEBUG
852 qDebug() << "[DDA::ActiveTouchesInfo]" << qPrintable(toString());
856qint64 ActiveTouchesInfo::mostRecentStartTime()
858 Q_ASSERT(!m_touchInfoPool.isEmpty());
860 qint64 highestStartTime = -1;
862 m_touchInfoPool.forEach([&](Pool<ActiveTouchInfo>::Iterator &activeTouchInfo) {
863 if (activeTouchInfo->startTime > highestStartTime) {
864 highestStartTime = activeTouchInfo->startTime;
869 return highestStartTime;
872void DirectionalDragAreaPrivate::updateSceneDirectionVector()
874 QPointF localOrigin(0., 0.);
875 QPointF localDirection;
877 case Direction::Upwards:
878 localDirection.rx() = 0.;
879 localDirection.ry() = -1.;
881 case Direction::Downwards:
882 case Direction::Vertical:
883 localDirection.rx() = 0.;
884 localDirection.ry() = 1;
886 case Direction::Leftwards:
887 localDirection.rx() = -1.;
888 localDirection.ry() = 0.;
890 default: // Direction::Rightwards || Direction.Horizontal
891 localDirection.rx() = 1.;
892 localDirection.ry() = 0.;
895 QPointF sceneOrigin = q->mapToScene(localOrigin);
896 QPointF sceneDirection = q->mapToScene(localDirection);
897 sceneDirectionVector = sceneDirection - sceneOrigin;
900qreal DirectionalDragAreaPrivate::projectOntoDirectionVector(const QPointF &sceneVector) const
902 // same as dot product as sceneDirectionVector is a unit vector
903 return sceneVector.x() * sceneDirectionVector.x() +
904 sceneVector.y() * sceneDirectionVector.y();
907DirectionalDragAreaPrivate::DirectionalDragAreaPrivate(DirectionalDragArea *q)
909 , status(WaitingForTouch)
912 , direction(Direction::Rightwards)
913 , distanceThreshold(0)
914 , distanceThresholdSquared(0.)
916 , compositionTime(60)
917 , immediateRecognition(false)
918 , recognitionTimer(nullptr)
919 , timeSource(new RealTimeSource)
920 , activeTouches(timeSource)
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