svcore  1.9
SparseValueModel.h
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #ifndef _SPARSE_VALUE_MODEL_H_
00017 #define _SPARSE_VALUE_MODEL_H_
00018 
00019 #include "SparseModel.h"
00020 #include "base/UnitDatabase.h"
00021 
00022 #include "system/System.h"
00023 
00030 template <typename PointType>
00031 class SparseValueModel : public SparseModel<PointType>
00032 {
00033 public:
00034     SparseValueModel(int sampleRate, int resolution,
00035                      bool notifyOnAdd = true) :
00036         SparseModel<PointType>(sampleRate, resolution, notifyOnAdd),
00037         m_valueMinimum(0.f),
00038         m_valueMaximum(0.f),
00039         m_haveExtents(false)
00040     { }
00041 
00042     SparseValueModel(int sampleRate, int resolution,
00043                      float valueMinimum, float valueMaximum,
00044                      bool notifyOnAdd = true) :
00045         SparseModel<PointType>(sampleRate, resolution, notifyOnAdd),
00046         m_valueMinimum(valueMinimum),
00047         m_valueMaximum(valueMaximum),
00048         m_haveExtents(true)
00049     { }
00050 
00051     using SparseModel<PointType>::m_points;
00052     using SparseModel<PointType>::modelChanged;
00053     using SparseModel<PointType>::getPoints;
00054     using SparseModel<PointType>::tr;
00055 
00056     QString getTypeName() const { return tr("Sparse Value"); }
00057 
00058     virtual float getValueMinimum() const { return m_valueMinimum; }
00059     virtual float getValueMaximum() const { return m_valueMaximum; }
00060 
00061     virtual QString getScaleUnits() const { return m_units; }
00062     virtual void setScaleUnits(QString units) {
00063         m_units = units;
00064         UnitDatabase::getInstance()->registerUnit(units);
00065     }
00066 
00067     virtual void addPoint(const PointType &point)
00068     {
00069         bool allChange = false;
00070 
00071         if (!ISNAN(point.value) && !ISINF(point.value)) {
00072             if (!m_haveExtents || point.value < m_valueMinimum) {
00073                 m_valueMinimum = point.value; allChange = true;
00074 //                std::cerr << "addPoint: value min = " << m_valueMinimum << std::endl;
00075             }
00076             if (!m_haveExtents || point.value > m_valueMaximum) {
00077                 m_valueMaximum = point.value; allChange = true;
00078 //                std::cerr << "addPoint: value max = " << m_valueMaximum << " (min = " << m_valueMinimum << ")" << std::endl;
00079             }
00080             m_haveExtents = true;
00081         }
00082 
00083         SparseModel<PointType>::addPoint(point);
00084         if (allChange) emit modelChanged();
00085     }
00086 
00087     virtual void deletePoint(const PointType &point)
00088     {
00089         SparseModel<PointType>::deletePoint(point);
00090 
00091         if (point.value == m_valueMinimum ||
00092             point.value == m_valueMaximum) {
00093 
00094             float formerMin = m_valueMinimum, formerMax = m_valueMaximum;
00095 
00096             for (typename SparseModel<PointType>::PointList::const_iterator i
00097                      = m_points.begin();
00098                  i != m_points.end(); ++i) {
00099 
00100                 if (i == m_points.begin() || i->value < m_valueMinimum) {
00101                     m_valueMinimum = i->value;
00102 //                    std::cerr << "deletePoint: value min = " << m_valueMinimum << std::endl;
00103                 } 
00104                 if (i == m_points.begin() || i->value > m_valueMaximum) {
00105                     m_valueMaximum = i->value;
00106 //                    std::cerr << "deletePoint: value max = " << m_valueMaximum << std::endl;
00107                 } 
00108             }
00109 
00110             if (formerMin != m_valueMinimum || formerMax != m_valueMaximum) {
00111                 emit modelChanged();
00112             }
00113         }
00114     }
00115 
00116     virtual void toXml(QTextStream &stream,
00117                        QString indent = "",
00118                        QString extraAttributes = "") const
00119     {
00120         std::cerr << "SparseValueModel::toXml: extraAttributes = \"" 
00121                   << extraAttributes.toStdString() << std::endl;
00122 
00123         SparseModel<PointType>::toXml
00124             (stream,
00125              indent,
00126              QString("%1 minimum=\"%2\" maximum=\"%3\" units=\"%4\"")
00127              .arg(extraAttributes).arg(m_valueMinimum).arg(m_valueMaximum)
00128              .arg(this->encodeEntities(m_units)));
00129     }
00130 
00131 protected:
00132     float m_valueMinimum;
00133     float m_valueMaximum;
00134     bool m_haveExtents;
00135     QString m_units;
00136 };
00137 
00138 
00139 #endif
00140