📄 noteinserter.cpp
字号:
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: *//* Rosegarden A MIDI and audio sequencer and musical notation editor. This program is Copyright 2000-2007 Guillaume Laurent <glaurent@telegraph-road.org>, Chris Cannam <cannam@all-day-breakfast.com>, Richard Bown <richard.bown@ferventsoftware.com> The moral rights of Guillaume Laurent, Chris Cannam, and Richard Bown to claim authorship of this work have been asserted. Other copyrights also apply to some parts of this work. Please see the AUTHORS file and individual file headers for details. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the file COPYING included with this distribution for more information.*/#include "NoteInserter.h"#include "misc/Debug.h"#include <kapplication.h>#include "base/BaseProperties.h"#include <klocale.h>#include "misc/Strings.h"#include "document/ConfigGroups.h"#include "base/Event.h"#include "base/NotationTypes.h"#include "base/Segment.h"#include "base/Staff.h"#include "base/ViewElement.h"#include "commands/notation/NoteInsertionCommand.h"#include "commands/notation/RestInsertionCommand.h"#include "commands/notation/TupletCommand.h"#include "gui/general/EditTool.h"#include "gui/general/LinedStaff.h"#include "gui/general/RosegardenCanvasView.h"#include "NotationProperties.h"#include "NotationStrings.h"#include "NotationTool.h"#include "NotationView.h"#include "NotePixmapFactory.h"#include "NoteStyleFactory.h"#include <kaction.h>#include <kcommand.h>#include <kconfig.h>#include <qiconset.h>#include <qregexp.h>#include <qstring.h>namespace Rosegarden{NoteInserter::NoteInserter(NotationView* view) : NotationTool("NoteInserter", view), m_noteType(Note::Quaver), m_noteDots(0), m_autoBeam(true), m_accidental(Accidentals::NoAccidental), m_lastAccidental(Accidentals::NoAccidental), m_followAccidental(false){ QIconSet icon; KConfig *config = kapp->config(); config->setGroup(NotationViewConfigGroup); m_autoBeam = config->readBoolEntry("autobeam", true); m_matrixInsertType = (config->readNumEntry("inserttype", 0) > 0); m_defaultStyle = qstrtostr(config->readEntry ("style", strtoqstr(NoteStyleFactory::DefaultStyle))); KToggleAction *autoBeamAction = new KToggleAction(i18n("Auto-Beam when appropriate"), 0, this, SLOT(slotToggleAutoBeam()), actionCollection(), "toggle_auto_beam"); autoBeamAction->setChecked(m_autoBeam); for (unsigned int i = 0; i < 6; ++i) { icon = QIconSet (NotePixmapFactory::toQPixmap(NotePixmapFactory:: makeToolbarPixmap(m_actionsAccidental[i][3]))); KRadioAction* noteAction = new KRadioAction(i18n(m_actionsAccidental[i][0]), icon, 0, this, m_actionsAccidental[i][1], actionCollection(), m_actionsAccidental[i][2]); noteAction->setExclusiveGroup("accidentals"); } icon = QIconSet (NotePixmapFactory::toQPixmap(NotePixmapFactory:: makeToolbarPixmap("dotted-crotchet"))); new KToggleAction(i18n("Dotted note"), icon, 0, this, SLOT(slotToggleDot()), actionCollection(), "toggle_dot"); icon = QIconSet(NotePixmapFactory::toQPixmap(NotePixmapFactory:: makeToolbarPixmap("select"))); new KAction(i18n("Switch to Select Tool"), icon, 0, this, SLOT(slotSelectSelected()), actionCollection(), "select"); new KAction(i18n("Switch to Erase Tool"), "eraser", 0, this, SLOT(slotEraseSelected()), actionCollection(), "erase"); icon = QIconSet (NotePixmapFactory::toQPixmap(NotePixmapFactory:: makeToolbarPixmap("rest-crotchet"))); new KAction(i18n("Switch to Inserting Rests"), icon, 0, this, SLOT(slotRestsSelected()), actionCollection(), "rests"); createMenu("noteinserter.rc"); connect(m_parentView, SIGNAL(changeAccidental(Accidental, bool)), this, SLOT(slotSetAccidental(Accidental, bool)));}NoteInserter::NoteInserter(const QString& menuName, NotationView* view) : NotationTool(menuName, view), m_noteType(Note::Quaver), m_noteDots(0), m_autoBeam(false), m_clickHappened(false), m_accidental(Accidentals::NoAccidental), m_lastAccidental(Accidentals::NoAccidental), m_followAccidental(false){ connect(m_parentView, SIGNAL(changeAccidental(Accidental, bool)), this, SLOT(slotSetAccidental(Accidental, bool)));}NoteInserter::~NoteInserter(){}void NoteInserter::ready(){ m_clickHappened = false; m_nParentView->setCanvasCursor(Qt::crossCursor); m_nParentView->setHeightTracking(true);}voidNoteInserter::handleLeftButtonPress(timeT, int, int staffNo, QMouseEvent* e, ViewElement*){ if (staffNo < 0) return ; computeLocationAndPreview(e);}intNoteInserter::handleMouseMove(timeT, int, QMouseEvent *e){ if (m_clickHappened) { computeLocationAndPreview(e); } return RosegardenCanvasView::NoFollow;}voidNoteInserter::handleMouseRelease(timeT, int, QMouseEvent *e){ if (!m_clickHappened) return ; bool okay = computeLocationAndPreview(e); m_clickHappened = false; if (!okay) return ; clearPreview(); Note note(m_noteType, m_noteDots); timeT endTime = m_clickTime + note.getDuration(); Segment &segment = m_nParentView->getStaff(m_clickStaffNo)->getSegment(); Segment::iterator realEnd = segment.findTime(endTime); if (!segment.isBeforeEndMarker( realEnd) || !segment.isBeforeEndMarker(++realEnd)) { endTime = segment.getEndMarkerTime(); } else { endTime = std::max(endTime, (*realEnd)->getNotationAbsoluteTime()); } Event *lastInsertedEvent = doAddCommand (segment, m_clickTime, endTime, note, m_clickPitch, (m_accidental == Accidentals::NoAccidental && m_followAccidental) ? m_lastAccidental : m_accidental); if (lastInsertedEvent) { m_nParentView->setSingleSelectedEvent (m_clickStaffNo, lastInsertedEvent); if (m_nParentView->isInChordMode()) { m_nParentView->slotSetInsertCursorAndRecentre (lastInsertedEvent->getAbsoluteTime(), e->x(), (int)e->y(), false); } else { m_nParentView->slotSetInsertCursorAndRecentre (lastInsertedEvent->getAbsoluteTime() + lastInsertedEvent->getDuration(), e->x(), (int)e->y(), false); } }}voidNoteInserter::insertNote(Segment &segment, timeT insertionTime, int pitch, Accidental accidental, bool suppressPreview){ Note note(m_noteType, m_noteDots); timeT endTime = insertionTime + note.getDuration(); Segment::iterator realEnd = segment.findTime(endTime); if (!segment.isBeforeEndMarker( realEnd) || !segment.isBeforeEndMarker(++realEnd)) { endTime = segment.getEndMarkerTime(); } else { endTime = std::max(endTime, (*realEnd)->getNotationAbsoluteTime()); } Event *lastInsertedEvent = doAddCommand (segment, insertionTime, endTime, note, pitch, accidental); if (lastInsertedEvent) { m_nParentView->setSingleSelectedEvent(segment, lastInsertedEvent); if (m_nParentView->isInChordMode()) { m_nParentView->slotSetInsertCursorPosition (lastInsertedEvent->getAbsoluteTime(), true, false); } else { m_nParentView->slotSetInsertCursorPosition (lastInsertedEvent->getAbsoluteTime() + lastInsertedEvent->getDuration(), true, false); } } if (!suppressPreview) m_nParentView->playNote(segment, pitch);}boolNoteInserter::computeLocationAndPreview(QMouseEvent *e){ double x = e->x(); int y = (int)e->y(); LinedStaff *staff = m_nParentView->getStaffForCanvasCoords(e->x(), y); if (!staff) { clearPreview(); return false; } int staffNo = staff->getId(); if (m_clickHappened && staffNo != m_clickStaffNo) { // abandon clearPreview(); return false; } int height = staff->getHeightAtCanvasCoords(x, y); Event *clefEvt = 0, *keyEvt = 0; Clef clef; Rosegarden::Key key; NotationElementList::iterator itr = staff->getElementUnderCanvasCoords(x, y, clefEvt, keyEvt); if (itr == staff->getViewElementList()->end()) { clearPreview(); return false; } timeT time = (*itr)->event()->getAbsoluteTime(); // not getViewAbsoluteTime() m_clickInsertX = (*itr)->getLayoutX(); if (clefEvt) clef = Clef(*clefEvt); if (keyEvt) key = Rosegarden::Key(*keyEvt); NotationElement* el = static_cast<NotationElement*>(*itr); if (el->isRest() && el->getCanvasItem()) { time += getOffsetWithinRest(staffNo, itr, x); m_clickInsertX += (x - el->getCanvasX()); } Pitch p(height, clef, key, m_accidental); int pitch = p.getPerformancePitch(); // [RFE 987960] When inserting via mouse, if no accidental is // selected, we use the same accidental (and thus the same pitch) // as of the previous note found at this height -- iff such a note // is found more recently than the last key signature. if (m_accidental == Accidentals::NoAccidental && m_followAccidental) { Segment &segment = staff->getSegment(); m_lastAccidental = m_accidental; Segment::iterator i = segment.findNearestTime(time); while (i != segment.end()) { if ((*i)->isa(Rosegarden::Key::EventType)) break; if ((*i)->isa(Note::EventType)) { if ((*i)->has(NotationProperties::HEIGHT_ON_STAFF) && (*i)->has(BaseProperties::PITCH)) { int h = (*i)->get<Int>(NotationProperties::HEIGHT_ON_STAFF); if (h == height) { pitch = (*i)->get<Int>(BaseProperties::PITCH); (*i)->get<String>(BaseProperties::ACCIDENTAL, m_lastAccidental); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -