UBGraphicsMediaItemDelegate.cpp 9.24 KB
Newer Older
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * 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).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
25 26
 */

27

Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

30 31 32 33 34
#include <QtGui>
#include <QtSvg>

#include "UBGraphicsMediaItem.h"
#include "UBGraphicsMediaItemDelegate.h"
35
#include "UBGraphicsDelegateFrame.h"
36 37 38 39 40 41 42 43 44 45 46 47

#include "UBGraphicsScene.h"

#include "core/UBSettings.h"
#include "core/UBApplication.h"
#include "core/UBApplicationController.h"
#include "core/UBDisplayManager.h"

#include "domain/UBGraphicsMediaItem.h"

#include "core/memcheck.h"

48
UBGraphicsMediaItemDelegate::UBGraphicsMediaItemDelegate(UBGraphicsMediaItem* pDelegated, QObject * parent)
49 50
    : UBGraphicsItemDelegate(pDelegated, parent, GF_COMMON
                             | GF_RESPECT_RATIO
51
                             | GF_TOOLBAR_USED)
52
    , mPlayPauseButton(NULL)
53 54
    , mToolBarShowTimer(NULL)
    , m_iToolBarShowingInterval(5000)
55 56 57 58
{
    QPalette palette;
    palette.setBrush ( QPalette::Light, Qt::darkGray );

59 60
    if (delegated()->isMuted())
        delegated()->setMute(true);
61

62 63 64 65 66 67
}

bool UBGraphicsMediaItemDelegate::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event);
    mToolBarItem->show();
68
    positionHandles();
69 70 71 72

    if (mToolBarShowTimer)
        mToolBarShowTimer->start();

73 74 75
    return UBGraphicsItemDelegate::mousePressEvent(event);
}

76 77 78
/**
 * @brief Show the toolbar (play/pause, seek, mute).
 *
79 80
 * The toolbar then auto-hides after a set amount of time, if the video is currently
 * playing or is paused.
81
 */
82
void UBGraphicsMediaItemDelegate::showToolBar(bool autohide)
83 84
{
    mToolBarItem->show();
85 86 87 88 89 90 91 92 93 94
    if (mToolBarShowTimer) {

        if (delegated()->isPlaying() || delegated()->isPaused())
            mToolBarShowTimer->start();
        else
            mToolBarShowTimer->stop();

        // Don't hide the toolbar if we're at the beginning of the video
        if (delegated()->mediaPosition() == delegated()->initialPos())
            mToolBarShowTimer->stop();
95 96 97 98

        // Don't hide the toolbar if it was explicitly requested
        if (!autohide)
            mToolBarShowTimer->stop();
99
    }
100 101
}

102 103 104 105 106
void UBGraphicsMediaItemDelegate::hideToolBar()
{
    mToolBarItem->hide();
}

107 108
void UBGraphicsMediaItemDelegate::buildButtons()
{
109 110
    if(!mPlayPauseButton){
        mPlayPauseButton = new DelegateButton(":/images/play.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
111 112
        connect(mPlayPauseButton, SIGNAL(clicked(bool)),
                this, SLOT(togglePlayPause()));
113

114
        mStopButton = new DelegateButton(":/images/stop.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
115
        connect(mStopButton, SIGNAL(clicked(bool)),
116
                delegated(), SLOT(stop()));
117

118 119 120
        mMediaControl = new DelegateMediaControl(delegated(), mToolBarItem);
        mMediaControl->setFlag(QGraphicsItem::ItemIsSelectable, true);
        UBGraphicsItem::assignZValue(mMediaControl, delegated()->zValue());
Claudio Valerio's avatar
Claudio Valerio committed
121

122 123 124 125
        if (delegated()->isMuted())
            mMuteButton = new DelegateButton(":/images/soundOff.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
        else
            mMuteButton = new DelegateButton(":/images/soundOn.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
126

127 128 129 130
        connect(mMuteButton, SIGNAL(clicked(bool)),
                delegated(), SLOT(toggleMute()));
        connect(mMuteButton, SIGNAL(clicked(bool)),
                this, SLOT(toggleMute())); // for changing button image
131

132
        mToolBarButtons << mPlayPauseButton << mStopButton << mMuteButton;
133

134 135 136
        mToolBarItem->setItemsOnToolBar(QList<QGraphicsItem*>() << mPlayPauseButton << mStopButton << mMediaControl  << mMuteButton );
        mToolBarItem->setVisibleOnBoard(true);
        mToolBarItem->setShifting(false);
137

138 139 140 141
        if (!mToolBarShowTimer) {
            if (delegated()->hasLinkedImage()) {
                mToolBarShowTimer = new QTimer();
                mToolBarShowTimer->setInterval(m_iToolBarShowingInterval);
142
                connect(mToolBarShowTimer, SIGNAL(timeout()), this, SLOT(hideToolBar()));
143 144 145 146
            }
        }

        else {
147 148 149 150 151 152 153 154 155 156 157
            connect(mPlayPauseButton, SIGNAL(clicked(bool)),
                    mToolBarShowTimer, SLOT(start()));

            connect(mStopButton, SIGNAL(clicked(bool)),
                    mToolBarShowTimer, SLOT(start()));

            connect(mMediaControl, SIGNAL(used()),
                    mToolBarShowTimer, SLOT(start()));

            connect(mMuteButton, SIGNAL(clicked(bool)),
                    mToolBarShowTimer, SLOT(start()));
158
        }
159

160

161 162
        positionHandles();
    }
163 164 165 166
}

UBGraphicsMediaItemDelegate::~UBGraphicsMediaItemDelegate()
{
167
    if (mToolBarShowTimer){
168
        delete mToolBarShowTimer;
169 170
        mToolBarShowTimer = NULL;
    }
171 172 173 174 175 176 177 178 179
}

void UBGraphicsMediaItemDelegate::positionHandles()
{
    UBGraphicsItemDelegate::positionHandles();

    UBGraphicsMediaItem *mediaItem = dynamic_cast<UBGraphicsMediaItem*>(mDelegated);
    if (mediaItem)
    {
180
        QRectF toolBarRect = mToolBarItem->rect();
181

182
        mToolBarItem->setPos(0, mediaItem->boundingRect().height()-mToolBarItem->rect().height());
183

184
        toolBarRect.setWidth(mediaItem->boundingRect().width());
185 186
        mToolBarItem->show();

187
        mToolBarItem->setRect(toolBarRect);
188 189
    }

190
    int toolBarButtonsWidth = 0;
191
    foreach (DelegateButton* button, mToolBarButtons)
192
        toolBarButtonsWidth += button->boundingRect().width() + mToolBarItem->getElementsPadding();
193 194

    QRectF mediaItemRect = mMediaControl->rect();
195
    mediaItemRect.setWidth(mediaItem->boundingRect().width() - toolBarButtonsWidth);
196 197 198 199
    mediaItemRect.setHeight(mToolBarItem->boundingRect().height());
    mMediaControl->setRect(mediaItemRect);

    mToolBarItem->positionHandles();
Claudio Valerio's avatar
Claudio Valerio committed
200
    mMediaControl->positionHandles();
201

202
    if (mediaItem)
Claudio Valerio's avatar
Claudio Valerio committed
203
        mToolBarItem->show();
204 205 206 207
}

void UBGraphicsMediaItemDelegate::remove(bool canUndo)
{
208 209
    if (delegated())
        delegated()->stop();
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

    UBGraphicsItemDelegate::remove(canUndo);
}


void UBGraphicsMediaItemDelegate::toggleMute()
{
    if (delegated()->isMuted())
        mMuteButton->setFileName(":/images/soundOff.svg");
    else
        mMuteButton->setFileName(":/images/soundOn.svg");
}


UBGraphicsMediaItem* UBGraphicsMediaItemDelegate::delegated()
{
    return dynamic_cast<UBGraphicsMediaItem*>(mDelegated);
}

void UBGraphicsMediaItemDelegate::togglePlayPause()
{
231 232
    if (delegated())
        delegated()->togglePlayPause();
233 234
}

235
void UBGraphicsMediaItemDelegate::mediaStatusChanged(QMediaPlayer::MediaStatus status)
236
{
237 238
    // Possible statuses are: UnknownMediaStatus, NoMedia, LoadingMedia, LoadedMedia,
    // StalledMedia, BufferingMedia, BufferedMedia, EndOfMedia, InvalidMedia
239

240 241
    //qDebug() << "Media status changed to " << status << "; state: " << delegated()->playerState();

242
    if (status == QMediaPlayer::LoadedMedia)
243
        mMediaControl->totalTimeChanged(delegated()->mediaDuration());
244

245 246
    // At the beginning of the video, play/pause to load and display the first frame
    if ((status == QMediaPlayer::LoadedMedia || status == QMediaPlayer::BufferedMedia)
247 248
            && delegated()->mediaPosition() == delegated()->initialPos()
            && !delegated()->isStopped()) {
249 250 251 252
        delegated()->play();
        delegated()->pause();
    }

253 254
    // At the end of the video, make sure the progress bar doesn't autohide
    if (status == QMediaPlayer::EndOfMedia)
255
        showToolBar(false);
256

257

258
    // in most cases, the only necessary action is to update the play/pause state
259 260 261
    updatePlayPauseState();
}

262 263
void UBGraphicsMediaItemDelegate::mediaStateChanged(QMediaPlayer::State state)
{
Craig Watson's avatar
Craig Watson committed
264
    Q_UNUSED(state);
265 266 267 268 269 270
    // Possible states are StoppedState, PlayingState and PausedState

    // updatePlayPauseState handles this functionality
    updatePlayPauseState();
}

271 272 273

void UBGraphicsMediaItemDelegate::updatePlayPauseState()
{
274
    if (delegated()->playerState() == QMediaPlayer::PlayingState)
275 276 277 278 279 280 281 282
        mPlayPauseButton->setFileName(":/images/pause.svg");
    else
        mPlayPauseButton->setFileName(":/images/play.svg");
}


void UBGraphicsMediaItemDelegate::updateTicker(qint64 time)
{
283
    mMediaControl->totalTimeChanged(delegated()->mediaDuration());
284 285 286 287 288 289 290
    mMediaControl->updateTicker(time);
}


void UBGraphicsMediaItemDelegate::totalTimeChanged(qint64 newTotalTime)
{
    mMediaControl->totalTimeChanged(newTotalTime);
291
}
292 293 294 295 296 297 298 299 300

void UBGraphicsMediaItemDelegate::showHide(bool show)
{
    QVariant showFlag = QVariant(show ? UBItemLayerType::Object : UBItemLayerType::Control);
    showHideRecurs(showFlag, mDelegated);
    mDelegated->update();

    emit showOnDisplayChanged(show);
}