svgui  1.9
ImageLayer.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_LAYER_H_
00017 #define _IMAGE_LAYER_H_
00018 
00019 #include "Layer.h"
00020 #include "data/model/ImageModel.h"
00021 
00022 #include <QObject>
00023 #include <QColor>
00024 #include <QImage>
00025 #include <QMutex>
00026 
00027 #include <map>
00028 
00029 class View;
00030 class QPainter;
00031 class FileSource;
00032 
00033 class ImageLayer : public Layer
00034 {
00035     Q_OBJECT
00036 
00037 public:
00038     ImageLayer();
00039     virtual ~ImageLayer();
00040 
00041     virtual void paint(View *v, QPainter &paint, QRect rect) const;
00042 
00043     virtual QString getFeatureDescription(View *v, QPoint &) const;
00044 
00045     virtual bool snapToFeatureFrame(View *v, int &frame,
00046                                     int &resolution,
00047                                     SnapType snap) const;
00048 
00049     virtual void drawStart(View *v, QMouseEvent *);
00050     virtual void drawDrag(View *v, QMouseEvent *);
00051     virtual void drawEnd(View *v, QMouseEvent *);
00052 
00053     virtual void editStart(View *v, QMouseEvent *);
00054     virtual void editDrag(View *v, QMouseEvent *);
00055     virtual void editEnd(View *v, QMouseEvent *);
00056 
00057     virtual void moveSelection(Selection s, int newStartFrame);
00058     virtual void resizeSelection(Selection s, Selection newSize);
00059     virtual void deleteSelection(Selection s);
00060 
00061     virtual void copy(View *v, Selection s, Clipboard &to);
00062     virtual bool paste(View *v, const Clipboard &from, int frameOffset,
00063                        bool interactive);
00064 
00065     virtual bool editOpen(View *, QMouseEvent *); // on double-click
00066 
00067     virtual const Model *getModel() const { return m_model; }
00068     void setModel(ImageModel *model);
00069 
00070     virtual PropertyList getProperties() const;
00071     virtual QString getPropertyLabel(const PropertyName &) const;
00072     virtual PropertyType getPropertyType(const PropertyName &) const;
00073     virtual int getPropertyRangeAndValue(const PropertyName &,
00074                                          int *min, int *max, int *deflt) const;
00075     virtual QString getPropertyValueLabel(const PropertyName &,
00076                                           int value) const;
00077     virtual void setProperty(const PropertyName &, int value);
00078 
00079     virtual ColourSignificance getLayerColourSignificance() const {
00080         return ColourAbsent;
00081     }
00082 
00083     virtual bool isLayerScrollable(const View *v) const;
00084 
00085     virtual bool isLayerEditable() const { return true; }
00086 
00087     virtual int getCompletion(View *) const { return m_model->getCompletion(); }
00088 
00089     virtual bool getValueExtents(float &min, float &max,
00090                                  bool &logarithmic, QString &unit) const;
00091 
00092     virtual void toXml(QTextStream &stream, QString indent = "",
00093                        QString extraAttributes = "") const;
00094 
00095     virtual int getVerticalScaleWidth(View *, bool, QPainter &) const { return 0; }
00096 
00097     virtual void setLayerDormant(const View *v, bool dormant);
00098 
00099     void setProperties(const QXmlAttributes &attributes);
00100 
00101     virtual bool addImage(int frame, QString url); // using a command
00102 
00103 protected slots:
00104     void checkAddSources();
00105     void fileSourceReady();
00106 
00107 protected:
00108     ImageModel::PointList getLocalPoints(View *v, int x, int y) const;
00109 
00110     bool getImageOriginalSize(QString name, QSize &size) const;
00111     QImage getImage(View *v, QString name, QSize maxSize) const;
00112 
00113     void drawImage(View *v, QPainter &paint, const ImageModel::Point &p,
00114                    int x, int nx) const;
00115 
00117 
00118     typedef std::map<QString, QImage> ImageMap;
00119     typedef std::map<const View *, ImageMap> ViewImageMap;
00120     typedef std::map<QString, FileSource *> FileSourceMap;
00121 
00122     static ImageMap m_images;
00123     static QMutex m_imageMapMutex;
00124     mutable ViewImageMap m_scaled;
00125     mutable FileSourceMap m_fileSources;
00126 
00127     QString getLocalFilename(QString img) const;
00128     void checkAddSource(QString img) const;
00129 
00130     ImageModel *m_model;
00131     bool m_editing;
00132     QPoint m_editOrigin;
00133     ImageModel::Point m_originalPoint;
00134     ImageModel::Point m_editingPoint;
00135     ImageModel::EditCommand *m_editingCommand;
00136 
00137     void finish(ImageModel::EditCommand *command) {
00138         Command *c = command->finish();
00139         if (c) CommandHistory::getInstance()->addCommand(c, false);
00140     }
00141 };
00142 
00143 #endif
00144