📄 pitchdraglabel.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 "PitchDragLabel.h"#include "base/NotationRules.h"#include "base/NotationTypes.h"#include "gui/editors/notation/NotePixmapFactory.h"#include <qcanvas.h>#include <qpainter.h>#include <qpixmap.h>#include <qsize.h>#include <qwidget.h>namespace Rosegarden{PitchDragLabel::PitchDragLabel(QWidget *parent, int defaultPitch, bool defaultSharps) : QWidget(parent), m_pitch(defaultPitch), m_usingSharps(defaultSharps), m_clickedY(0), m_clicked(false), m_npf(new NotePixmapFactory()){ calculatePixmap();}PitchDragLabel::~PitchDragLabel(){ delete m_npf;}voidPitchDragLabel::slotSetPitch(int p){ if (m_pitch == p) return ; bool up = (p > m_pitch); m_usingSharps = up; m_pitch = p; calculatePixmap(); emitPitchChange(); paintEvent(0);}voidPitchDragLabel::slotSetPitch(int pitch, int octave, int step){ if (m_pitch == pitch) return ; m_pitch = pitch; calculatePixmap(pitch, octave, step); emit pitchChanged(pitch); emit pitchChanged(pitch, octave, step); paintEvent(0);}voidPitchDragLabel::mousePressEvent(QMouseEvent *e){ if (e->button() == LeftButton) { m_clickedY = e->y(); m_clickedPitch = m_pitch; m_clicked = true; emit preview(m_pitch); }}voidPitchDragLabel::mouseMoveEvent(QMouseEvent *e){ if (m_clicked) { int y = e->y(); int diff = y - m_clickedY; int pitchDiff = diff * 4 / m_npf->getLineSpacing(); int newPitch = m_clickedPitch - pitchDiff; if (newPitch < 0) newPitch = 0; if (newPitch > 127) newPitch = 127; if (m_pitch != newPitch) { if (newPitch > m_pitch) { // use sharps emit pitchDragged(newPitch, (int)(((long)newPitch) / 12), steps_Cmajor_with_sharps[newPitch % 12]); } else { // use flats emit pitchDragged(newPitch, (int)(((long)newPitch) / 12), steps_Cmajor_with_flats[newPitch % 12]); } } }}voidPitchDragLabel::mouseReleaseEvent(QMouseEvent *e){ mouseMoveEvent(e); emitPitchChange(); m_clicked = false;}voidPitchDragLabel::emitPitchChange(){ emit pitchChanged(m_pitch); Pitch newPitch(m_pitch); if (m_usingSharps) { Rosegarden::Key key = Rosegarden::Key("C major"); emit pitchDragged(m_pitch, newPitch.getOctave(0), newPitch.getNoteInScale(key)); } else { Rosegarden::Key key = Rosegarden::Key("A minor"); emit pitchDragged(m_pitch, newPitch.getOctave(0), (newPitch.getNoteInScale(key) + 5) % 7); }}voidPitchDragLabel::wheelEvent(QWheelEvent *e){ if (e->delta() > 0) { if (m_pitch < 127) { int newPitch = m_pitch + 1; // use sharps emit pitchDragged(newPitch, (int)(((long)newPitch) / 12), steps_Cmajor_with_sharps[newPitch % 12]); } } else { if (m_pitch > 0) { int newPitch = m_pitch - 1; // use flats emit pitchDragged(newPitch, (int)(((long)newPitch) / 12), steps_Cmajor_with_flats[newPitch % 12]); } }}voidPitchDragLabel::paintEvent(QPaintEvent *){ QPainter paint(this); paint.fillRect(0, 0, width(), height(), paint.backgroundColor()); int x = width() / 2 - m_pixmap.width() / 2; if (x < 0) x = 0; int y = height() / 2 - m_pixmap.height() / 2; if (y < 0) y = 0; paint.drawPixmap(x, y, m_pixmap);}QSizePitchDragLabel::sizeHint() const{ return QSize(150, 135);}voidPitchDragLabel::calculatePixmap(int pitch, int octave, int step) const{ std::string clefType = Clef::Treble; int octaveOffset = 0; if (m_pitch > 94) { octaveOffset = 2; } else if (m_pitch > 82) { octaveOffset = 1; } else if (m_pitch < 60) { clefType = Clef::Bass; if (m_pitch < 24) { octaveOffset = -2; } else if (m_pitch < 36) { octaveOffset = -1; } } QCanvasPixmap *pmap = m_npf->makePitchDisplayPixmap (m_pitch, Clef(clefType, octaveOffset), octave, step); m_pixmap = *pmap; delete pmap;}voidPitchDragLabel::calculatePixmap() const{ std::string clefType = Clef::Treble; int octaveOffset = 0; if (m_pitch > 94) { octaveOffset = 2; } else if (m_pitch > 82) { octaveOffset = 1; } else if (m_pitch < 60) { clefType = Clef::Bass; if (m_pitch < 24) { octaveOffset = -2; } else if (m_pitch < 36) { octaveOffset = -1; } } QCanvasPixmap *pmap = m_npf->makePitchDisplayPixmap (m_pitch, Clef(clefType, octaveOffset), m_usingSharps); m_pixmap = *pmap; delete pmap;}}#include "PitchDragLabel.moc"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -