comparison data/model/NoteModel.h @ 437:7226ebac8bd3

* Add IntervalModel as base class for NoteModel (and other, further models, hopefully)
author Chris Cannam
date Thu, 07 Aug 2008 15:59:20 +0000
parents f5e8f12d2e58
children 288f45533041
comparison
equal deleted inserted replaced
436:cff476cfce77 437:7226ebac8bd3
14 */ 14 */
15 15
16 #ifndef _NOTE_MODEL_H_ 16 #ifndef _NOTE_MODEL_H_
17 #define _NOTE_MODEL_H_ 17 #define _NOTE_MODEL_H_
18 18
19 #include "SparseValueModel.h" 19 #include "IntervalModel.h"
20 #include "base/RealTime.h" 20 #include "base/RealTime.h"
21 #include "base/PlayParameterRepository.h" 21 #include "base/PlayParameterRepository.h"
22 22
23 /** 23 /**
24 * Note type for use in a SparseModel or SparseValueModel. All we 24 * Note type for use in a SparseModel or SparseValueModel. All we
84 } 84 }
85 }; 85 };
86 }; 86 };
87 87
88 88
89 class NoteModel : public SparseValueModel<Note> 89 class NoteModel : public IntervalModel<Note>
90 { 90 {
91 Q_OBJECT 91 Q_OBJECT
92 92
93 public: 93 public:
94 NoteModel(size_t sampleRate, size_t resolution, 94 NoteModel(size_t sampleRate, size_t resolution,
95 bool notifyOnAdd = true) : 95 bool notifyOnAdd = true) :
96 SparseValueModel<Note>(sampleRate, resolution, 96 IntervalModel<Note>(sampleRate, resolution, notifyOnAdd),
97 notifyOnAdd),
98 m_valueQuantization(0) 97 m_valueQuantization(0)
99 { 98 {
100 PlayParameterRepository::getInstance()->addPlayable(this); 99 PlayParameterRepository::getInstance()->addPlayable(this);
101 } 100 }
102 101
103 NoteModel(size_t sampleRate, size_t resolution, 102 NoteModel(size_t sampleRate, size_t resolution,
104 float valueMinimum, float valueMaximum, 103 float valueMinimum, float valueMaximum,
105 bool notifyOnAdd = true) : 104 bool notifyOnAdd = true) :
106 SparseValueModel<Note>(sampleRate, resolution, 105 IntervalModel<Note>(sampleRate, resolution,
107 valueMinimum, valueMaximum, 106 valueMinimum, valueMaximum,
108 notifyOnAdd), 107 notifyOnAdd),
109 m_valueQuantization(0) 108 m_valueQuantization(0)
110 { 109 {
111 PlayParameterRepository::getInstance()->addPlayable(this); 110 PlayParameterRepository::getInstance()->addPlayable(this);
112 } 111 }
113 112
116 PlayParameterRepository::getInstance()->removePlayable(this); 115 PlayParameterRepository::getInstance()->removePlayable(this);
117 } 116 }
118 117
119 float getValueQuantization() const { return m_valueQuantization; } 118 float getValueQuantization() const { return m_valueQuantization; }
120 void setValueQuantization(float q) { m_valueQuantization = q; } 119 void setValueQuantization(float q) { m_valueQuantization = q; }
121
122 /**
123 * Notes have a duration, so this returns all points that span any
124 * of the given range (as well as the usual additional few before
125 * and after). Consequently this can be very slow (optimised data
126 * structures still to be done!).
127 */
128 virtual PointList getPoints(long start, long end) const;
129
130 /**
131 * Notes have a duration, so this returns all points that span the
132 * given frame. Consequently this can be very slow (optimised
133 * data structures still to be done!).
134 */
135 virtual PointList getPoints(long frame) const;
136 120
137 QString getTypeName() const { return tr("Note"); } 121 QString getTypeName() const { return tr("Note"); }
138 122
139 virtual bool canPlay() const { return true; } 123 virtual bool canPlay() const { return true; }
140 124
153 QString extraAttributes = "") const 137 QString extraAttributes = "") const
154 { 138 {
155 std::cerr << "NoteModel::toXml: extraAttributes = \"" 139 std::cerr << "NoteModel::toXml: extraAttributes = \""
156 << extraAttributes.toStdString() << std::endl; 140 << extraAttributes.toStdString() << std::endl;
157 141
158 SparseValueModel<Note>::toXml 142 IntervalModel<Note>::toXml
159 (out, 143 (out,
160 indent, 144 indent,
161 QString("%1 valueQuantization=\"%2\"") 145 QString("%1 valueQuantization=\"%2\"")
162 .arg(extraAttributes).arg(m_valueQuantization)); 146 .arg(extraAttributes).arg(m_valueQuantization));
163 } 147 }
184 } 168 }
185 } 169 }
186 170
187 virtual QVariant getData(int row, int column, int role) const 171 virtual QVariant getData(int row, int column, int role) const
188 { 172 {
189 if (column < 2) { 173 if (column < 4) {
190 return SparseValueModel<Note>::getData 174 return IntervalModel<Note>::getData(row, column, role);
191 (row, column, role);
192 } 175 }
193 176
194 PointListIterator i = getPointListIteratorForRow(row); 177 PointListIterator i = getPointListIteratorForRow(row);
195 if (i == m_points.end()) return QVariant(); 178 if (i == m_points.end()) return QVariant();
196 179
197 switch (column) { 180 switch (column) {
198 case 2:
199 if (role == Qt::EditRole || role == SortRole) return i->value;
200 else return QString("%1 %2").arg(i->value).arg(getScaleUnits());
201 case 3: return int(i->duration);
202 case 4: return i->level; 181 case 4: return i->level;
203 case 5: return i->label; 182 case 5: return i->label;
204 default: return QVariant(); 183 default: return QVariant();
205 } 184 }
206 } 185 }
207 186
208 virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role) 187 virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role)
209 { 188 {
210 if (column < 2) { 189 if (column < 4) {
211 return SparseValueModel<Note>::getSetDataCommand 190 return IntervalModel<Note>::getSetDataCommand
212 (row, column, value, role); 191 (row, column, value, role);
213 } 192 }
214 193
215 if (role != Qt::EditRole) return false; 194 if (role != Qt::EditRole) return false;
216 PointListIterator i = getPointListIteratorForRow(row); 195 PointListIterator i = getPointListIteratorForRow(row);
219 198
220 Point point(*i); 199 Point point(*i);
221 command->deletePoint(point); 200 command->deletePoint(point);
222 201
223 switch (column) { 202 switch (column) {
224 case 0: case 1: point.frame = value.toInt(); break;
225 case 2: point.value = value.toDouble(); break;
226 case 3: point.duration = value.toInt(); break;
227 case 4: point.level = value.toDouble(); break; 203 case 4: point.level = value.toDouble(); break;
228 case 5: point.label = value.toString(); break; 204 case 5: point.label = value.toString(); break;
229 } 205 }
230 206
231 command->addPoint(point); 207 command->addPoint(point);
232 return command->finish(); 208 return command->finish();
233 }
234
235 virtual bool isColumnTimeValue(int column) const
236 {
237 return (column < 2);
238 } 209 }
239 210
240 virtual SortType getSortType(int column) const 211 virtual SortType getSortType(int column) const
241 { 212 {
242 if (column == 5) return SortAlphabetical; 213 if (column == 5) return SortAlphabetical;