UBGraphicsMediaItemDelegate.cpp 9.38 KB
Newer Older
1 2 3
/*
 * 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
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * (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/>.
 */

#include <QtGui>
#include <QtSvg>

#include "UBGraphicsMediaItem.h"
#include "UBGraphicsMediaItemDelegate.h"
21
#include "UBGraphicsDelegateFrame.h"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

#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"

UBGraphicsMediaItemDelegate::UBGraphicsMediaItemDelegate(UBGraphicsMediaItem* pDelegated, Phonon::MediaObject* pMedia, QObject * parent)
    : UBGraphicsItemDelegate(pDelegated, parent, true, false)
    , mMedia(pMedia)
37 38
    , mToolBarShowTimer(NULL)
    , m_iToolBarShowingInterval(5000)
39 40 41 42 43 44 45 46 47
{
    QPalette palette;
    palette.setBrush ( QPalette::Light, Qt::darkGray );

    mMedia->setTickInterval(50);
    connect(mMedia, SIGNAL(stateChanged (Phonon::State, Phonon::State)), this, SLOT(mediaStateChanged (Phonon::State, Phonon::State)));
    connect(mMedia, SIGNAL(finished()), this, SLOT(updatePlayPauseState()));
    connect(mMedia, SIGNAL(tick(qint64)), this, SLOT(updateTicker(qint64)));
    connect(mMedia, SIGNAL(totalTimeChanged(qint64)), this, SLOT(totalTimeChanged(qint64)));
48 49 50 51 52 53 54

    if (delegated()->hasLinkedImage())
    {
        mToolBarShowTimer = new QTimer();
        connect(mToolBarShowTimer, SIGNAL(timeout()), this, SLOT(hideToolBar()));
        mToolBarShowTimer->setInterval(m_iToolBarShowingInterval);
    }
55 56 57 58
    if (delegated()->isMuted())
    {
        delegated()->setMute(true);
    }
59 60 61 62

    //Wrapper function. Use it to set correct data() to QGraphicsItem as well
    setFlippable(false);
    setRotatable(false);
63 64 65 66 67 68
}

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

    if (mToolBarShowTimer)
        mToolBarShowTimer->start();

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

76 77 78 79 80
void UBGraphicsMediaItemDelegate::hideToolBar()
{
    mToolBarItem->hide();
}

81 82 83 84 85 86 87 88 89 90 91
void UBGraphicsMediaItemDelegate::buildButtons()
{
    mPlayPauseButton = new DelegateButton(":/images/play.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
    connect(mPlayPauseButton, SIGNAL(clicked(bool)), this, SLOT(togglePlayPause()));

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

    mMediaControl = new DelegateMediaControl(delegated(), mToolBarItem);
    mMediaControl->setFlag(QGraphicsItem::ItemIsSelectable, true);
    UBGraphicsItem::assignZValue(mMediaControl, delegated()->zValue());
92
    
93 94 95 96 97 98 99 100
    if (delegated()->isMuted())
        mMuteButton = new DelegateButton(":/images/soundOff.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
    else
        mMuteButton = new DelegateButton(":/images/soundOn.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);

    connect(mMuteButton, SIGNAL(clicked(bool)), delegated(), SLOT(toggleMute())); 
    connect(mMuteButton, SIGNAL(clicked(bool)), this, SLOT(toggleMute())); // for changing button image

101
    mToolBarButtons << mPlayPauseButton << mStopButton << mMuteButton;
102 103 104 105 106

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

107 108 109 110 111 112 113 114
    if (mToolBarShowTimer)
    {
        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()));
    }

115 116 117 118 119 120 121 122 123 124 125 126
    UBGraphicsMediaItem *audioItem = dynamic_cast<UBGraphicsMediaItem*>(mDelegated);
    if (audioItem)
    {
        if (audioItem->getMediaType() == UBGraphicsMediaItem::mediaType_Audio)
        {
            positionHandles();
        }
    }
}

UBGraphicsMediaItemDelegate::~UBGraphicsMediaItemDelegate()
{
127 128
    if (mToolBarShowTimer)
        delete mToolBarShowTimer;
129 130 131 132 133 134 135 136 137
}

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

    UBGraphicsMediaItem *mediaItem = dynamic_cast<UBGraphicsMediaItem*>(mDelegated);
    if (mediaItem)
    {
138
        QRectF toolBarRect = mToolBarItem->rect();
Aleksei Kanash's avatar
Aleksei Kanash committed
139 140
        if (mediaItem->getMediaType() == UBGraphicsMediaItem::mediaType_Video)
        {      
Aleksei Kanash's avatar
Aleksei Kanash committed
141
            mToolBarItem->setPos(0, delegated()->boundingRect().height()-mToolBarItem->rect().height());
142

Aleksei Kanash's avatar
Aleksei Kanash committed
143
            toolBarRect.setWidth(delegated()->boundingRect().width());
Aleksei Kanash's avatar
Aleksei Kanash committed
144 145
        }
        else if (mediaItem->getMediaType() == UBGraphicsMediaItem::mediaType_Audio)
146
        {
147
            int borderSize = 0;
148
            UBAudioPresentationWidget *audioWidget = dynamic_cast<UBAudioPresentationWidget*>(delegated()->widget());
149 150 151
            if (audioWidget)
                borderSize = audioWidget->borderSize();

Aleksei Kanash's avatar
Aleksei Kanash committed
152 153
            mToolBarItem->setPos(borderSize,delegated()->boundingRect().height()-(mToolBarItem->rect().height() + borderSize));
            toolBarRect.setWidth((delegated()->boundingRect().width()-2*borderSize));
154 155
            mToolBarItem->show();
        }
156 157

        mToolBarItem->setRect(toolBarRect);
158 159
    }

Aleksei Kanash's avatar
Aleksei Kanash committed
160
    int toolBarMinimumWidth = 0;
161
    int mediaItemWidth = mToolBarItem->boundingRect().width();
162
    foreach (DelegateButton* button, mToolBarButtons)
163
    {
164 165
        mediaItemWidth -= button->boundingRect().width() + mToolBarItem->getElementsPadding();
        toolBarMinimumWidth += button->boundingRect().width() + mToolBarItem->getElementsPadding();
Aleksei Kanash's avatar
Aleksei Kanash committed
166 167 168
    }
    toolBarMinimumWidth += mToolBarItem->boundingRect().height();

169
    UBAudioPresentationWidget* pAudioWidget = dynamic_cast<UBAudioPresentationWidget*>(delegated()->widget());
Aleksei Kanash's avatar
Aleksei Kanash committed
170 171 172
    if (pAudioWidget)
    {
       pAudioWidget->setMinimumSize(toolBarMinimumWidth + (int)mMediaControl->lcdAreaSize().width() + (int)mMediaControl->rect().height(),26+pAudioWidget->borderSize());
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
    }

    QRectF mediaItemRect = mMediaControl->rect();
    mediaItemRect.setWidth(mediaItemWidth);
    mediaItemRect.setHeight(mToolBarItem->boundingRect().height());
    mMediaControl->setRect(mediaItemRect);

    mToolBarItem->positionHandles();
    mMediaControl->positionHandles(); 

    if (mediaItem)
    {
        if (mediaItem->getMediaType() == UBGraphicsMediaItem::mediaType_Audio)
        {
            mToolBarItem->show();
        }
    }
}

void UBGraphicsMediaItemDelegate::remove(bool canUndo)
{
    if (delegated() && delegated()->mediaObject())
        delegated()->mediaObject()->stop();

    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()
{
    if (delegated() && delegated()->mediaObject()) {

        Phonon::MediaObject* media = delegated()->mediaObject();
        if (media->state() == Phonon::StoppedState) {
            media->play();
        } else if (media->state() == Phonon::PlayingState) {
            if (media->remainingTime() <= 0) {
                media->stop();
                media->play();
            } else {
                media->pause();
                if(delegated()->scene())
                        delegated()->scene()->setModified(true);
            }
        } else if (media->state() == Phonon::PausedState) {
            if (media->remainingTime() <= 0) {
                media->stop();
            }
            media->play();
        } else  if ( media->state() == Phonon::LoadingState ) {
            delegated()->mediaObject()->setCurrentSource(delegated()->mediaFileUrl());
            media->play();
        } else if (media->state() == Phonon::ErrorState){
            qDebug() << "Error appeared." << media->errorString();
        }
    }
}

void UBGraphicsMediaItemDelegate::mediaStateChanged ( Phonon::State newstate, Phonon::State oldstate )
{
    Q_UNUSED(newstate);
    Q_UNUSED(oldstate);
    updatePlayPauseState();
}


void UBGraphicsMediaItemDelegate::updatePlayPauseState()
{
    Phonon::MediaObject* media = delegated()->mediaObject();

    if (media->state() == Phonon::PlayingState)
        mPlayPauseButton->setFileName(":/images/pause.svg");
    else
        mPlayPauseButton->setFileName(":/images/play.svg");
}


void UBGraphicsMediaItemDelegate::updateTicker(qint64 time)
{
    Phonon::MediaObject* media = delegated()->mediaObject();
    mMediaControl->totalTimeChanged(media->totalTime());
    mMediaControl->updateTicker(time);
}


void UBGraphicsMediaItemDelegate::totalTimeChanged(qint64 newTotalTime)
{
    mMediaControl->totalTimeChanged(newTotalTime);
276
}