1 #include "enableplayforwidget.h"
2 #include "musescore.h"
3
4 namespace Ms {
5
6
EnablePlayForWidget(QWidget * target)7 EnablePlayForWidget::EnablePlayForWidget(QWidget* target)
8 {
9 QAction* playAction = getAction("play");
10 _target = target;
11 _localPlayAction = new QAction(_target);
12 _localPlayAction->setData(playAction->data());
13 _localPlayAction->setCheckable(playAction->isCheckable());
14 _localPlayAction->setChecked(playAction->isChecked());
15 _localPlayAction->setShortcuts(playAction->shortcuts());
16 _localPlayAction->setShortcutContext(Qt::WidgetShortcut);
17 _target->addAction(_localPlayAction);
18 qApp->installEventFilter(_target);
19 QObject::connect(_localPlayAction, SIGNAL(triggered()), mscore->playButton(), SLOT(click()));
20 }
21
showEvent(QShowEvent *)22 void EnablePlayForWidget::showEvent(QShowEvent*)
23 {
24 _target->setFocus();
25 }
26
27 //--------------------------------------------------------------------------------------------------
28 // eventFilter
29 //
30 // If any child of the target widget has focus when Escape Key is pressed it loses the focus
31 // and the target gains focus
32 // Also, it makes sure that the global play action and _localPlayAction always have the same shortcut
33 //--------------------------------------------------------------------------------------------------
eventFilter(QObject * obj,QEvent * e)34 bool EnablePlayForWidget::eventFilter(QObject* obj, QEvent* e)
35 {
36 if (obj == getAction("play")) {
37 _localPlayAction->setShortcuts(getAction("play")->shortcuts());
38 }
39
40 if (obj->isWidgetType() &&
41 e->type() == QEvent::KeyPress &&
42 obj != _target && // if obj == target, it means that the target has the focus. this case is handled by target::keyPressEvent
43 _target->isAncestorOf(static_cast<QWidget*>(obj))) {
44 QKeyEvent* ev = static_cast<QKeyEvent*>(e);
45 if (ev->key() == Qt::Key_Escape && ev->modifiers() == Qt::NoModifier) {
46 _target->setFocus();
47 return true;
48 }
49 }
50 return false;
51 }
52
53 //-----------------------------------------------------------------------
54 // connectLocalPlayToDifferentSlot
55 //
56 // Note: the _localPlayAction will always be connected to only one slot
57 // in order not to trigger the global play action twice
58 // By default it's set to click the play button from the main window
59 //-----------------------------------------------------------------------
60
connectLocalPlayToDifferentSlot(QObject * obj,const char * id)61 void EnablePlayForWidget::connectLocalPlayToDifferentSlot(QObject *obj, const char *id)
62 {
63 _localPlayAction->disconnect();
64 QObject::connect(_localPlayAction, SIGNAL(triggered()), obj, id);
65 }
66 }
67