Files
libopenshot/src/Qt/PlayerDemo.cpp

148 lines
3.2 KiB
C++
Raw Normal View History

2014-01-25 03:38:38 +08:00
/**
* @file
* @brief Source file for Demo QtPlayer application
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @ref License
*/
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
2014-01-25 03:38:38 +08:00
#include <string>
#include "PlayerDemo.h"
#include "../QtPlayer.h"
2014-01-25 03:38:38 +08:00
#include <QMessageBox>
#include <QFileDialog>
#include <QWidget>
#include <QBoxLayout>
#include <QMenuBar>
#include <QMenu>
#include <QKeyEvent>
#include <QCloseEvent>
#include <QApplication>
2014-01-25 03:38:38 +08:00
PlayerDemo::PlayerDemo(QWidget *parent)
: QWidget(parent)
, vbox(new QVBoxLayout(this))
, menu(new QMenuBar(this))
, video(new VideoRenderWidget(this))
, player(new openshot::QtPlayer(video->GetRenderer()))
2014-01-25 03:38:38 +08:00
{
setWindowTitle("OpenShot Player");
2014-01-25 03:38:38 +08:00
menu->setNativeMenuBar(false);
2014-01-25 03:38:38 +08:00
QAction *action = NULL;
action = menu->addAction("Choose File");
2014-01-25 03:38:38 +08:00
connect(action, SIGNAL(triggered(bool)), this, SLOT(open(bool)));
vbox->addWidget(menu, 0);
vbox->addWidget(video, 1);
vbox->setMargin(0);
vbox->setSpacing(0);
resize(600, 480);
// Accept keyboard event
setFocusPolicy(Qt::StrongFocus);
2014-01-25 03:38:38 +08:00
}
PlayerDemo::~PlayerDemo()
{
}
void PlayerDemo::closeEvent(QCloseEvent *event)
{
// Close window, stop player, and quit
QWidget *pWin = QApplication::activeWindow();
pWin->hide();
player->Stop();
QApplication::quit();
}
void PlayerDemo::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space || event->key() == Qt::Key_K) {
if (player->Mode() == openshot::PLAYBACK_PAUSED)
{
// paused, so start playing again
player->Play();
}
else if (player->Mode() == openshot::PLAYBACK_PLAY)
{
if (player->Speed() == 0)
// already playing, but speed is zero... so just speed up to normal
player->Speed(1);
else
// already playing... so pause
player->Pause();
}
}
else if (event->key() == Qt::Key_J) {
if (player->Speed() - 1 != 0)
player->Speed(player->Speed() - 1);
else
player->Speed(player->Speed() - 2);
if (player->Mode() == openshot::PLAYBACK_PAUSED)
player->Play();
}
else if (event->key() == Qt::Key_L) {
if (player->Speed() + 1 != 0)
player->Speed(player->Speed() + 1);
else
player->Speed(player->Speed() + 2);
if (player->Mode() == openshot::PLAYBACK_PAUSED)
player->Play();
}
else if (event->key() == Qt::Key_Left) {
if (player->Speed() != 0)
player->Speed(0);
player->Seek(player->Position() - 1);
}
else if (event->key() == Qt::Key_Right) {
if (player->Speed() != 0)
player->Speed(0);
player->Seek(player->Position() + 1);
}
else if (event->key() == Qt::Key_Escape) {
QWidget *pWin = QApplication::activeWindow();
pWin->hide();
player->Stop();
QApplication::quit();
}
event->accept();
QWidget::keyPressEvent(event);
}
2014-01-25 03:38:38 +08:00
void PlayerDemo::open(bool checked)
{
// Get filename of media files
2014-01-25 03:38:38 +08:00
const QString filename = QFileDialog::getOpenFileName(this, "Open Video File");
if (filename.isEmpty()) return;
// Create FFmpegReader and open file
2014-01-25 03:38:38 +08:00
player->SetSource(filename.toStdString());
// Set aspect ratio of widget
video->SetAspectRatio(player->Reader()->info.display_ratio, player->Reader()->info.pixel_ratio);
// Play video
2014-01-25 03:38:38 +08:00
player->Play();
}