File: SparseValueModel.h

package info (click to toggle)
sonic-visualiser 2.5~repack1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,596 kB
  • ctags: 9,853
  • sloc: cpp: 93,843; ansic: 1,138; sh: 1,012; xml: 64; makefile: 35
file content (140 lines) | stat: -rw-r--r-- 4,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2006 Chris Cannam.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#ifndef _SPARSE_VALUE_MODEL_H_
#define _SPARSE_VALUE_MODEL_H_

#include "SparseModel.h"
#include "base/UnitDatabase.h"

#include "system/System.h"

/**
 * Model containing sparse data (points with some properties) of which
 * one of the properties is an arbitrary float value.  The other
 * properties depend on the point type.
 */

template <typename PointType>
class SparseValueModel : public SparseModel<PointType>
{
public:
    SparseValueModel(sv_samplerate_t sampleRate, int resolution,
		     bool notifyOnAdd = true) :
	SparseModel<PointType>(sampleRate, resolution, notifyOnAdd),
	m_valueMinimum(0.f),
	m_valueMaximum(0.f),
        m_haveExtents(false)
    { }

    SparseValueModel(sv_samplerate_t sampleRate, int resolution,
		     float valueMinimum, float valueMaximum,
		     bool notifyOnAdd = true) :
	SparseModel<PointType>(sampleRate, resolution, notifyOnAdd),
	m_valueMinimum(valueMinimum),
	m_valueMaximum(valueMaximum),
        m_haveExtents(true)
    { }

    using SparseModel<PointType>::m_points;
    using SparseModel<PointType>::modelChanged;
    using SparseModel<PointType>::getPoints;
    using SparseModel<PointType>::tr;

    QString getTypeName() const { return tr("Sparse Value"); }

    virtual float getValueMinimum() const { return m_valueMinimum; }
    virtual float getValueMaximum() const { return m_valueMaximum; }

    virtual QString getScaleUnits() const { return m_units; }
    virtual void setScaleUnits(QString units) {
        m_units = units;
        UnitDatabase::getInstance()->registerUnit(units);
    }

    virtual void addPoint(const PointType &point)
    {
	bool allChange = false;

        if (!ISNAN(point.value) && !ISINF(point.value)) {
            if (!m_haveExtents || point.value < m_valueMinimum) {
                m_valueMinimum = point.value; allChange = true;
//                std::cerr << "addPoint: value min = " << m_valueMinimum << std::endl;
            }
            if (!m_haveExtents || point.value > m_valueMaximum) {
                m_valueMaximum = point.value; allChange = true;
//                std::cerr << "addPoint: value max = " << m_valueMaximum << " (min = " << m_valueMinimum << ")" << std::endl;
            }
            m_haveExtents = true;
        }

	SparseModel<PointType>::addPoint(point);
	if (allChange) emit modelChanged();
    }

    virtual void deletePoint(const PointType &point)
    {
	SparseModel<PointType>::deletePoint(point);

	if (point.value == m_valueMinimum ||
	    point.value == m_valueMaximum) {

	    float formerMin = m_valueMinimum, formerMax = m_valueMaximum;

	    for (typename SparseModel<PointType>::PointList::const_iterator i
		     = m_points.begin();
		 i != m_points.end(); ++i) {

		if (i == m_points.begin() || i->value < m_valueMinimum) {
		    m_valueMinimum = i->value;
//                    std::cerr << "deletePoint: value min = " << m_valueMinimum << std::endl;
		} 
		if (i == m_points.begin() || i->value > m_valueMaximum) {
		    m_valueMaximum = i->value;
//                    std::cerr << "deletePoint: value max = " << m_valueMaximum << std::endl;
		} 
	    }

	    if (formerMin != m_valueMinimum || formerMax != m_valueMaximum) {
		emit modelChanged();
	    }
	}
    }

    virtual void toXml(QTextStream &stream,
                       QString indent = "",
                       QString extraAttributes = "") const
    {
        std::cerr << "SparseValueModel::toXml: extraAttributes = \"" 
                  << extraAttributes.toStdString() << std::endl;

	SparseModel<PointType>::toXml
	    (stream,
             indent,
	     QString("%1 minimum=\"%2\" maximum=\"%3\" units=\"%4\"")
	     .arg(extraAttributes).arg(m_valueMinimum).arg(m_valueMaximum)
             .arg(this->encodeEntities(m_units)));
    }

protected:
    float m_valueMinimum;
    float m_valueMaximum;
    bool m_haveExtents;
    QString m_units;
};


#endif