annotate data/model/Model.cpp @ 323:a71dec01c4d3

* Some tidying up to handling of alignment; add alignment status label to pane; ensure alignment when dragging with mouse as well as when playing
author Chris Cannam
date Thu, 25 Oct 2007 14:32:23 +0000
parents 3ff8f571da09
children 1afaf98dbf11
rev   line source
Chris@150 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@150 2
Chris@150 3 /*
Chris@150 4 Sonic Visualiser
Chris@150 5 An audio file viewer and annotation editor.
Chris@150 6 Centre for Digital Music, Queen Mary, University of London.
Chris@150 7 This file copyright 2006 Chris Cannam.
Chris@150 8
Chris@150 9 This program is free software; you can redistribute it and/or
Chris@150 10 modify it under the terms of the GNU General Public License as
Chris@150 11 published by the Free Software Foundation; either version 2 of the
Chris@150 12 License, or (at your option) any later version. See the file
Chris@150 13 COPYING included with this distribution for more information.
Chris@150 14 */
Chris@150 15
Chris@150 16 #include "Model.h"
Chris@319 17 #include "AlignmentModel.h"
Chris@150 18 #include "base/PlayParameterRepository.h"
Chris@150 19
Chris@150 20 #include <QTextStream>
Chris@150 21
Chris@150 22 #include <iostream>
Chris@150 23
Chris@150 24 const int Model::COMPLETION_UNKNOWN = -1;
Chris@150 25
Chris@150 26 Model::~Model()
Chris@150 27 {
Chris@150 28 // std::cerr << "Model::~Model(" << this << ")" << std::endl;
Chris@150 29
Chris@319 30 if (!m_aboutToDelete) {
Chris@319 31 std::cerr << "NOTE: Model::~Model(" << this << ", \""
Chris@319 32 << objectName().toStdString() << "\"): Model deleted "
Chris@319 33 << "with no aboutToDelete notification" << std::endl;
Chris@319 34 }
Chris@319 35
Chris@319 36 if (m_alignment) {
Chris@319 37 m_alignment->aboutToDelete();
Chris@319 38 delete m_alignment;
Chris@319 39 }
Chris@319 40
Chris@150 41 // Subclasses have to handle adding themselves to the repository,
Chris@150 42 // if they want to be played. We can't do it from here because
Chris@150 43 // the repository would be unable to tell whether we were playable
Chris@150 44 // or not (because dynamic_cast won't work from the base class ctor)
Chris@150 45 PlayParameterRepository::getInstance()->removeModel(this);
Chris@150 46 }
Chris@150 47
Chris@150 48 void
Chris@319 49 Model::setSourceModel(Model *model)
Chris@319 50 {
Chris@319 51 if (m_sourceModel) {
Chris@319 52 disconnect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
Chris@319 53 this, SLOT(sourceModelAboutToBeDeleted()));
Chris@319 54 }
Chris@319 55
Chris@319 56 m_sourceModel = model;
Chris@319 57
Chris@319 58 if (m_sourceModel) {
Chris@319 59 connect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
Chris@319 60 this, SLOT(sourceModelAboutToBeDeleted()));
Chris@319 61 }
Chris@319 62 }
Chris@319 63
Chris@319 64 void
Chris@319 65 Model::aboutToDelete()
Chris@319 66 {
Chris@319 67 if (m_aboutToDelete) {
Chris@319 68 std::cerr << "WARNING: Model(" << this << ", \""
Chris@319 69 << objectName().toStdString() << "\")::aboutToDelete: "
Chris@319 70 << "aboutToDelete called more than once for the same model"
Chris@319 71 << std::endl;
Chris@319 72 }
Chris@319 73
Chris@319 74 emit aboutToBeDeleted();
Chris@319 75 m_aboutToDelete = true;
Chris@319 76 }
Chris@319 77
Chris@319 78 void
Chris@319 79 Model::sourceModelAboutToBeDeleted()
Chris@319 80 {
Chris@319 81 m_sourceModel = 0;
Chris@319 82 }
Chris@319 83
Chris@319 84 void
Chris@319 85 Model::setAlignment(AlignmentModel *alignment)
Chris@319 86 {
Chris@319 87 if (m_alignment) {
Chris@319 88 m_alignment->aboutToDelete();
Chris@319 89 delete m_alignment;
Chris@319 90 }
Chris@319 91 m_alignment = alignment;
Chris@319 92 connect(m_alignment, SIGNAL(completionChanged()),
Chris@319 93 this, SIGNAL(alignmentCompletionChanged()));
Chris@319 94 }
Chris@319 95
Chris@319 96 const Model *
Chris@319 97 Model::getAlignmentReference() const
Chris@319 98 {
Chris@323 99 if (!m_alignment) return this;
Chris@319 100 return m_alignment->getReferenceModel();
Chris@319 101 }
Chris@319 102
Chris@319 103 size_t
Chris@319 104 Model::alignToReference(size_t frame) const
Chris@319 105 {
Chris@319 106 if (!m_alignment) return frame;
Chris@319 107 return m_alignment->toReference(frame);
Chris@319 108 }
Chris@319 109
Chris@319 110 size_t
Chris@319 111 Model::alignFromReference(size_t refFrame) const
Chris@319 112 {
Chris@319 113 if (!m_alignment) return refFrame;
Chris@319 114 return m_alignment->fromReference(refFrame);
Chris@319 115 }
Chris@319 116
Chris@319 117 int
Chris@319 118 Model::getAlignmentCompletion() const
Chris@319 119 {
Chris@323 120 // std::cerr << "Model::getAlignmentCompletion" << std::endl;
Chris@319 121 if (!m_alignment) return 100;
Chris@319 122 int completion = 0;
Chris@319 123 (void)m_alignment->isReady(&completion);
Chris@323 124 // std::cerr << " -> " << completion << std::endl;
Chris@319 125 return completion;
Chris@319 126 }
Chris@319 127
Chris@319 128 void
Chris@150 129 Model::toXml(QTextStream &stream, QString indent,
Chris@150 130 QString extraAttributes) const
Chris@150 131 {
Chris@150 132 stream << indent;
Chris@150 133 stream << QString("<model id=\"%1\" name=\"%2\" sampleRate=\"%3\" start=\"%4\" end=\"%5\" %6/>\n")
Chris@150 134 .arg(getObjectExportId(this))
Chris@150 135 .arg(encodeEntities(objectName()))
Chris@150 136 .arg(getSampleRate())
Chris@150 137 .arg(getStartFrame())
Chris@150 138 .arg(getEndFrame())
Chris@150 139 .arg(extraAttributes);
Chris@150 140 }
Chris@150 141
Chris@150 142