1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "eventcapturer.h"
#include <QDebug>
#include <QMetaEnum>
#include <QMouseEvent>
#include <QTimer>
/*!
Installs an event filter on a particular object to record specific events
that can be retrieved as C++ source code.
For example:
\code
EventCapturer eventCapturer;
view.show();
eventCapturer.startCapturing(&view, 5000);
// interact with the view here, in order for the events to be captured
qDebug() << "\n";
const auto capturedEvents = eventCapturer.capturedEvents();
for (CapturedEvent event : capturedEvents)
qDebug().noquote() << event.cppCommand();
\endcode
It is recommended to set the \c Qt::FramelessWindowHint flag on the view
(this code has not been tested under other usage):
view.setFlags(view.flags() | Qt::FramelessWindowHint);
*/
EventCapturer::EventCapturer(QObject *parent) :
QObject(parent),
mEventSource(nullptr),
mStopCaptureKey(Qt::Key_Escape),
mMoveEventTrimFlags(TrimNone),
mDuration(0),
mLastCaptureTime(0)
{
mCapturedEventTypes << QEvent::MouseButtonPress << QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick << QEvent::MouseMove;
}
void EventCapturer::startCapturing(QObject *eventSource, int duration)
{
mEventSource = eventSource;
if (!mEventSource)
return;
mEventSource->installEventFilter(this);
mDelayTimer.start();
mDuration = duration;
mLastCaptureTime = 0;
QTimer::singleShot(mDuration, this, SLOT(stopCapturing()));
}
void EventCapturer::setStopCaptureKey(Qt::Key stopCaptureKey)
{
mStopCaptureKey = stopCaptureKey;
}
/*!
Move events generate a lot of clutter, and for most cases they're not
necessary. Here's a list of scenarios where various trim flags make sense:
Scenario Flags
Record the mouse cursor TrimNone
Record mouseover/hover effects TrimNone
Dragging/flicking TrimAll
*/
void EventCapturer::setMoveEventTrimFlags(MoveEventTrimFlags trimFlags)
{
mMoveEventTrimFlags = trimFlags;
}
QSet<QEvent::Type> EventCapturer::capturedEventTypes()
{
return mCapturedEventTypes;
}
void EventCapturer::setCapturedEventTypes(QSet<QEvent::Type> types)
{
mCapturedEventTypes = types;
}
QList<CapturedEvent> EventCapturer::capturedEvents() const
{
if (mMoveEventTrimFlags == TrimNone || mEvents.isEmpty())
return mEvents;
// We can't easily trim "trailing" move events as they come in without
// storing them in some form, so we just do it all here.
int firstEventIndex = 0;
int lastEventIndex = mEvents.size() - 1;
// The accumulated delay of all of the move events that we remove.
// We keep this in order to maintain the correct timing between events.
int accumulatedDelay = 0;
bool encounteredNonMoveEvent = false;
if (mMoveEventTrimFlags.testFlag(TrimLeading)) {
for (int eventIndex = 0; !encounteredNonMoveEvent && eventIndex < mEvents.size(); ++eventIndex) {
const CapturedEvent event = mEvents.at(eventIndex);
if (event.type() != QEvent::MouseMove) {
encounteredNonMoveEvent = true;
firstEventIndex = eventIndex;
} else {
accumulatedDelay += event.delay();
}
}
}
if (mMoveEventTrimFlags.testFlag(TrimTrailing)) {
encounteredNonMoveEvent = false;
for (int eventIndex = mEvents.size() - 1; !encounteredNonMoveEvent && eventIndex >= 0; --eventIndex) {
const CapturedEvent event = mEvents.at(eventIndex);
if (event.type() != QEvent::MouseMove) {
encounteredNonMoveEvent = true;
lastEventIndex = eventIndex;
// Don't need to bother with delays for trailing mouse moves, as there is nothing after them.
}
}
}
// Before we go any further, we need to copy the subset of commands while
// the indices are still valid - we could be removing from the middle of
// the commands next. Also, the function is const, so we can't remove from
// mEvents anyway. :)
QList<CapturedEvent> events = mEvents.mid(firstEventIndex, (lastEventIndex - firstEventIndex) + 1);
if (mMoveEventTrimFlags.testFlag(TrimAfterReleases)) {
bool lastNonMoveEventWasRelease = false;
for (int eventIndex = 0; eventIndex < events.size(); ) {
CapturedEvent &event = events[eventIndex];
if (event.type() == QEvent::MouseMove && lastNonMoveEventWasRelease) {
accumulatedDelay += event.delay();
events.remove(eventIndex);
} else {
lastNonMoveEventWasRelease = event.type() == QEvent::MouseButtonRelease;
if (event.type() == QEvent::MouseButtonPress) {
event.setDelay(event.delay() + accumulatedDelay);
accumulatedDelay = 0;
}
++eventIndex;
}
}
}
return events;
}
bool EventCapturer::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::KeyPress && static_cast<QKeyEvent*>(event)->key() == mStopCaptureKey) {
stopCapturing();
return true;
}
if (object != mEventSource)
return false;
if (!mCapturedEventTypes.contains(event->type()))
return false;
if (event->type() == QEvent::MouseButtonPress) {
captureEvent(event);
} else if (event->type() == QEvent::MouseButtonRelease) {
captureEvent(event);
} else if (event->type() == QEvent::MouseButtonDblClick) {
captureEvent(event);
} else if (event->type() == QEvent::MouseMove) {
captureEvent(event);
} else {
qWarning() << "No support for event type" << QMetaEnum::fromType<QEvent::Type>().valueToKey(event->type());
}
return false;
}
void EventCapturer::stopCapturing()
{
if (mEventSource) {
mEventSource->removeEventFilter(this);
mEventSource = 0;
mDuration = 0;
mLastCaptureTime = 0;
}
}
void EventCapturer::captureEvent(const QEvent *event)
{
qDebug() << "captured" << event->type();
CapturedEvent capturedEvent(*event, mDelayTimer.elapsed() - mLastCaptureTime);
mEvents.append(capturedEvent);
mLastCaptureTime = mDelayTimer.elapsed();
}
|