annotate data/model/Model.cpp @ 961:fae5689f7e27

Frame this condition more defensively (from coverity scan)
author Chris Cannam
date Wed, 03 Sep 2014 09:57:30 +0100
parents 4b9fc70a37d5
children 4a2c150ecd67
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
Chris@150 19 #include <QTextStream>
Chris@150 20
Chris@150 21 #include <iostream>
Chris@150 22
Chris@150 23 const int Model::COMPLETION_UNKNOWN = -1;
Chris@150 24
Chris@150 25 Model::~Model()
Chris@150 26 {
Chris@690 27 // SVDEBUG << "Model::~Model(" << this << ")" << endl;
Chris@150 28
Chris@319 29 if (!m_aboutToDelete) {
Chris@690 30 SVDEBUG << "NOTE: Model::~Model(" << this << ", \""
Chris@686 31 << objectName() << "\"): Model deleted "
Chris@687 32 << "with no aboutToDelete notification" << endl;
Chris@319 33 }
Chris@319 34
Chris@319 35 if (m_alignment) {
Chris@319 36 m_alignment->aboutToDelete();
Chris@319 37 delete m_alignment;
Chris@319 38 }
Chris@150 39 }
Chris@150 40
Chris@150 41 void
Chris@319 42 Model::setSourceModel(Model *model)
Chris@319 43 {
Chris@319 44 if (m_sourceModel) {
Chris@319 45 disconnect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
Chris@319 46 this, SLOT(sourceModelAboutToBeDeleted()));
Chris@319 47 }
Chris@319 48
Chris@319 49 m_sourceModel = model;
Chris@319 50
Chris@319 51 if (m_sourceModel) {
Chris@333 52 connect(m_sourceModel, SIGNAL(alignmentCompletionChanged()),
Chris@333 53 this, SIGNAL(alignmentCompletionChanged()));
Chris@319 54 connect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
Chris@319 55 this, SLOT(sourceModelAboutToBeDeleted()));
Chris@319 56 }
Chris@319 57 }
Chris@319 58
Chris@319 59 void
Chris@319 60 Model::aboutToDelete()
Chris@319 61 {
Chris@843 62 // cerr << "Model(" << this << ")::aboutToDelete()" << endl;
Chris@360 63
Chris@319 64 if (m_aboutToDelete) {
Chris@843 65 cerr << "WARNING: Model(" << this << ", \""
Chris@686 66 << objectName() << "\")::aboutToDelete: "
Chris@319 67 << "aboutToDelete called more than once for the same model"
Chris@843 68 << endl;
Chris@319 69 }
Chris@319 70
Chris@319 71 emit aboutToBeDeleted();
Chris@319 72 m_aboutToDelete = true;
Chris@319 73 }
Chris@319 74
Chris@319 75 void
Chris@319 76 Model::sourceModelAboutToBeDeleted()
Chris@319 77 {
Chris@319 78 m_sourceModel = 0;
Chris@319 79 }
Chris@319 80
Chris@319 81 void
Chris@319 82 Model::setAlignment(AlignmentModel *alignment)
Chris@319 83 {
Chris@319 84 if (m_alignment) {
Chris@319 85 m_alignment->aboutToDelete();
Chris@319 86 delete m_alignment;
Chris@319 87 }
Chris@319 88 m_alignment = alignment;
Chris@319 89 connect(m_alignment, SIGNAL(completionChanged()),
Chris@319 90 this, SIGNAL(alignmentCompletionChanged()));
Chris@319 91 }
Chris@319 92
Chris@407 93 const AlignmentModel *
Chris@407 94 Model::getAlignment() const
Chris@407 95 {
Chris@407 96 return m_alignment;
Chris@407 97 }
Chris@407 98
Chris@319 99 const Model *
Chris@319 100 Model::getAlignmentReference() const
Chris@319 101 {
Chris@333 102 if (!m_alignment) {
Chris@333 103 if (m_sourceModel) return m_sourceModel->getAlignmentReference();
Chris@371 104 return 0;
Chris@333 105 }
Chris@319 106 return m_alignment->getReferenceModel();
Chris@319 107 }
Chris@319 108
Chris@929 109 int
Chris@929 110 Model::alignToReference(int frame) const
Chris@319 111 {
Chris@951 112 // cerr << "Model(" << this << ")::alignToReference(" << frame << ")" << endl;
Chris@333 113 if (!m_alignment) {
Chris@333 114 if (m_sourceModel) return m_sourceModel->alignToReference(frame);
Chris@333 115 else return frame;
Chris@333 116 }
Chris@929 117 int refFrame = m_alignment->toReference(frame);
Chris@340 118 const Model *m = m_alignment->getReferenceModel();
Chris@340 119 if (m && refFrame > m->getEndFrame()) refFrame = m->getEndFrame();
Chris@951 120 // cerr << "have alignment, aligned is " << refFrame << endl;
Chris@333 121 return refFrame;
Chris@319 122 }
Chris@319 123
Chris@929 124 int
Chris@929 125 Model::alignFromReference(int refFrame) const
Chris@319 126 {
Chris@951 127 // cerr << "Model(" << this << ")::alignFromReference(" << refFrame << ")" << endl;
Chris@333 128 if (!m_alignment) {
Chris@333 129 if (m_sourceModel) return m_sourceModel->alignFromReference(refFrame);
Chris@333 130 else return refFrame;
Chris@333 131 }
Chris@929 132 int frame = m_alignment->fromReference(refFrame);
Chris@340 133 if (frame > getEndFrame()) frame = getEndFrame();
Chris@951 134 // cerr << "have alignment, aligned is " << frame << endl;
Chris@333 135 return frame;
Chris@319 136 }
Chris@319 137
Chris@319 138 int
Chris@319 139 Model::getAlignmentCompletion() const
Chris@319 140 {
Chris@690 141 // SVDEBUG << "Model::getAlignmentCompletion" << endl;
Chris@333 142 if (!m_alignment) {
Chris@333 143 if (m_sourceModel) return m_sourceModel->getAlignmentCompletion();
Chris@333 144 else return 100;
Chris@333 145 }
Chris@319 146 int completion = 0;
Chris@319 147 (void)m_alignment->isReady(&completion);
Chris@843 148 // cerr << " -> " << completion << endl;
Chris@319 149 return completion;
Chris@319 150 }
Chris@319 151
Chris@333 152 QString
Chris@333 153 Model::getTitle() const
Chris@333 154 {
Chris@333 155 if (m_sourceModel) return m_sourceModel->getTitle();
Chris@345 156 else return "";
Chris@333 157 }
Chris@333 158
Chris@333 159 QString
Chris@333 160 Model::getMaker() const
Chris@333 161 {
Chris@333 162 if (m_sourceModel) return m_sourceModel->getMaker();
Chris@345 163 else return "";
Chris@345 164 }
Chris@345 165
Chris@345 166 QString
Chris@345 167 Model::getLocation() const
Chris@345 168 {
Chris@345 169 if (m_sourceModel) return m_sourceModel->getLocation();
Chris@345 170 else return "";
Chris@333 171 }
Chris@333 172
Chris@319 173 void
Chris@150 174 Model::toXml(QTextStream &stream, QString indent,
Chris@150 175 QString extraAttributes) const
Chris@150 176 {
Chris@150 177 stream << indent;
Chris@150 178 stream << QString("<model id=\"%1\" name=\"%2\" sampleRate=\"%3\" start=\"%4\" end=\"%5\" %6/>\n")
Chris@150 179 .arg(getObjectExportId(this))
Chris@150 180 .arg(encodeEntities(objectName()))
Chris@150 181 .arg(getSampleRate())
Chris@150 182 .arg(getStartFrame())
Chris@150 183 .arg(getEndFrame())
Chris@150 184 .arg(extraAttributes);
Chris@150 185 }
Chris@150 186
Chris@150 187