annotate data/model/ImageModel.h @ 302:726b32522e3f

* Phase 1 of an image layer.
author Chris Cannam
date Thu, 04 Oct 2007 16:34:11 +0000
parents
children 70a232b1f12a
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@302 42 QString toXmlString(QString indent = "",
Chris@302 43 QString extraAttributes = "") const
Chris@302 44 {
Chris@302 45 return QString("%1<point frame=\"%2\" image=\"%3\" label=\"%4\" %5/>\n")
Chris@302 46 .arg(indent).arg(frame)
Chris@302 47 .arg(encodeEntities(image))
Chris@302 48 .arg(encodeEntities(label))
Chris@302 49 .arg(extraAttributes);
Chris@302 50 }
Chris@302 51
Chris@302 52 QString toDelimitedDataString(QString delimiter, size_t sampleRate) const
Chris@302 53 {
Chris@302 54 QStringList list;
Chris@302 55 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
Chris@302 56 list << image;
Chris@302 57 list << label;
Chris@302 58 return list.join(delimiter);
Chris@302 59 }
Chris@302 60
Chris@302 61 struct Comparator {
Chris@302 62 bool operator()(const ImagePoint &p1,
Chris@302 63 const ImagePoint &p2) const {
Chris@302 64 if (p1.frame != p2.frame) return p1.frame < p2.frame;
Chris@302 65 if (p1.label != p2.label) return p1.label < p2.label;
Chris@302 66 return p1.image < p2.image;
Chris@302 67 }
Chris@302 68 };
Chris@302 69
Chris@302 70 struct OrderComparator {
Chris@302 71 bool operator()(const ImagePoint &p1,
Chris@302 72 const ImagePoint &p2) const {
Chris@302 73 return p1.frame < p2.frame;
Chris@302 74 }
Chris@302 75 };
Chris@302 76 };
Chris@302 77
Chris@302 78
Chris@302 79 // Make this a class rather than a typedef so it can be predeclared.
Chris@302 80
Chris@302 81 class ImageModel : public SparseModel<ImagePoint>
Chris@302 82 {
Chris@302 83 public:
Chris@302 84 ImageModel(size_t sampleRate, size_t resolution, bool notifyOnAdd = true) :
Chris@302 85 SparseModel<ImagePoint>(sampleRate, resolution, notifyOnAdd)
Chris@302 86 { }
Chris@302 87
Chris@302 88 virtual void toXml(QTextStream &out,
Chris@302 89 QString indent = "",
Chris@302 90 QString extraAttributes = "") const
Chris@302 91 {
Chris@302 92 SparseModel<ImagePoint>::toXml
Chris@302 93 (out,
Chris@302 94 indent,
Chris@302 95 QString("%1 subtype=\"image\"")
Chris@302 96 .arg(extraAttributes));
Chris@302 97 }
Chris@302 98
Chris@302 99 /**
Chris@302 100 * Command to change the image for a point.
Chris@302 101 */
Chris@302 102 class ChangeImageCommand : public Command
Chris@302 103 {
Chris@302 104 public:
Chris@302 105 ChangeImageCommand(ImageModel *model,
Chris@302 106 const ImagePoint &point,
Chris@302 107 QString newImage,
Chris@302 108 QString newLabel) :
Chris@302 109 m_model(model), m_oldPoint(point), m_newPoint(point) {
Chris@302 110 m_newPoint.image = newImage;
Chris@302 111 m_newPoint.label = newLabel;
Chris@302 112 }
Chris@302 113
Chris@302 114 virtual QString getName() const { return tr("Edit Image"); }
Chris@302 115
Chris@302 116 virtual void execute() {
Chris@302 117 m_model->deletePoint(m_oldPoint);
Chris@302 118 m_model->addPoint(m_newPoint);
Chris@302 119 std::swap(m_oldPoint, m_newPoint);
Chris@302 120 }
Chris@302 121
Chris@302 122 virtual void unexecute() { execute(); }
Chris@302 123
Chris@302 124 private:
Chris@302 125 ImageModel *m_model;
Chris@302 126 ImagePoint m_oldPoint;
Chris@302 127 ImagePoint m_newPoint;
Chris@302 128 };
Chris@302 129 };
Chris@302 130
Chris@302 131
Chris@302 132 #endif
Chris@302 133
Chris@302 134
Chris@302 135