annotate data/model/Model.cpp @ 1752:6d09d68165a4 by-id

Further review of ById: make IDs only available when adding a model to the ById store, not by querying the item directly. This means any id encountered in the wild must have been added to the store at some point (even if later released), which simplifies reasoning about lifecycles
author Chris Cannam
date Fri, 05 Jul 2019 15:28:07 +0100
parents 565575463752
children 8529763e2258
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@1696 23 //#define DEBUG_COMPLETION 1
Chris@1696 24
Chris@150 25 Model::~Model()
Chris@150 26 {
Chris@1739 27 SVDEBUG << "Model::~Model: " << this << " with id " << getId() << endl;
Chris@1739 28 /*!!!
Chris@319 29 if (!m_aboutToDelete) {
Chris@1384 30 SVDEBUG << "NOTE: Model(" << this << ", \""
Chris@1384 31 << objectName() << "\", type uri <"
Chris@1384 32 << m_typeUri << ">)::~Model(): Model deleted "
Chris@1384 33 << "with no aboutToDelete notification"
Chris@1384 34 << endl;
Chris@319 35 }
Chris@1739 36 */
Chris@1739 37 //!!! see notes in header - sort this out
Chris@1739 38 /*
Chris@1735 39 if (!m_alignmentModel.isNone()) {
Chris@1735 40 ModelById::release(m_alignmentModel);
Chris@319 41 }
Chris@1739 42 */
Chris@150 43 }
Chris@150 44
Chris@150 45 void
Chris@1735 46 Model::setSourceModel(ModelId modelId)
Chris@319 47 {
Chris@1735 48 /*!!!
Chris@319 49 if (m_sourceModel) {
Chris@319 50 disconnect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
Chris@319 51 this, SLOT(sourceModelAboutToBeDeleted()));
Chris@319 52 }
Chris@1735 53 */
Chris@1735 54
Chris@1735 55 m_sourceModel = modelId;
Chris@319 56
Chris@1735 57 auto model = ModelById::get(m_sourceModel);
Chris@1735 58 if (model) {
Chris@1752 59 connect(model.get(), SIGNAL(alignmentCompletionChanged(ModelId)),
Chris@1752 60 this, SIGNAL(alignmentCompletionChanged(ModelId)));
Chris@1735 61 }
Chris@1735 62
Chris@1735 63
Chris@1735 64 /*
Chris@319 65 if (m_sourceModel) {
Chris@333 66 connect(m_sourceModel, SIGNAL(alignmentCompletionChanged()),
Chris@333 67 this, SIGNAL(alignmentCompletionChanged()));
Chris@319 68 connect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
Chris@319 69 this, SLOT(sourceModelAboutToBeDeleted()));
Chris@319 70 }
Chris@1735 71 */
Chris@319 72 }
Chris@1735 73 /*!!!
Chris@319 74 void
Chris@319 75 Model::aboutToDelete()
Chris@319 76 {
Chris@1668 77 SVDEBUG << "Model(" << this << ", \""
Chris@1668 78 << objectName() << "\", type name \""
Chris@1668 79 << getTypeName() << "\", type uri <"
Chris@1668 80 << m_typeUri << ">)::aboutToDelete()" << endl;
Chris@360 81
Chris@319 82 if (m_aboutToDelete) {
Chris@1384 83 SVDEBUG << "WARNING: Model(" << this << ", \""
Chris@1384 84 << objectName() << "\", type uri <"
Chris@1384 85 << m_typeUri << ">)::aboutToDelete: "
Chris@1384 86 << "aboutToDelete called more than once for the same model"
Chris@1384 87 << endl;
Chris@319 88 }
Chris@319 89
Chris@319 90 emit aboutToBeDeleted();
Chris@319 91 m_aboutToDelete = true;
Chris@319 92 }
Chris@319 93
Chris@319 94 void
Chris@319 95 Model::sourceModelAboutToBeDeleted()
Chris@319 96 {
Chris@1582 97 m_sourceModel = nullptr;
Chris@319 98 }
Chris@1735 99 */
Chris@319 100 void
Chris@1735 101 Model::setAlignment(ModelId alignmentModel)
Chris@319 102 {
Chris@1668 103 SVDEBUG << "Model(" << this << "): accepting alignment model "
Chris@1735 104 << alignmentModel << endl;
Chris@1668 105
Chris@1735 106 if (!m_alignmentModel.isNone()) {
Chris@1735 107 ModelById::release(m_alignmentModel);
Chris@319 108 }
Chris@1018 109
Chris@1735 110 m_alignmentModel = alignmentModel;
Chris@1018 111
Chris@1735 112 auto model = ModelById::get(m_alignmentModel);
Chris@1735 113 if (model) {
Chris@1735 114 connect(model.get(), SIGNAL(completionChanged()),
Chris@1018 115 this, SIGNAL(alignmentCompletionChanged()));
Chris@1018 116 }
Chris@319 117 }
Chris@319 118
Chris@1735 119 const ModelId
Chris@407 120 Model::getAlignment() const
Chris@407 121 {
Chris@1735 122 return m_alignmentModel;
Chris@407 123 }
Chris@407 124
Chris@1735 125 const ModelId
Chris@319 126 Model::getAlignmentReference() const
Chris@319 127 {
Chris@1735 128 auto model = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1735 129 if (model) return model->getReferenceModel();
Chris@1735 130 else return {};
Chris@319 131 }
Chris@319 132
Chris@1038 133 sv_frame_t
Chris@1038 134 Model::alignToReference(sv_frame_t frame) const
Chris@319 135 {
Chris@1739 136 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1739 137
Chris@1739 138 if (!alignmentModel) {
Chris@1739 139 auto sourceModel = ModelById::get(m_sourceModel);
Chris@1739 140 if (sourceModel) {
Chris@1739 141 return sourceModel->alignToReference(frame);
Chris@1739 142 }
Chris@1739 143 return frame;
Chris@333 144 }
Chris@1739 145
Chris@1739 146 sv_frame_t refFrame = alignmentModel->toReference(frame);
Chris@1739 147 auto refModel = ModelById::get(alignmentModel->getReferenceModel());
Chris@1739 148 if (refModel && refFrame > refModel->getEndFrame()) {
Chris@1739 149 refFrame = refModel->getEndFrame();
Chris@1739 150 }
Chris@333 151 return refFrame;
Chris@319 152 }
Chris@319 153
Chris@1038 154 sv_frame_t
Chris@1038 155 Model::alignFromReference(sv_frame_t refFrame) const
Chris@319 156 {
Chris@1739 157 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1739 158
Chris@1739 159 if (!alignmentModel) {
Chris@1739 160 auto sourceModel = ModelById::get(m_sourceModel);
Chris@1739 161 if (sourceModel) {
Chris@1739 162 return sourceModel->alignFromReference(refFrame);
Chris@1739 163 }
Chris@1739 164 return refFrame;
Chris@333 165 }
Chris@1739 166
Chris@1739 167 sv_frame_t frame = alignmentModel->fromReference(refFrame);
Chris@340 168 if (frame > getEndFrame()) frame = getEndFrame();
Chris@333 169 return frame;
Chris@319 170 }
Chris@319 171
Chris@319 172 int
Chris@319 173 Model::getAlignmentCompletion() const
Chris@319 174 {
Chris@1696 175 #ifdef DEBUG_COMPLETION
Chris@1739 176 SVCERR << "Model(" << this
Chris@1739 177 << ")::getAlignmentCompletion: m_alignmentModel = "
Chris@1739 178 << m_alignmentModel << endl;
Chris@1696 179 #endif
Chris@1739 180
Chris@1739 181 auto alignmentModel = ModelById::getAs<AlignmentModel>(m_alignmentModel);
Chris@1739 182
Chris@1739 183 if (!alignmentModel) {
Chris@1739 184 auto sourceModel = ModelById::get(m_sourceModel);
Chris@1739 185 if (sourceModel) {
Chris@1739 186 return sourceModel->getAlignmentCompletion();
Chris@1739 187 }
Chris@1739 188 return 100;
Chris@333 189 }
Chris@1739 190
Chris@319 191 int completion = 0;
Chris@1739 192 (void)alignmentModel->isReady(&completion);
Chris@1696 193 #ifdef DEBUG_COMPLETION
Chris@1739 194 SVCERR << "Model(" << this
Chris@1739 195 << ")::getAlignmentCompletion: completion = " << completion
Chris@1696 196 << endl;
Chris@1696 197 #endif
Chris@319 198 return completion;
Chris@319 199 }
Chris@319 200
Chris@333 201 QString
Chris@333 202 Model::getTitle() const
Chris@333 203 {
Chris@1739 204 auto source = ModelById::get(m_sourceModel);
Chris@1739 205 if (source) return source->getTitle();
Chris@345 206 else return "";
Chris@333 207 }
Chris@333 208
Chris@333 209 QString
Chris@333 210 Model::getMaker() const
Chris@333 211 {
Chris@1739 212 auto source = ModelById::get(m_sourceModel);
Chris@1739 213 if (source) return source->getMaker();
Chris@345 214 else return "";
Chris@345 215 }
Chris@345 216
Chris@345 217 QString
Chris@345 218 Model::getLocation() const
Chris@345 219 {
Chris@1739 220 auto source = ModelById::get(m_sourceModel);
Chris@1739 221 if (source) return source->getLocation();
Chris@345 222 else return "";
Chris@333 223 }
Chris@333 224
Chris@319 225 void
Chris@150 226 Model::toXml(QTextStream &stream, QString indent,
Chris@150 227 QString extraAttributes) const
Chris@150 228 {
Chris@150 229 stream << indent;
Chris@150 230 stream << QString("<model id=\"%1\" name=\"%2\" sampleRate=\"%3\" start=\"%4\" end=\"%5\" %6/>\n")
Chris@1677 231 .arg(getExportId())
Chris@1429 232 .arg(encodeEntities(objectName()))
Chris@1429 233 .arg(getSampleRate())
Chris@1429 234 .arg(getStartFrame())
Chris@1429 235 .arg(getEndFrame())
Chris@1429 236 .arg(extraAttributes);
Chris@150 237 }
Chris@150 238
Chris@150 239