Skip to content
Snippets Groups Projects
Score.qml 5.33 KiB
Newer Older
/** This file is part of Nootka (http://nootka.sf.net)               *
 * Copyright (C) 2017-2021 by Tomasz Bojczuk (seelook@gmail.com)     *
 * on the terms of GNU GPLv3 license (http://www.gnu.org/licenses)   */
SeeLook's avatar
SeeLook committed
import Nootka 1.0
import QtQuick 2.12
import QtQuick.Controls 2.12
import Nootka.Music
SeeLook's avatar
SeeLook committed
    //   maximumFlickVelocity: 1000 // 2500 by default

    id: score

    property alias scoreObj: scoreObj
    property alias scale: staff0.scale
    property alias firstStaff: staff0
    property Staff lastStaff: staff0
    property alias clef: scoreObj.clefType
    property alias upperLine: staff0.upperLine
    property alias meter: scoreObj.meter
    property alias bgColor: bgRect.color
    property alias enableKeySign: scoreObj.keySignatureEnabled
    property alias keySignature: scoreObj.keySignature
    property bool enableDoubleAccids: false
    property alias workRhythm: scoreObj.workRhythm
    property alias scaleFactor: scoreObj.scaleFactor
    property alias notesCount: scoreObj.notesCount
    property alias currentNote: scoreObj.selectedItem
    property alias note: scoreObj.selectedNote
    property alias readOnly: scoreObj.readOnly
    property alias singleNote: scoreObj.singleNote
    property alias bgRect: bgRect
    property alias alterText: scoreObj.alterText
    property alias editText: editText
    property alias insertNoteAct: scoreObj.insertNoteAct
    property alias deleteNoteAct: scoreObj.deleteNoteAct
    property alias clearScoreAct: scoreObj.clearScoreAct
    // private
    property var staves: [staff0]
    property var noteAdd: null
    property var delControl: null
    property var cursor: null
    property var scoreToobox: null

SeeLook's avatar
SeeLook committed
    function ensureVisible(yy: real, hh: real) : void {
SeeLook's avatar
SeeLook committed
        if (contentY >= yy)
            contentY = yy;
        else if (contentY + height <= yy + hh)
            contentY = yy + hh - height;
    }

SeeLook's avatar
SeeLook committed
    function addNote(n: tnote) : void {
SeeLook's avatar
SeeLook committed
        scoreObj.addNote(n, true);
    }

SeeLook's avatar
SeeLook committed
    function setNote(noteItem: TnoteItem, note: tnote) : void {
SeeLook's avatar
SeeLook committed
        scoreObj.setNote(noteItem, note);
    }

SeeLook's avatar
SeeLook committed
    function clearScore() : void {
SeeLook's avatar
SeeLook committed
        scoreObj.clearScore();
    }

SeeLook's avatar
SeeLook committed
    function deleteLast() : void {
SeeLook's avatar
SeeLook committed
        scoreObj.deleteLastNote();
    }

    clip: true
    boundsBehavior: Flickable.StopAtBounds
    width: parent.width
    contentWidth: score.width
    onCurrentNoteChanged: {
        if (currentNote && staves.length > 1)
            ensureVisible(currentNote.staffItem.y, currentNote.staffItem.height * scale);

    }

    TscoreObject {
        id: scoreObj

SeeLook's avatar
SeeLook committed
        width: score.width / score.scale
SeeLook's avatar
SeeLook committed
        enableDoubleAccidentals: score.enableDoubleAccids
SeeLook's avatar
SeeLook committed
        onClicked: score.currentNote = scoreObj.activeNote
SeeLook's avatar
SeeLook committed
        onStaffCreate: {
SeeLook's avatar
SeeLook committed
            score.staves.push(Qt.createComponent("qrc:/score/Staff.qml").createObject(score.contentItem, { score: score }));
            score.lastStaff = score.staves[score.staves.length - 1];
SeeLook's avatar
SeeLook committed
        }
        onStavesHeightChanged: score.contentHeight = Math.max(stavesHeight, score.height)
        onStaffDestroying: staffNr => {
SeeLook's avatar
SeeLook committed
            score.staves.splice(staffNr, 1);
            lastStaff = score.staves[score.staves.length - 1];
SeeLook's avatar
SeeLook committed
        }
        onNoteWasAdded: {
SeeLook's avatar
SeeLook committed
            if (score.staves.length > 1)
SeeLook's avatar
SeeLook committed
                ensureVisible(lastNote.staffItem.y, lastNote.staffItem.height * scale);

        }
        onAllowAddingChanged: {
            if (allowAdding) {
SeeLook's avatar
SeeLook committed
                if (!score.delControl)
                    score.delControl = Qt.createComponent("qrc:/score/DelControl.qml").createObject(score.contentItem);
                if (!score.noteAdd)
                    score.noteAdd = Qt.createComponent("qrc:/score/NoteAdd.qml").createObject(score.contentItem);
                if (!score.scoreToobox)
                    score.scoreToobox = Qt.createComponent("qrc:/score/ScoreToolbox.qml").createObject(score);
SeeLook's avatar
SeeLook committed
            }
        }
        onActiveNoteChanged: {
SeeLook's avatar
SeeLook committed
            if (!score.cursor) {
                score.cursor = Qt.createComponent("qrc:/score/ScoreCursor.qml").createObject(score.contentItem);
                score.cursor.parent = Qt.binding(function() {
SeeLook's avatar
SeeLook committed
                    return scoreObj.activeNote;
                });
            }
SeeLook's avatar
SeeLook committed
            if (!score.scoreToobox && !score.readOnly)
                score.scoreToobox = Qt.createComponent("qrc:/score/ScoreToolbox.qml").createObject(score);
SeeLook's avatar
SeeLook committed
        onScoreWasCleared: score.ensureVisible(0, 0)
SeeLook's avatar
SeeLook committed
    }

    // entire score background
    Rectangle {
        id: bgRect

        parent: score
        z: -1
        width: score.width
        height: score.height
        color: NOO.alpha(scoreObj.bgColor, 230)
SeeLook's avatar
SeeLook committed
    // first staff (always exists)
    Staff {
        id: staff0
SeeLook's avatar
SeeLook committed
        score: score
SeeLook's avatar
SeeLook committed
        meter: Meter {
            parent: staff0
        }
SeeLook's avatar
SeeLook committed
    // edit mode symbol
    Text {
        id: editText

        opacity: scoreObj.editMode && !singleNote ? 1 : 0
        x: NOO.factor() * 3
        y: score.contentY + NOO.factor() / 2
        text: "\u0080"
        color: NOO.alpha(GLOB.noteCursorColor, 200)

        font {
            family: "Nootka"
            pixelSize: NOO.factor() * 2
        }

        Behavior on opacity {
            enabled: GLOB.useAnimations

            NumberAnimation {
                duration: 500
            }

        }
SeeLook's avatar
SeeLook committed
    ScrollBar.vertical: ScrollBar {
        active: false
        visible: active