Various fixes and clean-ups for media items:

- Hovering over the video now makes the seek bar visible
- The size of the video item is no longer changed when the video
finishes playing
- Media errors are now handled by the mediaItem and displayed for the
user

- Code clean-up
preferencesAboutTextFull
Craig Watson 9 years ago
parent fe9affeea1
commit d6e1a7ad7c
  1. 2
      src/domain/UBGraphicsItemDelegate.h
  2. 64
      src/domain/UBGraphicsMediaItem.cpp
  3. 7
      src/domain/UBGraphicsMediaItem.h
  4. 35
      src/domain/UBGraphicsMediaItemDelegate.cpp
  5. 3
      src/domain/UBGraphicsMediaItemDelegate.h

@ -296,6 +296,8 @@ class UBGraphicsItemDelegate : public QObject
void setUBFlags(UBGraphicsFlags pf); void setUBFlags(UBGraphicsFlags pf);
void setUBFlag(UBGraphicsFlags pf, bool set = true); void setUBFlag(UBGraphicsFlags pf, bool set = true);
virtual void showToolBar() {}
signals: signals:
void showOnDisplayChanged(bool shown); void showOnDisplayChanged(bool shown);
void lockChanged(bool locked); void lockChanged(bool locked);

@ -80,19 +80,12 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte
setFlag(ItemIsMovable, true); setFlag(ItemIsMovable, true);
setFlag(ItemSendsGeometryChanges, true); setFlag(ItemSendsGeometryChanges, true);
setAcceptHoverEvents(true);
connect(mMediaObject, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), connect(mMediaObject, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
Delegate(), SLOT(mediaStatusChanged(QMediaPlayer::MediaStatus))); Delegate(), SLOT(mediaStatusChanged(QMediaPlayer::MediaStatus)));
connect(mMediaObject, SIGNAL(stateChanged(QMediaPlayer::State)), connect(mMediaObject, SIGNAL(stateChanged(QMediaPlayer::State)),
Delegate(), SLOT(mediaStateChanged(QMediaPlayer::State))); Delegate(), SLOT(mediaStateChanged(QMediaPlayer::State)));
/*
connect(mMediaObject, static_cast<void(QMediaPlayer::*)(QMediaPlayer::Error)>(&QMediaPlayer::error),
Delegate(), &UBGraphicsMediaItemDelegate::mediaError);
*/
connect(mMediaObject, SIGNAL(positionChanged(qint64)), connect(mMediaObject, SIGNAL(positionChanged(qint64)),
Delegate(), SLOT(updateTicker(qint64))); Delegate(), SLOT(updateTicker(qint64)));
@ -104,6 +97,9 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte
connect(mMediaObject, SIGNAL(videoAvailableChanged(bool)), connect(mMediaObject, SIGNAL(videoAvailableChanged(bool)),
this, SLOT(hasMediaChanged(bool))); this, SLOT(hasMediaChanged(bool)));
connect(mMediaObject, static_cast<void(QMediaPlayer::*)(QMediaPlayer::Error)>(&QMediaPlayer::error),
this, &UBGraphicsMediaItem::mediaError);
} }
UBGraphicsAudioItem::UBGraphicsAudioItem(const QUrl &pMediaFileUrl, QGraphicsItem *parent) UBGraphicsAudioItem::UBGraphicsAudioItem(const QUrl &pMediaFileUrl, QGraphicsItem *parent)
@ -133,11 +129,13 @@ UBGraphicsVideoItem::UBGraphicsVideoItem(const QUrl &pMediaFileUrl, QGraphicsIte
setMinimumSize(QSize(320, 240)); setMinimumSize(QSize(320, 240));
setSize(320, 240); setSize(320, 240);
connect(mVideoItem, SIGNAL(nativeSizeChanged(QSizeF)), connect(mVideoItem, SIGNAL(nativeSizeChanged(QSizeF)),
this, SLOT(videoSizeChanged(QSizeF))); this, SLOT(videoSizeChanged(QSizeF)));
haveLinkedImage = true; haveLinkedImage = true;
setAcceptHoverEvents(true);
update(); update();
} }
@ -179,7 +177,6 @@ QVariant UBGraphicsMediaItem::itemChange(GraphicsItemChange change, const QVaria
return QGraphicsRectItem::itemChange(change, newValue); return QGraphicsRectItem::itemChange(change, newValue);
} }
else
return QGraphicsRectItem::itemChange(change, value); return QGraphicsRectItem::itemChange(change, value);
} }
@ -293,8 +290,7 @@ void UBGraphicsMediaItem::setMute(bool bMute)
void UBGraphicsMediaItem::hasMediaChanged(bool hasMedia) void UBGraphicsMediaItem::hasMediaChanged(bool hasMedia)
{ {
if(hasMedia && mMediaObject->isSeekable()) if(hasMedia && mMediaObject->isSeekable()) {
{
mMediaObject->setPosition(mInitialPos); mMediaObject->setPosition(mInitialPos);
UBGraphicsMediaItemDelegate *med = dynamic_cast<UBGraphicsMediaItemDelegate *>(Delegate()); UBGraphicsMediaItemDelegate *med = dynamic_cast<UBGraphicsMediaItemDelegate *>(Delegate());
@ -345,7 +341,6 @@ void UBGraphicsMediaItem::stop()
void UBGraphicsMediaItem::togglePlayPause() void UBGraphicsMediaItem::togglePlayPause()
{ {
if (mMediaObject->state() == QMediaPlayer::StoppedState) if (mMediaObject->state() == QMediaPlayer::StoppedState)
mMediaObject->play(); mMediaObject->play();
@ -374,10 +369,12 @@ void UBGraphicsMediaItem::togglePlayPause()
mMediaObject->setMedia(mediaFileUrl()); mMediaObject->setMedia(mediaFileUrl());
mMediaObject->play(); mMediaObject->play();
} }
}
if (mMediaObject->error()) void UBGraphicsMediaItem::mediaError(QMediaPlayer::Error errorCode)
qDebug() << "Error appeared." << mMediaObject->errorString(); {
if (errorCode != QMediaPlayer::NoError)
UBApplication::showMessage(mMediaObject->errorString());
} }
void UBGraphicsMediaItem::copyItemParameters(UBItem *copy) const void UBGraphicsMediaItem::copyItemParameters(UBItem *copy) const
@ -403,14 +400,11 @@ void UBGraphicsMediaItem::copyItemParameters(UBItem *copy) const
void UBGraphicsMediaItem::mousePressEvent(QGraphicsSceneMouseEvent *event) void UBGraphicsMediaItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
if (Delegate()) if (Delegate()) {
{
Delegate()->mousePressEvent(event); Delegate()->mousePressEvent(event);
if (parentItem() && UBGraphicsGroupContainerItem::Type == parentItem()->type()) if (parentItem() && UBGraphicsGroupContainerItem::Type == parentItem()->type()) {
{
UBGraphicsGroupContainerItem *group = qgraphicsitem_cast<UBGraphicsGroupContainerItem*>(parentItem()); UBGraphicsGroupContainerItem *group = qgraphicsitem_cast<UBGraphicsGroupContainerItem*>(parentItem());
if (group) if (group) {
{
QGraphicsItem *curItem = group->getCurrentItem(); QGraphicsItem *curItem = group->getCurrentItem();
if (curItem && this != curItem) if (curItem && this != curItem)
group->deselectCurrentItem(); group->deselectCurrentItem();
@ -418,7 +412,6 @@ void UBGraphicsMediaItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
this->setSelected(true); this->setSelected(true);
Delegate()->positionHandles(); Delegate()->positionHandles();
} }
} }
} }
@ -504,7 +497,34 @@ void UBGraphicsVideoItem::setSize(int width, int height)
void UBGraphicsVideoItem::videoSizeChanged(QSizeF newSize) void UBGraphicsVideoItem::videoSizeChanged(QSizeF newSize)
{ {
/* Depending on the platform, video size information becomes available
* at different times (either when the file is loaded, or when playback
* begins), so this slot is needed to resize the video item as soon as
* the information is available.
*/
// Don't resize the video item when playback has finished
if (mMediaObject->mediaStatus() != QMediaPlayer::EndOfMedia)
this->setSize(newSize.width(), newSize.height()); this->setSize(newSize.width(), newSize.height());
} }
void UBGraphicsVideoItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
// Display the seek bar
Delegate()->showToolBar();
QGraphicsRectItem::hoverEnterEvent(event);
}
void UBGraphicsVideoItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
Delegate()->showToolBar();
QGraphicsRectItem::hoverMoveEvent(event);
}
void UBGraphicsVideoItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
QGraphicsRectItem::hoverLeaveEvent(event);
}

@ -117,6 +117,9 @@ public slots:
virtual void stop(); virtual void stop();
virtual void togglePlayPause(); virtual void togglePlayPause();
protected slots:
void mediaError(QMediaPlayer::Error errorCode);
protected: protected:
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
@ -177,6 +180,10 @@ public slots:
protected: protected:
QGraphicsVideoItem *mVideoItem; QGraphicsVideoItem *mVideoItem;
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
}; };

@ -71,6 +71,18 @@ bool UBGraphicsMediaItemDelegate::mousePressEvent(QGraphicsSceneMouseEvent *even
return UBGraphicsItemDelegate::mousePressEvent(event); return UBGraphicsItemDelegate::mousePressEvent(event);
} }
/**
* @brief Show the toolbar (play/pause, seek, mute).
*
* The toolbar then auto-hides after a set amount of time.
*/
void UBGraphicsMediaItemDelegate::showToolBar()
{
mToolBarItem->show();
if (mToolBarShowTimer)
mToolBarShowTimer->start();
}
void UBGraphicsMediaItemDelegate::hideToolBar() void UBGraphicsMediaItemDelegate::hideToolBar()
{ {
mToolBarItem->hide(); mToolBarItem->hide();
@ -108,11 +120,10 @@ void UBGraphicsMediaItemDelegate::buildButtons()
mToolBarItem->setShifting(false); mToolBarItem->setShifting(false);
if (!mToolBarShowTimer) { if (!mToolBarShowTimer) {
if (delegated()->hasLinkedImage()) { if (delegated()->hasLinkedImage()) {
mToolBarShowTimer = new QTimer(); mToolBarShowTimer = new QTimer();
connect(mToolBarShowTimer, SIGNAL(timeout()), this, SLOT(hideToolBar()));
mToolBarShowTimer->setInterval(m_iToolBarShowingInterval); mToolBarShowTimer->setInterval(m_iToolBarShowingInterval);
connect(mToolBarShowTimer, SIGNAL(timeout()), this, SLOT(hideToolBar()));
} }
} }
@ -181,13 +192,6 @@ void UBGraphicsMediaItemDelegate::remove(bool canUndo)
if (delegated()) if (delegated())
delegated()->stop(); delegated()->stop();
/*
if (delegated()->videoItem()) {
UBGraphicsScene* scene = dynamic_cast<UBGraphicsScene*>(mDelegated->scene());
scene->removeItem(delegated()->videoItem());
}
*/
UBGraphicsItemDelegate::remove(canUndo); UBGraphicsItemDelegate::remove(canUndo);
} }
@ -232,15 +236,6 @@ void UBGraphicsMediaItemDelegate::mediaStateChanged(QMediaPlayer::State state)
updatePlayPauseState(); updatePlayPauseState();
} }
void UBGraphicsMediaItemDelegate::mediaError(QMediaPlayer::Error error)
{
// Possible errors are NoError, ResourceError, FormatError, NetworkError, AccessDeniedError,
// ServiceMissingError
Q_UNUSED(error);
qDebug() << "Error appeared.";// << mMedia->errorString();
}
void UBGraphicsMediaItemDelegate::updatePlayPauseState() void UBGraphicsMediaItemDelegate::updatePlayPauseState()
{ {
@ -253,9 +248,6 @@ void UBGraphicsMediaItemDelegate::updatePlayPauseState()
void UBGraphicsMediaItemDelegate::updateTicker(qint64 time) void UBGraphicsMediaItemDelegate::updateTicker(qint64 time)
{ {
// TODO: duration() getter for UBGMediaItem
// make sure that all delegate()->mediaObject() calls are removed. 'tis dirty.
mMediaControl->totalTimeChanged(delegated()->mediaDuration()); mMediaControl->totalTimeChanged(delegated()->mediaDuration());
mMediaControl->updateTicker(time); mMediaControl->updateTicker(time);
} }
@ -270,7 +262,6 @@ void UBGraphicsMediaItemDelegate::showHide(bool show)
{ {
QVariant showFlag = QVariant(show ? UBItemLayerType::Object : UBItemLayerType::Control); QVariant showFlag = QVariant(show ? UBItemLayerType::Object : UBItemLayerType::Control);
showHideRecurs(showFlag, mDelegated); showHideRecurs(showFlag, mDelegated);
// TODO: call showHideRecurs on the videoItem too (from UBGMI?)
mDelegated->update(); mDelegated->update();
emit showOnDisplayChanged(show); emit showOnDisplayChanged(show);

@ -50,6 +50,8 @@ class UBGraphicsMediaItemDelegate : public UBGraphicsItemDelegate
bool mousePressEvent(QGraphicsSceneMouseEvent* event); bool mousePressEvent(QGraphicsSceneMouseEvent* event);
void showToolBar();
public slots: public slots:
void toggleMute(); void toggleMute();
@ -58,7 +60,6 @@ class UBGraphicsMediaItemDelegate : public UBGraphicsItemDelegate
void mediaStatusChanged(QMediaPlayer::MediaStatus status); void mediaStatusChanged(QMediaPlayer::MediaStatus status);
void mediaStateChanged(QMediaPlayer::State state); void mediaStateChanged(QMediaPlayer::State state);
void mediaError(QMediaPlayer::Error error);
protected slots: protected slots:

Loading…
Cancel
Save