/
actioneventobserver.cpp
121 lines (94 loc) · 4.08 KB
/
actioneventobserver.cpp
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
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2019 MuseScore BVBA and others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================
#include "actioneventobserver.h"
#include "google_analytics/ganalytics.h"
#include <QEvent>
#include <QShortcutEvent>
#include <QMenu>
#include <QToolButton>
#include "shortcut.h"
//---------------------------------------------------------
// ActionEventObserver
//---------------------------------------------------------
ActionEventObserver::ActionEventObserver(QObject *parent) : QObject(parent)
{
}
//---------------------------------------------------------
// extractActionData
//---------------------------------------------------------
QPair<QString, QString> ActionEventObserver::extractActionData(QObject* watched)
{
QPair<QString, QString> result;
QString actionCategory;
QString actionName;
if (qobject_cast<QMenu*>(watched)) {
QMenu* watchedMenu = qobject_cast<QMenu*>(watched);
QAction* activeAction = watchedMenu->activeAction();
if (activeAction) {
if (activeAction->data().type() == QVariant::String)
actionName = activeAction->data().toString();
else if (activeAction->data().type() == QVariant::Map) {
QVariantMap actionDataMap = activeAction->data().toMap();
actionName = actionDataMap.value("actionName").toString();
}
actionCategory = QStringLiteral("menu item click");
}
}
else if (qobject_cast<QToolButton*>(watched)) {
QToolButton* watchedButton = qobject_cast<QToolButton*>(watched);
QAction* activeAction = watchedButton->defaultAction();
if (activeAction) {
actionName = activeAction->data().toString();
actionCategory = QStringLiteral("button clicked");
}
}
result.first = actionCategory;
result.second = actionName;
return result;
}
//---------------------------------------------------------
// eventFilter
//---------------------------------------------------------
bool ActionEventObserver::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::MouseButtonRelease) {
QPair<QString, QString> actionData = extractActionData(watched);
telemetryService()->sendEvent(actionData.first, actionData.second);
}
else if (event->type() == QEvent::Shortcut) {
QShortcutEvent* shortCutEvent = static_cast<QShortcutEvent*>(event);
Ms::Shortcut* shortcut = Ms::Shortcut::getShortcutByKeySequence(shortCutEvent->key(), m_scoreState);
if (!shortcut)
return false;
telemetryService()->sendEvent("shortcut", shortcut->key());
}
return false;
}
///---------------------------------------------------------
/// @name setScoreState
///
/// @brief Geting the current state of a score through the connection.
/// Because single shortcut can be used in different actions regarding to the current score state.
///
/// @see ActionEventObserver::eventFilter
///---------------------------------------------------------
void ActionEventObserver::setScoreState(const Ms::ScoreState state)
{
m_scoreState = state;
}