-
Notifications
You must be signed in to change notification settings - Fork 17
/
user_events_analyzer.hpp
168 lines (149 loc) · 4.13 KB
/
user_events_analyzer.hpp
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
#pragma once
#include <list>
#include <map>
#include <set>
#include <QKeySequence>
#include <QPoint>
#include <QtCore/QDateTime>
#include <QtCore/QEvent>
#include <QtCore/QObject>
#include "custom_event_analyzer.hpp"
class QTreeWidget;
class QTreeWidgetItem;
class QKeyEvent;
class QMouseEvent;
class QTreeView;
class QModelIndex;
class QAbstractItemModel;
class QAction;
namespace qt_monkey_agent
{
class Agent;
//@{
//! helper functions to implement custom event analyzers
//! Get full widget id, in form parent of parent id.parent id.widget id
QString fullQtWidgetId(const QObject &w);
//! convert mouse button constant <-> string
QString mouseButtonEnumToString(Qt::MouseButton b);
bool stringToMouseButton(const QString &str, Qt::MouseButton &bt);
void escapeTextForScript(QString &text);
//@}
/**
* Analyzer user event and genearte based of them javascript code
*/
class UserEventsAnalyzer
#ifndef Q_MOC_RUN
final
#endif
: public QObject
{
Q_OBJECT
signals:
void userEventInScriptForm(const QString &);
void scriptLog(const QString &);
public:
UserEventsAnalyzer(Agent &agent, const QKeySequence &showObjectShortCut,
std::list<CustomEventAnalyzer> customEventAnalyzers,
QObject *parent = nullptr);
private:
Agent &agent_;
struct {
QEvent::Type type = QEvent::None;
QDateTime timestamp;
int key = -1;
} lastKeyEvent_;
struct {
QEvent::Type type = QEvent::None;
QDateTime timestamp;
QPoint globalPos;
Qt::MouseButtons buttons;
QString widgetName;
} lastMouseEvent_;
size_t keyPress_ = 0;
size_t keyRelease_ = 0;
std::list<CustomEventAnalyzer> customEventAnalyzers_;
const GenerateCommand generateScriptCmd_;
const QKeySequence showObjectShortCut_;
bool eventFilter(QObject *obj, QEvent *event) override;
QString callCustomEventAnalyzers(QObject *obj, QEvent *event,
QWidget *widget,
const QString &widgetName) const;
bool alreadySawSuchKeyEvent(QKeyEvent *keyEvent);
bool alreadySawSuchMouseEvent(const QString &widgetName,
QMouseEvent *mouseEvent);
};
namespace Private
{
//@{
//! helper classes to catch signals of widgets
//! \todo remove when we drop support of Qt 4.x
class TreeWidgetWatcher
#ifndef Q_MOC_RUN
final
#endif
: public QObject
{
Q_OBJECT
public:
TreeWidgetWatcher(const GenerateCommand &generateScriptCmd,
QObject *parent = nullptr)
: QObject(parent), generateScriptCmd_(generateScriptCmd)
{
}
//! \return false if already watched
bool watch(QTreeWidget *tw);
void disconnectAll();
private slots:
void itemExpanded(QTreeWidgetItem *);
void treeWidgetDestroyed(QObject *);
private:
const GenerateCommand &generateScriptCmd_;
std::set<QObject *> treeWidgetsSet_;
};
class TreeViewWatcher
#ifndef Q_MOC_RUN
final
#endif
: public QObject
{
Q_OBJECT
public:
TreeViewWatcher(const GenerateCommand &generateScriptCmd,
QObject *parent = nullptr)
: QObject(parent), generateScriptCmd_(generateScriptCmd)
{
}
void watch(QTreeView &tw);
void disconnectAll();
private slots:
void treeViewExpanded(const QModelIndex &index);
void treeViewDestroyed(QObject *obj);
private:
const GenerateCommand &generateScriptCmd_;
std::set<const QObject *> treeViewSet_;
std::map<const QAbstractItemModel *, const QObject *> modelToView_;
};
class MacMenuActionWatcher
#ifndef Q_MOC_RUN
final
#endif
: public QObject
{
Q_OBJECT
public:
MacMenuActionWatcher(const GenerateCommand &generateScriptCmd,
QObject *parent = nullptr)
: QObject(parent), generateScriptCmd_(generateScriptCmd)
{
}
void startWatch(Agent &agent, QAction &act, QString menuName);
void stopWatch(Agent &agent, QAction &act);
private slots:
void onTriggered();
private:
const GenerateCommand &generateScriptCmd_;
std::map<QAction *, QString> actions_;
};
//@}
} // namespace Private
} // namespace qt_monkey_agent