svcore  1.9
SparseTimeValueModel.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 _SPARSE_TIME_VALUE_MODEL_H_
17 #define _SPARSE_TIME_VALUE_MODEL_H_
18 
19 #include "SparseValueModel.h"
21 #include "base/RealTime.h"
22 
30 {
31 public:
32  TimeValuePoint(long _frame) : frame(_frame), value(0.0f) { }
33  TimeValuePoint(long _frame, float _value, QString _label) :
34  frame(_frame), value(_value), label(_label) { }
35 
36  int getDimensions() const { return 2; }
37 
38  long frame;
39  float value;
40  QString label;
41 
42  QString getLabel() const { return label; }
43 
44  void toXml(QTextStream &stream, QString indent = "",
45  QString extraAttributes = "") const
46  {
47  stream << QString("%1<point frame=\"%2\" value=\"%3\" label=\"%4\" %5/>\n")
48  .arg(indent).arg(frame).arg(value).arg(XmlExportable::encodeEntities(label))
49  .arg(extraAttributes);
50  }
51 
52  QString toDelimitedDataString(QString delimiter, int sampleRate) const
53  {
54  QStringList list;
55  list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
56  list << QString("%1").arg(value);
57  if (label != "") list << label;
58  return list.join(delimiter);
59  }
60 
61  struct Comparator {
62  bool operator()(const TimeValuePoint &p1,
63  const TimeValuePoint &p2) const {
64  if (p1.frame != p2.frame) return p1.frame < p2.frame;
65  if (p1.value != p2.value) return p1.value < p2.value;
66  return p1.label < p2.label;
67  }
68  };
69 
70  struct OrderComparator {
71  bool operator()(const TimeValuePoint &p1,
72  const TimeValuePoint &p2) const {
73  return p1.frame < p2.frame;
74  }
75  };
76 };
77 
78 
79 class SparseTimeValueModel : public SparseValueModel<TimeValuePoint>
80 {
81  Q_OBJECT
82 
83 public:
84  SparseTimeValueModel(int sampleRate, int resolution,
85  bool notifyOnAdd = true) :
86  SparseValueModel<TimeValuePoint>(sampleRate, resolution,
87  notifyOnAdd)
88  {
89  // Model is playable, but may not sound (if units not Hz or
90  // range unsuitable)
92  }
93 
94  SparseTimeValueModel(int sampleRate, int resolution,
95  float valueMinimum, float valueMaximum,
96  bool notifyOnAdd = true) :
97  SparseValueModel<TimeValuePoint>(sampleRate, resolution,
98  valueMinimum, valueMaximum,
99  notifyOnAdd)
100  {
101  // Model is playable, but may not sound (if units not Hz or
102  // range unsuitable)
104  }
105 
107  {
109  }
110 
111  QString getTypeName() const { return tr("Sparse Time-Value"); }
112 
113  virtual bool canPlay() const { return true; }
114  virtual bool getDefaultPlayAudible() const { return false; } // user must unmute
115 
120  virtual int getColumnCount() const
121  {
122  return 4;
123  }
124 
125  virtual QString getHeading(int column) const
126  {
127  switch (column) {
128  case 0: return tr("Time");
129  case 1: return tr("Frame");
130  case 2: return tr("Value");
131  case 3: return tr("Label");
132  default: return tr("Unknown");
133  }
134  }
135 
136  virtual QVariant getData(int row, int column, int role) const
137  {
138  if (column < 2) {
140  (row, column, role);
141  }
142 
144  if (i == m_points.end()) return QVariant();
145 
146  switch (column) {
147  case 2:
148  if (role == Qt::EditRole || role == SortRole) return i->value;
149  else return QString("%1 %2").arg(i->value).arg(getScaleUnits());
150  case 3: return i->label;
151  default: return QVariant();
152  }
153  }
154 
155  virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role)
156  {
157  if (column < 2) {
159  (row, column, value, role);
160  }
161 
162  if (role != Qt::EditRole) return 0;
164  if (i == m_points.end()) return 0;
165  EditCommand *command = new EditCommand(this, tr("Edit Data"));
166 
167  Point point(*i);
168  command->deletePoint(point);
169 
170  switch (column) {
171  case 2: point.value = value.toDouble(); break;
172  case 3: point.label = value.toString(); break;
173  }
174 
175  command->addPoint(point);
176  return command->finish();
177  }
178 
179  virtual bool isColumnTimeValue(int column) const
180  {
181  return (column < 2);
182  }
183 
184  virtual SortType getSortType(int column) const
185  {
186  if (column == 3) return SortAlphabetical;
187  return SortNumeric;
188  }
189 };
190 
191 
192 #endif
193 
194 
195 
static PlayParameterRepository * getInstance()
TimeValuePoint(long _frame, float _value, QString _label)
static RealTime frame2RealTime(long frame, unsigned int sampleRate)
Convert a sample frame at the given sample rate into a RealTime.
Definition: RealTime.cpp:450
SparseTimeValueModel(int sampleRate, int resolution, bool notifyOnAdd=true)
TimeValuePoint(long _frame)
PointList::const_iterator PointListConstIterator
Definition: SparseModel.h:71
int getDimensions() const
void removePlayable(const Playable *playable)
virtual QVariant getData(int row, int column, int role) const
Definition: SparseModel.h:315
QString getTypeName() const
Return the type of the model.
static QString encodeEntities(QString)
Time/value point type for use in a SparseModel or SparseValueModel.
virtual QString getScaleUnits() const
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: SparseModel.h:333
SparseTimeValueModel(int sampleRate, int resolution, float valueMinimum, float valueMaximum, bool notifyOnAdd=true)
bool operator()(const TimeValuePoint &p1, const TimeValuePoint &p2) const
virtual QString getHeading(int column) const
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Model containing sparse data (points with some properties) of which one of the properties is an arbit...
virtual SortType getSortType(int column) const
bool operator()(const TimeValuePoint &p1, const TimeValuePoint &p2) const
virtual bool getDefaultPlayAudible() const
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
QString toDelimitedDataString(QString delimiter, int sampleRate) const
virtual bool isColumnTimeValue(int column) const
QString getLabel() const
virtual QVariant getData(int row, int column, int role) const
virtual bool canPlay() const
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
void addPlayable(const Playable *playable)
PointListIterator getPointListIteratorForRow(int row)
Definition: SparseModel.h:405
virtual int getColumnCount() const
TabularModel methods.