svcore  1.9
TextModel.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 _TEXT_MODEL_H_
17 #define _TEXT_MODEL_H_
18 
19 #include "SparseModel.h"
20 #include "base/XmlExportable.h"
21 #include "base/RealTime.h"
22 
23 #include <QStringList>
24 
31 struct TextPoint : public XmlExportable
32 {
33 public:
34  TextPoint(long _frame) : frame(_frame), height(0.0f) { }
35  TextPoint(long _frame, float _height, QString _label) :
36  frame(_frame), height(_height), label(_label) { }
37 
38  int getDimensions() const { return 2; }
39 
40  long frame;
41  float height;
42  QString label;
43 
44  QString getLabel() const { return label; }
45 
46  void toXml(QTextStream &stream, QString indent = "",
47  QString extraAttributes = "") const
48  {
49  stream << QString("%1<point frame=\"%2\" height=\"%3\" label=\"%4\" %5/>\n")
50  .arg(indent).arg(frame).arg(height)
51  .arg(encodeEntities(label)).arg(extraAttributes);
52  }
53 
54  QString toDelimitedDataString(QString delimiter, int sampleRate) const
55  {
56  QStringList list;
57  list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
58  list << QString("%1").arg(height);
59  if (label != "") list << label;
60  return list.join(delimiter);
61  }
62 
63  struct Comparator {
64  bool operator()(const TextPoint &p1,
65  const TextPoint &p2) const {
66  if (p1.frame != p2.frame) return p1.frame < p2.frame;
67  if (p1.height != p2.height) return p1.height < p2.height;
68  return p1.label < p2.label;
69  }
70  };
71 
72  struct OrderComparator {
73  bool operator()(const TextPoint &p1,
74  const TextPoint &p2) const {
75  return p1.frame < p2.frame;
76  }
77  };
78 };
79 
80 
81 // Make this a class rather than a typedef so it can be predeclared.
82 
83 class TextModel : public SparseModel<TextPoint>
84 {
85  Q_OBJECT
86 
87 public:
88  TextModel(int sampleRate, int resolution, bool notifyOnAdd = true) :
89  SparseModel<TextPoint>(sampleRate, resolution, notifyOnAdd)
90  { }
91 
92  virtual void toXml(QTextStream &out,
93  QString indent = "",
94  QString extraAttributes = "") const
95  {
97  (out,
98  indent,
99  QString("%1 subtype=\"text\"")
100  .arg(extraAttributes));
101  }
102 
103  QString getTypeName() const { return tr("Text"); }
104 
109  virtual int getColumnCount() const
110  {
111  return 4;
112  }
113 
114  virtual QString getHeading(int column) const
115  {
116  switch (column) {
117  case 0: return tr("Time");
118  case 1: return tr("Frame");
119  case 2: return tr("Height");
120  case 3: return tr("Label");
121  default: return tr("Unknown");
122  }
123  }
124 
125  virtual QVariant getData(int row, int column, int role) const
126  {
127  if (column < 2) {
129  (row, column, role);
130  }
131 
133  if (i == m_points.end()) return QVariant();
134 
135  switch (column) {
136  case 2: return i->height;
137  case 3: return i->label;
138  default: return QVariant();
139  }
140  }
141 
142  virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role)
143  {
144  if (column < 2) {
146  (row, column, value, role);
147  }
148 
149  if (role != Qt::EditRole) return 0;
151  if (i == m_points.end()) return 0;
152  EditCommand *command = new EditCommand(this, tr("Edit Data"));
153 
154  Point point(*i);
155  command->deletePoint(point);
156 
157  switch (column) {
158  case 2: point.height = value.toDouble(); break;
159  case 3: point.label = value.toString(); break;
160  }
161 
162  command->addPoint(point);
163  return command->finish();
164  }
165 
166  virtual bool isColumnTimeValue(int column) const
167  {
168  return (column < 2);
169  }
170 
171  virtual SortType getSortType(int column) const
172  {
173  if (column == 3) return SortAlphabetical;
174  return SortNumeric;
175  }
176 
177 };
178 
179 
180 #endif
181 
182 
183 
virtual void toXml(QTextStream &out, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
Definition: TextModel.h:92
QString getTypeName() const
Return the type of the model.
Definition: TextModel.h:103
TextModel(int sampleRate, int resolution, bool notifyOnAdd=true)
Definition: TextModel.h:88
virtual QString getHeading(int column) const
Definition: TextModel.h:114
static RealTime frame2RealTime(long frame, unsigned int sampleRate)
Convert a sample frame at the given sample rate into a RealTime.
Definition: RealTime.cpp:450
QString label
Definition: TextModel.h:42
virtual int getColumnCount() const
TabularModel methods.
Definition: TextModel.h:109
virtual SortType getSortType(int column) const
Definition: TextModel.h:171
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
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
Definition: TextModel.h:46
QString toDelimitedDataString(QString delimiter, int sampleRate) const
Definition: TextModel.h:54
TextPoint(long _frame)
Definition: TextModel.h:34
virtual QVariant getData(int row, int column, int role) const
Definition: SparseModel.h:315
int getDimensions() const
Definition: TextModel.h:38
virtual bool isColumnTimeValue(int column) const
Definition: TextModel.h:166
static QString encodeEntities(QString)
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: SparseModel.h:333
TextPoint(long _frame, float _height, QString _label)
Definition: TextModel.h:35
Text point type for use in a SparseModel.
Definition: TextModel.h:31
bool operator()(const TextPoint &p1, const TextPoint &p2) const
Definition: TextModel.h:64
float height
Definition: TextModel.h:41
PointList::iterator PointListIterator
Definition: SparseModel.h:70
long frame
Definition: TextModel.h:40
bool operator()(const TextPoint &p1, const TextPoint &p2) const
Definition: TextModel.h:73
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: TextModel.h:142
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
Model containing sparse data (points with some properties).
Definition: SparseModel.h:42
QString getLabel() const
Definition: TextModel.h:44
virtual QVariant getData(int row, int column, int role) const
Definition: TextModel.h:125