svcore  1.9
SparseValueModel.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_VALUE_MODEL_H_
17 #define _SPARSE_VALUE_MODEL_H_
18 
19 #include "SparseModel.h"
20 #include "base/UnitDatabase.h"
21 
22 #include "system/System.h"
23 
30 template <typename PointType>
31 class SparseValueModel : public SparseModel<PointType>
32 {
33 public:
34  SparseValueModel(int sampleRate, int resolution,
35  bool notifyOnAdd = true) :
36  SparseModel<PointType>(sampleRate, resolution, notifyOnAdd),
37  m_valueMinimum(0.f),
38  m_valueMaximum(0.f),
39  m_haveExtents(false)
40  { }
41 
42  SparseValueModel(int sampleRate, int resolution,
43  float valueMinimum, float valueMaximum,
44  bool notifyOnAdd = true) :
45  SparseModel<PointType>(sampleRate, resolution, notifyOnAdd),
46  m_valueMinimum(valueMinimum),
47  m_valueMaximum(valueMaximum),
48  m_haveExtents(true)
49  { }
50 
55 
56  QString getTypeName() const { return tr("Sparse Value"); }
57 
58  virtual float getValueMinimum() const { return m_valueMinimum; }
59  virtual float getValueMaximum() const { return m_valueMaximum; }
60 
61  virtual QString getScaleUnits() const { return m_units; }
62  virtual void setScaleUnits(QString units) {
63  m_units = units;
65  }
66 
67  virtual void addPoint(const PointType &point)
68  {
69  bool allChange = false;
70 
71  if (!ISNAN(point.value) && !ISINF(point.value)) {
72  if (!m_haveExtents || point.value < m_valueMinimum) {
73  m_valueMinimum = point.value; allChange = true;
74 // std::cerr << "addPoint: value min = " << m_valueMinimum << std::endl;
75  }
76  if (!m_haveExtents || point.value > m_valueMaximum) {
77  m_valueMaximum = point.value; allChange = true;
78 // std::cerr << "addPoint: value max = " << m_valueMaximum << " (min = " << m_valueMinimum << ")" << std::endl;
79  }
80  m_haveExtents = true;
81  }
82 
84  if (allChange) emit modelChanged();
85  }
86 
87  virtual void deletePoint(const PointType &point)
88  {
90 
91  if (point.value == m_valueMinimum ||
92  point.value == m_valueMaximum) {
93 
94  float formerMin = m_valueMinimum, formerMax = m_valueMaximum;
95 
97  = m_points.begin();
98  i != m_points.end(); ++i) {
99 
100  if (i == m_points.begin() || i->value < m_valueMinimum) {
101  m_valueMinimum = i->value;
102 // std::cerr << "deletePoint: value min = " << m_valueMinimum << std::endl;
103  }
104  if (i == m_points.begin() || i->value > m_valueMaximum) {
105  m_valueMaximum = i->value;
106 // std::cerr << "deletePoint: value max = " << m_valueMaximum << std::endl;
107  }
108  }
109 
110  if (formerMin != m_valueMinimum || formerMax != m_valueMaximum) {
111  emit modelChanged();
112  }
113  }
114  }
115 
116  virtual void toXml(QTextStream &stream,
117  QString indent = "",
118  QString extraAttributes = "") const
119  {
120  std::cerr << "SparseValueModel::toXml: extraAttributes = \""
121  << extraAttributes.toStdString() << std::endl;
122 
124  (stream,
125  indent,
126  QString("%1 minimum=\"%2\" maximum=\"%3\" units=\"%4\"")
127  .arg(extraAttributes).arg(m_valueMinimum).arg(m_valueMaximum)
128  .arg(this->encodeEntities(m_units)));
129  }
130 
131 protected:
135  QString m_units;
136 };
137 
138 
139 #endif
140 
virtual void deletePoint(const PointType &point)
Remove a point.
void registerUnit(QString unit)
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
virtual float getValueMaximum() const
virtual float getValueMinimum() const
PointList m_points
Definition: SparseModel.h:381
QString getTypeName() const
Return the type of the model.
#define ISNAN
Definition: System.h:92
virtual void addPoint(const PointType &point)
Add a point.
Definition: SparseModel.h:704
static QString encodeEntities(QString)
virtual QString getScaleUnits() const
SparseValueModel(int sampleRate, int resolution, bool notifyOnAdd=true)
SparseValueModel(int sampleRate, int resolution, float valueMinimum, float valueMaximum, bool notifyOnAdd=true)
Model containing sparse data (points with some properties) of which one of the properties is an arbit...
virtual void setScaleUnits(QString units)
static UnitDatabase * getInstance()
void modelChanged()
Emitted when a model has been edited (or more data retrieved from cache, in the case of a cached mode...
virtual void addPoint(const PointType &point)
Add a point.
#define ISINF
Definition: System.h:93
virtual void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
virtual void deletePoint(const PointType &point)
Remove a point.
Definition: SparseModel.h:736
Model containing sparse data (points with some properties).
Definition: SparseModel.h:42