Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2025 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "widgetdialogadapter.h"
23 :
24 : #include <QGuiApplication>
25 : #include <QKeyEvent>
26 :
27 : #include "log.h"
28 :
29 : using namespace muse::interactive;
30 :
31 0 : WidgetDialogAdapter::WidgetDialogAdapter(QDialog* parent)
32 0 : : QObject(parent), m_dialog(parent)
33 : {
34 0 : IF_ASSERT_FAILED(m_dialog) {
35 0 : return;
36 : }
37 :
38 0 : m_dialog->installEventFilter(this);
39 :
40 0 : connect(m_dialog, &QDialog::accepted, this, [this](){
41 0 : if (m_onHideCallBack) {
42 0 : m_onHideCallBack();
43 : }
44 0 : });
45 :
46 0 : connect(m_dialog, &QDialog::rejected, this, [this](){
47 0 : if (m_onHideCallBack) {
48 0 : m_onHideCallBack();
49 : }
50 0 : });
51 0 : }
52 :
53 0 : bool WidgetDialogAdapter::eventFilter(QObject* watched, QEvent* event)
54 : {
55 0 : if (event->type() == QEvent::Show && m_onShownCallBack) {
56 0 : m_onShownCallBack();
57 : }
58 :
59 0 : if (event->type() == QEvent::Close && m_onHideCallBack) {
60 0 : m_onHideCallBack();
61 : }
62 :
63 0 : if (event->type() == QEvent::ShortcutOverride) {
64 0 : if (QKeyEvent* keyEvent = dynamic_cast<QKeyEvent*>(event)) {
65 0 : if (keyEvent->key() == Qt::Key_Escape && keyEvent->modifiers() == Qt::NoModifier) {
66 0 : m_dialog->close();
67 0 : return true;
68 : }
69 : }
70 : }
71 :
72 0 : return QObject::eventFilter(watched, event);
73 : }
74 :
75 0 : WidgetDialogAdapter& WidgetDialogAdapter::onShow(const std::function<void()>& func)
76 : {
77 0 : m_onShownCallBack = func;
78 0 : return *this;
79 : }
80 :
81 0 : WidgetDialogAdapter& WidgetDialogAdapter::onHide(const std::function<void()>& func)
82 : {
83 0 : m_onHideCallBack = func;
84 0 : return *this;
85 : }
|