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