#ifndef VIDEOWIDGET_H
#define VIDEOWIDGET_H
#include <QHash>
#include <QWidget>
class QGraphicsGridLayout;
class VideoPlayer;
class ImagePlayer;
{
Q_OBJECT
public:
explicit VideoWidget
(QWidget *parent
= 0);
~VideoWidget();
QSize sizeHint
() const { return QSize(800,
600);
}
VideoPlayer* GetActiveVideoPlayer() { return _activeVideoPlayer; }
ImagePlayer* GetActiveImagePlayer() { return _activeImagePlayer; }
signals:
void Signal_VideoActive
(QString name,
bool active
);
protected:
private:
void SetupUI();
void Test();
int _rows, _cols;
QGraphicsGridLayout *_grid;
VideoPlayer* _activeVideoPlayer;
ImagePlayer* _activeImagePlayer;
QHash<QString, VideoPlayer*> _videos;
QHash<QString, ImagePlayer*> _images;
};
#endif // VIDEOWIDGET_H
#ifndef VIDEOWIDGET_H
#define VIDEOWIDGET_H
#include <QHash>
#include <QWidget>
class QGraphicsView;
class QGraphicsScene;
class QGraphicsGridLayout;
class VideoPlayer;
class ImagePlayer;
class VideoWidget : public QWidget
{
Q_OBJECT
public:
explicit VideoWidget(QWidget *parent = 0);
~VideoWidget();
QSize sizeHint() const { return QSize(800, 600); }
QGraphicsView* GetGraphicsView() { return _view; }
QGraphicsScene* GetGraphicsScene() { return _scene; }
VideoPlayer* GetActiveVideoPlayer() { return _activeVideoPlayer; }
ImagePlayer* GetActiveImagePlayer() { return _activeImagePlayer; }
signals:
void Signal_VideoActive(QString name, bool active);
protected:
void resizeEvent(QResizeEvent* event);
private:
void SetupUI();
void Test();
int _rows, _cols;
QGraphicsView* _view;
QGraphicsScene* _scene;
QGraphicsGridLayout *_grid;
VideoPlayer* _activeVideoPlayer;
ImagePlayer* _activeImagePlayer;
QHash<QString, VideoPlayer*> _videos;
QHash<QString, ImagePlayer*> _images;
};
#endif // VIDEOWIDGET_H
To copy to clipboard, switch view to plain text mode
#include <QtWidgets>
#include <QHash>
#include <QHashIterator>
#include <QGraphicsVideoItem>
#include <QGLWidget>
#include <QGraphicsView>
#include <QGraphicsWidget>
#include "videowidget.h"
#include "videoplayer.h"
#include "imageplayer.h"
VideoWidget
::VideoWidget(QWidget *parent
) :{
_rows = 2;
_cols = 2;
_activeVideoPlayer = NULL;
_activeImagePlayer = NULL;
SetupUI();
Test();
_view->fitInView(_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
}
VideoWidget::~VideoWidget()
{
_videos.clear();
_images.clear();
_activeVideoPlayer = NULL;
_activeImagePlayer = NULL;
}
void VideoWidget::SetupUI()
{
_view->setAcceptDrops(true);
_view->viewport()->setMouseTracking(true);
_view
->setRenderHints
(QPainter::Antialiasing |
QPainter::SmoothPixmapTransform);
_scene
->setBackgroundBrush
(QColor(39,
39,
39));
_view->setScene(_scene);
_grid = new QGraphicsGridLayout;
_grid->setContentsMargins(5, 5, 5, 5);
QGraphicsWidget *container = new QGraphicsWidget;
container->setAcceptHoverEvents(true);
container->setAcceptTouchEvents(true);
container->setLayout(_grid);
container->setContentsMargins(0, 0, 0, 0);
_scene->addItem(container);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(_view);
this->setLayout(layout);
}
void VideoWidget::Test()
{
VideoPlayer* videoPlayer;
QString video
("/Users/chucks/test.mp4");
int count = 0;
for(int i=0; i<_rows; i++)
{
for(int j=0; j<_cols; j++)
{
videoPlayer = new VideoPlayer(name, video);
_scene->addItem(videoPlayer);
_videos.insert(name, videoPlayer);
_grid->addItem(videoPlayer, i, j);
_grid->setColumnAlignment(j, Qt::AlignHCenter);
_grid->setColumnMinimumWidth(j, 400);
_grid->setColumnSpacing(j, 10);
}
_grid->setRowAlignment(i, Qt::AlignCenter);
_grid->setRowMinimumHeight(i, 300);
_grid->setRowSpacing(i, 10);
}
}
{
_view->fitInView(_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
}
#include <QtWidgets>
#include <QHash>
#include <QHashIterator>
#include <QGraphicsVideoItem>
#include <QGLWidget>
#include <QGraphicsView>
#include <QGraphicsWidget>
#include "videowidget.h"
#include "videoplayer.h"
#include "imageplayer.h"
VideoWidget::VideoWidget(QWidget *parent) :
QWidget(parent)
{
_rows = 2;
_cols = 2;
_activeVideoPlayer = NULL;
_activeImagePlayer = NULL;
SetupUI();
Test();
_view->fitInView(_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
}
VideoWidget::~VideoWidget()
{
_videos.clear();
_images.clear();
_activeVideoPlayer = NULL;
_activeImagePlayer = NULL;
}
void VideoWidget::SetupUI()
{
_view = new QGraphicsView(this);
_view->setAcceptDrops(true);
_view->viewport()->setMouseTracking(true);
_view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
_view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
_view->setResizeAnchor(QGraphicsView::AnchorViewCenter);
_scene = new QGraphicsScene(this);
_scene->setBackgroundBrush(QColor(39,39,39));
_view->setScene(_scene);
_grid = new QGraphicsGridLayout;
_grid->setContentsMargins(5, 5, 5, 5);
QGraphicsWidget *container = new QGraphicsWidget;
container->setAcceptHoverEvents(true);
container->setAcceptTouchEvents(true);
container->setLayout(_grid);
container->setContentsMargins(0, 0, 0, 0);
_scene->addItem(container);
QBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(_view);
this->setLayout(layout);
}
void VideoWidget::Test()
{
VideoPlayer* videoPlayer;
QString video("/Users/chucks/test.mp4");
int count = 0;
for(int i=0; i<_rows; i++)
{
for(int j=0; j<_cols; j++)
{
QString name = "Video" + QString::number(++count);
videoPlayer = new VideoPlayer(name, video);
_scene->addItem(videoPlayer);
_videos.insert(name, videoPlayer);
_grid->addItem(videoPlayer, i, j);
_grid->setColumnAlignment(j, Qt::AlignHCenter);
_grid->setColumnMinimumWidth(j, 400);
_grid->setColumnSpacing(j, 10);
}
_grid->setRowAlignment(i, Qt::AlignCenter);
_grid->setRowMinimumHeight(i, 300);
_grid->setRowSpacing(i, 10);
}
}
void VideoWidget::resizeEvent(QResizeEvent* event)
{
_view->fitInView(_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
QWidget::resizeEvent(event);
}
To copy to clipboard, switch view to plain text mode
#ifndef VIDEOPLAYER_H
#define VIDEOPLAYER_H
#include <QMediaPlayer>
#include <QMovie>
#include <QGraphicsLayoutItem>
#include <QGraphicsVideoItem>
#include <QPainter>
#include <QPixmap>
#include <QGraphicsSceneMouseEvent>
class VideoPlayer : public QGraphicsVideoItem, public QGraphicsLayoutItem
{
public:
QString file() { return _fileName;
} bool isPlaying() { return _isPlaying; }
void setGeometry
(const QRectF & rect
);
void updateGeometry();
signals:
public slots:
void Slot_OpenFile();
void Slot_Play();
void Slot_Stop();
void Slot_Rewind();
void Slot_FastForward();
protected:
private slots:
void mediaStateChanged(QMediaPlayer::State state);
void positionChanged(qint64 position);
void durationChanged(qint64 duration);
void setPosition(int position);
private:
QMediaPlayer* _mediaPlayer;
bool _isPlaying;
};
#endif // VIDEOPLAYER_H
#ifndef VIDEOPLAYER_H
#define VIDEOPLAYER_H
#include <QMediaPlayer>
#include <QMovie>
#include <QGraphicsLayoutItem>
#include <QGraphicsVideoItem>
#include <QPainter>
#include <QPixmap>
#include <QGraphicsSceneMouseEvent>
class VideoPlayer : public QGraphicsVideoItem, public QGraphicsLayoutItem
{
public:
VideoPlayer(QString name, QString fileName);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QString name() { return _name; }
QString file() { return _fileName; }
bool isPlaying() { return _isPlaying; }
void setGeometry(const QRectF & rect);
void updateGeometry();
signals:
public slots:
void Slot_OpenFile();
void Slot_Play();
void Slot_Stop();
void Slot_Rewind();
void Slot_FastForward();
protected:
QSizeF sizeHint(Qt::SizeHint which, const QSizeF & constraint = QSizeF()) const;
void mouseMoveEvent(QGraphicsSceneMouseEvent * event);
void mousePressEvent(QGraphicsSceneMouseEvent * event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event);
private slots:
void mediaStateChanged(QMediaPlayer::State state);
void positionChanged(qint64 position);
void durationChanged(qint64 duration);
void setPosition(int position);
private:
QMediaPlayer* _mediaPlayer;
QPixmap *_pm;
QString _name;
QString _fileName;
bool _isPlaying;
};
#endif // VIDEOPLAYER_H
To copy to clipboard, switch view to plain text mode
#include "videoplayer.h"
#include <QtWidgets>
#include <QVideoSurfaceFormat>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>
QGraphicsVideoItem(),
QGraphicsLayoutItem()
{
setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
setAcceptHoverEvents(true);
setAcceptDrops(true);
setAspectRatioMode(Qt::KeepAspectRatio);
_name = name;
_fileName = fileName;
_isPlaying = false;
_mediaPlayer = new QMediaPlayer(0, QMediaPlayer::VideoSurface);
_mediaPlayer->setVideoOutput(this);
connect(_mediaPlayer, &QMediaPlayer::stateChanged, this, &VideoPlayer::mediaStateChanged);
connect(_mediaPlayer, &QMediaPlayer::positionChanged, this, &VideoPlayer::positionChanged);
connect(_mediaPlayer, &QMediaPlayer::durationChanged, this, &VideoPlayer::durationChanged);
setGraphicsItem(this);
}
QRectF VideoPlayer
::boundingRect() const {
}
{
Q_UNUSED(widget);
Q_UNUSED(option);
qreal w = (parentItem()->boundingRect().width()/2) - 10;
qreal h = (parentItem()->boundingRect().height()/2) - 10;
// paint a rect around the pixmap
innerFrame.adjust(-4, -4, 4, 4);
painter
->setBrush
(QBrush(Qt
::darkGreen));
if(isSelected())
{
painter
->setBrush
(QBrush(Qt
::green));
painter->drawRoundedRect(innerFrame, 10.0, 10.0);
}
else
{
painter
->setBrush
(QBrush(Qt
::darkGreen));
painter->drawRect(innerFrame);
}
// paint a title bar rect
QRect rectangle
(-w
/2,
-h
/2, w,
20);
painter
->setPen
(QPen(Qt
::darkGreen));
painter->drawRect(rectangle);
// paint video name in title bar
painter->drawText(rectangle, Qt::AlignCenter, _name);
// paint the default background pixmap
painter->drawPixmap(pixpos.x(), pixpos.y()+rectangle.height(), w, h-rectangle.height(), *_pm);
painter->drawText(innerFrame, Qt::AlignCenter, "No Video");
QGraphicsVideoItem::paint(painter, option, widget);
}
void VideoPlayer::Slot_OpenFile()
{
}
void VideoPlayer::Slot_Play()
{
}
void VideoPlayer::Slot_Stop()
{
_mediaPlayer->stop();
}
void VideoPlayer::Slot_Rewind()
{
}
void VideoPlayer::Slot_FastForward()
{
}
void VideoPlayer::mediaStateChanged(QMediaPlayer::State state)
{
}
void VideoPlayer::positionChanged(qint64 position)
{
}
void VideoPlayer::durationChanged(qint64 duration)
{
}
void VideoPlayer::setPosition(int position)
{
}
void VideoPlayer
::setGeometry(const QRectF & rect
) {
prepareGeometryChange();
QGraphicsLayoutItem::setGeometry(rect);
setPos(rect.topLeft());
}
void VideoPlayer::updateGeometry()
{
QGraphicsLayoutItem::updateGeometry();
}
QSizeF VideoPlayer
::sizeHint(Qt
::SizeHint which,
const QSizeF & constraint
) const {
switch ( which )
{
case Qt::MinimumSize:
case Qt::PreferredSize:
return this->boundingRect().size();
case Qt::MaximumSize:
return QSizeF(parentItem
()->boundingRect
().
width(), parentItem
()->boundingRect
().
height());
default:
return this->boundingRect().size();
}
return constraint;
}
{
QGraphicsVideoItem::mouseMoveEvent(event);
}
{
QGraphicsVideoItem::mousePressEvent(event);
}
{
QGraphicsVideoItem::mouseReleaseEvent(event);
}
{
QGraphicsVideoItem::mouseDoubleClickEvent(event);
}
#include "videoplayer.h"
#include <QtWidgets>
#include <QVideoSurfaceFormat>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>
VideoPlayer::VideoPlayer(QString name, QString fileName) :
QGraphicsVideoItem(),
QGraphicsLayoutItem()
{
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemIsFocusable, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
setAcceptHoverEvents(true);
setAcceptDrops(true);
setAspectRatioMode(Qt::KeepAspectRatio);
_name = name;
_fileName = fileName;
_isPlaying = false;
_mediaPlayer = new QMediaPlayer(0, QMediaPlayer::VideoSurface);
_mediaPlayer->setVideoOutput(this);
connect(_mediaPlayer, &QMediaPlayer::stateChanged, this, &VideoPlayer::mediaStateChanged);
connect(_mediaPlayer, &QMediaPlayer::positionChanged, this, &VideoPlayer::positionChanged);
connect(_mediaPlayer, &QMediaPlayer::durationChanged, this, &VideoPlayer::durationChanged);
_pm = new QPixmap(QLatin1String(":/images/Resources/noise.png"));
setGraphicsItem(this);
}
QRectF VideoPlayer::boundingRect() const
{
return QRectF(QPointF(0, 0), geometry().size());
}
void VideoPlayer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
Q_UNUSED(option);
QRectF frame(QPointF(0,0), geometry().size());
qreal w = (parentItem()->boundingRect().width()/2) - 10;
qreal h = (parentItem()->boundingRect().height()/2) - 10;
// paint a rect around the pixmap
QPointF pixpos = frame.center() - (QPointF(w, h) / 2);
QRectF innerFrame(pixpos, QSizeF(w, h));
innerFrame.adjust(-4, -4, 4, 4);
painter->setBrush(QBrush(Qt::darkGreen));
if(isSelected())
{
painter->setBrush(QBrush(Qt::green));
painter->drawRoundedRect(innerFrame, 10.0, 10.0);
}
else
{
painter->setBrush(QBrush(Qt::darkGreen));
painter->drawRect(innerFrame);
}
// paint a title bar rect
QRect rectangle(-w/2, -h/2, w, 20);
painter->setPen(QPen(Qt::darkGreen));
painter->setBrush(QBrush(QColor(39,39,39)));
painter->drawRect(rectangle);
// paint video name in title bar
painter->drawText(rectangle, Qt::AlignCenter, _name);
// paint the default background pixmap
painter->drawPixmap(pixpos.x(), pixpos.y()+rectangle.height(), w, h-rectangle.height(), *_pm);
painter->setPen(QPen(QBrush(Qt::black), 10));
painter->drawText(innerFrame, Qt::AlignCenter, "No Video");
QGraphicsVideoItem::paint(painter, option, widget);
}
void VideoPlayer::Slot_OpenFile()
{
}
void VideoPlayer::Slot_Play()
{
}
void VideoPlayer::Slot_Stop()
{
_mediaPlayer->stop();
}
void VideoPlayer::Slot_Rewind()
{
}
void VideoPlayer::Slot_FastForward()
{
}
void VideoPlayer::mediaStateChanged(QMediaPlayer::State state)
{
}
void VideoPlayer::positionChanged(qint64 position)
{
}
void VideoPlayer::durationChanged(qint64 duration)
{
}
void VideoPlayer::setPosition(int position)
{
}
void VideoPlayer::setGeometry(const QRectF & rect)
{
prepareGeometryChange();
QGraphicsLayoutItem::setGeometry(rect);
setPos(rect.topLeft());
}
void VideoPlayer::updateGeometry()
{
QGraphicsLayoutItem::updateGeometry();
}
QSizeF VideoPlayer::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
{
switch ( which )
{
case Qt::MinimumSize:
case Qt::PreferredSize:
return this->boundingRect().size();
case Qt::MaximumSize:
return QSizeF(parentItem()->boundingRect().width(), parentItem()->boundingRect().height());
default:
return this->boundingRect().size();
}
return constraint;
}
void VideoPlayer::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{
QGraphicsVideoItem::mouseMoveEvent(event);
}
void VideoPlayer::mousePressEvent(QGraphicsSceneMouseEvent * event)
{
QGraphicsVideoItem::mousePressEvent(event);
}
void VideoPlayer::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
{
QGraphicsVideoItem::mouseReleaseEvent(event);
}
void VideoPlayer::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event)
{
QGraphicsVideoItem::mouseDoubleClickEvent(event);
}
To copy to clipboard, switch view to plain text mode
Bookmarks