File indexing completed on 2024-12-22 04:12:44

0001 /*
0002  *  SPDX-FileCopyrightText: 2014 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_tablet_debugger.h"
0008 
0009 #include <QEvent>
0010 #include <QMessageBox>
0011 #include <QApplication>
0012 
0013 #include <kis_debug.h>
0014 #include <kis_config.h>
0015 
0016 #include <QGlobalStatic>
0017 
0018 #include <klocalizedstring.h>
0019 
0020 Q_GLOBAL_STATIC(KisTabletDebugger, s_instance)
0021 
0022 
0023 inline QString button(const QWheelEvent &ev) {
0024     Q_UNUSED(ev);
0025     return "-";
0026 }
0027 
0028 template <class T>
0029 QString button(const T &ev) {
0030     return QString::number(ev.button());
0031 }
0032 
0033 template <class T>
0034 QString buttons(const T &ev) {
0035     return QString::number(ev.buttons());
0036 }
0037 
0038 template <class Event>
0039     void dumpBaseParams(QTextStream &s, const Event &ev, const QString &prefix)
0040 {
0041     s << qSetFieldWidth(5)  << left << prefix << reset << " ";
0042     s << qSetFieldWidth(17) << left << KisTabletDebugger::exTypeToString(ev.type()) << reset;
0043 }
0044 
0045 template <class Event>
0046     void dumpMouseRelatedParams(QTextStream &s, const Event &ev)
0047 {
0048     s << "btn: " << button(ev) << " ";
0049     s << "btns: " << buttons(ev) << " ";
0050     s << "pos: " << qSetFieldWidth(4) << ev.x() << qSetFieldWidth(0) << "," << qSetFieldWidth(4) << ev.y() << qSetFieldWidth(0) << " ";
0051     s << "gpos: "  << qSetFieldWidth(4) << ev.globalX() << qSetFieldWidth(0) << "," << qSetFieldWidth(4) << ev.globalY() << qSetFieldWidth(0) << " ";
0052 }
0053 
0054 QString KisTabletDebugger::exTypeToString(QEvent::Type type) {
0055     return
0056         type == QEvent::TabletEnterProximity ? "TabletEnterProximity" :
0057         type == QEvent::TabletLeaveProximity ? "TabletLeaveProximity" :
0058         type == QEvent::Enter ? "Enter" :
0059         type == QEvent::Leave ? "Leave" :
0060         type == QEvent::FocusIn ? "FocusIn" :
0061         type == QEvent::FocusOut ? "FocusOut" :
0062         type == QEvent::Wheel ? "Wheel" :
0063         type == QEvent::KeyPress ? "KeyPress" :
0064         type == QEvent::KeyRelease ? "KeyRelease" :
0065         type == QEvent::ShortcutOverride ? "ShortcutOverride" :
0066         type == QMouseEvent::MouseButtonPress ? "MouseButtonPress" :
0067         type == QMouseEvent::MouseButtonRelease ? "MouseButtonRelease" :
0068         type == QMouseEvent::MouseButtonDblClick ? "MouseButtonDblClick" :
0069         type == QMouseEvent::MouseMove ? "MouseMove" :
0070         type == QTabletEvent::TabletMove ? "TabletMove" :
0071         type == QTabletEvent::TabletPress ? "TabletPress" :
0072         type == QTabletEvent::TabletRelease ? "TabletRelease" :
0073         type == QTouchEvent::TouchBegin ? "TouchBegin" :
0074         type == QTouchEvent::TouchUpdate ? "TouchUpdate" :
0075         type == QTouchEvent::TouchEnd ? "TouchEnd" :
0076         type == QTouchEvent::TouchCancel ? "TouchCancel" :
0077         "unknown";
0078 }
0079 
0080 
0081 KisTabletDebugger::KisTabletDebugger()
0082     : m_debugEnabled(false)
0083 {
0084     KisConfig cfg(true);
0085     m_shouldEatDriverShortcuts = cfg.shouldEatDriverShortcuts();
0086 }
0087 
0088 KisTabletDebugger* KisTabletDebugger::instance()
0089 {
0090     return s_instance;
0091 }
0092 
0093 void KisTabletDebugger::toggleDebugging()
0094 {
0095     m_debugEnabled = !m_debugEnabled;
0096     QMessageBox::information(qApp->activeWindow(), i18nc("@title:window", "Krita"), m_debugEnabled ?
0097                              i18n("Tablet Event Logging Enabled") :
0098                              i18n("Tablet Event Logging Disabled"));
0099     if (m_debugEnabled) {
0100         dbgTablet << "vvvvvvvvvvvvvvvvvvvvvvv START TABLET EVENT LOG vvvvvvvvvvvvvvvvvvvvvvv";
0101     }
0102     else {
0103         dbgTablet << "^^^^^^^^^^^^^^^^^^^^^^^ END TABLET EVENT LOG ^^^^^^^^^^^^^^^^^^^^^^^";
0104     }
0105 }
0106 
0107 bool KisTabletDebugger::debugEnabled() const
0108 {
0109     return m_debugEnabled;
0110 }
0111 
0112 bool KisTabletDebugger::initializationDebugEnabled() const
0113 {
0114     // FIXME: make configurable!
0115     return true;
0116 }
0117 
0118 bool KisTabletDebugger::debugRawTabletValues() const
0119 {
0120     // FIXME: make configurable!
0121     return m_debugEnabled;
0122 }
0123 
0124 bool KisTabletDebugger::shouldEatDriverShortcuts() const
0125 {
0126     return m_shouldEatDriverShortcuts;
0127 }
0128 
0129 QString KisTabletDebugger::eventToString(const QMouseEvent &ev, const QString &prefix)
0130 {
0131     QString string;
0132     QTextStream s(&string);
0133 
0134     dumpBaseParams(s, ev, prefix);
0135     dumpMouseRelatedParams(s, ev);
0136     s << "hires: " << qSetFieldWidth(8) << ev.screenPos().x() << qSetFieldWidth(0) << "," << qSetFieldWidth(8) << ev.screenPos().y() << qSetFieldWidth(0) << " ";
0137     s << "Source:" << ev.source();
0138 
0139     return string;
0140 }
0141 
0142 QString KisTabletDebugger::eventToString(const QKeyEvent &ev, const QString &prefix)
0143 {
0144     QString string;
0145     QTextStream s(&string);
0146     s.setCodec("UTF-8");
0147 
0148     dumpBaseParams(s, ev, prefix);
0149 
0150     s << "key: 0x" << hex << ev.key() << reset << " ";
0151     s << "mod: 0x" << hex << ev.modifiers() << reset << " ";
0152     s << "text: " << (ev.text().isEmpty() ? "none" : ev.text()) << " ";
0153     s << "autorepeat: " << bool(ev.isAutoRepeat());
0154 
0155     return string;
0156 }
0157 
0158 QString KisTabletDebugger::eventToString(const QWheelEvent &ev, const QString &prefix)
0159 {
0160     QString string;
0161     QTextStream s(&string);
0162     s.setCodec("UTF-8");
0163 
0164     dumpBaseParams(s, ev, prefix);
0165     dumpMouseRelatedParams(s, ev);
0166 
0167     s << "delta: " << ev.delta() << " ";
0168     s << "orientation: " << (ev.orientation() == Qt::Horizontal ? "H" : "V") << " ";
0169 
0170     return string;
0171 }
0172 
0173 QString KisTabletDebugger::eventToString(const QTouchEvent &ev, const QString &prefix)
0174 {
0175     QString string;
0176     QTextStream s(&string);
0177     s.setCodec("UTF-8");
0178 
0179     dumpBaseParams(s, ev, prefix);
0180 
0181     s << (ev.device()->type() ? "TouchPad" : "TouchScreen") << " ";
0182     for (const auto& touchpoint: ev.touchPoints()) {
0183         s << "id: " << touchpoint.id() << " ";
0184         s << "hires: " << qSetFieldWidth(8) << touchpoint.screenPos().x() << qSetFieldWidth(0) << "," << qSetFieldWidth(8) << touchpoint.screenPos().y() << qSetFieldWidth(0) << " ";
0185         s << "prs: " << touchpoint.pressure() << " ";
0186         s << "rot: "<< touchpoint.rotation() << " ";
0187         s << "state: 0x" << hex << touchpoint.state() << "; ";
0188         s << dec;
0189     }
0190 
0191     return string;
0192 }
0193 
0194 QString KisTabletDebugger::eventToString(const QEvent &ev, const QString &prefix)
0195 {
0196     QString string;
0197     QTextStream s(&string);
0198     s.setCodec("UTF-8");
0199 
0200     dumpBaseParams(s, ev, prefix);
0201 
0202     return string;
0203 }
0204 
0205 template <class Event>
0206     QString tabletEventToString(const Event &ev, const QString &prefix)
0207 {
0208     QString string;
0209     QTextStream s(&string);
0210     s.setCodec("UTF-8");
0211 
0212     dumpBaseParams(s, ev, prefix);
0213     dumpMouseRelatedParams(s, ev);
0214 
0215     s << "hires: " << qSetFieldWidth(8) << ev.hiResGlobalX() << qSetFieldWidth(0) << "," << qSetFieldWidth(8) << ev.hiResGlobalY() << qSetFieldWidth(0) << " ";
0216     s << "prs: " << qSetFieldWidth(4) << fixed << ev.pressure() << reset << " ";
0217 
0218     s << KisTabletDebugger::tabletDeviceToString((QTabletEvent::TabletDevice) ev.device()) << " ";
0219     s << KisTabletDebugger::pointerTypeToString((QTabletEvent::PointerType) ev.pointerType()) << " ";
0220     s << "id: " << ev.uniqueId() << " ";
0221 
0222     s << "xTilt: " << ev.xTilt() << " ";
0223     s << "yTilt: " << ev.yTilt() << " ";
0224     s << "rot: " << ev.rotation() << " ";
0225     s << "z: " << ev.z() << " ";
0226     s << "tp: " << ev.tangentialPressure() << " ";
0227 
0228     return string;
0229 }
0230 
0231 QString KisTabletDebugger::eventToString(const QTabletEvent &ev, const QString &prefix)
0232 {
0233     return tabletEventToString(ev, prefix);
0234 }
0235 
0236 QString KisTabletDebugger::tabletDeviceToString(QTabletEvent::TabletDevice device)
0237 {
0238     return
0239         device == QTabletEvent::NoDevice ? "NoDevice" :
0240         device == QTabletEvent::Puck ? "Puck" :
0241         device == QTabletEvent::Stylus ? "Stylus" :
0242         device == QTabletEvent::Airbrush ? "Airbrush" :
0243         device == QTabletEvent::FourDMouse ? "FourDMouse" :
0244         device == QTabletEvent::XFreeEraser ? "XFreeEraser" :
0245         device == QTabletEvent::RotationStylus ? "RotationStylus" :
0246         "unknown";
0247 }
0248 
0249 QString KisTabletDebugger::pointerTypeToString(QTabletEvent::PointerType pointer) {
0250     return
0251         pointer == QTabletEvent::UnknownPointer ? "UnknownPointer" :
0252         pointer == QTabletEvent::Pen ? "Pen" :
0253         pointer == QTabletEvent::Cursor ? "Cursor" :
0254         pointer == QTabletEvent::Eraser ? "Eraser" :
0255         "unknown";
0256 }
0257