Last active
November 18, 2019 11:50
-
-
Save mitchcurtis/79a1281d34aefa6156294e5306da780f to your computer and use it in GitHub Desktop.
TestCase mouse event debugger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtQuickTest/quicktest.h> | |
#include <QGuiApplication> | |
#include <QQmlContext> | |
#include <QQmlEngine> | |
class MouseDebuggerSetup : public QObject | |
{ | |
Q_OBJECT | |
public: | |
MouseDebuggerSetup() { | |
} | |
public slots: | |
// Qt Quick Test doesn't use QQmlApplicationEngine, otherwise we could cast | |
// the engine here and connect to its objectCreated signal. | |
void qmlEngineAvailable(QQmlEngine *engine) { | |
mEngine = engine; | |
mTimerId = startTimer(0); | |
engine->rootContext()->setContextProperty("mouseDebugger", this); | |
} | |
protected: | |
void timerEvent(QTimerEvent */*event*/) override { | |
auto topLevelWindows = qApp->topLevelWindows(); | |
if (topLevelWindows.isEmpty()) | |
return; | |
killTimer(mTimerId); | |
topLevelWindows.first()->installEventFilter(this); | |
} | |
bool eventFilter(QObject */*object*/, QEvent *event) override { | |
if (event->type() == QEvent::MouseButtonPress) { | |
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); | |
qDebug() << "Mouse press at" << mouseEvent->pos(); | |
markerRequested(mouseEvent->pos(), QLatin1String("press")); | |
} else if (event->type() == QEvent::MouseMove) { | |
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); | |
qDebug() << "Mouse move at" << mouseEvent->pos(); | |
markerRequested(mouseEvent->pos(), QLatin1String("move")); | |
} else if (event->type() == QEvent::MouseButtonRelease) { | |
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); | |
qDebug() << "Mouse release at" << mouseEvent->pos(); | |
markerRequested(mouseEvent->pos(), QLatin1String("release")); | |
} | |
return false; | |
} | |
signals: | |
void markerRequested(const QPoint &pos, const QString &eventType); | |
private: | |
int mTimerId = 0; | |
QQmlEngine *mEngine = nullptr; | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
QTEST_SET_MAIN_SOURCE_PATH | |
qputenv("QML_NO_TOUCH_COMPRESSION", "1"); | |
MouseDebuggerSetup setup; | |
return quick_test_main_with_setup(argc, argv, "tst_controls::Default", TST_CONTROLS_DATA, &setup); | |
} | |
#include "tst_default.moc" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Component { | |
id: markerComponent | |
Rectangle { | |
x: eventPosition.x - width / 2 | |
y: eventPosition.y - height / 2 | |
width: eventType !== "move" ? 20 : 10 | |
height: width | |
radius: width / 2 | |
opacity: 0.5 | |
color: eventType === "press" ? "mediumseagreen" : eventType === "move" ? "steelblue" : "tomato" | |
property string eventType | |
property point eventPosition | |
} | |
} | |
Connections { | |
// Give it an id so that it can be enabled/disabled by a test | |
id: mouseDebuggerConnections | |
target: mouseDebugger | |
onMarkerRequested: { | |
testCase.createTemporaryObject(markerComponent, container, | |
{ | |
eventPosition: pos, z: 1000, eventType: eventType | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment