📄 compositionmodelimpl.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 <cmath>#include <algorithm>#include "CompositionModelImpl.h"#include "base/BaseProperties.h"#include "misc/Debug.h"#include "misc/Strings.h"#include "AudioPreviewThread.h"#include "AudioPreviewUpdater.h"#include "base/Composition.h"#include "base/Event.h"#include "base/MidiProgram.h"#include "base/NotationTypes.h"#include "base/Profiler.h"#include "base/RulerScale.h"#include "base/Segment.h"#include "base/Selection.h"#include "base/SnapGrid.h"#include "base/Studio.h"#include "base/Track.h"#include "CompositionItemHelper.h"#include "CompositionItemImpl.h"#include "CompositionModel.h"#include "CompositionRect.h"#include "CompositionColourCache.h"#include "AudioPreviewPainter.h"#include "gui/general/GUIPalette.h"#include "SegmentOrderer.h"#include <qbrush.h>#include <qcolor.h>#include <qpen.h>#include <qpoint.h>#include <qrect.h>#include <qregexp.h>#include <qsize.h>#include <qstring.h>namespace Rosegarden{CompositionModelImpl::CompositionModelImpl(Composition& compo, Studio& studio, RulerScale *rulerScale, int vStep) : m_composition(compo), m_studio(studio), m_grid(rulerScale, vStep), m_pointerTimePos(0), m_audioPreviewThread(0){ m_notationPreviewDataCache.setAutoDelete(true); m_audioPreviewDataCache.setAutoDelete(true); m_composition.addObserver(this); const Composition::segmentcontainer& segments = m_composition.getSegments(); Composition::segmentcontainer::iterator segEnd = segments.end(); for (Composition::segmentcontainer::iterator i = segments.begin(); i != segEnd; ++i) { (*i)->addObserver(this); }}CompositionModelImpl::~CompositionModelImpl(){ RG_DEBUG << "CompositionModelImpl::~CompositionModelImpl()" << endl; if (!isCompositionDeleted()) { m_composition.removeObserver(this); const Composition::segmentcontainer& segments = m_composition.getSegments(); Composition::segmentcontainer::iterator segEnd = segments.end(); for (Composition::segmentcontainer::iterator i = segments.begin(); i != segEnd; ++i) { (*i)->removeObserver(this); } } RG_DEBUG << "CompositionModelImpl::~CompositionModelImpl(): removal from Segment & Composition observers OK" << endl; if (m_audioPreviewThread) { while (!m_audioPreviewUpdaterMap.empty()) { // Cause any running previews to be cancelled delete m_audioPreviewUpdaterMap.begin()->second; m_audioPreviewUpdaterMap.erase(m_audioPreviewUpdaterMap.begin()); } }}struct RectCompare { bool operator()(const QRect &r1, const QRect &r2) const { return r1.x() < r2.x(); }};void CompositionModelImpl::makeNotationPreviewRects(RectRanges* npRects, QPoint basePoint, const Segment* segment, const QRect& clipRect){ rectlist* cachedNPData = getNotationPreviewData(segment); if (cachedNPData->empty()) return ; rectlist::iterator npEnd = cachedNPData->end(); rectlist::iterator npi = std::lower_bound(cachedNPData->begin(), npEnd, clipRect, RectCompare()); if (npi == npEnd) return ; if (npi != cachedNPData->begin()) --npi; RectRange interval; interval.range.first = npi; int segEndX = int(nearbyint(m_grid.getRulerScale()->getXForTime(segment->getEndMarkerTime()))); int xLim = std::min(clipRect.topRight().x(), segEndX); // RG_DEBUG << "CompositionModelImpl::makeNotationPreviewRects : basePoint.x : " // << basePoint.x() << endl; // move iterator forward // while (npi->x() < xLim && npi != npEnd) ++npi; interval.range.second = npi; interval.basePoint.setX(0); interval.basePoint.setY(basePoint.y()); interval.color = computeSegmentPreviewColor(segment); npRects->push_back(interval);}void CompositionModelImpl::makeNotationPreviewRectsMovingSegment(RectRanges* npRects, QPoint basePoint, const Segment* segment, const QRect& currentSR){ CompositionRect unmovedSR = computeSegmentRect(*segment); rectlist* cachedNPData = getNotationPreviewData(segment); if (cachedNPData->empty()) return ; rectlist::iterator npEnd = cachedNPData->end(), npBegin = cachedNPData->begin(); rectlist::iterator npi; if (getChangeType() == ChangeResizeFromStart) npi = std::lower_bound(npBegin, npEnd, currentSR, RectCompare()); else npi = std::lower_bound(npBegin, npEnd, unmovedSR, RectCompare()); if (npi == npEnd) return ; if (npi != npBegin && getChangeType() != ChangeResizeFromStart) { --npi; } RectRange interval; interval.range.first = npi; int xLim = getChangeType() == ChangeMove ? unmovedSR.topRight().x() : currentSR.topRight().x(); // RG_DEBUG << "CompositionModelImpl::makeNotationPreviewRectsMovingSegment : basePoint.x : " // << basePoint.x() << endl; // move iterator forward // while (npi->x() < xLim && npi != npEnd) ++npi; interval.range.second = npi; interval.basePoint.setY(basePoint.y()); if (getChangeType() == ChangeMove) interval.basePoint.setX(basePoint.x() - unmovedSR.x()); else interval.basePoint.setX(0); interval.color = computeSegmentPreviewColor(segment); npRects->push_back(interval);}void CompositionModelImpl::makeAudioPreviewRects(AudioPreviewDrawData* apRects, const Segment* segment, const CompositionRect& segRect, const QRect& clipRect){ Profiler profiler("CompositionModelImpl::makeAudioPreviewRects", true); RG_DEBUG << "CompositionModelImpl::makeAudioPreviewRects - segRect = " << segRect << endl; PixmapArray previewImage = getAudioPreviewPixmap(segment); QPoint basePoint = segRect.topLeft(); AudioPreviewDrawDataItem previewItem(previewImage, basePoint, segRect); if (getChangeType() == ChangeResizeFromStart) { CompositionRect originalRect = computeSegmentRect(*segment); previewItem.resizeOffset = segRect.x() - originalRect.x(); } apRects->push_back(previewItem);}void CompositionModelImpl::computeRepeatMarks(CompositionItem& item){ Segment* s = CompositionItemHelper::getSegment(item); CompositionRect& sr = dynamic_cast<CompositionItemImpl*>((_CompositionItem*)item)->getCompRect(); computeRepeatMarks(sr, s);}void CompositionModelImpl::computeRepeatMarks(CompositionRect& sr, const Segment* s){ if (s->isRepeating()) { timeT startTime = s->getStartTime(); timeT endTime = s->getEndMarkerTime(); timeT repeatInterval = endTime - startTime; if (repeatInterval <= 0) { // std::cerr << "WARNING: CompositionModelImpl::computeRepeatMarks: Segment at " << startTime << " has repeatInterval " << repeatInterval << std::endl; // std::cerr << kdBacktrace() << std::endl; return ; } timeT repeatStart = endTime; timeT repeatEnd = s->getRepeatEndTime(); sr.setWidth(int(nearbyint(m_grid.getRulerScale()->getWidthForDuration(startTime, repeatEnd - startTime)))); CompositionRect::repeatmarks repeatMarks; // RG_DEBUG << "CompositionModelImpl::computeRepeatMarks : repeatStart = " // << repeatStart << " - repeatEnd = " << repeatEnd << endl; for (timeT repeatMark = repeatStart; repeatMark < repeatEnd; repeatMark += repeatInterval) { int mark = int(nearbyint(m_grid.getRulerScale()->getXForTime(repeatMark))); // RG_DEBUG << "CompositionModelImpl::computeRepeatMarks : mark at " << mark << endl; repeatMarks.push_back(mark); } sr.setRepeatMarks(repeatMarks); if (repeatMarks.size() > 0) sr.setBaseWidth(repeatMarks[0] - sr.x()); else { // RG_DEBUG << "CompositionModelImpl::computeRepeatMarks : no repeat marks\n"; sr.setBaseWidth(sr.width()); } // RG_DEBUG << "CompositionModelImpl::computeRepeatMarks : s = " // << s << " base width = " << sr.getBaseWidth() // << " - nb repeat marks = " << repeatMarks.size() << endl; }}void CompositionModelImpl::setAudioPreviewThread(AudioPreviewThread *thread){ // std::cerr << "\nCompositionModelImpl::setAudioPreviewThread()\n" << std::endl; while (!m_audioPreviewUpdaterMap.empty()) { // Cause any running previews to be cancelled delete m_audioPreviewUpdaterMap.begin()->second; m_audioPreviewUpdaterMap.erase(m_audioPreviewUpdaterMap.begin()); } m_audioPreviewThread = thread;}void CompositionModelImpl::clearPreviewCache(){ RG_DEBUG << "CompositionModelImpl::clearPreviewCache\n"; m_notationPreviewDataCache.clear(); m_audioPreviewDataCache.clear(); m_audioSegmentPreviewMap.clear(); for (AudioPreviewUpdaterMap::iterator i = m_audioPreviewUpdaterMap.begin(); i != m_audioPreviewUpdaterMap.end(); ++i) { i->second->cancel(); } const Composition::segmentcontainer& segments = m_composition.getSegments(); Composition::segmentcontainer::iterator segEnd = segments.end(); for (Composition::segmentcontainer::iterator i = segments.begin(); i != segEnd; ++i) { if ((*i)->getType() == Segment::Audio) { // This will create the audio preview updater. The // preview won't be calculated and cached until the // updater completes and calls back. updatePreviewCacheForAudioSegment((*i), 0); } }}void CompositionModelImpl::updatePreviewCacheForNotationSegment(const Segment* segment, rectlist* npData){ npData->clear(); int segStartX = int(nearbyint(m_grid.getRulerScale()->getXForTime(segment->getStartTime()))); bool isPercussion = false; Track *track = m_composition.getTrackById(segment->getTrack()); if (track) { InstrumentId iid = track->getInstrument(); Instrument *instrument = m_studio.getInstrumentById(iid); if (instrument && instrument->isPercussion()) isPercussion = true; } for (Segment::iterator i = segment->begin(); i != segment->end(); ++i) { long pitch = 0; if (!(*i)->isa(Note::EventType) || !(*i)->get<Int>(BaseProperties::PITCH, pitch)) { continue; } timeT eventStart = (*i)->getAbsoluteTime(); timeT eventEnd = eventStart + (*i)->getDuration(); // if (eventEnd > segment->getEndMarkerTime()) { // eventEnd = segment->getEndMarkerTime(); // } int x = int(nearbyint(m_grid.getRulerScale()->getXForTime(eventStart))); int width = int(nearbyint(m_grid.getRulerScale()->getWidthForDuration(eventStart, eventEnd - eventStart))); if (x <= segStartX) { ++x; if (width > 1) --width; } if (width > 1) --width; if (width < 1) ++width; double y0 = 0; double y1 = m_grid.getYSnap(); double y = y1 + ((y0 - y1) * (pitch - 16)) / 96; int height = 2; if (isPercussion) { height = 3; if (width > 2) width = 2; } if (y < y0) y = y0; if (y > y1 - height + 1) y = y1 - height + 1; QRect r(x, (int)y, width, height); // RG_DEBUG << "CompositionModelImpl::updatePreviewCacheForNotationSegment() : npData = " // << npData << ", preview rect = " // << r << endl; npData->push_back(r); }}QColor CompositionModelImpl::computeSegmentPreviewColor(const Segment* segment){ // compute the preview color so it's as visible as possible over the segment's color QColor segColor = GUIPalette::convertColour(m_composition.getSegmentColourMap().getColourByIndex(segment->getColourIndex())); int h, s, v; segColor.hsv(&h, &s, &v); // colors with saturation lower than value should be pastel tints, and // they get a value of 0; yellow and green hues close to the dead center
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -