Chris@127: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@127: Chris@127: /* Chris@127: Sonic Visualiser Chris@127: An audio file viewer and annotation editor. Chris@127: Centre for Digital Music, Queen Mary, University of London. Chris@182: This file copyright 2006 Chris Cannam and QMUL. Chris@127: Chris@127: This program is free software; you can redistribute it and/or Chris@127: modify it under the terms of the GNU General Public License as Chris@127: published by the Free Software Foundation; either version 2 of the Chris@127: License, or (at your option) any later version. See the file Chris@127: COPYING included with this distribution for more information. Chris@127: */ Chris@127: Chris@127: #include "Layer.h" Chris@128: #include "view/View.h" Chris@128: #include "data/model/Model.h" Chris@268: #include "base/CommandHistory.h" Chris@127: Chris@127: #include Chris@127: Chris@131: #include Chris@267: #include Chris@131: Chris@131: #include "LayerFactory.h" Chris@128: #include "base/PlayParameterRepository.h" Chris@127: Chris@267: Layer::Layer() : Chris@267: m_haveDraggingRect(false) Chris@127: { Chris@127: } Chris@127: Chris@127: Layer::~Layer() Chris@127: { Chris@127: // std::cerr << "Layer::~Layer(" << this << ")" << std::endl; Chris@127: } Chris@127: Chris@127: QString Chris@127: Layer::getPropertyContainerIconName() const Chris@127: { Chris@127: return LayerFactory::getInstance()->getLayerIconName Chris@127: (LayerFactory::getInstance()->getLayerType(this)); Chris@127: } Chris@127: Chris@127: QString Chris@127: Layer::getLayerPresentationName() const Chris@127: { Chris@203: // QString layerName = objectName(); Chris@203: Chris@203: LayerFactory *factory = LayerFactory::getInstance(); Chris@203: QString layerName = factory->getLayerPresentationName Chris@203: (factory->getLayerType(this)); Chris@203: Chris@127: QString modelName; Chris@127: if (getModel()) modelName = getModel()->objectName(); Chris@127: Chris@127: QString text; Chris@127: if (modelName != "") { Chris@127: text = QString("%1: %2").arg(modelName).arg(layerName); Chris@127: } else { Chris@127: text = layerName; Chris@127: } Chris@127: Chris@127: return text; Chris@127: } Chris@127: Chris@127: void Chris@127: Layer::setObjectName(const QString &name) Chris@127: { Chris@127: QObject::setObjectName(name); Chris@127: emit layerNameChanged(); Chris@127: } Chris@127: Chris@127: PlayParameters * Chris@127: Layer::getPlayParameters() Chris@127: { Chris@127: // std::cerr << "Layer (" << this << ", " << objectName().toStdString() << ")::getPlayParameters: model is "<< getModel() << std::endl; Chris@127: const Model *model = getModel(); Chris@127: if (model) { Chris@127: return PlayParameterRepository::getInstance()->getPlayParameters(model); Chris@127: } Chris@127: return 0; Chris@127: } Chris@127: Chris@127: void Chris@131: Layer::setLayerDormant(const View *v, bool dormant) Chris@131: { Chris@131: const void *vv = (const void *)v; Chris@131: QMutexLocker locker(&m_dormancyMutex); Chris@131: m_dormancy[vv] = dormant; Chris@131: } Chris@131: Chris@131: bool Chris@131: Layer::isLayerDormant(const View *v) const Chris@131: { Chris@131: const void *vv = (const void *)v; Chris@131: QMutexLocker locker(&m_dormancyMutex); Chris@131: if (m_dormancy.find(vv) == m_dormancy.end()) return false; Chris@131: return m_dormancy.find(vv)->second; Chris@131: } Chris@131: Chris@131: void Chris@127: Layer::showLayer(View *view, bool show) Chris@127: { Chris@127: setLayerDormant(view, !show); Chris@127: emit layerParametersChanged(); Chris@127: } Chris@127: Chris@260: bool Chris@267: Layer::getXScaleValue(const View *v, int x, float &value, QString &unit) const Chris@260: { Chris@260: if (!hasTimeXAxis()) return false; Chris@260: Chris@260: const Model *m = getModel(); Chris@260: if (!m) return false; Chris@260: Chris@260: value = float(v->getFrameForX(x)) / m->getSampleRate(); Chris@260: unit = "s"; Chris@260: return true; Chris@260: } Chris@260: Chris@268: bool Chris@268: Layer::MeasureRect::operator<(const MeasureRect &mr) const Chris@268: { Chris@268: if (haveFrames) { Chris@268: if (startFrame == mr.startFrame) { Chris@268: if (endFrame != mr.endFrame) { Chris@268: return endFrame < mr.endFrame; Chris@268: } Chris@268: } else { Chris@268: return startFrame < mr.startFrame; Chris@268: } Chris@268: } else { Chris@268: if (pixrect.x() == mr.pixrect.x()) { Chris@268: if (pixrect.width() != mr.pixrect.width()) { Chris@268: return pixrect.width() < mr.pixrect.width(); Chris@268: } Chris@268: } else { Chris@268: return pixrect.x() < mr.pixrect.x(); Chris@268: } Chris@268: } Chris@268: Chris@268: // the two rects are equal in x and width Chris@268: Chris@268: if (pixrect.y() == mr.pixrect.y()) { Chris@268: return pixrect.height() < mr.pixrect.height(); Chris@268: } else { Chris@268: return pixrect.y() < mr.pixrect.y(); Chris@268: } Chris@268: } Chris@268: Chris@268: QString Chris@269: Layer::MeasureRect::toXmlString(QString indent) const Chris@269: { Chris@269: QString s; Chris@269: Chris@269: s += indent; Chris@269: s += QString("\n") Chris@269: .arg(pixrect.y()).arg(pixrect.y() + pixrect.height()); Chris@269: Chris@269: return s; Chris@269: } Chris@269: Chris@269: void Chris@269: Layer::addMeasurementRect(const QXmlAttributes &attributes) Chris@269: { Chris@269: MeasureRect rect; Chris@269: QString fs = attributes.value("startFrame"); Chris@269: int x0 = 0, y0 = 0, x1 = 0, y1 = 0; Chris@269: if (fs != "") { Chris@269: rect.startFrame = fs.toLong(); Chris@269: rect.endFrame = attributes.value("endFrame").toLong(); Chris@269: rect.haveFrames = true; Chris@269: } else { Chris@269: x0 = attributes.value("startX").toInt(); Chris@269: x1 = attributes.value("endX").toInt(); Chris@269: rect.haveFrames = false; Chris@269: } Chris@269: y0 = attributes.value("startY").toInt(); Chris@269: y1 = attributes.value("endY").toInt(); Chris@269: rect.pixrect = QRect(x0, y0, x1 - x0, y1 - y0); Chris@269: addMeasureRectToSet(rect); Chris@269: } Chris@269: Chris@269: QString Chris@268: Layer::AddMeasurementRectCommand::getName() const Chris@268: { Chris@268: return tr("Make Measurement"); Chris@268: } Chris@268: Chris@268: void Chris@268: Layer::AddMeasurementRectCommand::execute() Chris@268: { Chris@269: m_layer->addMeasureRectToSet(m_rect); Chris@268: } Chris@268: Chris@268: void Chris@268: Layer::AddMeasurementRectCommand::unexecute() Chris@268: { Chris@269: m_layer->deleteMeasureRectFromSet(m_rect); Chris@268: } Chris@268: Chris@267: void Chris@267: Layer::measureStart(View *v, QMouseEvent *e) Chris@267: { Chris@267: m_draggingRect.pixrect = QRect(e->x(), e->y(), 0, 0); Chris@267: if (hasTimeXAxis()) { Chris@268: m_draggingRect.haveFrames = true; Chris@267: m_draggingRect.startFrame = v->getFrameForX(e->x()); Chris@267: m_draggingRect.endFrame = m_draggingRect.startFrame; Chris@268: } else { Chris@268: m_draggingRect.haveFrames = false; Chris@267: } Chris@267: m_haveDraggingRect = true; Chris@267: } Chris@267: Chris@267: void Chris@267: Layer::measureDrag(View *v, QMouseEvent *e) Chris@267: { Chris@267: if (!m_haveDraggingRect) return; Chris@268: Chris@267: m_draggingRect.pixrect = QRect(m_draggingRect.pixrect.x(), Chris@267: m_draggingRect.pixrect.y(), Chris@267: e->x() - m_draggingRect.pixrect.x(), Chris@268: e->y() - m_draggingRect.pixrect.y()) Chris@268: .normalized(); Chris@268: Chris@267: if (hasTimeXAxis()) { Chris@267: m_draggingRect.endFrame = v->getFrameForX(e->x()); Chris@267: } Chris@267: } Chris@267: Chris@267: void Chris@267: Layer::measureEnd(View *v, QMouseEvent *e) Chris@267: { Chris@267: if (!m_haveDraggingRect) return; Chris@267: measureDrag(v, e); Chris@268: Chris@268: CommandHistory::getInstance()->addCommand Chris@268: (new AddMeasurementRectCommand(this, m_draggingRect)); Chris@268: Chris@267: m_haveDraggingRect = false; Chris@267: } Chris@267: Chris@267: void Chris@267: Layer::paintMeasurementRects(View *v, QPainter &paint) const Chris@267: { Chris@267: if (m_haveDraggingRect) { Chris@268: paintMeasurementRect(v, paint, m_draggingRect); Chris@267: } Chris@267: Chris@268: for (MeasureRectSet::const_iterator i = m_measureRects.begin(); Chris@268: i != m_measureRects.end(); ++i) { Chris@268: paintMeasurementRect(v, paint, *i); Chris@267: } Chris@267: } Chris@267: Chris@268: void Chris@269: Layer::paintMeasurementRect(View *v, QPainter &paint, const MeasureRect &r) const Chris@268: { Chris@268: if (r.haveFrames) { Chris@268: Chris@268: int x0 = -1; Chris@268: int x1 = v->width() + 1; Chris@268: Chris@268: if (r.startFrame >= v->getStartFrame()) { Chris@268: x0 = v->getXForFrame(r.startFrame); Chris@268: } Chris@268: if (r.endFrame <= v->getEndFrame()) { Chris@268: x1 = v->getXForFrame(r.endFrame); Chris@268: } Chris@268: Chris@268: QRect pr = QRect(x0, r.pixrect.y(), Chris@268: x1 - x0, r.pixrect.height()); Chris@268: Chris@268: r.pixrect = pr; Chris@268: } Chris@268: Chris@268: v->drawMeasurementRect(paint, this, r.pixrect); Chris@268: } Chris@268: Chris@268: QString Chris@268: Layer::toXmlString(QString indent, QString extraAttributes) const Chris@268: { Chris@268: QString s; Chris@268: Chris@268: s += indent; Chris@268: Chris@269: s += QString("getLayerTypeName Chris@268: (LayerFactory::getInstance()->getLayerType(this)))) Chris@268: .arg(getObjectExportId(this)) Chris@268: .arg(encodeEntities(objectName())) Chris@268: .arg(getObjectExportId(getModel())) Chris@268: .arg(extraAttributes); Chris@268: Chris@269: if (m_measureRects.empty()) { Chris@269: s += QString("/>\n"); Chris@269: return s; Chris@269: } Chris@269: Chris@269: s += QString(">\n"); Chris@269: Chris@269: for (MeasureRectSet::const_iterator i = m_measureRects.begin(); Chris@269: i != m_measureRects.end(); ++i) { Chris@269: s += i->toXmlString(indent + " "); Chris@269: } Chris@269: Chris@269: s += QString("\n"); Chris@269: Chris@268: return s; Chris@268: } Chris@269: Chris@269: QString Chris@269: Layer::toBriefXmlString(QString indent, QString extraAttributes) const Chris@269: { Chris@269: QString s; Chris@269: Chris@269: s += indent; Chris@269: Chris@269: s += QString("\n") Chris@269: .arg(encodeEntities(LayerFactory::getInstance()->getLayerTypeName Chris@269: (LayerFactory::getInstance()->getLayerType(this)))) Chris@269: .arg(getObjectExportId(this)) Chris@269: .arg(encodeEntities(objectName())) Chris@269: .arg(getObjectExportId(getModel())) Chris@269: .arg(extraAttributes); Chris@269: Chris@269: return s; Chris@269: } Chris@269: