UBMediaWidget.cpp 9.54 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 39 40
{
    setAttribute(Qt::WA_StyledBackground, true);
    setStyleSheet(UBApplication::globalStyleSheet());

41
    addAction(eAction_Close);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    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);
58
    mSeekerLayout.setContentsMargins(0, 0, 0, 0);
59 60 61 62 63 64

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

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

80 81 82 83
/**
  * \brief Set the media file
  * @param filePath as the media file path
  */
84 85 86 87 88 89 90 91 92 93 94 95 96
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();
}

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

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
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);
}

121 122 123
/**
  * \brief Create the media player
  */
124 125
void UBMediaWidget::createMediaPlayer()
{
shibakaneki's avatar
shibakaneki committed
126 127 128 129
    mpMediaContainer = new QWidget(this);
    mpMediaContainer->setObjectName("UBMediaVideoContainer");
    mpMediaContainer->setLayout(&mMediaLayout);

130
    if(eMediaType_Video == mType){
shibakaneki's avatar
shibakaneki committed
131
        mMediaLayout.setContentsMargins(10, 10, 25, 10);
132 133 134 135 136 137 138 139
        if(isVisible()){
            mpVideoWidget = new Phonon::VideoWidget(this);
            mMediaLayout.addStretch(1);
            mMediaLayout.addWidget(mpVideoWidget, 0);
            mMediaLayout.addStretch(1);
            Phonon::createPath(mpMediaObject, mpVideoWidget);
            adaptSizeToVideo();
        }
140 141 142
        mpAudioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
        Phonon::createPath(mpMediaObject, mpAudioOutput);
    }else if(eMediaType_Audio == mType){
shibakaneki's avatar
shibakaneki committed
143 144 145 146 147 148 149 150
        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);
151 152 153
        mpAudioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
        Phonon::createPath(mpMediaObject, mpAudioOutput);
    }
shibakaneki's avatar
shibakaneki committed
154
    mLayout.addWidget(mpMediaContainer, 1);
155
    mLayout.addLayout(&mSeekerLayout, 0);
156
    setActionsParent(mpMediaContainer);
157 158
}

159 160 161
/**
  * \brief Adapt the widget size to the video in order to keep the good aspect ratio
  */
162 163
void UBMediaWidget::adaptSizeToVideo()
{
shibakaneki's avatar
shibakaneki committed
164 165 166
    if(NULL != mpMediaContainer){
        int origW = mpMediaContainer->width();
        int origH = mpMediaContainer->height();
167 168 169
        int newW = width();
        float scaleFactor = (float)origW/(float)newW;
        int newH = origH/scaleFactor;
170
        resize(newW, height() + newH);
171 172 173
    }
}

174 175 176 177 178
/**
  * \brief Handle the media state change notification
  * @param newState as the new state
  * @param oldState as the old state
  */
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
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);
        }
    }
}

205 206 207 208
/**
  * \brief Handles the total time change notification
  * @param total as the new total time
  */
209 210 211 212 213
void UBMediaWidget::onTotalTimeChanged(qint64 total)
{
    mpSlider->setMaximum(total);
}

214 215 216 217
/**
  * \brief Handles the tick notification
  * @param currentTime as the current time
  */
218 219 220 221 222 223 224
void UBMediaWidget::onTick(qint64 currentTime)
{
    mAutoUpdate = true;
    mpSlider->setValue((int)currentTime);
    mAutoUpdate = false;
}

225 226 227 228
/**
  * \brief Handles the seeker value change notification
  * @param value as the new seeker value
  */
229 230 231 232 233 234 235
void UBMediaWidget::onSliderChanged(int value)
{
    if(!mAutoUpdate){
        mpMediaObject->seek(value);
    }
}

236 237 238
/**
  * \brief Toggle Play-Stop
  */
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
void UBMediaWidget::onPlayStopClicked()
{
    switch(mpMediaObject->state()){
        case Phonon::PlayingState:
            mpMediaObject->stop();
            break;

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

255 256 257
/**
  * \brief Pause the media
  */
258 259 260 261 262
void UBMediaWidget::onPauseClicked()
{
    mpMediaObject->pause();
}

263 264 265 266
/**
  * Get the border
  * @returns the actual border
  */
267 268 269 270 271
int UBMediaWidget::border()
{
    return mBorder;
}

272 273 274 275
/**
  * \brief Handles the resize event
  * @param ev as the resize event
  */
276 277 278 279 280
void UBMediaWidget::resizeEvent(QResizeEvent* ev)
{
    Q_UNUSED(ev);
}

281 282 283 284
/**
  * \brief Set the audio cover
  * @param coverPath as the cover image file path
  */
shibakaneki's avatar
shibakaneki committed
285 286 287 288 289 290 291
void UBMediaWidget::setAudioCover(const QString &coverPath)
{
    if(NULL != mpCover){
        mpCover->setPixmap(QPixmap(coverPath));
    }
}

292
// -----------------------------------------------------------------------------------------------------------
293 294 295 296 297
/**
  * \brief Constructor
  * @param parent as the parent widget
  * @param name as the object name
  */
298 299 300 301 302
UBMediaButton::UBMediaButton(QWidget *parent, const char *name):QLabel(parent)
  , mPressed(false)
{
    setObjectName(name);
    resize(UBMEDIABUTTON_SIZE, UBMEDIABUTTON_SIZE);
303
    setStyleSheet(QString("padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;"));
304 305
}

306 307 308
/**
  * \brief Destructor
  */
309 310 311 312 313
UBMediaButton::~UBMediaButton()
{

}

314 315 316 317
/**
  * \brief Handles the mouse press notification
  * @param ev as the mouse press event
  */
318 319 320 321 322 323
void UBMediaButton::mousePressEvent(QMouseEvent* ev)
{
    Q_UNUSED(ev);
    mPressed = true;
}

324 325 326 327
/**
  * \brief Handles the mouse release notification
  * @param ev as the mouse release event
  */
328 329 330 331 332 333 334 335
void UBMediaButton::mouseReleaseEvent(QMouseEvent* ev)
{
    Q_UNUSED(ev);
    if(mPressed){
        mPressed = false;
        emit clicked();
    }
}