| 1 |
import QtQuick 2.0 |
| 2 |
|
| 3 |
Rectangle { |
| 4 |
id: root |
| 5 |
|
| 6 |
property int numOctaves: 2 |
| 7 |
property bool latch: false |
| 8 |
|
| 9 |
signal turnon(real notenum); |
| 10 |
signal turnoff(real notenum); |
| 11 |
signal genNote(variant on, variant note); |
| 12 |
property bool mouseHeld: false |
| 13 |
|
| 14 |
function indextomidi(notenum) { |
| 15 |
var octave = Math.floor(notenum/7) |
| 16 |
var note = (notenum%7)*2 |
| 17 |
if (note > 4) { note--} |
| 18 |
return octave*12 + note |
| 19 |
} |
| 20 |
|
| 21 |
onTurnon: { |
| 22 |
var midinum = indextomidi(notenum) |
| 23 |
genNote(1, midinum) |
| 24 |
//console.log("TURNON: ", notenum); |
| 25 |
} |
| 26 |
onTurnoff: { |
| 27 |
var midinum = indextomidi(notenum) |
| 28 |
genNote(0, midinum) |
| 29 |
} |
| 30 |
|
| 31 |
// Set keyboard mapping to play from computer Keyboard |
| 32 |
property var whiteKeys : [Qt.Key_Z, Qt.Key_X,Qt.Key_C,Qt.Key_V,Qt.Key_B,Qt.Key_N,Qt.Key_M, // first octava |
| 33 |
Qt.Key_Q,Qt.Key_W,Qt.Key_E,Qt.Key_R,Qt.Key_T,Qt.Key_Y,Qt.Key_U, Qt.Key_I,Qt.Key_O,Qt.Key_P]; // second and third until E |
| 34 |
property var blackKeys : [ Qt.Key_S,Qt.Key_D,'none',Qt.Key_G,Qt.Key_H,Qt.Key_J,'none', //first |
| 35 |
Qt.Key_2,Qt.Key_3,'none',Qt.Key_5,Qt.Key_6,Qt.Key_7,'none',Qt.Key_9,Qt.Key_0 ]; // sedond and third cis, dis |
| 36 |
|
| 37 |
function handleKeyEvent(pressed, key) { |
| 38 |
var whiteIndex = whiteKeys.indexOf(key); // -1 if not found; |
| 39 |
var blackIndex = blackKeys.indexOf(key); |
| 40 |
if (whiteIndex>=0) { |
| 41 |
keyDrawer.itemAt(whiteIndex).checked = pressed; |
| 42 |
} |
| 43 |
if (blackIndex>=0) { |
| 44 |
blackkeyDrawer.itemAt(blackIndex).checked = pressed; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
focus:true |
| 49 |
Keys.onPressed: { |
| 50 |
if (!event.isAutoRepeat) { |
| 51 |
//console.log("KEYBOARD key down: ", event.key); |
| 52 |
handleKeyEvent(true, event.key); |
| 53 |
} |
| 54 |
} |
| 55 |
Keys.onReleased: { |
| 56 |
if (!event.isAutoRepeat) { |
| 57 |
//console.log("KEYBOARD key up: ", event.key); |
| 58 |
handleKeyEvent(false, event.key); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
Row { |
| 63 |
id: keyRow |
| 64 |
anchors.fill: parent |
| 65 |
property int numKeys: root.numOctaves * 7 + 1 |
| 66 |
Repeater { |
| 67 |
id: keyDrawer |
| 68 |
model: keyRow.numKeys |
| 69 |
Key {} |
| 70 |
} |
| 71 |
} |
| 72 |
Row { |
| 73 |
id: blackkeyRow |
| 74 |
anchors.fill: parent |
| 75 |
property int numKeys: root.numOctaves * 7 + 1 |
| 76 |
anchors.leftMargin: (2.0/3.0) *root.width/(numKeys) |
| 77 |
spacing: (1.0/3.0) *root.width/(numKeys) |
| 78 |
Repeater { |
| 79 |
id: blackkeyDrawer |
| 80 |
model: blackkeyRow.numKeys |
| 81 |
Key { |
| 82 |
enabled: (index% 7)!=2 && (index% 7)!=6 |
| 83 |
width: (2.0/3.0) * root.width/(blackkeyRow.numKeys) |
| 84 |
height: root.height*0.6 |
| 85 |
color: enabled ? (!checked ? "black" : "grey"):" transparent" |
| 86 |
border.color: !enabled ? "transparent" :"grey" |
| 87 |
border.width: 0 |
| 88 |
blackKey: true |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |