xrdp/vrplayer/mainwindow.cpp

407 lines
10 KiB
C++
Raw Normal View History

2012-12-09 04:36:41 +04:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
/*
* TODO
* o should we use tick marks in QSlider?
*/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
/* connect to remote client */
interface = new OurInterface();
if (interface->oneTimeInit())
{
oneTimeInitSuccess = false;
/* connection to remote client failed; error msg has */
/* already been displayed so it's ok to close app now */
2012-12-31 11:02:06 +04:00
QTimer::singleShot(1000, this, SLOT(close()));
}
else
{
oneTimeInitSuccess = true;
}
remoteClientInited = false;
2012-12-09 04:36:41 +04:00
ui->setupUi(this);
acceptSliderMove = false;
setupUI();
vcrFlag = 0;
2012-12-09 04:36:41 +04:00
connect(this, SIGNAL(onGeometryChanged(int,int,int,int)),
interface, SLOT(onGeometryChanged(int,int,int,int)));
2012-12-09 04:36:41 +04:00
connect(interface, SIGNAL(onMediaDurationInSeconds(int)),
this, SLOT(onMediaDurationInSeconds(int)));
2012-12-09 04:36:41 +04:00
}
MainWindow::~MainWindow()
{
2012-12-31 11:02:06 +04:00
delete ui;
2012-12-09 04:36:41 +04:00
}
void MainWindow::closeEvent(QCloseEvent *event)
{
2012-12-31 11:02:06 +04:00
if (!oneTimeInitSuccess)
{
QMessageBox::warning(this, "Closing application",
"This is not an xrdp session with xrdpvr");
}
2012-12-09 04:36:41 +04:00
event->accept();
}
void MainWindow::resizeEvent(QResizeEvent *)
2012-12-09 04:36:41 +04:00
{
QRect rect;
getVdoGeometry(&rect);
interface->sendGeometry(rect);
2012-12-09 04:36:41 +04:00
}
void MainWindow::moveEvent(QMoveEvent *)
2012-12-09 04:36:41 +04:00
{
QRect rect;
getVdoGeometry(&rect);
interface->sendGeometry(rect);
2012-12-09 04:36:41 +04:00
}
void MainWindow::setupUI()
{
/* setup area to display video */
lblVideo = new QLabel();
lblVideo->setMinimumWidth(320);
lblVideo->setMinimumHeight(200);
QPalette palette = lblVideo->palette();
palette.setColor(lblVideo->backgroundRole(), Qt::black);
palette.setColor(lblVideo->foregroundRole(), Qt::black);
lblVideo->setAutoFillBackground(true);
lblVideo->setPalette(palette);
hboxLayoutTop = new QHBoxLayout;
hboxLayoutTop->addWidget(lblVideo);
/* setup label to display current pos in media */
lblCurrentPos = new QLabel("00:00:00");
lblCurrentPos->setMinimumHeight(20);
lblCurrentPos->setMaximumHeight(20);
/* setup slider to seek into media */
slider = new QSlider();
slider->setOrientation(Qt::Horizontal);
slider->setMinimumHeight(20);
slider->setMaximumHeight(20);
connect(slider, SIGNAL(actionTriggered(int)), this, SLOT(onSliderActionTriggered(int)));
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(onSliderValueChanged(int)));
2012-12-09 04:36:41 +04:00
/* setup label to display media duration */
lblDuration = new QLabel("00:00:00");
lblDuration->setMinimumHeight(20);
lblDuration->setMaximumHeight(20);
/* add above three widgets to mid layout */
hboxLayoutMiddle = new QHBoxLayout;
hboxLayoutMiddle->addWidget(lblCurrentPos);
hboxLayoutMiddle->addWidget(slider);
hboxLayoutMiddle->addWidget(lblDuration);
/* setup play button */
btnPlay = new QPushButton("Play");
2012-12-09 04:36:41 +04:00
btnPlay->setMinimumHeight(40);
btnPlay->setMaximumHeight(40);
btnPlay->setMinimumWidth(40);
btnPlay->setMaximumWidth(40);
btnPlay->setCheckable(true);
connect(btnPlay, SIGNAL(clicked(bool)),
this, SLOT(onBtnPlayClicked(bool)));
2012-12-09 04:36:41 +04:00
/* setup stop button */
btnStop = new QPushButton("Stop");
2012-12-09 04:36:41 +04:00
btnStop->setMinimumHeight(40);
btnStop->setMaximumHeight(40);
btnStop->setMinimumWidth(40);
btnStop->setMaximumWidth(40);
connect(btnStop, SIGNAL(clicked(bool)),
this, SLOT(onBtnStopClicked(bool)));
2012-12-09 04:36:41 +04:00
/* setup rewind button */
btnRewind = new QPushButton("R");
btnRewind->setMinimumHeight(40);
btnRewind->setMaximumHeight(40);
btnRewind->setMinimumWidth(40);
btnRewind->setMaximumWidth(40);
connect(btnRewind, SIGNAL(clicked(bool)),
this, SLOT(onBtnRewindClicked(bool)));
2012-12-09 04:36:41 +04:00
/* add buttons to bottom panel */
hboxLayoutBottom = new QHBoxLayout;
hboxLayoutBottom->addWidget(btnPlay);
hboxLayoutBottom->addWidget(btnStop);
//hboxLayoutBottom->addWidget(btnRewind);
2012-12-09 04:36:41 +04:00
hboxLayoutBottom->addStretch();
/* add all three layouts to one vertical layout */
vboxLayout = new QVBoxLayout;
vboxLayout->addLayout(hboxLayoutTop);
vboxLayout->addLayout(hboxLayoutMiddle);
vboxLayout->addLayout(hboxLayoutBottom);
/* add all of them to central widget */
window = new QWidget;
window->setLayout(vboxLayout);
this->setCentralWidget(window);
}
void MainWindow::openMediaFile()
{
/* TODO take last stored value from QSettings */
if (filename.length() == 0)
{
/* no previous selection - open user's home folder TODO */
// TODO filename = QFileDialog::getOpenFileName(this, "Select Media File", "/");
2012-12-31 11:02:06 +04:00
filename = QFileDialog::getOpenFileName(this, "Select Media File",
QDir::currentPath());
2012-12-09 04:36:41 +04:00
}
else
{
/* show last selected file */
filename = QFileDialog::getOpenFileName(this, "Select Media File",
filename);
}
interface->setFilename(filename);
2012-12-09 04:36:41 +04:00
}
void MainWindow::getVdoGeometry(QRect *rect)
{
int x;
int y;
int width;
int height;
QPoint pt;
pt = lblVideo->mapToGlobal(QPoint(0, 0));
x = pt.x();
y = pt.y();
width = lblVideo->width();
height = lblVideo->height();
2012-12-09 04:36:41 +04:00
rect->setX(x);
rect->setY(y);
rect->setWidth(width);
rect->setHeight(height);
2012-12-09 04:36:41 +04:00
}
void MainWindow::clearDisplay()
{
QPixmap pixmap(100,100);
pixmap.fill(QColor(0x00, 0x00, 0x00));
QPainter painter(&pixmap);
painter.setBrush(QBrush(Qt::black));
lblVideo->setPixmap(pixmap);
}
2012-12-09 04:36:41 +04:00
/*******************************************************************************
* actions and slots go here *
******************************************************************************/
void MainWindow::on_actionOpen_Media_File_triggered()
{
if (vcrFlag != 0)
onBtnStopClicked(false);
2012-12-09 04:36:41 +04:00
openMediaFile();
if (filename.length() == 0)
{
/* cancel btn was clicked */
return;
}
if (remoteClientInited)
{
remoteClientInited = false;
interface->deInitRemoteClient();
interface->initRemoteClient();
}
else
{
interface->initRemoteClient();
}
playVideo = interface->getPlayVideoInstance();
if (playVideo)
{
connect(playVideo, SIGNAL(onElapsedtime(int)),
this, SLOT(onElapsedTime(int)));
}
remoteClientInited = true;
interface->playMedia();
if (vcrFlag != 0)
{
interface->setVcrOp(VCR_PLAY);
btnPlay->setText("Pause");
vcrFlag = VCR_PLAY;
}
2012-12-09 04:36:41 +04:00
}
void MainWindow::on_actionExit_triggered()
{
clearDisplay();
2012-12-09 04:36:41 +04:00
this->close();
}
void MainWindow::onBtnPlayClicked(bool)
2012-12-09 04:36:41 +04:00
{
if (vcrFlag == 0)
{
/* first time play button has been clicked */
on_actionOpen_Media_File_triggered();
btnPlay->setText("Pause");
vcrFlag = VCR_PLAY;
}
else if (vcrFlag == VCR_PLAY)
{
/* btn clicked while in play mode - enter pause mode */
btnPlay->setText("Play");
interface->setVcrOp(VCR_PAUSE);
vcrFlag = VCR_PAUSE;
}
else if (vcrFlag == VCR_PAUSE)
{
/* btn clicked while in pause mode - enter play mode */
btnPlay->setText("Pause");
interface->setVcrOp(VCR_PLAY);
vcrFlag = VCR_PLAY;
}
else if (vcrFlag == VCR_STOP)
{
/* btn clicked while stopped - enter play mode */
btnPlay->setText("Play");
interface->setVcrOp(VCR_PLAY);
vcrFlag = VCR_PLAY;
}
2012-12-09 04:36:41 +04:00
}
void MainWindow::onBtnRewindClicked(bool)
2012-12-09 04:36:41 +04:00
{
if (playVideo)
playVideo->onMediaSeek(0);
2012-12-09 04:36:41 +04:00
}
void MainWindow::onBtnStopClicked(bool)
2012-12-09 04:36:41 +04:00
{
vcrFlag = VCR_STOP;
btnPlay->setText("Play");
interface->setVcrOp(VCR_STOP);
/* reset slider */
slider->setSliderPosition(0);
lblCurrentPos->setText("00:00:00");
2012-12-09 04:36:41 +04:00
/* clear screen by filling it with black */
clearDisplay();
2012-12-09 04:36:41 +04:00
}
void MainWindow::onMediaDurationInSeconds(int duration)
2012-12-09 04:36:41 +04:00
{
int hours = 0;
int minutes = 0;
int secs = 0;
char buf[20];
/* setup progress bar */
slider->setMinimum(0);
slider->setMaximum(duration * 100); /* in hundredth of a sec */
slider->setValue(0);
slider->setSliderPosition(0);
lblCurrentPos->setText("00:00:00");
//qDebug() << "media_duration=" << duration << " in hundredth of a sec:" << duration * 100;
2012-12-09 04:36:41 +04:00
/* convert from seconds to hours:minutes:seconds */
hours = duration / 3600;
if (hours)
duration -= (hours * 3600);
minutes = duration / 60;
if (minutes)
duration -= minutes * 60;
secs = duration;
sprintf(buf, "%.2d:%.2d:%.2d", hours, minutes, secs);
lblDuration->setText(QString(buf));
}
/**
* time elapsed in 1/100th sec units since play started
******************************************************************************/
void MainWindow::onElapsedTime(int val)
2012-12-09 04:36:41 +04:00
{
int hours = 0;
int minutes = 0;
int secs = 0;
int duration = 0;
2012-12-09 04:36:41 +04:00
char buf[20];
if (vcrFlag == VCR_STOP)
return;
2012-12-09 04:36:41 +04:00
/* if slider bar is down, do not update */
if (slider->isSliderDown())
return;
/* update progress bar */
if (val >= slider->maximum())
val = 0;
2012-12-09 04:36:41 +04:00
slider->setSliderPosition(val);
/* convert from seconds to hours:minutes:seconds */
duration = val / 100;
2012-12-09 04:36:41 +04:00
hours = duration / 3600;
if (hours)
duration -= (hours * 3600);
minutes = duration / 60;
if (minutes)
duration -= minutes * 60;
secs = duration;
/* update current position in progress bar */
sprintf(buf, "%.2d:%.2d:%.2d", hours, minutes, secs);
lblCurrentPos->setText(QString(buf));
}
void MainWindow::onSliderValueChanged(int value)
2012-12-09 04:36:41 +04:00
{
if (acceptSliderMove)
{
acceptSliderMove = false;
if (playVideo)
playVideo->onMediaSeek(value / 100);
2012-12-09 04:36:41 +04:00
}
}
void MainWindow::onSliderActionTriggered(int action)
2012-12-09 04:36:41 +04:00
{
switch (action)
{
case QAbstractSlider::SliderPageStepAdd:
acceptSliderMove = true;
break;
case QAbstractSlider::SliderPageStepSub:
acceptSliderMove = true;
break;
case QAbstractSlider::SliderMove:
if (slider->isSliderDown())
acceptSliderMove = true;
break;
}
}