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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
* Copyright (C) 2013 Open Education Foundation
*
* Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
* l'Education Numérique en Afrique (GIP ENA)
*
* This file is part of OpenBoard.
*
* OpenBoard 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, version 3 of the License,
* with a specific linking exception for the OpenSSL project's
* "OpenSSL" library (or with modified versions of it that use the
* same license as the "OpenSSL" library).
*
* OpenBoard 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 OpenBoard. If not, see <http://www.gnu.org/licenses/>.
*/
#include "UBGraphicsGroupContainerItem.h"
#include "UBGraphicsMediaItem.h"
#include "UBGraphicsMediaItemDelegate.h"
#include "UBGraphicsScene.h"
#include "UBGraphicsDelegateFrame.h"
#include "document/UBDocumentProxy.h"
#include "core/UBApplication.h"
#include "board/UBBoardController.h"
#include "core/memcheck.h"
#include <QGraphicsVideoItem>
bool UBGraphicsMediaItem::sIsMutedByDefault = false;
/**
* @brief Create and return a UBGraphicsMediaItem instance. The type (audio or video) is determined from the URL.
* @param pMediaFileUrl The URL of the audio or video file
* @param parent (Optional) the parent item
* @return A pointer to the newly created instance.
*/
UBGraphicsMediaItem* UBGraphicsMediaItem::createMediaItem(const QUrl &pMediaFileUrl, QGraphicsItem* parent)
{
UBGraphicsMediaItem * mediaItem;
QString mediaPath = pMediaFileUrl.toString();
if ("" == mediaPath)
mediaPath = pMediaFileUrl.toLocalFile();
if (mediaPath.toLower().contains("videos"))
mediaItem = new UBGraphicsVideoItem(pMediaFileUrl, parent);
else if (mediaPath.toLower().contains("audios"))
mediaItem = new UBGraphicsAudioItem(pMediaFileUrl, parent);
return mediaItem;
}
UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsItem *parent)
: QGraphicsRectItem(parent)
, mMuted(sIsMutedByDefault)
, mMutedByUserAction(sIsMutedByDefault)
, mStopped(false)
, mMediaFileUrl(pMediaFileUrl)
, mLinkedImage(NULL)
, mInitialPos(0)
{
mErrorString = "";
mMediaObject = new QMediaPlayer(this);
mMediaObject->setMedia(pMediaFileUrl);
setDelegate(new UBGraphicsMediaItemDelegate(this));
setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::ObjectItem));
setFlag(ItemIsMovable, true);
setFlag(ItemSendsGeometryChanges, true);
connect(mMediaObject, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
Delegate(), SLOT(mediaStatusChanged(QMediaPlayer::MediaStatus)));
connect(mMediaObject, SIGNAL(stateChanged(QMediaPlayer::State)),
Delegate(), SLOT(mediaStateChanged(QMediaPlayer::State)));
connect(mMediaObject, SIGNAL(positionChanged(qint64)),
Delegate(), SLOT(updateTicker(qint64)));
connect(mMediaObject, SIGNAL(durationChanged(qint64)),
Delegate(), SLOT(totalTimeChanged(qint64)));
connect(Delegate(), SIGNAL(showOnDisplayChanged(bool)),
this, SLOT(showOnDisplayChanged(bool)));
connect(mMediaObject, static_cast<void(QMediaPlayer::*)(QMediaPlayer::Error)>(&QMediaPlayer::error),
this, &UBGraphicsMediaItem::mediaError);
}
UBGraphicsAudioItem::UBGraphicsAudioItem(const QUrl &pMediaFileUrl, QGraphicsItem *parent)
:UBGraphicsMediaItem(pMediaFileUrl, parent)
{
haveLinkedImage = false;
Delegate()->createControls();
Delegate()->frame()->setOperationMode(UBGraphicsDelegateFrame::ResizingHorizontally);
this->setSize(320, 26);
this->setMinimumSize(QSize(150, 26));
mMediaObject->setNotifyInterval(1000);
}
UBGraphicsVideoItem::UBGraphicsVideoItem(const QUrl &pMediaFileUrl, QGraphicsItem *parent)
:UBGraphicsMediaItem(pMediaFileUrl, parent)
{
haveLinkedImage = true;
setPlaceholderVisible(true);
Delegate()->createControls();
mVideoItem = new QGraphicsVideoItem(this);
mVideoItem->setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Object);
mVideoItem->setFlag(ItemStacksBehindParent, true);
mMediaObject->setVideoOutput(mVideoItem);
mMediaObject->setNotifyInterval(50);
setMinimumSize(QSize(320, 240));
setSize(320, 240);
connect(mVideoItem, SIGNAL(nativeSizeChanged(QSizeF)),
this, SLOT(videoSizeChanged(QSizeF)));
connect(mMediaObject, SIGNAL(videoAvailableChanged(bool)),
this, SLOT(hasVideoChanged(bool)));
connect(mMediaObject, SIGNAL(stateChanged(QMediaPlayer::State)),
this, SLOT(mediaStateChanged(QMediaPlayer::State)));
connect(mMediaObject, static_cast<void(QMediaPlayer::*)(QMediaPlayer::Error)>(&QMediaPlayer::error),
this, &UBGraphicsVideoItem::mediaError);
setAcceptHoverEvents(true);
update();
}
UBGraphicsMediaItem::~UBGraphicsMediaItem()
{
if (mMediaObject)
mMediaObject->stop();
}
QVariant UBGraphicsMediaItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
if ((change == QGraphicsItem::ItemEnabledChange)
|| (change == QGraphicsItem::ItemSceneChange)
|| (change == QGraphicsItem::ItemVisibleChange))
{
if (mMediaObject && (!isEnabled() || !isVisible() || !scene()))
mMediaObject->pause();
}
else if (change == QGraphicsItem::ItemSceneHasChanged)
{
if (!scene())
mMediaObject->stop();
else {
QString absoluteMediaFilename;
if(mMediaFileUrl.toLocalFile().startsWith("audios/") || mMediaFileUrl.toLocalFile().startsWith("videos/"))
absoluteMediaFilename = scene()->document()->persistencePath() + "/" + mMediaFileUrl.toLocalFile();
else
absoluteMediaFilename = mMediaFileUrl.toLocalFile();
if (absoluteMediaFilename.length() > 0)
mMediaObject->setMedia(QUrl::fromLocalFile(absoluteMediaFilename));
}
}
if (Delegate()) {
QVariant newValue = Delegate()->itemChange(change, value);
return QGraphicsRectItem::itemChange(change, newValue);
}
return QGraphicsRectItem::itemChange(change, value);
}
void UBGraphicsMediaItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->save();
//painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
Delegate()->postpaint(painter, option, widget);
painter->restore();
}
QMediaPlayer::State UBGraphicsMediaItem::playerState() const
{
return mMediaObject->state();
}
/**
* @brief Returns true if the video was manually stopped, false otherwise.
*/
bool UBGraphicsMediaItem::isStopped() const
{
return mStopped;
}
qint64 UBGraphicsMediaItem::mediaDuration() const
{
return mMediaObject->duration();
}
qint64 UBGraphicsMediaItem::mediaPosition() const
{
return mMediaObject->position();
}
bool UBGraphicsMediaItem::isMediaSeekable() const
{
return mMediaObject->isSeekable();
}
/**
* @brief Set the item's minimum size. If the current size is smaller, it will be resized.
* @param size The new minimum size
*/
void UBGraphicsMediaItem::setMinimumSize(const QSize& size)
{
mMinimumSize = size;
QSizeF newSize = rect().size();
int width = newSize.width();
int height = newSize.height();
if (rect().width() < mMinimumSize.width())
width = mMinimumSize.width();
if (rect().height() < mMinimumSize.height())
height = mMinimumSize.height();
this->setSize(width, height);
}
void UBGraphicsMediaItem::setMediaFileUrl(QUrl url)
{
mMediaFileUrl = url;
}
void UBGraphicsMediaItem::setInitialPos(qint64 p)
{
mInitialPos = p;
}
void UBGraphicsMediaItem::setMediaPos(qint64 p)
{
mMediaObject->setPosition(p);
}
void UBGraphicsMediaItem::setSelected(bool selected)
{
if(selected){
Delegate()->createControls();
if (this->getMediaType() == mediaType_Audio)
Delegate()->frame()->setOperationMode(UBGraphicsDelegateFrame::ResizingHorizontally);
else
Delegate()->frame()->setOperationMode(UBGraphicsDelegateFrame::Resizing);
}
QGraphicsRectItem::setSelected(selected);
}
void UBGraphicsMediaItem::setSourceUrl(const QUrl &pSourceUrl)
{
UBItem::setSourceUrl(pSourceUrl);
}
void UBGraphicsMediaItem::clearSource()
{
QString path = mediaFileUrl().toLocalFile();
//if path is absolute clean duplicated path string
if (!path.contains(UBApplication::boardController->selectedDocument()->persistencePath()))
path = UBApplication::boardController->selectedDocument()->persistencePath() + "/" + path;
if (!UBFileSystemUtils::deleteFile(path))
qDebug() << "cannot delete file: " << path;
}
void UBGraphicsMediaItem::toggleMute()
{
mMuted = !mMuted;
setMute(mMuted);
}
void UBGraphicsMediaItem::setMute(bool bMute)
{
mMuted = bMute;
mMediaObject->setMuted(mMuted);
mMutedByUserAction = mMuted;
sIsMutedByDefault = mMuted;
}
UBGraphicsScene* UBGraphicsMediaItem::scene()
{
return qobject_cast<UBGraphicsScene*>(QGraphicsItem::scene());
}
void UBGraphicsMediaItem::activeSceneChanged()
{
if (UBApplication::boardController->activeScene() != scene())
mMediaObject->pause();
}
void UBGraphicsMediaItem::showOnDisplayChanged(bool shown)
{
if (!shown) {
mMuted = true;
mMediaObject->setMuted(mMuted);
}
else if (!mMutedByUserAction) {
mMuted = false;
mMediaObject->setMuted(mMuted);
}
}
void UBGraphicsMediaItem::play()
{
mMediaObject->play();
mStopped = false;
}
void UBGraphicsMediaItem::pause()
{
mMediaObject->pause();
mStopped = false;
}
void UBGraphicsMediaItem::stop()
{
mMediaObject->stop();
mStopped = true;
}
void UBGraphicsMediaItem::togglePlayPause()
{
if (!mErrorString.isEmpty()) {
UBApplication::showMessage("Can't play media: " + mErrorString);
return;
}
if (mMediaObject->state() == QMediaPlayer::StoppedState)
mMediaObject->play();
else if (mMediaObject->state() == QMediaPlayer::PlayingState) {
if ((mMediaObject->duration() - mMediaObject->position()) <= 0) {
mMediaObject->stop();
mMediaObject->play();
}
else {
mMediaObject->pause();
if(scene())
scene()->setModified(true);
}
}
else if (mMediaObject->state() == QMediaPlayer::PausedState) {
if ((mMediaObject->duration() - mMediaObject->position()) <= 0)
mMediaObject->stop();
mMediaObject->play();
}
else if ( mMediaObject->mediaStatus() == QMediaPlayer::LoadingMedia) {
mMediaObject->setMedia(mediaFileUrl());
mMediaObject->play();
}
}
void UBGraphicsMediaItem::mediaError(QMediaPlayer::Error errorCode)
{
// QMediaPlayer::errorString() isn't very descriptive, so we generate our own message
switch (errorCode) {
case QMediaPlayer::NoError:
mErrorString = "";
break;
case QMediaPlayer::ResourceError:
mErrorString = tr("Media resource couldn't be resolved");
break;
case QMediaPlayer::FormatError:
mErrorString = tr("Unsupported media format");
break;
case QMediaPlayer::ServiceMissingError:
mErrorString = tr("Media playback service not found");
break;
default:
mErrorString = tr("Media error: ") + QString(errorCode) + " (" + mMediaObject->errorString() + ")";
}
if (!mErrorString.isEmpty() ) {
UBApplication::showMessage(mErrorString);
qDebug() << mErrorString;
}
}
void UBGraphicsMediaItem::copyItemParameters(UBItem *copy) const
{
UBGraphicsMediaItem *cp = dynamic_cast<UBGraphicsMediaItem*>(copy);
if (cp)
{
cp->setPos(this->pos());
cp->setTransform(this->transform());
cp->setFlag(QGraphicsItem::ItemIsMovable, true);
cp->setFlag(QGraphicsItem::ItemIsSelectable, true);
cp->setData(UBGraphicsItemData::ItemLayerType, this->data(UBGraphicsItemData::ItemLayerType));
cp->setData(UBGraphicsItemData::ItemLocked, this->data(UBGraphicsItemData::ItemLocked));
cp->setSourceUrl(this->sourceUrl());
cp->setSize(rect().width(), rect().height());
cp->setZValue(this->zValue());
connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), cp, SLOT(activeSceneChanged()));
// TODO UB 4.7 complete all members
}
}
void UBGraphicsMediaItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (Delegate()) {
Delegate()->mousePressEvent(event);
if (parentItem() && UBGraphicsGroupContainerItem::Type == parentItem()->type()) {
UBGraphicsGroupContainerItem *group = qgraphicsitem_cast<UBGraphicsGroupContainerItem*>(parentItem());
if (group) {
QGraphicsItem *curItem = group->getCurrentItem();
if (curItem && this != curItem)
group->deselectCurrentItem();
group->setCurrentItem(this);
this->setSelected(true);
Delegate()->positionHandles();
}
}
}
if (parentItem() && parentItem()->type() == UBGraphicsGroupContainerItem::Type) {
mShouldMove = false;
if (!Delegate()->mousePressEvent(event))
event->accept();
}
else {
mShouldMove = (event->buttons() & Qt::LeftButton);
mMousePressPos = event->scenePos();
mMouseMovePos = mMousePressPos;
event->accept();
setSelected(true);
}
QGraphicsRectItem::mousePressEvent(event);
}
QRectF UBGraphicsMediaItem::boundingRect() const
{
return rect();
}
void UBGraphicsMediaItem::setSize(int width, int height)
{
QRectF r = rect();
r.setWidth(width);
r.setHeight(height);
setRect(r);
if (Delegate())
Delegate()->positionHandles();
if (scene())
scene()->setModified(true);
}
UBItem* UBGraphicsAudioItem::deepCopy() const
{
QUrl url = this->mediaFileUrl();
UBGraphicsMediaItem *copy = new UBGraphicsAudioItem(url, parentItem());
copy->setUuid(this->uuid()); // this is OK for now as long as Widgets are imutable
copyItemParameters(copy);
return copy;
}
UBItem* UBGraphicsVideoItem::deepCopy() const
{
QUrl url = this->mediaFileUrl();
UBGraphicsMediaItem *copy = new UBGraphicsVideoItem(url, parentItem());
copy->setUuid(this->uuid());
copyItemParameters(copy);
return copy;
}
void UBGraphicsVideoItem::setSize(int width, int height)
{
// Resize the video, then the rest of the Item
int sizeX = 0;
int sizeY = 0;
if (mMinimumSize.width() > width)
sizeX = mMinimumSize.width();
else
sizeX = width;
if (mMinimumSize.height() > height)
sizeY = mMinimumSize.height();
else
sizeY = height;
mVideoItem->setSize(QSize(sizeX, sizeY));
UBGraphicsMediaItem::setSize(sizeX, sizeY);
}
void UBGraphicsVideoItem::videoSizeChanged(QSizeF newSize)
{
/* Depending on the platform/video backend, video size information becomes
* available at different times (either when the file is loaded, or when
* playback begins), so this slot is needed to resize the video item as
* soon as the information is available.
*/
// We don't want the video item to resize when the video is stopped or finished;
// and in those cases, the new size is reported as (0, 0).
if (newSize != QSizeF(0,0))
this->setSize(newSize.width(), newSize.height());
else // Make sure the toolbar doesn't disappear
Delegate()->showToolBar(false);
}
void UBGraphicsVideoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
// When selected, a QGraphicsRectItem is drawn with a dashed line border. We don't want this
QStyleOptionGraphicsItem styleOption = QStyleOptionGraphicsItem(*option);
styleOption.state &= ~QStyle::State_Selected;
QGraphicsRectItem::paint(painter, &styleOption, widget);
UBGraphicsMediaItem::paint(painter, option, widget);
}
void UBGraphicsVideoItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
// Display the seek bar
Delegate()->showToolBar();
QGraphicsRectItem::hoverEnterEvent(event);
}
void UBGraphicsVideoItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
Delegate()->showToolBar();
QGraphicsRectItem::hoverMoveEvent(event);
}
void UBGraphicsVideoItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
QGraphicsRectItem::hoverLeaveEvent(event);
}
void UBGraphicsVideoItem::hasVideoChanged(bool hasVideo)
{
// On Linux, this is called (with hasVideo == true) when the video is first played
// and when it finishes (hasVideo == false). But on Windows and OS X, it isn't called when
// the video finishes, so those platforms require another solution to showing/hiding the
// placeholder black rectangle.
setPlaceholderVisible(!hasVideo);
}
void UBGraphicsVideoItem::mediaStateChanged(QMediaPlayer::State state)
{
#if defined(Q_OS_OSX) || defined(Q_OS_WIN)
setPlaceholderVisible((state == QMediaPlayer::StoppedState));
#else
Q_UNUSED(state);
#endif
}
void UBGraphicsVideoItem::activeSceneChanged()
{
// Update the visibility of the placeholder, to prevent it being hidden when switching pages
setPlaceholderVisible(!mErrorString.isEmpty());
UBGraphicsMediaItem::activeSceneChanged();
}
void UBGraphicsVideoItem::mediaError(QMediaPlayer::Error errorCode)
{
setPlaceholderVisible(errorCode != QMediaPlayer::NoError);
}
/**
* @brief Set the brush and fill to display a black rectangle
* @param visible If true, a black rectangle is displayed in place of the video
*
* Depending on platforms, when a video is finished or stopped, the video might be
* removed altogether. To avoid just having the controls bar visible at that point,
* we can display a "fake" black video frame in its place using this method.
*/
void UBGraphicsVideoItem::setPlaceholderVisible(bool visible)
{
if (visible) {
setBrush(QColor(Qt::black));
setPen(QColor(Qt::white));
}
else {
setBrush(QColor(Qt::transparent));
setPen(QColor(Qt::transparent));
}
}