-
Notifications
You must be signed in to change notification settings - Fork 7
/
Fretboard.qml
178 lines (143 loc) · 5.53 KB
/
Fretboard.qml
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import QtQuick 2.9
import MuseScore 3.0
MuseScore {
id: plugin
version: "0.6.0"
description: "Simple guitar fretboard to visualise and edit tablature scores"
requiresScore: compatibility.useApi4
implicitWidth: fretLoader.item ? fretLoader.item.implicitWidth : 0
implicitHeight: fretLoader.item ? fretLoader.item.implicitHeight : 0
// Special comments for MuseScore 4.4 plugin metadata handling.
// For some reason these comments don't work if put after the QtObject below.
// https://github.com/musescore/MuseScore/wiki/Updating-plugins-for-MuseScore-Studio-4.4
//4.4 title: "Fretboard"
//4.4 categoryCode: "note-input"
QtObject {
id: compatibility
readonly property bool useApi4: mscoreVersion >= 40000
readonly property bool useApi44: mscoreVersion >= 40400
}
Component.onCompleted: {
if (compatibility.useApi4) {
plugin.title = "Fretboard";
plugin.categoryCode = "note-input";
} else {
plugin.menuPath = "Plugins.Fretboard";
plugin.pluginType = "dock";
plugin.dockArea = "bottom";
}
}
readonly property string pluginPanelObjectName: "FretPluginPanel"
readonly property var fretboard: pluginDockPanel ? pluginDockPanel.fretboard : fretLoader.item
property var pluginDockPanel: null
Loader {
id: fretLoader
source: compatibility.useApi4 ? "" : "FretboardComponent.qml"
onLoaded: item.mscore = plugin
}
function findChild(item, objectName) {
var children = item.children;
for (var i = 0; i < children.length; ++i) {
if (children[i].objectName == objectName) {
return children[i];
}
var obj = findChild(children[i], objectName);
if (obj) {
return obj;
}
}
return null;
}
function addPluginToDockMU4() {
var root = ui.rootItem;
var windowContent = findChild(root, "WindowContent");
var notationPage = null;
if (windowContent) {
for (var i = 0; i < windowContent.pages.length; ++i) {
var page = windowContent.pages[i];
if (page.objectName == "Notation") {
notationPage = page;
break;
}
}
}
if (notationPage) {
pluginDockPanel = null;
var pianoKeyboard = null;
var pianoKeyboardObjectName = notationPage.pageModel.pianoKeyboardPanelName();
for (var i = 0; i < notationPage.panels.length; ++i) {
var panel = notationPage.panels[i];
if (panel.objectName == pluginPanelObjectName) {
pluginDockPanel = panel;
} else if (panel.objectName == pianoKeyboardObjectName) {
pianoKeyboard = panel;
}
}
if (!pluginDockPanel) {
var pluginDockComponent = Qt.createComponent(compatibility.useApi44 ? "MU44FretboardDockPanel.qml" : "MU40FretboardDockPanel.qml");
pluginDockPanel = pluginDockComponent.createObject(notationPage, {root: notationPage, mscore: plugin});
notationPage.panels.push(pluginDockPanel);
}
var alreadyVisible = pluginDockPanel.visible;
if (pianoKeyboard) {
var wasPianoKeyboardHidden = !pianoKeyboard.visible;
// Need to show a dock widget at the bottom. Otherwise our dock widget would start with a floating state.
if (wasPianoKeyboardHidden) {
cmd("toggle-piano-keyboard");
}
var pluginDockPanelObjectName = pluginDockPanel.objectName;
pianoKeyboard.objectName = "tmp";
pluginDockPanel.objectName = pianoKeyboardObjectName;
if (alreadyVisible) {
// Toggle twice to bring the plugin to the top.
cmd("toggle-piano-keyboard");
}
cmd("toggle-piano-keyboard");
pluginDockPanel.objectName = pluginDockPanelObjectName;
pianoKeyboard.objectName = pianoKeyboardObjectName;
if (wasPianoKeyboardHidden) {
cmd("toggle-piano-keyboard");
}
fretboard.onRun();
}
}
}
// MuseScore 4 doesn't yet emit the scoreStateChanged signal,
// so update state periodically in a certain interval instead.
Timer {
id: updateTimer
interval: 33 // ms
repeat: true
onTriggered: fretboard.updateState(true)
function updateState() {
if (fretboard.mscore == plugin && fretboard.visible && Qt.application.state == Qt.ApplicationActive) {
start();
} else {
stop();
}
}
}
Connections {
target: fretboard
enabled: compatibility.useApi4
onVisibleChanged: updateTimer.updateState()
}
Connections {
target: Qt.application
enabled: compatibility.useApi4
onStateChanged: updateTimer.updateState()
}
onScoreStateChanged: {
if (compatibility.useApi4) {
// MuseScore 4 doesn't yet provide this signal, no handling for now.
} else {
fretboard.updateState(state.selectionChanged);
}
}
onRun: {
if (compatibility.useApi4) {
addPluginToDockMU4();
}
fretboard.onRun();
}
}