TextModel.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 _TEXT_MODEL_H_
00017 #define _TEXT_MODEL_H_
00018 
00019 #include "SparseModel.h"
00020 #include "base/XmlExportable.h"
00021 #include "base/RealTime.h"
00022 
00029 struct TextPoint : public XmlExportable
00030 {
00031 public:
00032     TextPoint(long _frame) : frame(_frame), height(0.0f) { }
00033     TextPoint(long _frame, float _height, QString _label) : 
00034         frame(_frame), height(_height), label(_label) { }
00035 
00036     int getDimensions() const { return 2; }
00037     
00038     long frame;
00039     float height;
00040     QString label;
00041 
00042     QString getLabel() const { return label; }
00043     
00044     void toXml(QTextStream &stream, QString indent = "",
00045                QString extraAttributes = "") const
00046     {
00047         stream << QString("%1<point frame=\"%2\" height=\"%3\" label=\"%4\" %5/>\n")
00048             .arg(indent).arg(frame).arg(height)
00049             .arg(encodeEntities(label)).arg(extraAttributes);
00050     }
00051 
00052     QString toDelimitedDataString(QString delimiter, size_t sampleRate) const
00053     {
00054         QStringList list;
00055         list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
00056         list << QString("%1").arg(height);
00057         if (label != "") list << label;
00058         return list.join(delimiter);
00059     }
00060 
00061     struct Comparator {
00062         bool operator()(const TextPoint &p1,
00063                         const TextPoint &p2) const {
00064             if (p1.frame != p2.frame) return p1.frame < p2.frame;
00065             if (p1.height != p2.height) return p1.height < p2.height;
00066             return p1.label < p2.label;
00067         }
00068     };
00069     
00070     struct OrderComparator {
00071         bool operator()(const TextPoint &p1,
00072                         const TextPoint &p2) const {
00073             return p1.frame < p2.frame;
00074         }
00075     };
00076 };
00077 
00078 
00079 // Make this a class rather than a typedef so it can be predeclared.
00080 
00081 class TextModel : public SparseModel<TextPoint>
00082 {
00083 public:
00084     TextModel(size_t sampleRate, size_t resolution, bool notifyOnAdd = true) :
00085         SparseModel<TextPoint>(sampleRate, resolution, notifyOnAdd)
00086     { }
00087 
00088     virtual void toXml(QTextStream &out,
00089                        QString indent = "",
00090                        QString extraAttributes = "") const
00091     {
00092         SparseModel<TextPoint>::toXml
00093             (out, 
00094              indent,
00095              QString("%1 subtype=\"text\"")
00096              .arg(extraAttributes));
00097     }
00098 
00099     QString getTypeName() const { return tr("Text"); }
00100 };
00101 
00102 
00103 #endif
00104 
00105 
00106     

Generated on Wed Feb 20 15:45:29 2008 for SonicVisualiser by  doxygen 1.5.1