UBMediaWidget.cpp 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
15
#include "core/UBApplication.h"
16
#include "globals/UBGlobals.h"
17 18
#include "UBMediaWidget.h"

19 20 21 22 23 24
/**
  * \brief Constructor
  * @param type as the media type
  * @param parent as the parent widget
  * @param name as the object name
  */
25
UBMediaWidget::UBMediaWidget(eMediaType type, QWidget *parent, const char *name):UBActionableWidget(parent, name)
26 27 28 29 30 31 32 33 34
  , mpMediaObject(NULL)
  , mpVideoWidget(NULL)
  , mpAudioOutput(NULL)
  , mpPlayStopButton(NULL)
  , mpPauseButton(NULL)
  , mpSlider(NULL)
  , mAutoUpdate(false)
  , mGeneratingThumbnail(false)
  , mBorder(5)
shibakaneki's avatar
shibakaneki committed
35 36
  , mpMediaContainer(NULL)
  , mpCover(NULL)
37 38
//  , mpVideoStackedWidget(NULL)
//  , mpSnapshotVideoWidget(NULL)
39
{
40
    SET_STYLE_SHEET();
41

42
    addAction(eAction_Close);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    mType = type;
    setLayout(&mLayout);

    mpPlayStopButton = new UBMediaButton(this);
    mpPlayStopButton->setPixmap(QPixmap(":images/play.svg"));
    mpPauseButton = new UBMediaButton(this);
    mpPauseButton->setPixmap(QPixmap(":images/pause.svg"));
    mpPauseButton->setEnabled(false);
    mpSlider = new QSlider(this);
    mpSlider->setOrientation(Qt::Horizontal);
    mpSlider->setMinimum(0);
    mpSlider->setMaximum(0);

    mSeekerLayout.addWidget(mpPlayStopButton, 0);
    mSeekerLayout.addWidget(mpPauseButton, 0);
    mSeekerLayout.addWidget(mpSlider, 1);
59
    mSeekerLayout.setContentsMargins(0, 0, 0, 0);
60 61 62 63 64 65

    connect(mpPlayStopButton, SIGNAL(clicked()), this, SLOT(onPlayStopClicked()));
    connect(mpPauseButton, SIGNAL(clicked()), this, SLOT(onPauseClicked()));
    connect(mpSlider, SIGNAL(valueChanged(int)), this, SLOT(onSliderChanged(int)));
}

66 67 68
/**
  * \brief Destructor
  */
69 70
UBMediaWidget::~UBMediaWidget()
{
71
    unsetActionsParent();
72 73 74 75 76
    DELETEPTR(mpSlider);
    DELETEPTR(mpPauseButton);
    DELETEPTR(mpPlayStopButton);
    DELETEPTR(mpAudioOutput);
    DELETEPTR(mpVideoWidget);
77 78
//    DELETEPTR(mpVideoStackedWidget);
//    DELETEPTR(mpSnapshotVideoWidget);
79
    DELETEPTR(mpMediaObject);
shibakaneki's avatar
shibakaneki committed
80
    DELETEPTR(mpCover);
81 82
}

83 84 85 86
/**
  * \brief Set the media file
  * @param filePath as the media file path
  */
87 88 89 90 91 92 93 94 95 96 97 98 99
void UBMediaWidget::setFile(const QString &filePath)
{
    Q_ASSERT("" != filePath);
    mFilePath = filePath;
    mpMediaObject = new Phonon::MediaObject(this);
    mpMediaObject->setTickInterval(TICK_INTERVAL);
    connect(mpMediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(onStateChanged(Phonon::State,Phonon::State)));
    connect(mpMediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(onTotalTimeChanged(qint64)));
    connect(mpMediaObject, SIGNAL(tick(qint64)), this, SLOT(onTick(qint64)));
    mpMediaObject->setCurrentSource(Phonon::MediaSource(filePath));
    createMediaPlayer();
}

100 101 102 103
/**
  * \brief Get the media type
  * @returns the media type
  */
104 105 106 107 108
eMediaType UBMediaWidget::mediaType()
{
    return mType;
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
void UBMediaWidget::showEvent(QShowEvent* event)
{
    if(!mpVideoWidget){
        mpVideoWidget = new Phonon::VideoWidget(this);
        mMediaLayout.addStretch(1);
        mMediaLayout.addWidget(mpVideoWidget, 0);
        mMediaLayout.addStretch(1);
        Phonon::createPath(mpMediaObject, mpVideoWidget);
        adaptSizeToVideo();
        mpMediaObject->play();
        mpMediaObject->stop();
    }
    QWidget::showEvent(event);
}

124 125 126 127 128 129 130
void UBMediaWidget::hideEvent(QHideEvent* event)
{
    if(mpMediaObject->state() == Phonon::PlayingState)
        mpMediaObject->stop();
    UBActionableWidget::hideEvent(event);
}

131 132 133
/**
  * \brief Create the media player
  */
134 135
void UBMediaWidget::createMediaPlayer()
{
shibakaneki's avatar
shibakaneki committed
136 137 138 139
    mpMediaContainer = new QWidget(this);
    mpMediaContainer->setObjectName("UBMediaVideoContainer");
    mpMediaContainer->setLayout(&mMediaLayout);

140
    if(eMediaType_Video == mType){
141
        mMediaLayout.setContentsMargins(10, 10, 10, 10);
142 143 144
        if(isVisible()){
            mpVideoWidget = new Phonon::VideoWidget(this);
            mMediaLayout.addStretch(1);
145 146 147 148 149 150 151

//            mpVideoStackedWidget = new QStackedWidget(this);
//            mpVideoStackedWidget->addWidget(mpVideoWidget);
//            mpSnapshotVideoWidget = new QLabel(this);
//            mpVideoStackedWidget->addWidget(mpSnapshotVideoWidget);
//            mMediaLayout.addWidget(mpVideoStackedWidget,0);

152 153 154 155 156
            mMediaLayout.addWidget(mpVideoWidget, 0);
            mMediaLayout.addStretch(1);
            Phonon::createPath(mpMediaObject, mpVideoWidget);
            adaptSizeToVideo();
        }
157 158 159
        mpAudioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
        Phonon::createPath(mpMediaObject, mpAudioOutput);
    }else if(eMediaType_Audio == mType){
shibakaneki's avatar
shibakaneki committed
160 161 162 163 164 165 166 167
        mMediaLayout.setContentsMargins(10, 10, 10, 10);
        mpCover = new QLabel(mpMediaContainer);
        mpMediaContainer->setStyleSheet(QString("background: none;"));
        setAudioCover(":images/libpalette/soundIcon.svg");
        mpCover->setScaledContents(true);
        mMediaLayout.addStretch(1);
        mMediaLayout.addWidget(mpCover, 0);
        mMediaLayout.addStretch(1);
168 169 170
        mpAudioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
        Phonon::createPath(mpMediaObject, mpAudioOutput);
    }
shibakaneki's avatar
shibakaneki committed
171
    mLayout.addWidget(mpMediaContainer, 1);
172
    mLayout.addLayout(&mSeekerLayout, 0);
173
    setActionsParent(mpMediaContainer);
174 175
}

176 177 178
/**
  * \brief Adapt the widget size to the video in order to keep the good aspect ratio
  */
179 180
void UBMediaWidget::adaptSizeToVideo()
{
shibakaneki's avatar
shibakaneki committed
181 182 183
    if(NULL != mpMediaContainer){
        int origW = mpMediaContainer->width();
        int origH = mpMediaContainer->height();
184 185 186
        int newW = width();
        float scaleFactor = (float)origW/(float)newW;
        int newH = origH/scaleFactor;
187
        resize(newW, height() + newH);
188 189 190
    }
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
//void UBMediaWidget::updateView(Phonon::State nextState)
//{
//    if(eMediaType_Video == mType){
//        if(nextState != Phonon::PlayingState){
//            const QPixmap& snapshot = QPixmap::grabWindow(mpVideoWidget->winId());
//            if(snapshot.size().width()!= 0){
//                mpSnapshotVideoWidget->setPixmap(snapshot);
//                mpVideoStackedWidget->setCurrentWidget(mpSnapshotVideoWidget);
//            }
//        }
//        else
//            mpVideoStackedWidget->setCurrentWidget(mpVideoWidget);
//    }

//}

207 208 209 210 211
/**
  * \brief Handle the media state change notification
  * @param newState as the new state
  * @param oldState as the old state
  */
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
void UBMediaWidget::onStateChanged(Phonon::State newState, Phonon::State oldState)
{
    if(!mGeneratingThumbnail){
        if(Phonon::LoadingState == oldState && Phonon::StoppedState == newState){
            if(eMediaType_Video == mType){
                // We do that here to generate the thumbnail of the video
                mGeneratingThumbnail = true;
                mpMediaObject->play();
                mpMediaObject->pause();
                mGeneratingThumbnail = false;
            }
        }else if(Phonon::PlayingState == oldState && Phonon::PausedState == newState){
            mpPlayStopButton->setPixmap(QPixmap(":images/play.svg"));
            mpPauseButton->setEnabled(false);
        }else if((Phonon::PausedState == oldState && Phonon::PlayingState == newState) ||
                 (Phonon::StoppedState == oldState && Phonon::PlayingState == newState)){
            mpPlayStopButton->setPixmap(QPixmap(":images/stop.svg"));
            mpPauseButton->setEnabled(true);
        }else if(Phonon::PlayingState == oldState && Phonon::StoppedState == newState){
            mpPlayStopButton->setPixmap(QPixmap(":images/play.svg"));
            mpPauseButton->setEnabled(false);
            mpSlider->setValue(0);
        }
235
        //updateView(newState);
236 237 238
    }
}

239 240 241 242
/**
  * \brief Handles the total time change notification
  * @param total as the new total time
  */
243 244 245 246 247
void UBMediaWidget::onTotalTimeChanged(qint64 total)
{
    mpSlider->setMaximum(total);
}

248 249 250 251
/**
  * \brief Handles the tick notification
  * @param currentTime as the current time
  */
252 253 254 255 256 257 258
void UBMediaWidget::onTick(qint64 currentTime)
{
    mAutoUpdate = true;
    mpSlider->setValue((int)currentTime);
    mAutoUpdate = false;
}

259 260 261 262
/**
  * \brief Handles the seeker value change notification
  * @param value as the new seeker value
  */
263 264 265 266 267 268 269
void UBMediaWidget::onSliderChanged(int value)
{
    if(!mAutoUpdate){
        mpMediaObject->seek(value);
    }
}

270 271 272
/**
  * \brief Toggle Play-Stop
  */
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
void UBMediaWidget::onPlayStopClicked()
{
    switch(mpMediaObject->state()){
        case Phonon::PlayingState:
            mpMediaObject->stop();
            break;

        case Phonon::StoppedState:
        case Phonon::PausedState:
            mpMediaObject->play();
            break;
        default:
            break;
    }
}

289 290 291
/**
  * \brief Pause the media
  */
292 293 294 295 296
void UBMediaWidget::onPauseClicked()
{
    mpMediaObject->pause();
}

297 298 299 300
/**
  * Get the border
  * @returns the actual border
  */
301 302 303 304 305
int UBMediaWidget::border()
{
    return mBorder;
}

306 307 308 309
/**
  * \brief Handles the resize event
  * @param ev as the resize event
  */
310 311 312 313 314
void UBMediaWidget::resizeEvent(QResizeEvent* ev)
{
    Q_UNUSED(ev);
}

315 316 317 318
/**
  * \brief Set the audio cover
  * @param coverPath as the cover image file path
  */
shibakaneki's avatar
shibakaneki committed
319 320 321 322 323 324 325
void UBMediaWidget::setAudioCover(const QString &coverPath)
{
    if(NULL != mpCover){
        mpCover->setPixmap(QPixmap(coverPath));
    }
}

326
// -----------------------------------------------------------------------------------------------------------
327 328 329 330 331
/**
  * \brief Constructor
  * @param parent as the parent widget
  * @param name as the object name
  */
332 333 334 335 336
UBMediaButton::UBMediaButton(QWidget *parent, const char *name):QLabel(parent)
  , mPressed(false)
{
    setObjectName(name);
    resize(UBMEDIABUTTON_SIZE, UBMEDIABUTTON_SIZE);
337
    setStyleSheet(QString("padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;"));
338 339
}

340 341 342
/**
  * \brief Destructor
  */
343 344 345 346 347
UBMediaButton::~UBMediaButton()
{

}

348 349 350 351
/**
  * \brief Handles the mouse press notification
  * @param ev as the mouse press event
  */
352 353 354 355 356 357
void UBMediaButton::mousePressEvent(QMouseEvent* ev)
{
    Q_UNUSED(ev);
    mPressed = true;
}

358 359 360 361
/**
  * \brief Handles the mouse release notification
  * @param ev as the mouse release event
  */
362 363 364 365 366 367 368 369
void UBMediaButton::mouseReleaseEvent(QMouseEvent* ev)
{
    Q_UNUSED(ev);
    if(mPressed){
        mPressed = false;
        emit clicked();
    }
}