ImageLayer.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006-2007 Chris Cannam and QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef SV_IMAGE_LAYER_H
17 #define SV_IMAGE_LAYER_H
18 
19 #include "Layer.h"
20 #include "data/model/ImageModel.h"
21 
22 #include <QObject>
23 #include <QColor>
24 #include <QImage>
25 #include <QMutex>
26 
27 #include <map>
28 
29 class View;
30 class QPainter;
31 class FileSource;
32 
33 class ImageLayer : public Layer
34 {
35  Q_OBJECT
36 
37 public:
38  ImageLayer();
39  virtual ~ImageLayer();
40 
41  void paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const override;
42 
43  QString getFeatureDescription(LayerGeometryProvider *v, QPoint &) const override;
44 
45  bool snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame,
46  int &resolution,
47  SnapType snap, int ycoord) const override;
48 
49  void drawStart(LayerGeometryProvider *v, QMouseEvent *) override;
50  void drawDrag(LayerGeometryProvider *v, QMouseEvent *) override;
51  void drawEnd(LayerGeometryProvider *v, QMouseEvent *) override;
52 
53  void editStart(LayerGeometryProvider *v, QMouseEvent *) override;
54  void editDrag(LayerGeometryProvider *v, QMouseEvent *) override;
55  void editEnd(LayerGeometryProvider *v, QMouseEvent *) override;
56 
57  void moveSelection(Selection s, sv_frame_t newStartFrame) override;
58  void resizeSelection(Selection s, Selection newSize) override;
59  void deleteSelection(Selection s) override;
60 
61  void copy(LayerGeometryProvider *v, Selection s, Clipboard &to) override;
62  bool paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t frameOffset,
63  bool interactive) override;
64 
65  bool editOpen(LayerGeometryProvider *, QMouseEvent *) override; // on double-click
66 
67  ModelId getModel() const override { return m_model; }
68  void setModel(ModelId model); // an ImageModel please
69 
70  PropertyList getProperties() const override;
71  QString getPropertyLabel(const PropertyName &) const override;
72  PropertyType getPropertyType(const PropertyName &) const override;
73  int getPropertyRangeAndValue(const PropertyName &,
74  int *min, int *max, int *deflt) const override;
75  QString getPropertyValueLabel(const PropertyName &,
76  int value) const override;
77  void setProperty(const PropertyName &, int value) override;
78 
80  return ColourAbsent;
81  }
82 
83  bool isLayerScrollable(const LayerGeometryProvider *v) const override;
84 
85  bool isLayerEditable() const override { return true; }
86 
87  int getCompletion(LayerGeometryProvider *) const override;
88 
89  bool getValueExtents(double &min, double &max,
90  bool &logarithmic, QString &unit) const override;
91 
92  void toXml(QTextStream &stream, QString indent = "",
93  QString extraAttributes = "") const override;
94 
95  int getVerticalScaleWidth(LayerGeometryProvider *, bool, QPainter &) const override { return 0; }
96 
97  void setLayerDormant(const LayerGeometryProvider *v, bool dormant) override;
98 
99  void setProperties(const QXmlAttributes &attributes) override;
100 
101  static bool isImageFileSupported(QString url); // based on extension alone
102 
103  virtual bool addImage(sv_frame_t frame, QString url); // using a command
104 
105 protected slots:
106  void checkAddSources();
107  void fileSourceReady();
108 
109 protected:
110  EventVector getLocalPoints(LayerGeometryProvider *v, int x, int y) const;
111 
112  bool getImageOriginalSize(QString name, QSize &size) const;
113  QImage getImage(LayerGeometryProvider *v, QString name, QSize maxSize) const;
114 
115  void drawImage(LayerGeometryProvider *v, QPainter &paint, const Event &p,
116  int x, int nx) const;
117 
119 
120  typedef std::map<QString, QImage> ImageMap;
121  typedef std::map<const LayerGeometryProvider *, ImageMap> ViewImageMap;
122  typedef std::map<QString, FileSource *> FileSourceMap;
123 
124  static ImageMap m_images;
125  static FileSourceMap m_fileSources;
126  static QMutex m_staticMutex;
127 
128  mutable ViewImageMap m_scaled;
129 
130  static QString getLocalFilename(QString img);
131  static void checkAddSource(QString img, bool synchronise);
132  void checkAddSourceAndConnect(QString img);
133 
134  ModelId m_model; // an ImageModel
135  bool m_editing;
136  QPoint m_editOrigin;
139  ChangeEventsCommand *m_editingCommand;
140 
141  void finish(ChangeEventsCommand *command) {
142  Command *c = command->finish();
143  if (c) CommandHistory::getInstance()->addCommand(c, false);
144  }
145 };
146 
147 #endif
148 
int getVerticalScaleWidth(LayerGeometryProvider *, bool, QPainter &) const override
Definition: ImageLayer.h:95
void setProperties(const QXmlAttributes &attributes) override
Set the particular properties of a layer (those specific to the subclass) from a set of XML attribute...
Definition: ImageLayer.cpp:995
The base class for visual representations of the data found in a Model.
Definition: Layer.h:54
void setModel(ModelId model)
Definition: ImageLayer.cpp:73
void finish(ChangeEventsCommand *command)
Definition: ImageLayer.h:141
bool isLayerScrollable(const LayerGeometryProvider *v) const override
This should return true if the layer can safely be scrolled automatically by a given view (simply cop...
Definition: ImageLayer.cpp:136
void checkAddSources()
Definition: ImageLayer.cpp:939
static bool isImageFileSupported(QString url)
Definition: ImageLayer.cpp:609
std::map< const LayerGeometryProvider *, ImageMap > ViewImageMap
Definition: ImageLayer.h:121
void drawEnd(LayerGeometryProvider *v, QMouseEvent *) override
Definition: ImageLayer.cpp:583
int getPropertyRangeAndValue(const PropertyName &, int *min, int *max, int *deflt) const override
Definition: ImageLayer.cpp:110
void addCommand(Command *command)
Add a command to the command history.
EventVector getLocalPoints(LayerGeometryProvider *v, int x, int y) const
Definition: ImageLayer.cpp:142
bool snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame, int &resolution, SnapType snap, int ycoord) const override
!! too much overlap with TimeValueLayer/TimeInstantLayer/TextLayer
Definition: ImageLayer.cpp:228
void resizeSelection(Selection s, Selection newSize) override
Definition: ImageLayer.cpp:759
bool m_editing
Definition: ImageLayer.h:135
static FileSourceMap m_fileSources
Definition: ImageLayer.h:125
bool editOpen(LayerGeometryProvider *, QMouseEvent *) override
Open an editor on the item under the mouse (e.g.
Definition: ImageLayer.cpp:706
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const override
Definition: ImageLayer.cpp:988
ColourSignificance
Definition: Layer.h:348
QImage getImage(LayerGeometryProvider *v, QString name, QSize maxSize) const
Definition: ImageLayer.cpp:503
std::map< QString, QImage > ImageMap
!! how to reap no-longer-used images?
Definition: ImageLayer.h:120
QPoint m_editOrigin
Definition: ImageLayer.h:136
std::map< QString, FileSource * > FileSourceMap
Definition: ImageLayer.h:122
void paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const override
Paint the given rectangle of this layer onto the given view using the given painter, superimposing it on top of any existing material in that view.
Definition: ImageLayer.cpp:260
bool isLayerEditable() const override
This should return true if the layer can be edited by the user.
Definition: ImageLayer.h:85
Interface for classes that provide geometry information (such as size, start frame, and a large number of other properties) about the disposition of a layer.
Event m_originalPoint
Definition: ImageLayer.h:137
static QMutex m_staticMutex
Definition: ImageLayer.h:126
static void checkAddSource(QString img, bool synchronise)
Definition: ImageLayer.cpp:918
void editEnd(LayerGeometryProvider *v, QMouseEvent *) override
Definition: ImageLayer.cpp:691
bool getValueExtents(double &min, double &max, bool &logarithmic, QString &unit) const override
Return the minimum and maximum values for the y axis of the model in this layer, as well as whether t...
Definition: ImageLayer.cpp:130
void moveSelection(Selection s, sv_frame_t newStartFrame) override
Definition: ImageLayer.cpp:737
ModelId m_model
Definition: ImageLayer.h:134
PropertyType getPropertyType(const PropertyName &) const override
Definition: ImageLayer.cpp:104
QString getPropertyValueLabel(const PropertyName &, int value) const override
Definition: ImageLayer.cpp:117
QString getFeatureDescription(LayerGeometryProvider *v, QPoint &) const override
Definition: ImageLayer.cpp:188
bool paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t frameOffset, bool interactive) override
Paste from the given clipboard onto the layer at the given frame offset.
Definition: ImageLayer.cpp:821
void fileSourceReady()
Definition: ImageLayer.cpp:952
void deleteSelection(Selection s) override
Definition: ImageLayer.cpp:788
virtual ~ImageLayer()
Definition: ImageLayer.cpp:56
SnapType
Definition: Layer.h:195
PropertyList getProperties() const override
Definition: ImageLayer.cpp:92
static CommandHistory * getInstance()
void drawDrag(LayerGeometryProvider *v, QMouseEvent *) override
Definition: ImageLayer.cpp:565
ColourSignificance getLayerColourSignificance() const override
This should return the degree of meaning associated with colour in this layer.
Definition: ImageLayer.h:79
void drawStart(LayerGeometryProvider *v, QMouseEvent *) override
Definition: ImageLayer.cpp:540
Event m_editingPoint
Definition: ImageLayer.h:138
void drawImage(LayerGeometryProvider *v, QPainter &paint, const Event &p, int x, int nx) const
Definition: ImageLayer.cpp:319
void setProperty(const PropertyName &, int value) override
Definition: ImageLayer.cpp:124
static ImageMap m_images
Definition: ImageLayer.h:124
ModelId getModel() const override
Return the ID of the model represented in this layer.
Definition: ImageLayer.h:67
View is the base class of widgets that display one or more overlaid views of data against a horizonta...
Definition: View.h:55
void setLayerDormant(const LayerGeometryProvider *v, bool dormant) override
Indicate that a layer is not currently visible in the given view and is not expected to become visibl...
Definition: ImageLayer.cpp:466
void copy(LayerGeometryProvider *v, Selection s, Clipboard &to) override
Definition: ImageLayer.cpp:807
int getCompletion(LayerGeometryProvider *) const override
Return the proportion of background work complete in drawing this view, as a percentage – in most ca...
Definition: ImageLayer.cpp:65
ChangeEventsCommand * m_editingCommand
Definition: ImageLayer.h:139
bool getImageOriginalSize(QString name, QSize &size) const
!! how to reap no-longer-used images?
Definition: ImageLayer.cpp:484
void editDrag(LayerGeometryProvider *v, QMouseEvent *) override
Definition: ImageLayer.cpp:669
QString getPropertyLabel(const PropertyName &) const override
Definition: ImageLayer.cpp:98
static QString getLocalFilename(QString img)
Definition: ImageLayer.cpp:891
ViewImageMap m_scaled
Definition: ImageLayer.h:128
void checkAddSourceAndConnect(QString img)
Definition: ImageLayer.cpp:906
void editStart(LayerGeometryProvider *v, QMouseEvent *) override
Definition: ImageLayer.cpp:646
virtual bool addImage(sv_frame_t frame, QString url)
Definition: ImageLayer.cpp:624