-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPaint.h
370 lines (270 loc) · 8 KB
/
Paint.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Paint.h
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PAINT_H_
#define PAINT_H_
#include <QtGlobal>
#include <QtOpenGL>
#include <string>
#include <QColor>
#include <QMutex>
#if __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#include "Element.h"
#include "Maths.h"
namespace mmp {
typedef enum {
VIDEO_URI,
VIDEO_WEBCAM,
VIDEO_SHMSRC
} VideoType;
/**
* A Paint is a style that can be applied when drawing potentially any shape.
*
* Defines the way to draw any shape.
* There must be a MappingGui that implements this paint for every shape
* so that this shape might be drawn with it.
*/
class Paint : public Element
{
Q_OBJECT
private:
static UidAllocator allocator;
uid _id;
protected:
Paint(uid id=NULL_UID);
public:
typedef QSharedPointer<Paint> ptr;
virtual ~Paint();
static const UidAllocator& getUidAllocator() { return allocator; }
/// This method should be called at each call of draw().
virtual void update() {}
/// Is the paint currently playing?
virtual bool isPlaying() const { return _isPlaying; }
/// Starts playback.
virtual void play() {
_doPlay();
_isPlaying = true;
}
/// Pauses playback.
virtual void pause() {
_doPause();
_isPlaying = false;
}
/// Rewinds.
virtual void rewind() {}
/// Locks mutex (default = no effect).
virtual void lockMutex() {}
/// Unlocks mutex (default = no effect).
virtual void unlockMutex() {}
virtual QString getType() const = 0;
protected:
virtual void _doPlay() {}
virtual void _doPause() {}
private:
bool _isPlaying;
};
class Color : public Paint
{
Q_OBJECT
Q_PROPERTY(QColor color READ getColor WRITE setColor)
protected:
QColor color;
public:
Q_INVOKABLE Color(int id=NULL_UID) : Paint(id) {}
Color(const QColor& color_, uid id=NULL_UID) : Paint(id), color(color_) {}
QColor getColor() const { return color; }
void setColor(const QColor& color_) { color = color_; }
virtual QString getType() const { return "color"; }
virtual QIcon getIcon() const {
QPixmap pixmap(100,100);
pixmap.fill(color);
return QIcon(pixmap);
}
};
/**
* Paint that uses an OpenGL texture to paint on potentially any MappingGui.
*
* This video texture is actually an OpenGL texture.
*/
class Texture : public Paint
{
Q_OBJECT
Q_PROPERTY(float x READ getX)
Q_PROPERTY(float y READ getY)
protected:
GLuint textureId;
GLfloat x;
GLfloat y;
mutable bool bitsChanged;
Texture(uid id=NULL_UID) :
Paint(id),
textureId(0),
x(0),
y(0)
{
}
public:
virtual ~Texture() {
// TODO: this needs to be fixed: it will not work unless it is executed from within a GL context
// see issue #229
if (textureId != 0)
glDeleteTextures(1, &textureId);
}
public:
virtual void update();
GLuint getTextureId() const { return textureId; }
virtual int getWidth() const = 0;
virtual int getHeight() const = 0;
/// Returns image bits data. Next call to bitsHaveChanged() will be false.
virtual const uchar* getBits() = 0;
/// Returns true iff bits have changed since last call to getBits().
virtual bool bitsHaveChanged() const = 0;
virtual GLfloat getX() const { return x; }
virtual GLfloat getY() const { return y; }
virtual void setX(GLfloat xPos) {
x = xPos;
}
virtual void setY(GLfloat yPos) {
y = yPos;
}
virtual void setPosition(GLfloat xPos, GLfloat yPos) {
setX(xPos);
setY(yPos);
}
virtual QRectF getRect() const { return QRectF(getX(), getY(), getWidth(), getHeight()); }
virtual void read(const QDomElement& obj);
virtual void write(QDomElement& obj);
protected:
// Lists QProperties that should NOT be parsed automatically.
virtual QList<QString> _propertiesSpecial() const { return Paint::_propertiesSpecial() << "x" << "y"; }
};
/**
* Paint that is a Texture loaded from an image file.
*/
class Image : public Texture
{
Q_OBJECT
Q_PROPERTY(QString uri READ getUri WRITE setUri)
Q_PROPERTY(double rate READ getRate WRITE setRate)
protected:
QString _uri;
QVector<QImage> _images;
double _rate;
uint _currentFrame;
qreal _currentFrameReal;
qreal _prevTime;
uchar* _bits;
QElapsedTimer _timer;
public:
Q_INVOKABLE Image(int id=NULL_UID);
Image(const QString uri_, uid id=NULL_UID);
virtual ~Image() {}
virtual void build();
virtual void update();
/// Rewinds.
virtual void rewind();
const QString getUri() const { return _uri; }
bool setUri(const QString &uri);
virtual QString getType() const { return "image"; }
bool isAnimation() const { return (_images.size() > 1); }
virtual int getWidth() const { return (_images.isEmpty() ? 0 : _images[0].width()); }
virtual int getHeight() const { return (_images.isEmpty() ? 0 : _images[0].height()); }
virtual const uchar* getBits();
virtual bool bitsHaveChanged() const { return bitsChanged; }
virtual QIcon getIcon() const
{
return QIcon(QPixmap::fromImage(_images[0]).scaled(MM::MAPPING_LIST_ICON_SIZE, MM::MAPPING_LIST_ICON_SIZE,
Qt::IgnoreAspectRatio));
}
/// Sets playback rate (in %). Negative values mean reverse playback.
virtual void setRate(double rate);
/// Returns playback rate.
double getRate() const { return _rate; }
protected:
/// Starts playback.
virtual void _doPlay();
/// Current elapsed time in seconds.
qreal _elapsedTime() const { return _timer.elapsed() / 1000.0; }
};
class VideoImpl; // forward declaration
/**
* Paint that is a Texture retrieved via a video file.
*/
class Video : public Texture
{
Q_OBJECT
Q_PROPERTY(QString uri READ getUri WRITE setUri)
Q_PROPERTY(double volume READ getVolume WRITE setVolume)
Q_PROPERTY(double rate READ getRate WRITE setRate)
public:
// Thumbnail generation timeout (in ms).
static const int ICON_TIMEOUT = 1000;
public:
Q_INVOKABLE Video(int id=NULL_UID);
Video(const QString uri_, VideoType type, double rate, uid id=NULL_UID);
virtual ~Video();
const QString getUri() const { return _uri; }
bool setUri(const QString &uri);
virtual void build();
virtual void update();
/// Rewinds.
virtual void rewind();
/// Locks mutex (default = no effect).
virtual void lockMutex();
/// Unlocks mutex (default = no effect).
virtual void unlockMutex();
virtual QString getType() const { return "media"; }
virtual int getWidth() const;
virtual int getHeight() const;
virtual const uchar* getBits();
virtual bool bitsHaveChanged() const;
/// Sets playback rate (in %). Negative values mean reverse playback.
virtual void setRate(double rate);
/// Returns playback rate.
double getRate() const;
/// Sets audio playback volume (in %).
virtual void setVolume(double volume);
/// Returns audio playback volume.
double getVolume() const;
/**
* Checks whether or not video is supported on this platform.
*/
static bool hasVideoSupport();
virtual QIcon getIcon() const { return _icon; }
protected:
/// Starts playback.
virtual void _doPlay();
/// Pauses playback.
virtual void _doPause();
// Try to generate a thumbnail from currently loaded movie.
bool _generateThumbnail();
QString _uri;
QIcon _icon;
/**
* Private implementation, so that GStreamer headers don't need
* to be included from every file in the project.
*/
VideoImpl *_impl;
};
}
#endif /* PAINT_H_ */