UBMediaWidget.cpp 9.99 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"

Claudio Valerio's avatar
Claudio Valerio committed
19 20
#include "core/memcheck.h"

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

45
    addAction(eAction_Close);
46
    mType = type;
47 48
    mpLayout = new QVBoxLayout(this);
    setLayout(mpLayout);
49 50 51 52 53 54 55 56 57 58 59

    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);

60
    mpSeekerLayout = new QHBoxLayout();
61 62 63 64
    mpSeekerLayout->addWidget(mpPlayStopButton, 0);
    mpSeekerLayout->addWidget(mpPauseButton, 0);
    mpSeekerLayout->addWidget(mpSlider, 1);
    mpSeekerLayout->setContentsMargins(0, 0, 0, 0);
65 66 67 68 69 70

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

71 72 73
/**
  * \brief Destructor
  */
74 75
UBMediaWidget::~UBMediaWidget()
{
76
    unsetActionsParent();
77
    DELETEPTR(mpMediaObject);
78 79 80 81 82
    DELETEPTR(mpSlider);
    DELETEPTR(mpPauseButton);
    DELETEPTR(mpPlayStopButton);
    DELETEPTR(mpAudioOutput);
    DELETEPTR(mpVideoWidget);
shibakaneki's avatar
shibakaneki committed
83
    DELETEPTR(mpCover);
84 85 86
    DELETEPTR(mpMediaContainer);
    DELETEPTR(mpSeekerLayout);
    DELETEPTR(mpLayout);
87 88
}

89 90 91 92
/**
  * \brief Set the media file
  * @param filePath as the media file path
  */
93 94 95 96 97 98 99 100 101 102 103 104 105
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();
}

106 107 108 109
/**
  * \brief Get the media type
  * @returns the media type
  */
110 111 112 113 114
eMediaType UBMediaWidget::mediaType()
{
    return mType;
}

115 116 117 118
void UBMediaWidget::showEvent(QShowEvent* event)
{
    if(!mpVideoWidget){
        mpVideoWidget = new Phonon::VideoWidget(this);
119
        mMediaLayout->addStretch(1);
120
        mMediaLayout->addWidget(mpVideoWidget);
121
        mMediaLayout->addStretch(1);
122 123 124 125 126 127 128 129
        Phonon::createPath(mpMediaObject, mpVideoWidget);
        adaptSizeToVideo();
        mpMediaObject->play();
        mpMediaObject->stop();
    }
    QWidget::showEvent(event);
}

130 131 132 133 134 135 136
void UBMediaWidget::hideEvent(QHideEvent* event)
{
    if(mpMediaObject->state() == Phonon::PlayingState)
        mpMediaObject->stop();
    UBActionableWidget::hideEvent(event);
}

137 138 139
/**
  * \brief Create the media player
  */
140 141
void UBMediaWidget::createMediaPlayer()
{
142
    mpMediaContainer = new QWidget();
shibakaneki's avatar
shibakaneki committed
143
    mpMediaContainer->setObjectName("UBMediaVideoContainer");
144
    mMediaLayout = new QHBoxLayout();
145
    mpMediaContainer->setLayout(mMediaLayout);
shibakaneki's avatar
shibakaneki committed
146

147
    if(eMediaType_Video == mType){
148
        mMediaLayout->setContentsMargins(10, 10, 10, 10);
149 150
        if(isVisible()){
            mpVideoWidget = new Phonon::VideoWidget(this);
151
            mMediaLayout->addStretch(1);
152
            mMediaLayout->addWidget(mpVideoWidget);
153
            mMediaLayout->addStretch(1);
154 155 156
            Phonon::createPath(mpMediaObject, mpVideoWidget);
            adaptSizeToVideo();
        }
157 158 159
        mpAudioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
        Phonon::createPath(mpMediaObject, mpAudioOutput);
    }else if(eMediaType_Audio == mType){
160
        mMediaLayout->setContentsMargins(10, 10, 10, 10);
shibakaneki's avatar
shibakaneki committed
161 162 163 164
        mpCover = new QLabel(mpMediaContainer);
        mpMediaContainer->setStyleSheet(QString("background: none;"));
        setAudioCover(":images/libpalette/soundIcon.svg");
        mpCover->setScaledContents(true);
165 166 167
        mMediaLayout->addStretch(1);
        mMediaLayout->addWidget(mpCover, 0);
        mMediaLayout->addStretch(1);
168 169 170
        mpAudioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
        Phonon::createPath(mpMediaObject, mpAudioOutput);
    }
171 172
    mpLayout->addWidget(mpMediaContainer, 1);
    mpLayout->addLayout(mpSeekerLayout, 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
/**
  * \brief Handle the media state change notification
  * @param newState as the new state
  * @param oldState as the old state
  */
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
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);
        }
219

220
    }
221 222
    //    if(mType == eMediaType_Video)
    //        updateView(newState);
223 224
}

225 226 227 228
/**
  * \brief Handles the total time change notification
  * @param total as the new total time
  */
229 230 231 232 233
void UBMediaWidget::onTotalTimeChanged(qint64 total)
{
    mpSlider->setMaximum(total);
}

234 235 236 237
/**
  * \brief Handles the tick notification
  * @param currentTime as the current time
  */
238 239 240 241 242 243 244
void UBMediaWidget::onTick(qint64 currentTime)
{
    mAutoUpdate = true;
    mpSlider->setValue((int)currentTime);
    mAutoUpdate = false;
}

245 246 247 248
/**
  * \brief Handles the seeker value change notification
  * @param value as the new seeker value
  */
249 250 251 252 253 254 255
void UBMediaWidget::onSliderChanged(int value)
{
    if(!mAutoUpdate){
        mpMediaObject->seek(value);
    }
}

256 257 258
/**
  * \brief Toggle Play-Stop
  */
259 260 261
void UBMediaWidget::onPlayStopClicked()
{
    switch(mpMediaObject->state()){
262 263 264
    case Phonon::PlayingState:
        mpMediaObject->stop();
        break;
265

266 267 268 269 270 271
    case Phonon::StoppedState:
    case Phonon::PausedState:
        mpMediaObject->play();
        break;
    default:
        break;
272 273 274
    }
}

275 276 277
/**
  * \brief Pause the media
  */
278 279 280 281 282
void UBMediaWidget::onPauseClicked()
{
    mpMediaObject->pause();
}

283 284 285 286
/**
  * Get the border
  * @returns the actual border
  */
287 288 289 290 291
int UBMediaWidget::border()
{
    return mBorder;
}

292 293 294 295
/**
  * \brief Handles the resize event
  * @param ev as the resize event
  */
296 297 298 299 300
void UBMediaWidget::resizeEvent(QResizeEvent* ev)
{
    Q_UNUSED(ev);
}

301 302 303 304
/**
  * \brief Set the audio cover
  * @param coverPath as the cover image file path
  */
shibakaneki's avatar
shibakaneki committed
305 306 307 308 309 310 311
void UBMediaWidget::setAudioCover(const QString &coverPath)
{
    if(NULL != mpCover){
        mpCover->setPixmap(QPixmap(coverPath));
    }
}

312
// -----------------------------------------------------------------------------------------------------------
313 314 315 316 317
/**
  * \brief Constructor
  * @param parent as the parent widget
  * @param name as the object name
  */
318 319 320 321 322
UBMediaButton::UBMediaButton(QWidget *parent, const char *name):QLabel(parent)
  , mPressed(false)
{
    setObjectName(name);
    resize(UBMEDIABUTTON_SIZE, UBMEDIABUTTON_SIZE);
323
    setStyleSheet(QString("padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;"));
324 325
}

326 327 328
/**
  * \brief Destructor
  */
329 330 331 332 333
UBMediaButton::~UBMediaButton()
{

}

334 335 336 337
/**
  * \brief Handles the mouse press notification
  * @param ev as the mouse press event
  */
338 339 340 341 342 343
void UBMediaButton::mousePressEvent(QMouseEvent* ev)
{
    Q_UNUSED(ev);
    mPressed = true;
}

344 345 346 347
/**
  * \brief Handles the mouse release notification
  * @param ev as the mouse release event
  */
348 349 350 351 352 353 354 355
void UBMediaButton::mouseReleaseEvent(QMouseEvent* ev)
{
    Q_UNUSED(ev);
    if(mPressed){
        mPressed = false;
        emit clicked();
    }
}