svcore  1.9
ImageModel.h
Go to the documentation of this file.
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 #include <QStringList>
24 
31 struct ImagePoint : public XmlExportable
32 {
33 public:
34  ImagePoint(long _frame) : frame(_frame) { }
35  ImagePoint(long _frame, QString _image, QString _label) :
36  frame(_frame), image(_image), label(_label) { }
37 
38  int getDimensions() const { return 1; }
39 
40  long frame;
41  QString image;
42  QString label;
43 
44  QString getLabel() const { return label; }
45 
46  void toXml(QTextStream &stream,
47  QString indent = "",
48  QString extraAttributes = "") const
49  {
50  stream <<
51  QString("%1<point frame=\"%2\" image=\"%3\" label=\"%4\" %5/>\n")
52  .arg(indent).arg(frame)
53  .arg(encodeEntities(image))
54  .arg(encodeEntities(label))
55  .arg(extraAttributes);
56  }
57 
58  QString toDelimitedDataString(QString delimiter, int sampleRate) const
59  {
60  QStringList list;
61  list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
62  list << image;
63  if (label != "") list << label;
64  return list.join(delimiter);
65  }
66 
67  struct Comparator {
68  bool operator()(const ImagePoint &p1,
69  const ImagePoint &p2) const {
70  if (p1.frame != p2.frame) return p1.frame < p2.frame;
71  if (p1.label != p2.label) return p1.label < p2.label;
72  return p1.image < p2.image;
73  }
74  };
75 
76  struct OrderComparator {
77  bool operator()(const ImagePoint &p1,
78  const ImagePoint &p2) const {
79  return p1.frame < p2.frame;
80  }
81  };
82 };
83 
84 
85 // Make this a class rather than a typedef so it can be predeclared.
86 
87 class ImageModel : public SparseModel<ImagePoint>
88 {
89  Q_OBJECT
90 
91 public:
92  ImageModel(int sampleRate, int resolution, bool notifyOnAdd = true) :
93  SparseModel<ImagePoint>(sampleRate, resolution, notifyOnAdd)
94  { }
95 
96  QString getTypeName() const { return tr("Image"); }
97 
98  virtual void toXml(QTextStream &out,
99  QString indent = "",
100  QString extraAttributes = "") const
101  {
103  (out,
104  indent,
105  QString("%1 subtype=\"image\"")
106  .arg(extraAttributes));
107  }
108 
113  {
114  public:
116  const ImagePoint &point,
117  QString newImage,
118  QString newLabel) :
119  m_model(model), m_oldPoint(point), m_newPoint(point) {
120  m_newPoint.image = newImage;
121  m_newPoint.label = newLabel;
122  }
123 
124  virtual QString getName() const { return tr("Edit Image"); }
125 
126  virtual void execute() {
129  std::swap(m_oldPoint, m_newPoint);
130  }
131 
132  virtual void unexecute() { execute(); }
133 
134  private:
138  };
139 
144  virtual int getColumnCount() const
145  {
146  return 4;
147  }
148 
149  virtual QString getHeading(int column) const
150  {
151  switch (column) {
152  case 0: return tr("Time");
153  case 1: return tr("Frame");
154  case 2: return tr("Image");
155  case 3: return tr("Label");
156  default: return tr("Unknown");
157  }
158  }
159 
160  virtual QVariant getData(int row, int column, int role) const
161  {
162  if (column < 2) {
164  (row, column, role);
165  }
166 
168  if (i == m_points.end()) return QVariant();
169 
170  switch (column) {
171  case 2: return i->image;
172  case 3: return i->label;
173  default: return QVariant();
174  }
175  }
176 
177  virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role)
178  {
179  if (column < 2) {
181  (row, column, value, role);
182  }
183 
184  if (role != Qt::EditRole) return 0;
186  if (i == m_points.end()) return 0;
187  EditCommand *command = new EditCommand(this, tr("Edit Data"));
188 
189  Point point(*i);
190  command->deletePoint(point);
191 
192  switch (column) {
193  case 2: point.image = value.toString(); break;
194  case 3: point.label = value.toString(); break;
195  }
196 
197  command->addPoint(point);
198  return command->finish();
199  }
200 
201  virtual bool isColumnTimeValue(int column) const
202  {
203  return (column < 2);
204  }
205 
206  virtual SortType getSortType(int column) const
207  {
208  if (column > 2) return SortAlphabetical;
209  return SortNumeric;
210  }
211 };
212 
213 
214 #endif
215 
216 
217 
Command to change the image for a point.
Definition: ImageModel.h:112
ChangeImageCommand(ImageModel *model, const ImagePoint &point, QString newImage, QString newLabel)
Definition: ImageModel.h:115
int getDimensions() const
Definition: ImageModel.h:38
QString getTypeName() const
Return the type of the model.
Definition: ImageModel.h:96
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: ImageModel.h:177
static RealTime frame2RealTime(long frame, unsigned int sampleRate)
Convert a sample frame at the given sample rate into a RealTime.
Definition: RealTime.cpp:450
virtual void toXml(QTextStream &out, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
Definition: ImageModel.h:98
virtual QString getName() const
Definition: ImageModel.h:124
virtual void toXml(QTextStream &out, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
Definition: SparseModel.h:797
PointList::const_iterator PointListConstIterator
Definition: SparseModel.h:71
virtual QVariant getData(int row, int column, int role) const
Definition: ImageModel.h:160
QString label
Definition: ImageModel.h:42
virtual SortType getSortType(int column) const
Definition: ImageModel.h:206
virtual int getColumnCount() const
TabularModel methods.
Definition: ImageModel.h:144
virtual void addPoint(const PointType &point)
Add a point.
Definition: SparseModel.h:704
virtual QVariant getData(int row, int column, int role) const
Definition: SparseModel.h:315
static QString encodeEntities(QString)
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: SparseModel.h:333
QString getLabel() const
Definition: ImageModel.h:44
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
Definition: ImageModel.h:46
ImageModel(int sampleRate, int resolution, bool notifyOnAdd=true)
Definition: ImageModel.h:92
QString toDelimitedDataString(QString delimiter, int sampleRate) const
Definition: ImageModel.h:58
bool operator()(const ImagePoint &p1, const ImagePoint &p2) const
Definition: ImageModel.h:68
QString image
Definition: ImageModel.h:41
long frame
Definition: ImageModel.h:40
ImagePoint(long _frame)
Definition: ImageModel.h:34
Image point type for use in a SparseModel.
Definition: ImageModel.h:31
PointList::iterator PointListIterator
Definition: SparseModel.h:70
virtual bool isColumnTimeValue(int column) const
Definition: ImageModel.h:201
virtual QString getHeading(int column) const
Definition: ImageModel.h:149
ImagePoint(long _frame, QString _image, QString _label)
Definition: ImageModel.h:35
bool operator()(const ImagePoint &p1, const ImagePoint &p2) const
Definition: ImageModel.h:77
std::string toString(bool align=false) const
Return a human-readable debug-type string to full precision (probably not a format to show to a user ...
Definition: RealTime.cpp:191
virtual void deletePoint(const PointType &point)
Remove a point.
Definition: SparseModel.h:736
PointListIterator getPointListIteratorForRow(int row)
Definition: SparseModel.h:405
Model containing sparse data (points with some properties).
Definition: SparseModel.h:42