comparison data/model/SparseValueModel.h @ 0:fc9323a41f5a

start base : Sonic Visualiser sv1-1.0rc1
author lbajardsilogic
date Fri, 11 May 2007 09:08:14 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:fc9323a41f5a
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 "system/System.h"
20 #include "SparseModel.h"
21 #include "base/UnitDatabase.h"
22
23 #include <cmath>
24
25 /**
26 * Model containing sparse data (points with some properties) of which
27 * one of the properties is an arbitrary float value. The other
28 * properties depend on the point type.
29 */
30
31 template <typename PointType>
32 class SparseValueModel : public SparseModel<PointType>
33 {
34 public:
35 SparseValueModel(size_t sampleRate, size_t resolution,
36 bool notifyOnAdd = true) :
37 SparseModel<PointType>(sampleRate, resolution, notifyOnAdd),
38 m_valueMinimum(0.f),
39 m_valueMaximum(0.f),
40 m_haveExtents(false)
41 { }
42
43 SparseValueModel(size_t sampleRate, size_t resolution,
44 float valueMinimum, float valueMaximum,
45 bool notifyOnAdd = true) :
46 SparseModel<PointType>(sampleRate, resolution, notifyOnAdd),
47 m_valueMinimum(valueMinimum),
48 m_valueMaximum(valueMaximum),
49 m_haveExtents(true)
50 { }
51
52 using SparseModel<PointType>::m_points;
53 using SparseModel<PointType>::modelChanged;
54
55 virtual float getValueMinimum() const { return m_valueMinimum; }
56 virtual float getValueMaximum() const { return m_valueMaximum; }
57
58 virtual QString getScaleUnits() const { return m_units; }
59 virtual void setScaleUnits(QString units) {
60 m_units = units;
61 UnitDatabase::getInstance()->registerUnit(units);
62 }
63
64 virtual void addPoint(const PointType &point)
65 {
66 bool allChange = false;
67
68 if (!isnan(point.value) && !isinf(point.value)) {
69 if (!m_haveExtents || point.value < m_valueMinimum) {
70 m_valueMinimum = point.value; allChange = true;
71 }
72 if (!m_haveExtents || point.value > m_valueMaximum) {
73 m_valueMaximum = point.value; allChange = true;
74 }
75 m_haveExtents = true;
76 }
77
78 SparseModel<PointType>::addPoint(point);
79 if (allChange) emit modelChanged();
80 }
81
82 virtual void deletePoint(const PointType &point)
83 {
84 SparseModel<PointType>::deletePoint(point);
85
86 if (point.value == m_valueMinimum ||
87 point.value == m_valueMaximum) {
88
89 float formerMin = m_valueMinimum, formerMax = m_valueMaximum;
90
91 for (typename SparseModel<PointType>::PointList::const_iterator i
92 = m_points.begin();
93 i != m_points.end(); ++i) {
94
95 if (i == m_points.begin() || i->value < m_valueMinimum) {
96 m_valueMinimum = i->value;
97 }
98 if (i == m_points.begin() || i->value > m_valueMaximum) {
99 m_valueMaximum = i->value;
100 }
101 }
102
103 if (formerMin != m_valueMinimum || formerMax != m_valueMaximum) {
104 emit modelChanged();
105 }
106 }
107 }
108
109 virtual QString toXmlString(QString indent = "",
110 QString extraAttributes = "") const
111 {
112 return SparseModel<PointType>::toXmlString
113 (indent,
114 QString("%1 minimum=\"%2\" maximum=\"%3\" units=\"%4\"")
115 .arg(extraAttributes).arg(m_valueMinimum).arg(m_valueMaximum)
116 .arg(this->encodeEntities(m_units)));
117 }
118
119 protected:
120 float m_valueMinimum;
121 float m_valueMaximum;
122 bool m_haveExtents;
123 QString m_units;
124 };
125
126
127 #endif
128