UBMediaWidget.cpp 9.99 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
 * (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
void UBMediaWidget::showEvent(QShowEvent* event)
{
shibakaneki's avatar
shibakaneki committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	if(mType == eMediaType_Audio){
		return;
	}else{
		if(!mpVideoWidget){
			mpVideoWidget = new Phonon::VideoWidget(this);
			mMediaLayout->addStretch(1);
			mMediaLayout->addWidget(mpVideoWidget);
			mMediaLayout->addStretch(1);
			Phonon::createPath(mpMediaObject, mpVideoWidget);
			adaptSizeToVideo();
			mpMediaObject->play();
			mpMediaObject->stop();
		}
		QWidget::showEvent(event);
	}
132 133
}

134 135 136 137 138 139 140
void UBMediaWidget::hideEvent(QHideEvent* event)
{
    if(mpMediaObject->state() == Phonon::PlayingState)
        mpMediaObject->stop();
    UBActionableWidget::hideEvent(event);
}

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

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

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

195 196 197 198 199
/**
  * \brief Handle the media state change notification
  * @param newState as the new state
  * @param oldState as the old state
  */
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
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);
        }
223

224
    }
225 226
    //    if(mType == eMediaType_Video)
    //        updateView(newState);
227 228
}

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

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

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

260 261 262
/**
  * \brief Toggle Play-Stop
  */
263 264 265
void UBMediaWidget::onPlayStopClicked()
{
    switch(mpMediaObject->state()){
266 267 268
    case Phonon::PlayingState:
        mpMediaObject->stop();
        break;
269

270 271 272 273 274 275
    case Phonon::StoppedState:
    case Phonon::PausedState:
        mpMediaObject->play();
        break;
    default:
        break;
276 277 278
    }
}

279 280 281
/**
  * \brief Pause the media
  */
282 283 284 285 286
void UBMediaWidget::onPauseClicked()
{
    mpMediaObject->pause();
}

287 288 289 290
/**
  * Get the border
  * @returns the actual border
  */
291 292 293 294 295
int UBMediaWidget::border()
{
    return mBorder;
}

296 297 298 299
/**
  * \brief Handles the resize event
  * @param ev as the resize event
  */
300 301 302 303 304
void UBMediaWidget::resizeEvent(QResizeEvent* ev)
{
    Q_UNUSED(ev);
}

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

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

330 331 332
/**
  * \brief Destructor
  */
333 334 335 336 337
UBMediaButton::~UBMediaButton()
{

}

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

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