svcore  1.9
RegionModel.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 Chris Cannam.
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 _REGION_MODEL_H_
17 #define _REGION_MODEL_H_
18 
19 #include "IntervalModel.h"
20 #include "base/RealTime.h"
21 
36 struct RegionRec
37 {
38 public:
39  RegionRec() : frame(0), value(0.f), duration(0) { }
40  RegionRec(long _frame) : frame(_frame), value(0.0f), duration(0) { }
41  RegionRec(long _frame, float _value, int _duration, QString _label) :
42  frame(_frame), value(_value), duration(_duration), label(_label) { }
43 
44  int getDimensions() const { return 3; }
45 
46  long frame;
47  float value;
48  int duration;
49  QString label;
50 
51  QString getLabel() const { return label; }
52 
53  void toXml(QTextStream &stream,
54  QString indent = "",
55  QString extraAttributes = "") const
56  {
57  stream <<
58  QString("%1<point frame=\"%2\" value=\"%3\" duration=\"%4\" label=\"%5\" %6/>\n")
59  .arg(indent).arg(frame).arg(value).arg(duration)
60  .arg(XmlExportable::encodeEntities(label)).arg(extraAttributes);
61  }
62 
63  QString toDelimitedDataString(QString delimiter, int sampleRate) const
64  {
65  QStringList list;
66  list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
67  list << QString("%1").arg(value);
68  list << RealTime::frame2RealTime(duration, sampleRate).toString().c_str();
69  if (label != "") list << label;
70  return list.join(delimiter);
71  }
72 
73  struct Comparator {
74  bool operator()(const RegionRec &p1,
75  const RegionRec &p2) const {
76  if (p1.frame != p2.frame) return p1.frame < p2.frame;
77  if (p1.value != p2.value) return p1.value < p2.value;
78  if (p1.duration != p2.duration) return p1.duration < p2.duration;
79  return p1.label < p2.label;
80  }
81  };
82 
83  struct OrderComparator {
84  bool operator()(const RegionRec &p1,
85  const RegionRec &p2) const {
86  return p1.frame < p2.frame;
87  }
88  };
89 };
90 
91 
92 class RegionModel : public IntervalModel<RegionRec>
93 {
94  Q_OBJECT
95 
96 public:
97  RegionModel(int sampleRate, int resolution,
98  bool notifyOnAdd = true) :
99  IntervalModel<RegionRec>(sampleRate, resolution, notifyOnAdd),
101  m_haveDistinctValues(false)
102  {
103  }
104 
105  RegionModel(int sampleRate, int resolution,
106  float valueMinimum, float valueMaximum,
107  bool notifyOnAdd = true) :
108  IntervalModel<RegionRec>(sampleRate, resolution,
109  valueMinimum, valueMaximum,
110  notifyOnAdd),
112  m_haveDistinctValues(false)
113  {
114  }
115 
116  virtual ~RegionModel()
117  {
118  }
119 
120  float getValueQuantization() const { return m_valueQuantization; }
122 
123  bool haveDistinctValues() const { return m_haveDistinctValues; }
124 
125  QString getTypeName() const { return tr("Region"); }
126 
127  virtual void toXml(QTextStream &out,
128  QString indent = "",
129  QString extraAttributes = "") const
130  {
131  std::cerr << "RegionModel::toXml: extraAttributes = \""
132  << extraAttributes.toStdString() << std::endl;
133 
135  (out,
136  indent,
137  QString("%1 subtype=\"region\" valueQuantization=\"%2\"")
138  .arg(extraAttributes).arg(m_valueQuantization));
139  }
140 
145  virtual int getColumnCount() const
146  {
147  return 5;
148  }
149 
150  virtual QString getHeading(int column) const
151  {
152  switch (column) {
153  case 0: return tr("Time");
154  case 1: return tr("Frame");
155  case 2: return tr("Value");
156  case 3: return tr("Duration");
157  case 4: return tr("Label");
158  default: return tr("Unknown");
159  }
160  }
161 
162  virtual QVariant getData(int row, int column, int role) const
163  {
164  if (column < 4) {
165  return IntervalModel<RegionRec>::getData(row, column, role);
166  }
167 
169  if (i == m_points.end()) return QVariant();
170 
171  switch (column) {
172  case 4: 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 < 4) {
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 4: point.label = value.toString(); break;
194  }
195 
196  command->addPoint(point);
197  return command->finish();
198  }
199 
200  virtual SortType getSortType(int column) const
201  {
202  if (column == 4) return SortAlphabetical;
203  return SortNumeric;
204  }
205 
206  virtual void addPoint(const Point &point)
207  {
208  if (point.value != 0.f) m_haveDistinctValues = true;
210  }
211 
212 protected:
215 };
216 
217 #endif
virtual void toXml(QTextStream &out, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
Definition: RegionModel.h:127
virtual SortType getSortType(int column) const
Definition: RegionModel.h:200
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Definition: RegionModel.h:53
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 QString getHeading(int column) const
Definition: RegionModel.h:150
PointList::const_iterator PointListConstIterator
Definition: SparseModel.h:71
RegionModel – a concrete IntervalModel for intervals associated with a value, which we call regions f...
Definition: RegionModel.h:36
bool operator()(const RegionRec &p1, const RegionRec &p2) const
Definition: RegionModel.h:74
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: RegionModel.h:177
virtual QVariant getData(int row, int column, int role) const
TabularModel methods.
Definition: RegionModel.h:162
virtual ~RegionModel()
Definition: RegionModel.h:116
bool m_haveDistinctValues
Definition: RegionModel.h:214
int duration
Definition: RegionModel.h:48
void setValueQuantization(float q)
Definition: RegionModel.h:121
virtual QVariant getData(int row, int column, int role) const
TabularModel methods.
Definition: IntervalModel.h:68
RegionModel(int sampleRate, int resolution, float valueMinimum, float valueMaximum, bool notifyOnAdd=true)
Definition: RegionModel.h:105
QString getLabel() const
Definition: RegionModel.h:51
static QString encodeEntities(QString)
bool operator()(const RegionRec &p1, const RegionRec &p2) const
Definition: RegionModel.h:84
virtual void addPoint(const Point &point)
Add a point.
Definition: RegionModel.h:206
float getValueQuantization() const
Definition: RegionModel.h:120
QString toDelimitedDataString(QString delimiter, int sampleRate) const
Definition: RegionModel.h:63
int getDimensions() const
Definition: RegionModel.h:44
RegionRec(long _frame, float _value, int _duration, QString _label)
Definition: RegionModel.h:41
virtual void addPoint(const PointType &point)
Add a point.
RegionRec(long _frame)
Definition: RegionModel.h:40
Model containing sparse data (points with some properties) of which the properties include a duration...
Definition: IntervalModel.h:29
PointList::iterator PointListIterator
Definition: SparseModel.h:70
float value
Definition: RegionModel.h:47
QString label
Definition: RegionModel.h:49
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: IntervalModel.h:89
long frame
Definition: RegionModel.h:46
virtual void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
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
PointListIterator getPointListIteratorForRow(int row)
Definition: SparseModel.h:405
QString getTypeName() const
Return the type of the model.
Definition: RegionModel.h:125
bool haveDistinctValues() const
Definition: RegionModel.h:123
float m_valueQuantization
Definition: RegionModel.h:213
RegionModel(int sampleRate, int resolution, bool notifyOnAdd=true)
Definition: RegionModel.h:97
virtual int getColumnCount() const
TabularModel methods.
Definition: RegionModel.h:145