ImageModel.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-2007 Chris Cannam and QMUL.
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 _IMAGE_MODEL_H_
00017 #define _IMAGE_MODEL_H_
00018 
00019 #include "SparseModel.h"
00020 #include "base/XmlExportable.h"
00021 #include "base/RealTime.h"
00022 
00029 struct ImagePoint : public XmlExportable
00030 {
00031 public:
00032     ImagePoint(long _frame) : frame(_frame) { }
00033     ImagePoint(long _frame, QString _image, QString _label) :
00034         frame(_frame), image(_image), label(_label) { }
00035 
00036     int getDimensions() const { return 1; }
00037     
00038     long frame;
00039     QString image;
00040     QString label;
00041 
00042     QString getLabel() const { return label; }
00043     
00044     void toXml(QTextStream &stream,
00045                QString indent = "",
00046                QString extraAttributes = "") const
00047     {
00048         stream <<
00049             QString("%1<point frame=\"%2\" image=\"%3\" label=\"%4\" %5/>\n")
00050             .arg(indent).arg(frame)
00051             .arg(encodeEntities(image))
00052             .arg(encodeEntities(label))
00053             .arg(extraAttributes);
00054     }
00055 
00056     QString toDelimitedDataString(QString delimiter, size_t sampleRate) const
00057     {
00058         QStringList list;
00059         list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
00060         list << image;
00061         if (label != "") list << label;
00062         return list.join(delimiter);
00063     }
00064 
00065     struct Comparator {
00066         bool operator()(const ImagePoint &p1,
00067                         const ImagePoint &p2) const {
00068             if (p1.frame != p2.frame) return p1.frame < p2.frame;
00069             if (p1.label != p2.label) return p1.label < p2.label;
00070             return p1.image < p2.image;
00071         }
00072     };
00073     
00074     struct OrderComparator {
00075         bool operator()(const ImagePoint &p1,
00076                         const ImagePoint &p2) const {
00077             return p1.frame < p2.frame;
00078         }
00079     };
00080 };
00081 
00082 
00083 // Make this a class rather than a typedef so it can be predeclared.
00084 
00085 class ImageModel : public SparseModel<ImagePoint>
00086 {
00087 public:
00088     ImageModel(size_t sampleRate, size_t resolution, bool notifyOnAdd = true) :
00089         SparseModel<ImagePoint>(sampleRate, resolution, notifyOnAdd)
00090     { }
00091 
00092     QString getTypeName() const { return tr("Image"); }
00093 
00094     virtual void toXml(QTextStream &out,
00095                        QString indent = "",
00096                        QString extraAttributes = "") const
00097     {
00098         SparseModel<ImagePoint>::toXml
00099             (out, 
00100              indent,
00101              QString("%1 subtype=\"image\"")
00102              .arg(extraAttributes));
00103     }
00104 
00108     class ChangeImageCommand : public Command
00109     {
00110     public:
00111         ChangeImageCommand(ImageModel *model,
00112                            const ImagePoint &point,
00113                            QString newImage,
00114                            QString newLabel) :
00115             m_model(model), m_oldPoint(point), m_newPoint(point) {
00116             m_newPoint.image = newImage;
00117             m_newPoint.label = newLabel;
00118         }
00119 
00120         virtual QString getName() const { return tr("Edit Image"); }
00121 
00122         virtual void execute() { 
00123             m_model->deletePoint(m_oldPoint);
00124             m_model->addPoint(m_newPoint);
00125             std::swap(m_oldPoint, m_newPoint);
00126         }
00127 
00128         virtual void unexecute() { execute(); }
00129 
00130     private:
00131         ImageModel *m_model;
00132         ImagePoint m_oldPoint;
00133         ImagePoint m_newPoint;
00134     };
00135 };
00136 
00137 
00138 #endif
00139 
00140 
00141     

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