svcore  1.9
SparseOneDimensionalModel.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_ONE_DIMENSIONAL_MODEL_H_
17 #define _SPARSE_ONE_DIMENSIONAL_MODEL_H_
18 
19 #include "SparseModel.h"
20 #include "NoteData.h"
22 #include "base/RealTime.h"
23 
24 #include <QStringList>
25 
27 {
28 public:
29  OneDimensionalPoint(int _frame) : frame(_frame) { }
30  OneDimensionalPoint(int _frame, QString _label) : frame(_frame), label(_label) { }
31 
32  int getDimensions() const { return 1; }
33 
34  int frame;
35  QString label;
36 
37  QString getLabel() const { return label; }
38 
39  void toXml(QTextStream &stream,
40  QString indent = "",
41  QString extraAttributes = "") const
42  {
43  stream << QString("%1<point frame=\"%2\" label=\"%3\" %4/>\n")
44  .arg(indent).arg(frame).arg(XmlExportable::encodeEntities(label))
45  .arg(extraAttributes);
46  }
47 
48  QString toDelimitedDataString(QString delimiter, int sampleRate) const
49  {
50  QStringList list;
51  list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
52  if (label != "") list << label;
53  return list.join(delimiter);
54  }
55 
56  struct Comparator {
58  const OneDimensionalPoint &p2) const {
59  if (p1.frame != p2.frame) return p1.frame < p2.frame;
60  return p1.label < p2.label;
61  }
62  };
63 
64  struct OrderComparator {
66  const OneDimensionalPoint &p2) const {
67  return p1.frame < p2.frame;
68  }
69  };
70 };
71 
72 
73 class SparseOneDimensionalModel : public SparseModel<OneDimensionalPoint>,
74  public NoteExportable
75 {
76  Q_OBJECT
77 
78 public:
79  SparseOneDimensionalModel(int sampleRate, int resolution,
80  bool notifyOnAdd = true) :
81  SparseModel<OneDimensionalPoint>(sampleRate, resolution, notifyOnAdd)
82  {
84  }
85 
87  {
89  }
90 
91  virtual bool canPlay() const { return true; }
92 
93  virtual QString getDefaultPlayClipId() const
94  {
95  return "tap";
96  }
97 
98  int getIndexOf(const Point &point)
99  {
100  // slow
101  int i = 0;
102  Point::Comparator comparator;
103  for (PointList::const_iterator j = m_points.begin();
104  j != m_points.end(); ++j, ++i) {
105  if (!comparator(*j, point) && !comparator(point, *j)) return i;
106  }
107  return -1;
108  }
109 
110  QString getTypeName() const { return tr("Sparse 1-D"); }
111 
116  virtual int getColumnCount() const
117  {
118  return 3;
119  }
120 
121  virtual QString getHeading(int column) const
122  {
123  switch (column) {
124  case 0: return tr("Time");
125  case 1: return tr("Frame");
126  case 2: return tr("Label");
127  default: return tr("Unknown");
128  }
129  }
130 
131  virtual QVariant getData(int row, int column, int role) const
132  {
133  if (column < 2) {
135  (row, column, role);
136  }
137 
139  if (i == m_points.end()) return QVariant();
140 
141  switch (column) {
142  case 2: return i->label;
143  default: return QVariant();
144  }
145  }
146 
147  virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role)
148  {
149  if (column < 2) {
151  (row, column, value, role);
152  }
153 
154  if (role != Qt::EditRole) return 0;
156  if (i == m_points.end()) return 0;
157  EditCommand *command = new EditCommand(this, tr("Edit Data"));
158 
159  Point point(*i);
160  command->deletePoint(point);
161 
162  switch (column) {
163  case 2: point.label = value.toString(); break;
164  }
165 
166  command->addPoint(point);
167  return command->finish();
168  }
169 
170 
171  virtual bool isColumnTimeValue(int column) const
172  {
173  return (column < 2);
174  }
175 
176  virtual SortType getSortType(int column) const
177  {
178  if (column == 2) return SortAlphabetical;
179  return SortNumeric;
180  }
181 
186  NoteList getNotes() const {
188  }
189 
190  NoteList getNotesWithin(int startFrame, int endFrame) const {
191 
192  PointList points = getPoints(startFrame, endFrame);
193  NoteList notes;
194 
195  for (PointList::iterator pli =
196  points.begin(); pli != points.end(); ++pli) {
197 
198  notes.push_back
199  (NoteData(pli->frame,
200  getSampleRate() / 6, // arbitrary short duration
201  64, // default pitch
202  100)); // default velocity
203  }
204 
205  return notes;
206  }
207 };
208 
209 #endif
210 
211 
212 
std::multiset< OneDimensionalPoint, typename OneDimensionalPoint ::OrderComparator > PointList
Definition: SparseModel.h:69
static PlayParameterRepository * getInstance()
static RealTime frame2RealTime(long frame, unsigned int sampleRate)
Convert a sample frame at the given sample rate into a RealTime.
Definition: RealTime.cpp:450
PointList::const_iterator PointListConstIterator
Definition: SparseModel.h:71
virtual int getSampleRate() const
Definition: SparseModel.h:53
QString getTypeName() const
Return the type of the model.
void removePlayable(const Playable *playable)
virtual int getColumnCount() const
TabularModel methods.
virtual QString getDefaultPlayClipId() const
bool operator()(const OneDimensionalPoint &p1, const OneDimensionalPoint &p2) const
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
virtual QVariant getData(int row, int column, int role) const
Definition: SparseModel.h:315
static QString encodeEntities(QString)
virtual SortType getSortType(int column) const
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: SparseModel.h:333
virtual int getStartFrame() const
Definition: SparseModel.h:484
bool operator()(const OneDimensionalPoint &p1, const OneDimensionalPoint &p2) const
virtual const PointList & getPoints() const
Get all points.
Definition: SparseModel.h:537
NoteList getNotes() const
NoteExportable methods.
static QString notes[]
Definition: Pitch.cpp:87
QString toDelimitedDataString(QString delimiter, int sampleRate) const
OneDimensionalPoint(int _frame, QString _label)
NoteList getNotesWithin(int startFrame, int endFrame) const
virtual QString getHeading(int column) const
SparseOneDimensionalModel(int sampleRate, int resolution, bool notifyOnAdd=true)
virtual int getEndFrame() const
Definition: SparseModel.h:496
std::vector< NoteData > NoteList
Definition: NoteData.h:44
int getIndexOf(const Point &point)
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") 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
Model containing sparse data (points with some properties).
Definition: SparseModel.h:42
virtual QVariant getData(int row, int column, int role) const
virtual bool isColumnTimeValue(int column) const