annotate data/model/ImageModel.h @ 314:70a232b1f12a

* Make XmlExportable::toXml the function that is universally overridden (and pure virtual) instead of toXmlString. Tidies up some classes, notably the model classes, significantly. Closes #1794561.
author Chris Cannam
date Thu, 18 Oct 2007 10:15:07 +0000
parents 726b32522e3f
children 7a4bd2c8585c
rev   line source
Chris@302 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@302 2
Chris@302 3 /*
Chris@302 4 Sonic Visualiser
Chris@302 5 An audio file viewer and annotation editor.
Chris@302 6 Centre for Digital Music, Queen Mary, University of London.
Chris@302 7 This file copyright 2006-2007 Chris Cannam and QMUL.
Chris@302 8
Chris@302 9 This program is free software; you can redistribute it and/or
Chris@302 10 modify it under the terms of the GNU General Public License as
Chris@302 11 published by the Free Software Foundation; either version 2 of the
Chris@302 12 License, or (at your option) any later version. See the file
Chris@302 13 COPYING included with this distribution for more information.
Chris@302 14 */
Chris@302 15
Chris@302 16 #ifndef _IMAGE_MODEL_H_
Chris@302 17 #define _IMAGE_MODEL_H_
Chris@302 18
Chris@302 19 #include "SparseModel.h"
Chris@302 20 #include "base/XmlExportable.h"
Chris@302 21 #include "base/RealTime.h"
Chris@302 22
Chris@302 23 /**
Chris@302 24 * Image point type for use in a SparseModel. This represents an
Chris@302 25 * image, identified by filename, at a given time. The filename can
Chris@302 26 * be empty, in which case we instead have a space to put an image in.
Chris@302 27 */
Chris@302 28
Chris@302 29 struct ImagePoint : public XmlExportable
Chris@302 30 {
Chris@302 31 public:
Chris@302 32 ImagePoint(long _frame) : frame(_frame) { }
Chris@302 33 ImagePoint(long _frame, QString _image, QString _label) :
Chris@302 34 frame(_frame), image(_image), label(_label) { }
Chris@302 35
Chris@302 36 int getDimensions() const { return 1; }
Chris@302 37
Chris@302 38 long frame;
Chris@302 39 QString image;
Chris@302 40 QString label;
Chris@302 41
Chris@314 42 void toXml(QTextStream &stream,
Chris@314 43 QString indent = "",
Chris@314 44 QString extraAttributes = "") const
Chris@302 45 {
Chris@314 46 stream <<
Chris@314 47 QString("%1<point frame=\"%2\" image=\"%3\" label=\"%4\" %5/>\n")
Chris@302 48 .arg(indent).arg(frame)
Chris@302 49 .arg(encodeEntities(image))
Chris@302 50 .arg(encodeEntities(label))
Chris@302 51 .arg(extraAttributes);
Chris@302 52 }
Chris@302 53
Chris@302 54 QString toDelimitedDataString(QString delimiter, size_t sampleRate) const
Chris@302 55 {
Chris@302 56 QStringList list;
Chris@302 57 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
Chris@302 58 list << image;
Chris@302 59 list << label;
Chris@302 60 return list.join(delimiter);
Chris@302 61 }
Chris@302 62
Chris@302 63 struct Comparator {
Chris@302 64 bool operator()(const ImagePoint &p1,
Chris@302 65 const ImagePoint &p2) const {
Chris@302 66 if (p1.frame != p2.frame) return p1.frame < p2.frame;
Chris@302 67 if (p1.label != p2.label) return p1.label < p2.label;
Chris@302 68 return p1.image < p2.image;
Chris@302 69 }
Chris@302 70 };
Chris@302 71
Chris@302 72 struct OrderComparator {
Chris@302 73 bool operator()(const ImagePoint &p1,
Chris@302 74 const ImagePoint &p2) const {
Chris@302 75 return p1.frame < p2.frame;
Chris@302 76 }
Chris@302 77 };
Chris@302 78 };
Chris@302 79
Chris@302 80
Chris@302 81 // Make this a class rather than a typedef so it can be predeclared.
Chris@302 82
Chris@302 83 class ImageModel : public SparseModel<ImagePoint>
Chris@302 84 {
Chris@302 85 public:
Chris@302 86 ImageModel(size_t sampleRate, size_t resolution, bool notifyOnAdd = true) :
Chris@302 87 SparseModel<ImagePoint>(sampleRate, resolution, notifyOnAdd)
Chris@302 88 { }
Chris@302 89
Chris@302 90 virtual void toXml(QTextStream &out,
Chris@302 91 QString indent = "",
Chris@302 92 QString extraAttributes = "") const
Chris@302 93 {
Chris@302 94 SparseModel<ImagePoint>::toXml
Chris@302 95 (out,
Chris@302 96 indent,
Chris@302 97 QString("%1 subtype=\"image\"")
Chris@302 98 .arg(extraAttributes));
Chris@302 99 }
Chris@302 100
Chris@302 101 /**
Chris@302 102 * Command to change the image for a point.
Chris@302 103 */
Chris@302 104 class ChangeImageCommand : public Command
Chris@302 105 {
Chris@302 106 public:
Chris@302 107 ChangeImageCommand(ImageModel *model,
Chris@302 108 const ImagePoint &point,
Chris@302 109 QString newImage,
Chris@302 110 QString newLabel) :
Chris@302 111 m_model(model), m_oldPoint(point), m_newPoint(point) {
Chris@302 112 m_newPoint.image = newImage;
Chris@302 113 m_newPoint.label = newLabel;
Chris@302 114 }
Chris@302 115
Chris@302 116 virtual QString getName() const { return tr("Edit Image"); }
Chris@302 117
Chris@302 118 virtual void execute() {
Chris@302 119 m_model->deletePoint(m_oldPoint);
Chris@302 120 m_model->addPoint(m_newPoint);
Chris@302 121 std::swap(m_oldPoint, m_newPoint);
Chris@302 122 }
Chris@302 123
Chris@302 124 virtual void unexecute() { execute(); }
Chris@302 125
Chris@302 126 private:
Chris@302 127 ImageModel *m_model;
Chris@302 128 ImagePoint m_oldPoint;
Chris@302 129 ImagePoint m_newPoint;
Chris@302 130 };
Chris@302 131 };
Chris@302 132
Chris@302 133
Chris@302 134 #endif
Chris@302 135
Chris@302 136
Chris@302 137