Skip to content

Commit

Permalink
Merge pull request #5565 from vpereverzev/action_filter_events
Browse files Browse the repository at this point in the history
Action filter events
  • Loading branch information
dmitrio95 committed Jan 9, 2020
2 parents 3fc5790 + efced91 commit 1c323d7
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -156,7 +156,7 @@ set(TELEMETRY_TRACK_ID "" CACHE STRING "Telemetry track id")

if (MSCORE_UNSTABLE OR TELEMETRY_TRACK_ID STREQUAL "")
message("Telemetry feature is disabled")
message("Build is stable = ${MSCORE_UNSTABLE}")
message("Build is unstable = ${MSCORE_UNSTABLE}")

if (TELEMETRY_TRACK_ID STREQUAL "")
message("Telemetry track id is empty")
Expand Down
45 changes: 34 additions & 11 deletions mscore/musescore.cpp
Expand Up @@ -2266,21 +2266,34 @@ void MuseScore::aboutMusicXML()

void MuseScore::selectScore(QAction* action)
{
QString a = action->data().toString();
if (!a.isEmpty()) {
if (a == "clear-recent") {
_recentScores.clear();
if (startcenter)
startcenter->updateRecentScores();
QVariant actionData = action->data();

if (!actionData.isValid())
return;

switch (actionData.type()) {
case QVariant::String: {
if (actionData.toString() == "clear-recent") {
_recentScores.clear();

if (startcenter)
startcenter->updateRecentScores();
}
break;
}
else {
MasterScore* score = readScore(a);
case QVariant::Map: {
QVariantMap pathMap = actionData.toMap();

MasterScore* score = readScore(pathMap.value("filePath").toString());
if (score) {
setCurrentScoreView(appendScore(score));
addRecentScore(score);
writeSessionFile(false);
}
break;
}
default:
return;
}
}

Expand Down Expand Up @@ -2446,9 +2459,15 @@ void MuseScore::openRecentMenu()
bool one = false;
for (const QFileInfo& fi : recentScores()) {
QAction* action = openRecent->addAction(fi.fileName().replace("&", "&&")); // show filename only
QString dta(fi.canonicalFilePath());
action->setData(dta);
action->setToolTip(dta);

QString filePath = fi.canonicalFilePath();

QVariantMap actionData;
actionData.insert("actionName", "open-recent");
actionData.insert("filePath", filePath);

action->setData(actionData);
action->setToolTip(filePath);
one = true;
}
if (one) {
Expand Down Expand Up @@ -4338,6 +4357,8 @@ void MuseScore::changeState(ScoreState val)
a->setChecked(noteEntry);
_sstate = val;

emit scoreStateChanged(_sstate);

Element* e = cv && (_sstate & STATE_ALLTEXTUAL_EDIT || _sstate == STATE_EDIT) ? cv->getEditElement() : 0;
if (!e) {
textTools()->hide();
Expand Down Expand Up @@ -7757,6 +7778,8 @@ void MuseScore::init(QStringList& argv)

#ifndef TELEMETRY_DISABLED
QApplication::instance()->installEventFilter(ActionEventObserver::instance());
ActionEventObserver::instance()->setScoreState(mscore->state());
QObject::connect(mscore, &MuseScore::scoreStateChanged, ActionEventObserver::instance(), &ActionEventObserver::setScoreState);
#endif

mscore->setRevision(Ms::revision);
Expand Down
1 change: 1 addition & 0 deletions mscore/musescore.h
Expand Up @@ -471,6 +471,7 @@ class MuseScore : public QMainWindow, public MuseScoreCore {
void windowSplit(bool);
void musescoreWindowWasShown();
void workspacesChanged();
void scoreStateChanged(ScoreState state);

private slots:
void cmd(QAction* a, const QString& cmd);
Expand Down
5 changes: 3 additions & 2 deletions mscore/shortcut.cpp
Expand Up @@ -4378,16 +4378,17 @@ void Shortcut::resetToDefault()
// getShortcutByKeySequence
//---------------------------------------------------------

Shortcut* Shortcut::getShortcutByKeySequence(const QKeySequence& keySequence)
Shortcut* Shortcut::getShortcutByKeySequence(const QKeySequence& keySequence, const ScoreState state)
{
for (Shortcut* shortcut : _shortcuts.values()) {
if (!(shortcut->state() & state))
continue;

QAction* action = shortcut->action();

if (!action)
continue;


for (const QKeySequence& _keySequence : action->shortcuts()) {
if (_keySequence == keySequence)
return shortcut;
Expand Down
2 changes: 1 addition & 1 deletion mscore/shortcut.h
Expand Up @@ -176,7 +176,7 @@ class Shortcut {
static void resetToDefault();
static bool dirty;
static bool customSource() { return source != defaultFileName; }
static Shortcut* getShortcutByKeySequence(const QKeySequence &keySequence);
static Shortcut* getShortcutByKeySequence(const QKeySequence &keySequence, const ScoreState state);
static Shortcut* getShortcut(const char* key);
static const QHash<QByteArray, Shortcut*>& shortcuts() { return _shortcuts; }
static QActionGroup* getActionGroupForWidget(MsWidget w);
Expand Down
33 changes: 28 additions & 5 deletions telemetry/actioneventobserver.cpp
Expand Up @@ -44,15 +44,21 @@ QPair<QString, QString> ActionEventObserver::extractActionData(QObject* watched)
QPair<QString, QString> result;

QString actionCategory;
QString actionKey;
QString actionName;

if (qobject_cast<QMenu*>(watched)) {
QMenu* watchedMenu = qobject_cast<QMenu*>(watched);

QAction* activeAction = watchedMenu->activeAction();

if (activeAction) {
actionKey = activeAction->data().toString();
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");
}
}
Expand All @@ -62,13 +68,13 @@ QPair<QString, QString> ActionEventObserver::extractActionData(QObject* watched)
QAction* activeAction = watchedButton->defaultAction();

if (activeAction) {
actionKey = activeAction->data().toString();
actionName = activeAction->data().toString();
actionCategory = QStringLiteral("button clicked");
}
}

result.first = actionCategory;
result.second = actionKey;
result.second = actionName;

return result;
}
Expand All @@ -89,10 +95,27 @@ bool ActionEventObserver::eventFilter(QObject *watched, QEvent *event)
else if (event->type() == QEvent::Shortcut) {
QShortcutEvent* shortCutEvent = static_cast<QShortcutEvent*>(event);

Ms::Shortcut* shortcut = Ms::Shortcut::getShortcutByKeySequence(shortCutEvent->key());
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;
}
6 changes: 6 additions & 0 deletions telemetry/actioneventobserver.h
Expand Up @@ -24,6 +24,7 @@
#include <QAction>
#include <QPair>
#include "serviceinjector.h"
#include "globals.h"

#include "interfaces/itelemetryservice.h"

Expand All @@ -45,11 +46,16 @@ class ActionEventObserver : public QObject, public ServiceInjector<ITelemetrySer

bool eventFilter(QObject *watched, QEvent *event) override;

public slots:
void setScoreState(const Ms::ScoreState state);

private:
Q_DISABLE_COPY(ActionEventObserver)

explicit ActionEventObserver(QObject* parent = nullptr);
QPair<QString, QString> extractActionData(QObject *watched);

Ms::ScoreState m_scoreState;
};

#endif // MENUBAR_H

0 comments on commit 1c323d7

Please sign in to comment.