Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qeventpoint.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qeventpoint.h"
5#include "private/qeventpoint_p.h"
6#include "private/qpointingdevice_p.h"
7
9
10Q_LOGGING_CATEGORY(lcPointerVel, "qt.pointer.velocity")
11Q_LOGGING_CATEGORY(lcEPDetach, "qt.pointer.eventpoint.detach")
12
14
52
57QEventPoint::QEventPoint(int pointId, State state, const QPointF &scenePosition, const QPointF &globalPosition)
58 : d(new QEventPointPrivate(pointId, state, scenePosition, globalPosition)) {}
59
63QEventPoint::QEventPoint(const QEventPoint &other) noexcept = default;
64
69QEventPoint &QEventPoint::operator=(const QEventPoint &other) noexcept = default;
70
87bool QEventPoint::operator==(const QEventPoint &other) const noexcept
88{
89 if (d == other.d)
90 return true;
91 if (!d || !other.d)
92 return false;
93 return *d == *other.d;
94}
95
106QEventPoint::~QEventPoint() = default;
107
122{ return d ? d->pos : QPointF(); }
123
133{ return d ? d->globalPressPos - d->globalPos + d->pos : QPointF(); }
134
144{ return d ? d->globalGrabPos - d->globalPos + d->pos : QPointF(); }
145
155{ return d ? d->globalLastPos - d->globalPos + d->pos : QPointF(); }
156
168{ return d ? d->scenePos : QPointF(); }
169
182
195
208
218{ return d ? d->globalPos : QPointF(); }
219
230
241
252
270{ return d ? d->velocity : QVector2D(); }
271
278
284{ return d ? d->device : nullptr; }
285
295{ return d ? d->pointId : -1; }
296
312
320{ return d ? d->timestamp : 0; }
321
329{ return d ? d->lastTimestamp : 0; }
330
338{ return d ? d->pressTimestamp : 0; }
339
347{ return d ? (d->timestamp - d->pressTimestamp) / qreal(1000) : 0.0; }
348
356{ return d ? d->pressure : 0.0; }
357
368{ return d ? d->rotation : 0.0; }
369
381
398void QEventPoint::setAccepted(bool accepted)
399{
400 if (d)
401 d->accept = accepted;
402}
403
405{ return d ? d->accept : false; }
406
407
423{
424 if (!d)
425 return {};
426
427 auto geom = d->device->availableVirtualGeometry();
428 if (geom.isNull())
429 return QPointF();
430 return (globalPosition() - geom.topLeft()) / geom.width();
431}
432
433#if QT_DEPRECATED_SINCE(6, 0)
439QPointF QEventPoint::startNormalizedPos() const
440{
441 if (!d)
442 return {};
443
444 auto geom = d->device->availableVirtualGeometry();
445 if (geom.isNull())
446 return QPointF();
447 return (globalPressPosition() - geom.topLeft()) / geom.width();
448}
449
461QPointF QEventPoint::lastNormalizedPos() const
462{
463 if (!d)
464 return {};
465
466 auto geom = d->device->availableVirtualGeometry();
467 if (geom.isNull())
468 return QPointF();
469 return (globalLastPosition() - geom.topLeft()) / geom.width();
470}
471#endif // QT_DEPRECATED_SINCE(6, 0)
472
484{
485 if (p.d)
486 p.d.detach();
487 else
488 p.d.reset(new QEventPointPrivate(-1, nullptr));
489}
490
506{
507 detach(target);
508 setPressure(target, other.pressure());
509
510 switch (other.state()) {
512 setGlobalPressPosition(target, other.globalPosition());
513 setGlobalLastPosition(target, other.globalPosition());
514 if (target.pressure() < 0)
515 setPressure(target, 1);
516 break;
517
519 if (target.globalPosition() != other.globalPosition())
520 setGlobalLastPosition(target, target.globalPosition());
521 setPressure(target, 0);
522 break;
523
524 default: // update or stationary
525 if (target.globalPosition() != other.globalPosition())
526 setGlobalLastPosition(target, target.globalPosition());
527 if (target.pressure() < 0)
528 setPressure(target, 1);
529 break;
530 }
531
532 setState(target, other.state());
533 setPosition(target, other.position());
534 setScenePosition(target, other.scenePosition());
535 setGlobalPosition(target, other.globalPosition());
536 setEllipseDiameters(target, other.ellipseDiameters());
537 setRotation(target, other.rotation());
538 setVelocity(target, other.velocity());
539 setUniqueId(target, other.uniqueId()); // for TUIO
540}
541
561{
562 // On mouse press, if the mouse has moved from its last-known location,
563 // QGuiApplicationPrivate::processMouseEvent() sends first a mouse move and
564 // then a press. Both events will get the same timestamp. So we need to set
565 // the press timestamp and position even when the timestamp isn't advancing,
566 // but skip setting lastTimestamp and velocity because those need a time delta.
567 if (p.d) {
568 if (p.state() == QEventPoint::State::Pressed) {
569 p.d->pressTimestamp = t;
570 p.d->globalPressPos = p.d->globalPos;
571 }
572 if (p.d->timestamp == t)
573 return;
574 }
575 detach(p);
576 if (p.device()) {
577 // get the persistent instance out of QPointingDevicePrivate::activePoints
578 // (which sometimes might be the same as this instance)
580 const_cast<QPointingDevice *>(p.d->device))->pointById(p.id())->eventPoint.d.get();
581 if (t > pd->timestamp) {
582 pd->lastTimestamp = pd->timestamp;
583 pd->timestamp = t;
584 if (p.state() == QEventPoint::State::Pressed)
585 pd->pressTimestamp = t;
586 if (pd->lastTimestamp > 0 && !p.device()->capabilities().testFlag(QInputDevice::Capability::Velocity)) {
587 // calculate instantaneous velocity according to time and distance moved since the previous point
588 QVector2D newVelocity = QVector2D(pd->globalPos - pd->globalLastPos) / (t - pd->lastTimestamp) * 1000;
589 // VERY simple kalman filter: does a weighted average
590 // where the older velocities get less and less significant
591 static const float KalmanGain = 0.7f;
592 pd->velocity = newVelocity * KalmanGain + pd->velocity * (1.0f - KalmanGain);
593 qCDebug(lcPointerVel) << "velocity" << newVelocity << "filtered" << pd->velocity <<
594 "based on movement" << pd->globalLastPos << "->" << pd->globalPos <<
595 "over time" << pd->lastTimestamp << "->" << pd->timestamp;
596 }
597 if (p.d != pd) {
598 p.d->lastTimestamp = pd->lastTimestamp;
599 p.d->velocity = pd->velocity;
600 }
601 }
602 }
603 p.d->timestamp = t;
604}
605
618
619#include "moc_qeventpoint.cpp"
IOBluetoothDevice * device
QEventPoint::State state
QPointingDeviceUniqueId uniqueId
const QPointingDevice * device
The QEventPoint class provides information about a point in a QPointerEvent.
Definition qeventpoint.h:20
QPointF pressPosition
the position at which this point was pressed.
Definition qeventpoint.h:36
QPointF globalPosition
the global position of this point.
Definition qeventpoint.h:43
QEventPoint(int id=-1, const QPointingDevice *device=nullptr)
bool isAccepted() const
qreal pressure
the pressure of this point.
Definition qeventpoint.h:31
~QEventPoint()
Destroys the event point.
QPointF normalizedPosition() const
Returns the normalized position of this point.
int id
the ID number of this event point.
Definition qeventpoint.h:24
bool operator==(const QEventPoint &other) const noexcept
Returns true if this event point is equal to other, otherwise return false.
QPointF sceneGrabPosition
the scene position at which this point was grabbed.
Definition qeventpoint.h:41
QPointF scenePosition
the scene position of this point.
Definition qeventpoint.h:39
QPointF globalGrabPosition
the global position at which this point was grabbed.
Definition qeventpoint.h:45
State state
the current state of the event point.
Definition qeventpoint.h:26
QEventPoint & operator=(const QEventPoint &other) noexcept
Assigns other to this event point and returns a reference to this event point.
void setAccepted(bool accepted=true)
ulong timestamp
the most recent time at which this point was included in a QPointerEvent.
Definition qeventpoint.h:27
qreal rotation
the angular orientation of this point.
Definition qeventpoint.h:32
QPointF scenePressPosition
the scene position at which this point was pressed.
Definition qeventpoint.h:40
ulong pressTimestamp
the most recent time at which this point was pressed.
Definition qeventpoint.h:28
QPointF globalLastPosition
the global position of this point from the previous press or move event.
Definition qeventpoint.h:46
QPointF sceneLastPosition
the scene position of this point from the previous press or move event.
Definition qeventpoint.h:42
QPointF grabPosition
the position at which this point was grabbed.
Definition qeventpoint.h:37
State
Specifies the state of this event point.
Definition qeventpoint.h:48
const QPointingDevice * device
the pointing device from which this event point originates.
Definition qeventpoint.h:23
QSizeF ellipseDiameters
the width and height of the bounding ellipse of the touch point.
Definition qeventpoint.h:33
QPointingDeviceUniqueId uniqueId
the unique ID of this point or token, if any.
Definition qeventpoint.h:25
QPointF globalPressPosition
the global position at which this point was pressed.
Definition qeventpoint.h:44
QPointF position
the position of this point.
Definition qeventpoint.h:35
QPointF lastPosition
the position of this point from the previous press or move event.
Definition qeventpoint.h:38
qreal timeHeld
the duration, in seconds, since this point was pressed and not released.
Definition qeventpoint.h:30
bool accepted
the accepted state of the event point.
Definition qeventpoint.h:22
QVector2D velocity
a velocity vector, in units of pixels per second, in the coordinate.
Definition qeventpoint.h:34
ulong lastTimestamp
the time from the previous QPointerEvent that contained this point.
Definition qeventpoint.h:29
QRect availableVirtualGeometry
static Q_GUI_EXPORT void detach(QEventPoint &p)
static Q_GUI_EXPORT void update(const QEventPoint &from, QEventPoint &to)
static Q_GUI_EXPORT void setTimestamp(QEventPoint &p, ulong t)
\inmodule QtCore\reentrant
Definition qpoint.h:217
static QPointingDevicePrivate * get(QPointingDevice *q)
QPointingDeviceUniqueId identifies a unique object, such as a tagged token or stylus,...
The QPointingDevice class describes a device from which mouse, touch or tablet events originate.
\inmodule QtCore
Definition qsize.h:208
The QVector2D class represents a vector or vertex in 2D space.
Definition qvectornd.h:31
else opt state
[0]
Combined button and popup list for selecting options.
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
n void setPosition(void) \n\
GLenum target
GLdouble GLdouble t
Definition qopenglext.h:243
GLsizei const void * pointer
Definition qopenglext.h:384
GLfloat GLfloat p
[1]
#define QT_DEFINE_QESDP_SPECIALIZATION_DTOR(Class)
unsigned long ulong
Definition qtypes.h:35
double qreal
Definition qtypes.h:187
QSharedPointer< T > other(t)
[5]