Mercurial > hg > svcore
comparison data/model/SparseValueModel.h @ 147:3a13b0d4934e
* Reorganising code base. This revision will not compile.
author | Chris Cannam |
---|---|
date | Mon, 31 Jul 2006 11:44:37 +0000 |
parents | |
children | 4b2ea82fd0ed |
comparison
equal
deleted
inserted
replaced
146:f90fad823cea | 147:3a13b0d4934e |
---|---|
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_VALUE_MODEL_H_ | |
17 #define _SPARSE_VALUE_MODEL_H_ | |
18 | |
19 #include "SparseModel.h" | |
20 #include "UnitDatabase.h" | |
21 | |
22 /** | |
23 * Model containing sparse data (points with some properties) of which | |
24 * one of the properties is an arbitrary float value. The other | |
25 * properties depend on the point type. | |
26 */ | |
27 | |
28 template <typename PointType> | |
29 class SparseValueModel : public SparseModel<PointType> | |
30 { | |
31 public: | |
32 SparseValueModel(size_t sampleRate, size_t resolution, | |
33 float valueMinimum, float valueMaximum, | |
34 bool notifyOnAdd = true) : | |
35 SparseModel<PointType>(sampleRate, resolution, notifyOnAdd), | |
36 m_valueMinimum(valueMinimum), | |
37 m_valueMaximum(valueMaximum) | |
38 { } | |
39 | |
40 using SparseModel<PointType>::m_points; | |
41 using SparseModel<PointType>::modelChanged; | |
42 | |
43 virtual float getValueMinimum() const { return m_valueMinimum; } | |
44 virtual float getValueMaximum() const { return m_valueMaximum; } | |
45 | |
46 virtual QString getScaleUnits() const { return m_units; } | |
47 virtual void setScaleUnits(QString units) { | |
48 m_units = units; | |
49 UnitDatabase::getInstance()->registerUnit(units); | |
50 } | |
51 | |
52 virtual void addPoint(const PointType &point) | |
53 { | |
54 bool allChange = false; | |
55 if (m_points.empty() || point.value < m_valueMinimum) { | |
56 m_valueMinimum = point.value; allChange = true; | |
57 } | |
58 if (m_points.empty() || point.value > m_valueMaximum) { | |
59 m_valueMaximum = point.value; allChange = true; | |
60 } | |
61 | |
62 SparseModel<PointType>::addPoint(point); | |
63 if (allChange) emit modelChanged(); | |
64 } | |
65 | |
66 virtual void deletePoint(const PointType &point) | |
67 { | |
68 SparseModel<PointType>::deletePoint(point); | |
69 | |
70 if (point.value == m_valueMinimum || | |
71 point.value == m_valueMaximum) { | |
72 | |
73 float formerMin = m_valueMinimum, formerMax = m_valueMaximum; | |
74 | |
75 for (typename SparseModel<PointType>::PointList::const_iterator i | |
76 = m_points.begin(); | |
77 i != m_points.end(); ++i) { | |
78 | |
79 if (i == m_points.begin() || i->value < m_valueMinimum) { | |
80 m_valueMinimum = i->value; | |
81 } | |
82 if (i == m_points.begin() || i->value > m_valueMaximum) { | |
83 m_valueMaximum = i->value; | |
84 } | |
85 } | |
86 | |
87 if (formerMin != m_valueMinimum || formerMax != m_valueMaximum) { | |
88 emit modelChanged(); | |
89 } | |
90 } | |
91 } | |
92 | |
93 virtual QString toXmlString(QString indent = "", | |
94 QString extraAttributes = "") const | |
95 { | |
96 return SparseModel<PointType>::toXmlString | |
97 (indent, | |
98 QString("%1 minimum=\"%2\" maximum=\"%3\" units=\"%4\"") | |
99 .arg(extraAttributes).arg(m_valueMinimum).arg(m_valueMaximum) | |
100 .arg(this->encodeEntities(m_units))); | |
101 } | |
102 | |
103 protected: | |
104 float m_valueMinimum; | |
105 float m_valueMaximum; | |
106 QString m_units; | |
107 }; | |
108 | |
109 | |
110 #endif | |
111 |