Logo Search packages:      
Sourcecode: canorus version File versions  Download package

void CAMainWin::scoreViewPortKeyPress ( QKeyEvent *  e  )  [private, slot]

Processes the key press event e. Any action happened in any of the viewports are always linked to its main window slots.

See also:
CAScoreViewPort::keyPressEvent(), scoreViewPortMousePress(), scoreViewPortMouseMove(), scoreViewPortWheel()

Definition at line 1873 of file mainwin.cpp.

References CAMusElementFactory::addNoteAccs(), CAMusElementFactory::addNoteExtraAccs(), CAMusElementFactory::addPlayableDotted(), CAPlayable::calculateTimeLength(), CAScoreViewPort::clearSelection(), CARest::composeRests(), CAScoreViewPort::currentContext(), currentVoice(), deleteSelection(), CAKeySignature::diatonicKey(), CANote::diatonicPitch(), CAPlayableLength::dotted(), CAVoice::getKeySig(), CAVoice::insert(), CAMusElement::isPlayable(), CAVoice::lyricsContextList(), CAMusElement::musElementType(), CAVoice::next(), CAStaff::next(), CADiatonicKey::noteAccs(), CADiatonicPitch::noteName(), CAMusElementFactory::playableLength(), CAPlayable::playableLength(), playImmediately(), CAVoice::remove(), CAScoreViewPort::selection(), CAScoreViewPort::selectMElement(), CAScoreViewPort::selectNextMusElement(), CAScoreViewPort::selectPrevMusElement(), CAScoreViewPort::selectUpMusElement(), CAScoreViewPort::setCurrentContext(), CAMenuToolButton::setCurrentId(), CANote::setDiatonicPitch(), CAPlayableLength::setDotted(), CAScoreViewPort::setDrawShadowNoteAccs(), CAScoreViewPort::setShadowNoteAccs(), CAScoreViewPort::setShadowNoteLength(), CAContext::sheet(), CAScoreViewPort::sheet(), CAPlayable::staff(), CAMusElementFactory::subNoteAccs(), CAMusElementFactory::subNoteExtraAccs(), CAStaff::synchronizeVoices(), CAMusElement::timeLength(), CAMusElement::timeStart(), CAScoreViewPort::updateHelpers(), updateToolBars(), CAPlayable::voice(), and CAStaff::voiceAt().

Referenced by initViewPort().

                                                  {
      CAScoreViewPort *v = static_cast<CAScoreViewPort*>(sender());
      setCurrentViewPort( v );

      // go to Insert mode (if in Select mode) before changing note length
      if(e->key() >= Qt::Key_0 && e->key() <= Qt::Key_9 && e->key() != Qt::Key_3) {
            if (mode()!=EditMode) {
                  uiInsertPlayable->setChecked(true);
            }
      }

      switch (e->key()) {
            // Music editing keys
            case Qt::Key_Right: {
                  // select next music element
                  v->selectNextMusElement( e->modifiers()==Qt::ShiftModifier );
                  v->repaint();
                  break;
            }

            case Qt::Key_Left: {
                  // select previous music element
                  v->selectPrevMusElement( e->modifiers()==Qt::ShiftModifier );
                  v->repaint();
                  break;
            }

            case Qt::Key_B: {
                  // place a barline
                  CADrawableContext *drawableContext;
                  drawableContext = v->currentContext();

                  if ( (!drawableContext) || (drawableContext->context()->contextType() != CAContext::Staff) )
                        return;

                  CAStaff *staff = static_cast<CAStaff*>(drawableContext->context());
                  CAMusElement *right = 0;
                  if (!v->selection().isEmpty()) {
                        CAMusElement *e = v->selection().back()->musElement();
                        if (e->musElementType()==CAMusElement::Note) {
                              right = staff->next(static_cast<CANote*>(e)->getChord().back());
                        } else {
                              right = staff->next(e);
                        }
                  }

                  CACanorus::undo()->createUndoCommand( document(), tr("insert barline", "undo") );
                  CABarline *bar = new CABarline(
                        CABarline::Single,
                        staff,
                        0
                  );

                  if (currentVoice()) {
                        currentVoice()->insert( right, bar ); // insert the barline in all the voices, timeStart is set
                  } else {
                        if ( right && right->isPlayable() )
                              static_cast<CAPlayable*>(right)->voice()->insert( right, bar );
                        else
                              staff->voiceAt(0)->insert( right, bar );
                  }

                  staff->synchronizeVoices();

                  CACanorus::undo()->pushUndoCommand();
                  CACanorus::rebuildUI(document(), v->sheet());
                  v->selectMElement(bar);
                  v->repaint();
                  break;
            }

            case Qt::Key_Up: {
                  if (mode() == SelectMode) {   // select the upper music element
                        v->selectUpMusElement();
                        v->repaint();
                  } else if ((mode() == InsertMode) || (mode() == EditMode)) {
                        bool rebuild=false;
                        if (v->selection().size())
                              CACanorus::undo()->createUndoCommand( document(), tr("rise note", "undo") );

                        QList<CAMusElement*> eltList;
                        for (int i=0; i<v->selection().size(); i++) {
                              CADrawableMusElement *elt = v->selection().at(i);

                              // pitch note for one step higher
                              if (elt->drawableMusElementType() == CADrawableMusElement::DrawableNote) {
                                    CANote *note = static_cast<CANote*>(elt->musElement());
                                    CADiatonicKey key;
                                    if (note->voice()->getKeySig(note)) {
                                          key = note->voice()->getKeySig(note)->diatonicKey();
                                    }
                                    CADiatonicPitch pitch( note->diatonicPitch().noteName()+1, key.noteAccs(note->diatonicPitch().noteName()+1) );
                                    note->setDiatonicPitch( pitch );
                                    CACanorus::undo()->pushUndoCommand();
                                    rebuild = true;
                                    eltList << note;
                              }
                        }

                        if ( CACanorus::settings()->playInsertedNotes() ) {
                              playImmediately(eltList);
                        }

                        if (rebuild)
                              CACanorus::rebuildUI(document(), currentSheet());
                  }
                  break;
            }

            case Qt::Key_Down: {
                  if (mode() == SelectMode) {   // select the upper music element
                        v->selectUpMusElement();
                        v->repaint();
                  } else if ((mode() == InsertMode) || (mode() == EditMode)) {
                        bool rebuild = false;
                        if (v->selection().size())
                              CACanorus::undo()->createUndoCommand( document(), tr("lower note", "undo") );

                        QList<CAMusElement*> eltList;
                        for (int i=0; i<v->selection().size(); i++) {
                              CADrawableMusElement *elt = v->selection().at(i);

                              // pitch note for one step higher
                              if (elt->drawableMusElementType() == CADrawableMusElement::DrawableNote) {
                                    CANote *note = static_cast<CANote*>(elt->musElement());
                                    CADiatonicKey key;
                                    if (note->voice()->getKeySig(note)) {
                                          key = note->voice()->getKeySig(note)->diatonicKey();
                                    }
                                    CADiatonicPitch pitch( note->diatonicPitch().noteName()-1, key.noteAccs(note->diatonicPitch().noteName()-1) );
                                    note->setDiatonicPitch( pitch );
                                    CACanorus::undo()->pushUndoCommand();
                                    rebuild = true;
                                    eltList << note;
                              }
                        }

                        if ( CACanorus::settings()->playInsertedNotes() ) {
                              playImmediately(eltList);
                        }

                        CACanorus::rebuildUI(document(), currentSheet());
                  }
                  break;
            }

            case Qt::Key_Plus: {
                  if (mode()==InsertMode) {
                        musElementFactory()->addNoteExtraAccs(1); musElementFactory()->addNoteAccs(1);
                        v->setDrawShadowNoteAccs(musElementFactory()->noteExtraAccs()!=0);
                        v->setShadowNoteAccs(musElementFactory()->noteAccs());
                        v->repaint();
                  } else if (mode()==EditMode) {
                        if (!v->selection().isEmpty()) {
                              QList<CAMusElement*> eltList;
                              CASheet* sheet = 0;
                              foreach(CADrawableMusElement* dElt, v->selection()) {
                                    CAMusElement *elt = dElt->musElement();
                                    if (elt->musElementType()==CAMusElement::Note) {
                                          if(!sheet) {
                                                sheet = static_cast<CANote*>(elt)->voice()->staff()->sheet();
                                                CACanorus::undo()->createUndoCommand( document(), tr("add sharp", "undo") );
                                          }
                                          if ( static_cast<CANote*>(elt)->diatonicPitch().accs() < 2 )       // limit the amount of accidentals
                                                static_cast<CANote*>(elt)->diatonicPitch().setAccs( static_cast<CANote*>(elt)->diatonicPitch().accs()+1 );
                                    }
                                    eltList << elt;
                              }
                              if(sheet) { // something's changed
                                    CACanorus::undo()->pushUndoCommand();
                                    CACanorus::rebuildUI(document(), sheet);
                                    if ( CACanorus::settings()->playInsertedNotes() ) {
                                          playImmediately( eltList );
                                    }
                              }
                        }
                  }
                  break;
            }

            case Qt::Key_Minus: {
                  if (mode()==InsertMode) {
                        musElementFactory()->subNoteExtraAccs(1); musElementFactory()->subNoteAccs(1);
                        v->setDrawShadowNoteAccs(musElementFactory()->noteExtraAccs()!=0);
                        v->setShadowNoteAccs(musElementFactory()->noteAccs());
                        v->repaint();
                  } else if (mode()==EditMode) {
                        if (!v->selection().isEmpty()) {
                              QList<CAMusElement*> eltList;
                              CASheet* sheet = 0;
                              foreach(CADrawableMusElement* dElt, v->selection()) {
                                    CAMusElement *elt = dElt->musElement();
                                    if (elt->musElementType()==CAMusElement::Note) {
                                          if(!sheet) {
                                                sheet = static_cast<CANote*>(elt)->voice()->staff()->sheet();
                                                CACanorus::undo()->createUndoCommand( document(), tr("add flat", "undo") );
                                          }
                                          if ( static_cast<CANote*>(elt)->diatonicPitch().accs() > -2 )       // limit the amount of accidentals
                                                static_cast<CANote*>(elt)->diatonicPitch().setAccs( static_cast<CANote*>(elt)->diatonicPitch().accs()-1 );
                                    }
                                    eltList << elt;
                              }
                              if(sheet) { // something's changed
                                    CACanorus::undo()->pushUndoCommand();
                                    CACanorus::rebuildUI(document(), sheet);
                                    if ( CACanorus::settings()->playInsertedNotes() ) {
                                          playImmediately( eltList );
                                    }
                              }
                        }
                  }
                  break;
            }

            case Qt::Key_Period: {
                  if (mode()==InsertMode) {
                        musElementFactory()->addPlayableDotted( 1, musElementFactory()->playableLength() );
                        currentScoreViewPort()->setShadowNoteLength( musElementFactory()->playableLength() );
                        currentScoreViewPort()->updateHelpers();
                        v->repaint();
                  } else if (mode()==EditMode) {
                        if (!((CAScoreViewPort*)v)->selection().isEmpty()) {
                              CACanorus::undo()->createUndoCommand( document(), tr("set dotted", "undo") );
                              CAPlayable *p = dynamic_cast<CAPlayable*>(currentScoreViewPort()->selection().front()->musElement());

                              if (p) {
                                    CAMusElement *next=0;
                                    int oldLength = p->timeLength();
                                    if ( p->musElementType()==CAMusElement::Note ) {                   // change the length of the whole chord
                                          QList<CANote*> chord = static_cast<CANote*>(p)->getChord();
                                          for (int i=0; i<chord.size(); i++) {
                                                next = p->voice()->next(p);
                                                p->voice()->remove(chord[i]);
                                                chord[i]->playableLength().setDotted( (p->playableLength().dotted()+1)%4 );
                                                chord[i]->calculateTimeLength();
                                          }
                                          p->voice()->insert( next, chord[0], false );
                                          for (int i=1; i<chord.size(); i++) {
                                                p->voice()->insert( chord[0], chord[i], true );
                                          }
                                    } else if ( p->musElementType()==CAMusElement::Rest ) {
                                          next = p->voice()->next(p);
                                          p->voice()->remove(p);
                                          p->playableLength().setDotted( (p->playableLength().dotted()+1)%4 );
                                          p->calculateTimeLength();
                                          p->voice()->insert( next, p );
                                    }

                                    int newLength = p->timeLength();
                                    if (newLength<oldLength) { // insert rests, if the new length is shorter to keep consistency
                                          QList<CARest*> rests = CARest::composeRests( oldLength - newLength, p->timeStart()+p->timeLength(), p->voice(), CARest::Normal );
                                          for (int i=rests.size()-1; i>=0; i--) {
                                                p->voice()->insert( next, rests[i] ); // insert rests from shortest to longest
                                          }
                                    } else {
                                          p->staff()->synchronizeVoices();
                                    }

                                    for (int j=0; j<p->voice()->lyricsContextList().size(); j++) { // reposit syllables
                                          p->voice()->lyricsContextList().at(j)->repositSyllables();
                                    }

                                    CACanorus::undo()->pushUndoCommand();
                                    CACanorus::rebuildUI(document(), p->staff()->sheet());
                              }
                        }
                  }
                  break;
            }

            case Qt::Key_Delete:
            case Qt::Key_Backspace:
                  deleteSelection( v, e->modifiers()==Qt::ShiftModifier, e->modifiers()==Qt::ShiftModifier, true );
                  break;

            // Mode keys
            case Qt::Key_Escape:
                  if (mode()==SelectMode) {
                        v->clearSelection();
                        v->setCurrentContext( 0 );
                  }
                  uiVoiceNum->setRealValue( 0 );
                  uiSelectMode->trigger();
                  break;
            case Qt::Key_I:
                  uiInsertPlayable->trigger();
                  break;
            case Qt::Key_E:
                  uiEditMode->trigger();
                  break;
            case Qt::Key_Space:
                  uiPlayFromSelection->trigger();
                  break;
            case Qt::Key_F5:
                  CACanorus::rebuildUI( document() );
                  break;

            // Note length keys
            case Qt::Key_1:
                  uiPlayableLength->setCurrentId( CAPlayableLength::Whole, true );
                  break;
            case Qt::Key_2:
                  uiPlayableLength->setCurrentId( CAPlayableLength::Half, true );
                  break;
            case Qt::Key_4:
                  uiPlayableLength->setCurrentId( CAPlayableLength::Quarter, true );
                  break;
            case Qt::Key_5:
                  uiPlayableLength->setCurrentId( CAPlayableLength::Eighth, true );
                  break;
            case Qt::Key_6:
                  uiPlayableLength->setCurrentId( CAPlayableLength::Sixteenth, true );
                  break;
            case Qt::Key_7:
                  uiPlayableLength->setCurrentId( CAPlayableLength::ThirtySecond, true );
                  break;
            case Qt::Key_8:
                  uiPlayableLength->setCurrentId( CAPlayableLength::SixtyFourth, true );
                  break;
            case Qt::Key_9:
                  uiPlayableLength->setCurrentId( CAPlayableLength::HundredTwentyEighth, true );
                  break;
            case Qt::Key_0:
                  uiPlayableLength->setCurrentId( CAPlayableLength::Breve, true );
                  break;
      }

      if(e->key() >= Qt::Key_0 && e->key() <= Qt::Key_9 && e->key() != Qt::Key_3)
      {
            musElementFactory()->playableLength().setDotted( 0 );
            v->setShadowNoteLength( musElementFactory()->playableLength() );
            v->updateHelpers();
            v->repaint();
      }

      updateToolBars();
}


Generated by  Doxygen 1.6.0   Back to index