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@423
|
23 #include <QStringList>
|
Chris@423
|
24
|
Chris@302
|
25 /**
|
Chris@302
|
26 * Image point type for use in a SparseModel. This represents an
|
Chris@302
|
27 * image, identified by filename, at a given time. The filename can
|
Chris@302
|
28 * be empty, in which case we instead have a space to put an image in.
|
Chris@302
|
29 */
|
Chris@302
|
30
|
Chris@302
|
31 struct ImagePoint : public XmlExportable
|
Chris@302
|
32 {
|
Chris@302
|
33 public:
|
Chris@302
|
34 ImagePoint(long _frame) : frame(_frame) { }
|
Chris@302
|
35 ImagePoint(long _frame, QString _image, QString _label) :
|
Chris@302
|
36 frame(_frame), image(_image), label(_label) { }
|
Chris@302
|
37
|
Chris@302
|
38 int getDimensions() const { return 1; }
|
Chris@302
|
39
|
Chris@302
|
40 long frame;
|
Chris@302
|
41 QString image;
|
Chris@302
|
42 QString label;
|
Chris@338
|
43
|
Chris@338
|
44 QString getLabel() const { return label; }
|
Chris@302
|
45
|
Chris@314
|
46 void toXml(QTextStream &stream,
|
Chris@314
|
47 QString indent = "",
|
Chris@314
|
48 QString extraAttributes = "") const
|
Chris@302
|
49 {
|
Chris@314
|
50 stream <<
|
Chris@314
|
51 QString("%1<point frame=\"%2\" image=\"%3\" label=\"%4\" %5/>\n")
|
Chris@302
|
52 .arg(indent).arg(frame)
|
Chris@302
|
53 .arg(encodeEntities(image))
|
Chris@302
|
54 .arg(encodeEntities(label))
|
Chris@302
|
55 .arg(extraAttributes);
|
Chris@302
|
56 }
|
Chris@302
|
57
|
Chris@302
|
58 QString toDelimitedDataString(QString delimiter, size_t sampleRate) const
|
Chris@302
|
59 {
|
Chris@302
|
60 QStringList list;
|
Chris@302
|
61 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
|
Chris@302
|
62 list << image;
|
Chris@318
|
63 if (label != "") list << label;
|
Chris@302
|
64 return list.join(delimiter);
|
Chris@302
|
65 }
|
Chris@302
|
66
|
Chris@302
|
67 struct Comparator {
|
Chris@302
|
68 bool operator()(const ImagePoint &p1,
|
Chris@302
|
69 const ImagePoint &p2) const {
|
Chris@302
|
70 if (p1.frame != p2.frame) return p1.frame < p2.frame;
|
Chris@302
|
71 if (p1.label != p2.label) return p1.label < p2.label;
|
Chris@302
|
72 return p1.image < p2.image;
|
Chris@302
|
73 }
|
Chris@302
|
74 };
|
Chris@302
|
75
|
Chris@302
|
76 struct OrderComparator {
|
Chris@302
|
77 bool operator()(const ImagePoint &p1,
|
Chris@302
|
78 const ImagePoint &p2) const {
|
Chris@302
|
79 return p1.frame < p2.frame;
|
Chris@302
|
80 }
|
Chris@302
|
81 };
|
Chris@302
|
82 };
|
Chris@302
|
83
|
Chris@302
|
84
|
Chris@302
|
85 // Make this a class rather than a typedef so it can be predeclared.
|
Chris@302
|
86
|
Chris@302
|
87 class ImageModel : public SparseModel<ImagePoint>
|
Chris@302
|
88 {
|
Chris@423
|
89 Q_OBJECT
|
Chris@423
|
90
|
Chris@302
|
91 public:
|
Chris@302
|
92 ImageModel(size_t sampleRate, size_t resolution, bool notifyOnAdd = true) :
|
Chris@302
|
93 SparseModel<ImagePoint>(sampleRate, resolution, notifyOnAdd)
|
Chris@302
|
94 { }
|
Chris@302
|
95
|
Chris@345
|
96 QString getTypeName() const { return tr("Image"); }
|
Chris@345
|
97
|
Chris@302
|
98 virtual void toXml(QTextStream &out,
|
Chris@302
|
99 QString indent = "",
|
Chris@302
|
100 QString extraAttributes = "") const
|
Chris@302
|
101 {
|
Chris@302
|
102 SparseModel<ImagePoint>::toXml
|
Chris@302
|
103 (out,
|
Chris@302
|
104 indent,
|
Chris@302
|
105 QString("%1 subtype=\"image\"")
|
Chris@302
|
106 .arg(extraAttributes));
|
Chris@302
|
107 }
|
Chris@302
|
108
|
Chris@302
|
109 /**
|
Chris@302
|
110 * Command to change the image for a point.
|
Chris@302
|
111 */
|
Chris@302
|
112 class ChangeImageCommand : public Command
|
Chris@302
|
113 {
|
Chris@302
|
114 public:
|
Chris@302
|
115 ChangeImageCommand(ImageModel *model,
|
Chris@302
|
116 const ImagePoint &point,
|
Chris@302
|
117 QString newImage,
|
Chris@302
|
118 QString newLabel) :
|
Chris@302
|
119 m_model(model), m_oldPoint(point), m_newPoint(point) {
|
Chris@302
|
120 m_newPoint.image = newImage;
|
Chris@302
|
121 m_newPoint.label = newLabel;
|
Chris@302
|
122 }
|
Chris@302
|
123
|
Chris@302
|
124 virtual QString getName() const { return tr("Edit Image"); }
|
Chris@302
|
125
|
Chris@302
|
126 virtual void execute() {
|
Chris@302
|
127 m_model->deletePoint(m_oldPoint);
|
Chris@302
|
128 m_model->addPoint(m_newPoint);
|
Chris@302
|
129 std::swap(m_oldPoint, m_newPoint);
|
Chris@302
|
130 }
|
Chris@302
|
131
|
Chris@302
|
132 virtual void unexecute() { execute(); }
|
Chris@302
|
133
|
Chris@302
|
134 private:
|
Chris@302
|
135 ImageModel *m_model;
|
Chris@302
|
136 ImagePoint m_oldPoint;
|
Chris@302
|
137 ImagePoint m_newPoint;
|
Chris@302
|
138 };
|
Chris@302
|
139 };
|
Chris@302
|
140
|
Chris@302
|
141
|
Chris@302
|
142 #endif
|
Chris@302
|
143
|
Chris@302
|
144
|
Chris@302
|
145
|