示例#1
0
void NoteEntryAction::mousePress(Staff* staff, int bar, const QPointF& pos)
{
    Clef* clef = staff->lastClefChange(bar);

    Voice* voice = staff->part()->voice(m_tool->voice());
    VoiceBar* vb = voice->bar(bar);

    // find element before which to insert the chord
    int before = 0;
    for (int i = 0; i < vb->elementCount(); i++) {
        VoiceElement* e = vb->element(i);
        if (e->x() >= pos.x()) break;
        before++;
    }

    int line = staff->line(pos.y());
    int pitch = 0, accidentals = 0;
    if (clef && !m_isRest) {
        pitch = clef->lineToPitch(line);
        // get correct accidentals for note
        KeySignature* ks = staff->lastKeySignatureChange(bar);
        if (ks) accidentals = ks->accidentals(pitch);
        for (int i = 0; i < before; i++) {
            Chord* c = dynamic_cast<Chord*>(vb->element(i));
            if (!c) continue;
            for (int n = 0; n < c->noteCount(); n++) {
                if (c->note(n)->pitch() == pitch) {
                    accidentals = c->note(n)->accidentals();
                }
            }
        }
    }

    Chord* join = NULL;
    if (before > 0) join = dynamic_cast<Chord*>(vb->element(before-1));
    if (join && join->x() + join->width() >= pos.x()) {
        if (clef && !m_isRest) {
            m_tool->addCommand(new AddNoteCommand(m_tool->shape(), join, staff, m_duration, pitch, accidentals));
        } else {
            m_tool->addCommand(new MakeRestCommand(m_tool->shape(), join));
        }
    } else {
        if (clef && !m_isRest) {
            m_tool->addCommand(new CreateChordCommand(m_tool->shape(), vb, staff, m_duration, before, pitch, accidentals));
        } else {
            m_tool->addCommand(new CreateChordCommand(m_tool->shape(), vb, staff, m_duration, before));
        }
    }
}
示例#2
0
void NoteEntryAction::keyPress(QKeyEvent* event, const MusicCursor& cursor)
{
    if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
        Staff* staff = cursor.staff();
        //Part* part = staff->part();
        //Sheet* sheet = part->sheet();
        //Bar* bar = sheet->bar(cursor.bar());
        Clef* clef = staff->lastClefChange(cursor.bar());
        int line = cursor.line();
        int pitch = 0, accidentals = 0;
        VoiceBar* vb = cursor.voiceBar();
        if (clef) {
            pitch = clef->lineToPitch(line);
            // get correct accidentals for note
            KeySignature* ks = staff->lastKeySignatureChange(cursor.bar());
            if (ks) accidentals = ks->accidentals(pitch);
            for (int i = 0; i < cursor.element(); i++) {
                Chord* c = dynamic_cast<Chord*>(vb->element(i));
                if (!c) continue;
                for (int n = 0; n < c->noteCount(); n++) {
                    if (c->note(n)->pitch() == pitch) {
                        accidentals = c->note(n)->accidentals();
                    }
                }
            }
        }

        Chord* join = 0;
        if (cursor.element() < vb->elementCount()) join = dynamic_cast<Chord*>(vb->element(cursor.element()));
        if (event->modifiers() & Qt::ShiftModifier || !join) {
            m_tool->addCommand(new CreateChordCommand(m_tool->shape(), vb, staff, m_duration, cursor.element(), pitch, accidentals));
        } else {
            m_tool->addCommand(new AddNoteCommand(m_tool->shape(), join, staff, join->duration(), pitch, accidentals));
        }
        event->accept();
    }
}