Skip to content

Instantly share code, notes, and snippets.

@wimleers
Created October 30, 2010 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wimleers/655122 to your computer and use it in GitHub Desktop.
Save wimleers/655122 to your computer and use it in GitHub Desktop.
Adds QEventLogger (commit: 31aae3b633c899ee7b91) to Sudoku (commit: cf6ed6c6f2bf76363549)
diff --git src/SudokuApp.cpp src/SudokuApp.cpp
index 3232b0f..18ae303 100644
--- src/SudokuApp.cpp
+++ src/SudokuApp.cpp
@@ -42,6 +42,10 @@ SudokuApp::SudokuApp(int & argc, char ** argv) : QApplication(argc, argv) {
m_mainWindow = new MainWindow();
m_mainWindow->show();
+
+ // Initialize & install the event logger.
+ m_eventLogger = new QEventLogger("./events");
+ this->installEventFilter(m_eventLogger);
}
diff --git src/SudokuApp.h src/SudokuApp.h
index a349da7..57aa29c 100644
--- src/SudokuApp.h
+++ src/SudokuApp.h
@@ -22,6 +22,7 @@
#include <QSettings>
#include <QDebug>
#include "Qt/MainWindow.h"
+#include "QEventLogger.h"
class SudokuApp : public QApplication
@@ -34,6 +35,7 @@ protected:
private:
MainWindow * m_mainWindow;
+ QEventLogger * m_eventLogger;
};
#endif // SUDOKUAPP_H
diff --git src/Sudoku_shared.pri src/Sudoku_shared.pri
index 9a29920..765c500 100644
--- src/Sudoku_shared.pri
+++ src/Sudoku_shared.pri
@@ -29,6 +29,7 @@ HEADERS += Board.h \
Sudoku.h \
BoardGenerator.h \
SudokuApp.h \
+ QEventLogger.h \
Qt/Dimensions.h \
Qt/MainWindow.h \
Qt/NewGameDialog.h \
@@ -45,6 +46,7 @@ SOURCES += Board.cpp \
Sudoku.cpp \
BoardGenerator.cpp \
SudokuApp.cpp \
+ QEventLogger.cpp \
Qt/MainWindow.cpp \
Qt/NewGameDialog.cpp \
Qt/PauseOverlay.cpp \
diff --git src/QEventLogger.cpp src/QEventLogger.cpp
new file mode 100644
index 0000000..828fc48
--- /dev/null
+++ src/QEventLogger.cpp
@@ -0,0 +1,122 @@
+#include "QEventLogger.h"
+
+QEventLogger::QEventLogger(const QString & logFileBaseName, QObject * parent) : QObject(parent) {
+ // Build log file name.
+ QDateTime now = QDateTime::currentDateTime();
+ QString fullLogFileName = logFileBaseName + ' ' + now.toString(Qt::ISODate) + ".csv";
+
+ // Open log file.
+ this->logFile = new QFile(fullLogFileName);
+ if (!this->logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
+ exit(1);
+ this->log = new QTextStream(this->logFile);
+
+ // Write header to log file.
+ *log << "; Date and time are: " << now.toString(Qt::ISODate) << '\n';
+ *log << "time,input type,event type,details\n";
+ log->flush();
+
+ // Start timer.
+ this->time = new QTime();
+ this->time->start();
+}
+
+bool QEventLogger::eventFilter(QObject * obj, QEvent * event) {
+ Q_UNUSED(obj);
+
+ static QMouseEvent * mouseEvent;
+ static QKeyEvent * keyEvent;
+ static QString eventType, details;
+ static int inputType, mouseButton, modifierKey;
+ inputType = NONE;
+
+ switch (event->type()) {
+ case QEvent::MouseMove:
+ inputType = MOUSE;
+ eventType = "MouseMove";
+ break;
+ case QEvent::MouseTrackingChange:
+ inputType = MOUSE;
+ eventType = "MouseTrackingChange";
+ break;
+ case QEvent::MouseButtonPress:
+ inputType = MOUSE;
+ eventType = "MouseButtonPress";
+ break;
+ case QEvent::MouseButtonRelease:
+ inputType = MOUSE;
+ eventType = "MouseButtonRelease";
+ break;
+ case QEvent::MouseButtonDblClick:
+ inputType = MOUSE;
+ eventType = "MouseButtonDblClick";
+ break;
+ case QEvent::KeyPress:
+ inputType = KEYBOARD;
+ eventType = "KeyPress";
+ case QEvent::KeyRelease:
+ inputType = KEYBOARD;
+ eventType = "KeyRelease";
+ break;
+ default:
+ break;
+ }
+
+ if (inputType == MOUSE) {
+ mouseEvent = static_cast<QMouseEvent *>(event);
+
+ // Collect the mouse buttons that are pressed.
+ mouseButton = mouseEvent->buttons();
+ QString buttonsPressed;
+ if (mouseButton & Qt::LeftButton)
+ buttonsPressed += 'L';
+ if (mouseButton & Qt::MidButton)
+ buttonsPressed += 'M';
+ if (mouseButton & Qt::RightButton)
+ buttonsPressed += 'R';
+
+ // Build the details string.
+ details = '"';
+ details += QString::number(mouseEvent->x()) + ',' + QString::number(mouseEvent->y());
+ details += ',' + buttonsPressed;
+ details += '"';
+
+ this->appendToLog("Mouse", eventType, details);
+ }
+ else if (inputType == KEYBOARD) {
+ keyEvent = static_cast<QKeyEvent *>(event);
+
+ // Collect the modifier keys that are pressed.
+ modifierKey = keyEvent->modifiers();
+ QString modifierKeysPressed;
+ if (modifierKey & Qt::ShiftModifier)
+ modifierKeysPressed += ":shift";
+ if (modifierKey & Qt::ControlModifier)
+ modifierKeysPressed += ":ctrl";
+ if (modifierKey & Qt::AltModifier)
+ modifierKeysPressed += ":alt";
+ if (modifierKey & Qt::MetaModifier)
+ modifierKeysPressed += ":meta";
+
+ // TODO: support special keys, such as ESC and arrow keys, when
+ // keyEvent->text() == "".
+
+ // Build the details string.
+ details = '"';
+ details += QString::number(keyEvent->key());
+ details += ',' + keyEvent->text();
+ details += ',' + modifierKeysPressed;
+ details += '"';
+
+ this->appendToLog("Keyboard", eventType, details);
+ }
+
+ // Always propagate the event further.
+ return false;
+}
+
+void QEventLogger::appendToLog(const QString & inputType, const QString & eventType, const QString & details) {
+ *(this->log) << this->time->elapsed() << ',' << inputType<< ',' << eventType << ',' << details << '\n';
+// qDebug() << this->time->elapsed() << inputType << eventType << details;
+ this->log->flush();
+}
diff --git src/QEventLogger.h src/QEventLogger.h
new file mode 100644
index 0000000..91999de
--- /dev/null
+++ src/QEventLogger.h
@@ -0,0 +1,33 @@
+#ifndef QEVENTLOGGER_H
+#define QEVENTLOGGER_H
+
+#include <QObject>
+#include <QMouseEvent>
+#include <QKeyEvent>
+#include <QDateTime>
+#include <QTime>
+#include <QFile>
+#include <QTextStream>
+#include <QDebug>
+
+#define NONE -1
+#define MOUSE 0
+#define KEYBOARD 1
+
+class QEventLogger : public QObject {
+ Q_OBJECT
+
+public:
+ explicit QEventLogger(const QString & logFileBaseName, QObject * parent = 0);
+
+protected:
+ bool eventFilter(QObject * obj, QEvent * event);
+ void appendToLog(const QString & inputType, const QString & eventType, const QString & details);
+
+private:
+ QFile * logFile;
+ QTextStream * log;
+ QTime * time;
+};
+
+#endif // QEVENTLOGGER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment