1 //=============================================================================
2 //  MuseScore
3 //  Music Composition & Notation
4 //
5 //  Copyright (C) 2019 Werner Schweer and others
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License version 2.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19 
20 #ifndef __QMLDOCKWIDGET_H__
21 #define __QMLDOCKWIDGET_H__
22 
23 #include "libmscore/mscore.h"
24 
25 namespace Ms {
26 
27 #ifndef NDEBUG
28 extern bool useSourceQmlFiles;
29 #endif
30 
31 //---------------------------------------------------------
32 //   FocusChainBreak
33 //---------------------------------------------------------
34 
35 class FocusChainBreak : public QQuickItem
36       {
37       Q_OBJECT
38 
39    signals:
40       void requestFocusTransfer(bool forward);
41 
42    public:
43       FocusChainBreak(QQuickItem* parent = nullptr);
44 
45       void focusInEvent(QFocusEvent*) override;
46       };
47 
48 //---------------------------------------------------------
49 //   MsQuickView
50 //---------------------------------------------------------
51 
52 class MsQuickView : public QQuickView
53       {
54       Q_OBJECT
55 
56       QWidget* prevFocusWidget = nullptr;
57 
58       static void registerQmlTypes();
59       void init();
60 
61    private slots:
62       void transferFocus(bool forward);
63       void onStatusChanged(QQuickView::Status);
64 
65    public:
66       MsQuickView(const QUrl& source, QWindow* parent = nullptr);
MsQuickView(QQmlEngine * engine,QWindow * parent)67       MsQuickView(QQmlEngine* engine, QWindow* parent)
68          : QQuickView(engine, parent) { init(); }
69       MsQuickView(QWindow* parent = nullptr)
QQuickView(parent)70          : QQuickView(parent) { init(); }
71 
72       void focusInEvent(QFocusEvent*) override;
73       void keyPressEvent(QKeyEvent* e) override;
74       };
75 
76 //---------------------------------------------------------
77 //   QmlStyle
78 ///   Implements setting colors and fonts for QML-based
79 ///   widgets styling. Color palette is not available in
80 ///   Qt Quick before Qt 5.10.
81 //---------------------------------------------------------
82 
83 class QmlStyle : public QObject
84       {
85       Q_OBJECT
86 
87       QPalette _palette;
88       QFont _font;
89       bool _shadowOverlay = false;
90 
91 #define COLOR_PROPERTY(name, role) \
92       Q_PROPERTY(QColor name READ get_##name CONSTANT) \
93       QColor get_##name() const { return _palette.color(role); }
94 
95       COLOR_PROPERTY(window, QPalette::Window)
96       COLOR_PROPERTY(windowText, QPalette::WindowText)
97       COLOR_PROPERTY(base, QPalette::Base)
98       COLOR_PROPERTY(alternateBase, QPalette::AlternateBase)
99       COLOR_PROPERTY(text, QPalette::Text)
100       COLOR_PROPERTY(button, QPalette::Button)
101       COLOR_PROPERTY(buttonText, QPalette::ButtonText)
102       COLOR_PROPERTY(brightText, QPalette::BrightText)
103       COLOR_PROPERTY(toolTipBase, QPalette::ToolTipBase)
104       COLOR_PROPERTY(toolTipText, QPalette::ToolTipText)
105       COLOR_PROPERTY(link, QPalette::Link)
106       COLOR_PROPERTY(linkVisited, QPalette::LinkVisited)
107       COLOR_PROPERTY(highlight, QPalette::Highlight)
108       COLOR_PROPERTY(highlightedText, QPalette::HighlightedText)
109 
110       COLOR_PROPERTY(shadow, QPalette::Shadow)
111 
112 #undef COLOR_PROPERTY
113 
114 #define COLOR_PROPERTY_EXPR(name, expr) \
115       Q_PROPERTY(QColor name READ get_##name CONSTANT) \
116       QColor get_##name() const { return expr; }
117 
118       COLOR_PROPERTY_EXPR(voice1Color, MScore::selectColor[0]);
119       COLOR_PROPERTY_EXPR(voice2Color, MScore::selectColor[1]);
120       COLOR_PROPERTY_EXPR(voice3Color, MScore::selectColor[2]);
121       COLOR_PROPERTY_EXPR(voice4Color, MScore::selectColor[3]);
122 
123 #undef COLOR_PROPERTY_EXPR
124 
125       Q_PROPERTY(QFont font READ font CONSTANT)
Q_PROPERTY(bool shadowOverlay READ shadowOverlay NOTIFY shadowOverlayChanged)126       Q_PROPERTY(bool shadowOverlay READ shadowOverlay NOTIFY shadowOverlayChanged)
127 
128       QFont font() const { return _font; }
129 
130    signals:
131       void shadowOverlayChanged();
132 
133    public:
134       QmlStyle(QPalette, QObject* parent = nullptr);
135 
shadowOverlay()136       bool shadowOverlay() const { return _shadowOverlay; }
137       void setShadowOverlay(bool);
138       };
139 
140 //---------------------------------------------------------
141 //   QmlDockWidget
142 //---------------------------------------------------------
143 
144 class QmlDockWidget : public QDockWidget
145       {
146       Q_OBJECT
147 
148       QQuickView* _view = nullptr;
149       QmlStyle* qmlStyle = nullptr;
150       QQmlEngine* engine;
151 
152       QQuickView* getView();
153       void setupStyle();
154 
155    protected:
initialViewSize()156       QSize initialViewSize() const { return _view ? _view->initialSize() : QSize(); }
157 
158    public:
159       QmlDockWidget(QQmlEngine* e = nullptr, QWidget* parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
160       QmlDockWidget(QQmlEngine* e, const QString& title, QWidget* parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
161 
162       static QString qmlSourcePrefix();
163       void setSource(const QUrl& url);
164       QUrl source() const;
165 
rootContext()166       QQmlContext* rootContext() { return getView()->rootContext(); }
rootObject()167       QQuickItem* rootObject() { return getView()->rootObject(); }
168 
view()169       const QQuickView* view() const { return _view; }
170 
171       void changeEvent(QEvent* evt) override;
172       void resizeEvent(QResizeEvent* evt) override;
173 
174       void ensureQmlViewFocused();
175 
setShadowOverlay(bool val)176       void setShadowOverlay(bool val) { qmlStyle->setShadowOverlay(val); }
177       };
178 
179 }
180 #endif
181 
182