Files
calyp/app/VideoHandle.cpp

1000 lines
35 KiB
C++

/* This file is a part of Calyp project
* Copyright (C) 2014-2021 by Joao Carreira (jfmcarreira@gmail.com)
* Luis Lucas (luisfrlucas@gmail.com)
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* \file VideoHandle.h
* \brief Class to handle video playback
*/
#include "VideoHandle.h"
#include <QDockWidget>
#include <QElapsedTimer>
#include <QLabel>
#include <QSignalMapper>
#include <QSlider>
#include <QTimer>
#include <QToolBar>
#include <QtGui>
#include <limits>
#include <memory>
#include "FrameNumberWidget.h"
#include "FramePropertiesDock.h"
#include "MainWindow.h"
#include "SeekStreamDialog.h"
#include "SubWindowHandle.h"
#include "SubWindowSelectorDialog.h"
#include "VideoStreamSubWindow.h"
#include "VideoSubWindow.h"
#include "lib/CalypFrame.h"
VideoHandle::VideoHandle( QWidget* parent, SubWindowHandle* windowManager )
: m_pcParent{ parent }
, m_pcMainWindowManager{ windowManager }
, m_pcPlayingTimer{ new QTimer{ this } }
, m_pcFrameRateFeedbackTimer{ std::make_unique<QElapsedTimer>() }
{
m_pcPlayingTimer->setTimerType( Qt::PreciseTimer );
connect( m_pcPlayingTimer, &QTimer::timeout, this, &VideoHandle::playEvent );
m_acPlayingSubWindows.clear();
}
VideoHandle::~VideoHandle() = default;
void VideoHandle::createActions()
{
if( m_pcParent == nullptr )
{
return;
}
m_arrayActions.resize( TOTAL_ACT );
m_arrayActions[PLAY_ACT] = new QAction( "Play", this );
m_arrayActions[PLAY_ACT]->setStatusTip( "Play/Pause" );
m_arrayActions[PLAY_ACT]->setIcon( QIcon( m_pcParent->style()->standardIcon( QStyle::SP_MediaPlay ) ) );
m_arrayActions[PLAY_ACT]->setShortcut( Qt::Key_Space );
connect( m_arrayActions[PLAY_ACT], SIGNAL( triggered() ), this, SLOT( play() ) );
m_arrayActions[STOP_ACT] = new QAction( "Stop", this );
m_arrayActions[STOP_ACT]->setStatusTip( "Stop" );
m_arrayActions[STOP_ACT]->setIcon( QIcon( m_pcParent->style()->standardIcon( QStyle::SP_MediaStop ) ) );
connect( m_arrayActions[STOP_ACT], SIGNAL( triggered() ), this, SLOT( stop() ) );
m_mapperVideoSeek = new QSignalMapper( this );
connect( m_mapperVideoSeek, SIGNAL( mapped( int ) ), this, SLOT( seekEvent( int ) ) );
m_arrayActions[VIDEO_BACKWARD_ACT] = new QAction( "Video Backward", this );
m_arrayActions[VIDEO_BACKWARD_ACT]->setStatusTip( "Seek backward" );
m_arrayActions[VIDEO_BACKWARD_ACT]->setIcon(
QIcon( m_pcParent->style()->standardIcon( QStyle::SP_MediaSeekBackward ) ) );
m_arrayActions[VIDEO_BACKWARD_ACT]->setShortcut( Qt::Key_Left );
connect( m_arrayActions[VIDEO_BACKWARD_ACT], SIGNAL( triggered() ), m_mapperVideoSeek, SLOT( map() ) );
m_mapperVideoSeek->setMapping( m_arrayActions[VIDEO_BACKWARD_ACT], 0 );
m_arrayActions[VIDEO_FORWARD_ACT] = new QAction( "Video Forward", this );
m_arrayActions[VIDEO_FORWARD_ACT]->setStatusTip( "Seek forward" );
m_arrayActions[VIDEO_FORWARD_ACT]->setIcon(
QIcon( m_pcParent->style()->standardIcon( QStyle::SP_MediaSeekForward ) ) );
m_arrayActions[VIDEO_FORWARD_ACT]->setShortcut( Qt::Key_Right );
connect( m_arrayActions[VIDEO_FORWARD_ACT], SIGNAL( triggered() ), m_mapperVideoSeek, SLOT( map() ) );
m_mapperVideoSeek->setMapping( m_arrayActions[VIDEO_FORWARD_ACT], 1 );
m_arrayActions[VIDEO_GOTO_ACT] = new QAction( "Go to", this );
m_arrayActions[VIDEO_GOTO_ACT]->setStatusTip( "Seek to a specific frame number" );
m_arrayActions[VIDEO_GOTO_ACT]->setShortcut( tr( "Ctrl+G" ) );
connect( m_arrayActions[VIDEO_GOTO_ACT], SIGNAL( triggered() ), this, SLOT( seekVideo() ) );
m_arrayActions[VIDEO_REPEAT_ACT] = new QAction( "Repeat", this );
m_arrayActions[VIDEO_REPEAT_ACT]->setStatusTip( "Enable loop through the sequence" );
m_arrayActions[VIDEO_REPEAT_ACT]->setShortcut( tr( "Ctrl+R" ) );
m_arrayActions[VIDEO_REPEAT_ACT]->setCheckable( true );
m_arrayActions[VIDEO_REPEAT_ACT]->setChecked( false );
m_arrayActions[VIDEO_ZOOM_LOCK_ACT] = new QAction( "Zoom Lock", this );
m_arrayActions[VIDEO_ZOOM_LOCK_ACT]->setStatusTip( "Sync zoom between all video windows" );
m_arrayActions[VIDEO_ZOOM_LOCK_ACT]->setShortcut( tr( "Ctrl+L" ) );
m_arrayActions[VIDEO_ZOOM_LOCK_ACT]->setCheckable( true );
m_arrayActions[VIDEO_ZOOM_LOCK_ACT]->setChecked( false );
m_arrayActions[VIDEO_LOCK_SELECTION_ACT] = new QAction( "Sync Play", this );
m_arrayActions[VIDEO_LOCK_SELECTION_ACT]->setStatusTip(
"Select which windows should be played synchronized. Press Ctrl while "
"click to quickly to select all!" );
connect( m_arrayActions[VIDEO_LOCK_SELECTION_ACT], SIGNAL( triggered() ), this, SLOT( videoSelectionButtonEvent() ) );
m_pcFrameSlider = new QSlider;
m_pcFrameSlider->setOrientation( Qt::Horizontal );
m_pcFrameSlider->setMaximumWidth( 2000 );
m_pcFrameSlider->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ) );
m_pcFrameSlider->setEnabled( false );
m_pcFrameSlider->setTracking( false );
connect( m_pcFrameSlider, SIGNAL( valueChanged( int ) ), this, SLOT( seekSliderEvent( int ) ) );
// ------------ Image ------------
m_mapperComponent = new QSignalMapper( this );
m_actionGroupComponent = new QActionGroup( this );
m_actionGroupComponent->setExclusive( true );
m_mapperComponent->setMapping( m_actionGroupComponent->addAction( "All" ), -1 );
m_mapperComponent->setMapping( m_actionGroupComponent->addAction( "Red" ), 0 );
m_mapperComponent->setMapping( m_actionGroupComponent->addAction( "Green" ), 1 );
m_mapperComponent->setMapping( m_actionGroupComponent->addAction( "Blue" ), 2 );
m_mapperComponent->setMapping( m_actionGroupComponent->addAction( "Alpha" ), 3 );
for( int i = 0; i < m_actionGroupComponent->actions().size(); i++ )
{
m_actionGroupComponent->actions().at( i )->setCheckable( true );
m_actionGroupComponent->actions().at( i )->setVisible( false );
connect( m_actionGroupComponent->actions().at( i ), SIGNAL( triggered() ), m_mapperComponent, SLOT( map() ) );
}
m_actionGroupComponent->actions().at( 0 )->setChecked( true );
m_actionGroupComponent->actions().at( 0 )->setVisible( true );
connect( m_mapperComponent, SIGNAL( mapped( int ) ), this, SLOT( setComponent( int ) ) );
m_actionGroupTools = new QActionGroup( this );
m_actionGroupTools->setExclusive( true );
m_mapperTools = new QSignalMapper( this );
connect( m_mapperTools, SIGNAL( mapped( int ) ), this, SLOT( setTool( int ) ) );
m_uiViewTool = ViewArea::NavigationView;
m_arrayActions[NAVIGATION_TOOL_ACT] = new QAction( tr( "Navigation Tool" ), this );
m_arrayActions[NAVIGATION_TOOL_ACT]->setCheckable( true );
m_arrayActions[NAVIGATION_TOOL_ACT]->setChecked( true );
m_arrayActions[NAVIGATION_TOOL_ACT]->setShortcut( Qt::CTRL | Qt::Key_1 );
m_actionGroupTools->addAction( m_arrayActions[NAVIGATION_TOOL_ACT] );
connect( m_arrayActions[NAVIGATION_TOOL_ACT], SIGNAL( triggered() ), m_mapperTools, SLOT( map() ) );
m_mapperTools->setMapping( m_arrayActions[NAVIGATION_TOOL_ACT], ViewArea::NavigationView );
m_arrayActions[SELECTION_TOOL_ACT] = new QAction( "Selection Tool", this );
m_arrayActions[SELECTION_TOOL_ACT]->setCheckable( true );
m_arrayActions[SELECTION_TOOL_ACT]->setChecked( false );
m_arrayActions[SELECTION_TOOL_ACT]->setShortcut( Qt::CTRL | Qt::Key_2 );
m_actionGroupTools->addAction( m_arrayActions[SELECTION_TOOL_ACT] );
connect( m_arrayActions[SELECTION_TOOL_ACT], SIGNAL( triggered() ), m_mapperTools, SLOT( map() ) );
m_mapperTools->setMapping( m_arrayActions[SELECTION_TOOL_ACT], ViewArea::NormalSelectionView );
m_arrayActions[BLOCK_SELECTION_TOOL_ACT] = new QAction( "Block Selection Tool", this );
m_arrayActions[BLOCK_SELECTION_TOOL_ACT]->setCheckable( true );
m_arrayActions[BLOCK_SELECTION_TOOL_ACT]->setChecked( false );
m_arrayActions[BLOCK_SELECTION_TOOL_ACT]->setShortcut( Qt::CTRL | Qt::Key_3 );
m_actionGroupTools->addAction( m_arrayActions[BLOCK_SELECTION_TOOL_ACT] );
connect( m_arrayActions[BLOCK_SELECTION_TOOL_ACT], SIGNAL( triggered() ), m_mapperTools, SLOT( map() ) );
m_mapperTools->setMapping( m_arrayActions[BLOCK_SELECTION_TOOL_ACT], ViewArea::BlockSelectionView );
m_mapperGrid = new QSignalMapper( this );
m_actionGroupGrid = new QActionGroup( this );
m_actionGroupGrid->setExclusive( true );
m_mapperGrid->setMapping( m_actionGroupGrid->addAction( "4x4" ), 4 );
m_mapperGrid->setMapping( m_actionGroupGrid->addAction( "8x8" ), 8 );
m_mapperGrid->setMapping( m_actionGroupGrid->addAction( "16x16" ), 16 );
m_mapperGrid->setMapping( m_actionGroupGrid->addAction( "64x64" ), 64 );
m_mapperGrid->setMapping( m_actionGroupGrid->addAction( "128x128" ), 128 );
for( int i = 0; i < m_actionGroupGrid->actions().size(); i++ )
{
m_actionGroupGrid->actions().at( i )->setCheckable( true );
connect( m_actionGroupGrid->actions().at( i ), SIGNAL( triggered() ), m_mapperGrid, SLOT( map() ) );
}
m_actionGroupGrid->actions().at( 2 )->setChecked( true );
connect( m_mapperGrid, SIGNAL( mapped( int ) ), this, SLOT( setGridSize( int ) ) );
m_arrayActions[SHOW_GRID_ACT] = new QAction( "Show grid", this );
m_arrayActions[SHOW_GRID_ACT]->setCheckable( true );
m_arrayActions[SHOW_GRID_ACT]->setChecked( false );
connect( m_arrayActions[SHOW_GRID_ACT], SIGNAL( toggled( bool ) ), this, SLOT( toggleGrid( bool ) ) );
}
QMenu* VideoHandle::createVideoMenu()
{
if( m_pcParent == nullptr )
{
return nullptr;
}
m_pcMenuVideo = new QMenu( "Video", m_pcParent );
m_pcMenuVideo->addAction( m_arrayActions[PLAY_ACT] );
m_pcMenuVideo->addAction( m_arrayActions[STOP_ACT] );
m_pcMenuVideo->addAction( m_arrayActions[VIDEO_BACKWARD_ACT] );
m_pcMenuVideo->addAction( m_arrayActions[VIDEO_FORWARD_ACT] );
m_pcMenuVideo->addAction( m_arrayActions[VIDEO_GOTO_ACT] );
m_pcMenuVideo->addSeparator();
m_pcMenuVideo->addAction( m_arrayActions[VIDEO_REPEAT_ACT] );
m_pcMenuVideo->addAction( m_arrayActions[VIDEO_ZOOM_LOCK_ACT] );
m_pcMenuVideo->addAction( m_arrayActions[VIDEO_LOCK_SELECTION_ACT] );
return m_pcMenuVideo;
}
QMenu* VideoHandle::createImageMenu()
{
if( m_pcParent == nullptr )
{
return nullptr;
}
m_pcMenuImage = new QMenu( "Image", m_pcParent );
m_menuComponent = m_pcMenuImage->addMenu( "Component" );
m_menuComponent->addActions( m_actionGroupComponent->actions() );
m_pcMenuImage->addAction( m_arrayActions[NAVIGATION_TOOL_ACT] );
m_pcMenuImage->addAction( m_arrayActions[SELECTION_TOOL_ACT] );
m_pcMenuImage->addAction( m_arrayActions[BLOCK_SELECTION_TOOL_ACT] );
m_pcMenuImage->addSeparator();
m_pcMenuImage->addAction( m_arrayActions[SHOW_GRID_ACT] );
QMenu* gridSizeMenu = m_pcMenuImage->addMenu( "Grid size" );
gridSizeMenu->addActions( m_actionGroupGrid->actions() );
return m_pcMenuImage;
}
auto VideoHandle::createToolBar() -> QPointer<QToolBar>
{
if( m_pcParent == nullptr ) return nullptr;
QPointer m_toolbarVideo = new QToolBar( tr( "Video" ) );
m_toolbarVideo->setAllowedAreas( Qt::TopToolBarArea | Qt::BottomToolBarArea );
m_toolbarVideo->addAction( m_arrayActions[PLAY_ACT] );
m_toolbarVideo->addAction( m_arrayActions[STOP_ACT] );
QPointer pcLockLabel = new QLabel( "PlayingLock" );
pcLockLabel->setAlignment( Qt::AlignCenter );
m_arrayActions[VIDEO_LOCK_ACT] = m_toolbarVideo->addWidget( pcLockLabel );
m_arrayActions[VIDEO_LOCK_ACT]->setVisible( false );
m_toolbarVideo->addAction( m_arrayActions[VIDEO_BACKWARD_ACT] );
m_toolbarVideo->addWidget( m_pcFrameSlider );
m_toolbarVideo->addAction( m_arrayActions[VIDEO_FORWARD_ACT] );
m_toolbarVideo->addWidget( new QLabel );
m_pcFrameNumInfo = new FrameNumberWidget;
m_toolbarVideo->addWidget( m_pcFrameNumInfo );
return m_toolbarVideo;
}
QDockWidget* VideoHandle::createDock()
{
if( m_pcParent == nullptr )
{
return nullptr;
}
m_pcFramePropertiesSideBar = new FramePropertiesDock( m_pcParent, &m_bIsPlaying );
m_pcFramePropertiesDock = new QDockWidget( tr( "Frame Information" ), m_pcParent );
m_pcFramePropertiesDock->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea );
m_pcFramePropertiesDock->setWidget( m_pcFramePropertiesSideBar );
connect( m_pcFramePropertiesDock, SIGNAL( visibilityChanged( bool ) ), this, SLOT( update() ) );
return m_pcFramePropertiesDock;
}
QWidget* VideoHandle::createStatusBarMessage()
{
if( m_pcParent == nullptr )
{
return nullptr;
}
QWidget* pcStatusBarWidget = new QWidget;
QHBoxLayout* mainlayout = new QHBoxLayout;
m_pcPlayingFPSLabel = new QLabel;
m_pcPlayingFPSLabel->setText( " " );
m_pcPlayingFPSLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
m_pcPlayingFPSLabel->setMinimumWidth( 150 );
m_pcPlayingFPSLabel->setAlignment( Qt::AlignCenter );
m_pcVideoFormatLabel = new QLabel;
m_pcVideoFormatLabel->setText( " " );
m_pcVideoFormatLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
m_pcVideoFormatLabel->setMinimumWidth( 150 );
m_pcVideoFormatLabel->setAlignment( Qt::AlignCenter );
m_pcResolutionLabel = new QLabel;
m_pcResolutionLabel->setText( " " );
m_pcResolutionLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
m_pcResolutionLabel->setMinimumWidth( 90 );
m_pcResolutionLabel->setAlignment( Qt::AlignCenter );
mainlayout->addWidget( m_pcPlayingFPSLabel );
mainlayout->addWidget( m_pcVideoFormatLabel );
mainlayout->addWidget( m_pcResolutionLabel );
pcStatusBarWidget->setLayout( mainlayout );
return pcStatusBarWidget;
}
void VideoHandle::updateComponentMenu()
{
VideoSubWindow* pcSubWindow = m_pcMainWindowManager->activeSubWindow<VideoSubWindow>();
if( pcSubWindow == nullptr )
{
m_menuComponent->setEnabled( false );
return;
}
const auto* frame = pcSubWindow->getCurrFrame();
if( frame == nullptr )
{
m_menuComponent->setEnabled( false );
return;
}
m_menuComponent->setEnabled( true );
for( int i = 1; i < m_actionGroupComponent->actions().size(); i++ )
{
m_actionGroupComponent->actions()[i]->setChecked( false );
m_actionGroupComponent->actions()[i]->setVisible( false );
}
switch( frame->getColorSpace() )
{
case CLP_COLOR_GRAY:
m_actionGroupComponent->actions().at( 1 )->setText( QStringLiteral( "Gray" ) );
m_actionGroupComponent->actions().at( 1 )->setVisible( true );
break;
case CLP_COLOR_YUV:
m_actionGroupComponent->actions().at( 1 )->setText( QStringLiteral( "Luminance" ) );
m_actionGroupComponent->actions().at( 1 )->setVisible( true );
m_actionGroupComponent->actions().at( 2 )->setText( QStringLiteral( "Chrominance U (Cb)" ) );
m_actionGroupComponent->actions().at( 2 )->setVisible( true );
m_actionGroupComponent->actions().at( 3 )->setText( QStringLiteral( "Chrominance V (Cr)" ) );
m_actionGroupComponent->actions().at( 3 )->setVisible( true );
break;
case CLP_COLOR_RGBA:
m_actionGroupComponent->actions().at( 4 )->setText( QStringLiteral( "Alpha" ) );
m_actionGroupComponent->actions().at( 4 )->setVisible( true );
[[fallthrough]];
case CLP_COLOR_RGB:
m_actionGroupComponent->actions().at( 1 )->setText( QStringLiteral( "Red" ) );
m_actionGroupComponent->actions().at( 1 )->setVisible( true );
m_actionGroupComponent->actions().at( 2 )->setText( QStringLiteral( "Green" ) );
m_actionGroupComponent->actions().at( 2 )->setVisible( true );
m_actionGroupComponent->actions().at( 3 )->setText( QStringLiteral( "Blue" ) );
m_actionGroupComponent->actions().at( 3 )->setVisible( true );
break;
default:
Q_ASSERT( false );
}
auto component = pcSubWindow->getViewArea()->getFilteredChannel();
int idx = component.has_value() ? *component : 0;
m_actionGroupComponent->actions().at( idx )->setChecked( true );
}
void VideoHandle::updateMenus()
{
VideoSubWindow* pcSubWindow = m_pcMainWindowManager->activeSubWindow<VideoSubWindow>();
bool hasSubWindow = pcSubWindow ? true : false;
m_arrayActions[PLAY_ACT]->setEnabled( hasSubWindow );
// m_arrayActions[PAUSE_ACT]->setEnabled( hasSubWindow );
m_arrayActions[STOP_ACT]->setEnabled( hasSubWindow );
m_arrayActions[VIDEO_BACKWARD_ACT]->setEnabled( hasSubWindow );
m_arrayActions[VIDEO_FORWARD_ACT]->setEnabled( hasSubWindow );
m_arrayActions[VIDEO_GOTO_ACT]->setEnabled( hasSubWindow );
m_arrayActions[VIDEO_LOCK_SELECTION_ACT]->setEnabled( hasSubWindow );
m_pcFrameSlider->setEnabled( hasSubWindow );
if( !hasSubWindow )
{
disconnect( m_pcFrameSlider, SIGNAL( valueChanged( int ) ), this, SLOT( seekSliderEvent( int ) ) );
m_pcFrameSlider->setValue( 0 );
m_pcFrameNumInfo->clear();
}
else
{
connect( m_pcFrameSlider, SIGNAL( valueChanged( int ) ), this, SLOT( seekSliderEvent( int ) ) );
}
m_arrayActions[NAVIGATION_TOOL_ACT]->setEnabled( hasSubWindow );
m_arrayActions[SELECTION_TOOL_ACT]->setEnabled( hasSubWindow );
m_arrayActions[BLOCK_SELECTION_TOOL_ACT]->setEnabled( hasSubWindow );
m_arrayActions[SHOW_GRID_ACT]->setEnabled( hasSubWindow );
updateComponentMenu();
}
void VideoHandle::readSettings()
{
QSettings appSettings;
m_arrayActions[VIDEO_REPEAT_ACT]->setChecked( appSettings.value( "VideoHandle/Repeat", false ).toBool() );
m_arrayActions[VIDEO_ZOOM_LOCK_ACT]->setChecked( appSettings.value( "VideoHandle/VideoZoomLock", false ).toBool() );
if( !appSettings.value( "VideoHandle/FrameProperties", true ).toBool() ) m_pcFramePropertiesDock->close();
m_uiViewTool = appSettings.value( "VideoHandle/SelectedTool", ViewArea::NavigationView ).toUInt();
setTool( m_uiViewTool );
m_arrayActions[SHOW_GRID_ACT]->setChecked( appSettings.value( "VideoHandle/ShowGrid", false ).toBool() );
}
void VideoHandle::writeSettings()
{
QSettings appSettings;
appSettings.setValue( "VideoHandle/Repeat", m_arrayActions[VIDEO_REPEAT_ACT]->isChecked() );
appSettings.setValue( "VideoHandle/VideoZoomLock", m_arrayActions[VIDEO_ZOOM_LOCK_ACT]->isChecked() );
appSettings.setValue( "VideoHandle/FrameProperties", m_pcFramePropertiesSideBar->isVisible() );
appSettings.setValue( "VideoHandle/SelectedTool", m_uiViewTool );
appSettings.setValue( "VideoHandle/ShowGrid", m_arrayActions[SHOW_GRID_ACT]->isChecked() );
}
void VideoHandle::update()
{
if( m_pcCurrentVideoSubWindow )
{
bool isPlaying{ false };
const CalypStream* pcStream{ nullptr };
if( auto* videoStreamSubWindow = qobject_cast<VideoStreamSubWindow*>( m_pcCurrentVideoSubWindow ) )
{
pcStream = videoStreamSubWindow->getInputStream();
isPlaying = videoStreamSubWindow->isPlaying();
}
auto pcFrame = m_pcCurrentVideoSubWindow->getCurrFrameAsset();
m_pcVideoFormatLabel->setText( m_pcCurrentVideoSubWindow->getVideoInformation() );
QString resolution;
if( pcFrame )
{
resolution.append( QString( "%1x%2" ).arg( pcFrame->getWidth() ).arg( pcFrame->getHeight() ) );
}
unsigned long frame_num = 0;
unsigned long total_frame_num = 1;
if( pcStream )
{
frame_num = pcStream->getCurrFrameNum();
m_pcFrameNumInfo->setCurrFrameNum( frame_num );
total_frame_num = getMaxFrameNumber();
resolution.append( QString( "@%1" ).arg( pcStream->getFrameRate() ) );
}
if( pcFrame )
{
resolution.append( QString( " %1 bpp" ).arg( pcFrame->getBitsPel() ) );
}
if( pcStream && pcFrame->getBitsPel() > 8 )
{
if( pcStream->getEndianess() == CLP_BIG_ENDIAN )
{
resolution.append( QStringLiteral( " (BE)" ) );
}
else
{
resolution.append( QStringLiteral( " (LE)" ) );
}
}
m_pcResolutionLabel->setText( resolution );
m_pcFramePropertiesSideBar->setFrame( pcFrame, isPlaying );
m_pcFrameNumInfo->setTotalFrameNum( total_frame_num );
m_pcFrameNumInfo->setCurrFrameNum( frame_num );
m_pcFrameSlider->blockSignals( true ); // Disable signals otherwise it will loop this function
m_pcFrameSlider->setMaximum( total_frame_num - 1 );
m_pcFrameSlider->setValue( frame_num );
m_pcFrameSlider->blockSignals( false );
if( m_uiRealAverageFrameRate > 0 )
{
m_pcPlayingFPSLabel->setText( QString( "%1 fps" ).arg( m_uiRealAverageFrameRate ) );
}
if( m_pcCurrentVideoSubWindow->isPlaying() )
{
m_arrayActions[PLAY_ACT]->setText( "Pause" );
m_arrayActions[PLAY_ACT]->setIcon( m_pcParent->style()->standardIcon( QStyle::SP_MediaPause ) );
}
else
{
m_arrayActions[PLAY_ACT]->setText( "Play" );
m_arrayActions[PLAY_ACT]->setIcon( m_pcParent->style()->standardIcon( QStyle::SP_MediaPlay ) );
}
}
else
{
m_pcPlayingFPSLabel->setText( "" );
m_pcVideoFormatLabel->setText( "" );
m_pcResolutionLabel->setText( "" );
m_pcFramePropertiesSideBar->reset();
m_pcFrameSlider->setValue( 0 );
m_pcFrameNumInfo->clear();
}
}
void VideoHandle::update( VideoSubWindow* currSubWindow )
{
m_pcCurrentVideoSubWindow = currSubWindow;
update();
}
void VideoHandle::updateSelectionArea( QRect area )
{
if( m_pcCurrentVideoSubWindow )
{
m_pcFramePropertiesSideBar->setSelection( area );
}
}
void VideoHandle::addSubWindow( VideoSubWindow* window )
{
window->zoomToFit();
window->getViewArea()->setTool( m_uiViewTool );
window->getViewArea()->setGridVisible( m_arrayActions[SHOW_GRID_ACT]->isChecked() );
connect( window, &SubWindowAbstract::aboutToClose, this, &VideoHandle::closeSubWindow );
connect( window, &SubWindowAbstract::zoomFactorChanged, this, &VideoHandle::zoomToFactorAll );
connect( window, &SubWindowAbstract::scrollBarMoved, this, &VideoHandle::moveAllScrollBars );
connect( window->getViewArea(), SIGNAL( selectionChanged( QRect ) ), this, SLOT( updateSelectionArea( QRect ) ) );
}
void VideoHandle::closeSubWindow( SubWindowAbstract* subWindow )
{
auto* videoStreamSubWindow = qobject_cast<VideoStreamSubWindow*>( subWindow );
if( m_acPlayingSubWindows.contains( videoStreamSubWindow ) )
{
int pos = m_acPlayingSubWindows.indexOf( videoStreamSubWindow );
m_acPlayingSubWindows.remove( pos );
if( m_acPlayingSubWindows.size() < 2 )
{
m_arrayActions[VIDEO_LOCK_ACT]->setVisible( false );
}
}
if( subWindow == m_pcCurrentVideoSubWindow )
{
m_pcFramePropertiesSideBar->reset();
}
}
void VideoHandle::zoomToFactorAll( const double scale, const QPoint center )
{
if( m_arrayActions[VIDEO_ZOOM_LOCK_ACT]->isChecked() )
{
double factor = m_pcCurrentVideoSubWindow->getScaleFactor();
QList<SubWindowAbstract*> subWindowList =
m_pcMainWindowManager->findSubWindow( SubWindowAbstract::VIDEO_SUBWINDOW );
for( int i = 0; i < subWindowList.size(); i++ )
{
SubWindowAbstract* subWindow = subWindowList.at( i );
if( m_pcCurrentVideoSubWindow == subWindow )
continue;
else
{
subWindow->zoomToFactor( factor, center );
}
}
}
}
#if 0
void VideoHandle::moveAllScrollBars( const QPoint& offset )
{
QPoint newOffset = offset;
if( m_arrayActions[VIDEO_ZOOM_LOCK_ACT]->isChecked() && m_pcCurrentVideoSubWindow )
{
VideoSubWindow* videoSubWindow;
QList<SubWindowAbstract*> subWindowList =
m_pcMainWindowManager->findSubWindow( SubWindowAbstract::VIDEO_SUBWINDOW );
QScrollBar* scrollBar;
scrollBar = m_pcCurrentVideoSubWindow->getScroll()->horizontalScrollBar();
// Do not move other images, if current image reached maximum or minimum
// scroll position
if( scrollBar->value() == scrollBar->maximum() && offset.x() > 0 )
newOffset.setX( 0 );
if( scrollBar->value() == scrollBar->minimum() && offset.x() < 0 )
newOffset.setX( 0 );
scrollBar = m_pcCurrentVideoSubWindow->getScroll()->verticalScrollBar();
if( scrollBar->value() == scrollBar->maximum() && offset.y() > 0 )
newOffset.setY( 0 );
if( scrollBar->value() == scrollBar->minimum() && offset.y() < 0 )
newOffset.setY( 0 );
for( int i = 0; i < subWindowList.size(); i++ )
{
videoSubWindow = qobject_cast<VideoSubWindow*>( subWindowList.at( i ) );
if( m_pcCurrentVideoSubWindow == videoSubWindow )
continue;
else
{
videoSubWindow->adjustScrollBarByOffset( newOffset );
}
}
}
}
#else
void VideoHandle::moveAllScrollBars( const double& horRatio, const double& verRatio )
{
if( m_arrayActions[VIDEO_ZOOM_LOCK_ACT]->isChecked() && m_pcCurrentVideoSubWindow )
{
QList<SubWindowAbstract*> subWindowList =
m_pcMainWindowManager->findSubWindow( SubWindowAbstract::VIDEO_SUBWINDOW );
for( int i = 0; i < subWindowList.size(); i++ )
{
VideoSubWindow* videoSubWindow = qobject_cast<VideoSubWindow*>( subWindowList.at( i ) );
if( m_pcCurrentVideoSubWindow == videoSubWindow )
continue;
else
{
videoSubWindow->adjustScrollBarToRatio( horRatio, verRatio );
}
}
}
}
#endif
unsigned long VideoHandle::getMaxFrameNumber()
{
std::uint64_t maxFrames = std::numeric_limits<std::uint64_t>::max();
if( m_pcCurrentVideoSubWindow )
{
if( m_acPlayingSubWindows.contains( qobject_cast<VideoStreamSubWindow*>( m_pcCurrentVideoSubWindow ) ) )
{
for( int i = 0; i < m_acPlayingSubWindows.size(); i++ )
{
auto currFrames = m_acPlayingSubWindows.at( i )->getFrameNum();
if( currFrames < maxFrames ) maxFrames = currFrames;
}
}
else
{
maxFrames = m_pcCurrentVideoSubWindow->getFrameNum();
}
}
return maxFrames;
}
void VideoHandle::setTimerStatus()
{
bool status = false;
for( int i = 0; i < m_acPlayingSubWindows.size(); i++ )
{
status |= m_acPlayingSubWindows.at( i )->isPlaying();
}
if( auto* videoStreamSubWindow = qobject_cast<VideoStreamSubWindow*>( m_pcCurrentVideoSubWindow ) )
{
status |= videoStreamSubWindow->isPlaying();
}
if( status )
{
if( !m_bIsPlaying )
{
m_pcPlayingTimer->start();
m_bIsPlaying = true;
m_uiNumberPlayedFrames = 0;
m_pcFrameRateFeedbackTimer->start();
m_uiRealAverageFrameRate = 0;
}
}
else
{
m_pcPlayingTimer->stop();
m_bIsPlaying = false;
}
}
void VideoHandle::configureFrameRateTimer()
{
double frame_rate = m_acPlayingSubWindows.at( 0 )->getInputStream()->getFrameRate();
unsigned int time_ms = (unsigned int)( 1000.0 / frame_rate + 0.5 );
m_pcPlayingTimer->setInterval( time_ms );
}
void VideoHandle::play()
{
auto* videoStreamSubWindow = qobject_cast<VideoStreamSubWindow*>( m_pcCurrentVideoSubWindow );
if( videoStreamSubWindow == nullptr ) return;
if( !videoStreamSubWindow->isPlaying() ) // Not playing
{
if( m_acPlayingSubWindows.size() < 2 )
{
for( int i = 0; i < m_acPlayingSubWindows.size(); i++ )
{
m_acPlayingSubWindows.at( i )->pause();
}
m_acPlayingSubWindows.clear();
m_acPlayingSubWindows.append( videoStreamSubWindow );
}
if( m_acPlayingSubWindows.contains( videoStreamSubWindow ) )
{
for( int i = 0; i < m_acPlayingSubWindows.size(); i++ )
{
m_acPlayingSubWindows.at( i )->play();
}
configureFrameRateTimer();
}
}
else
{
if( m_acPlayingSubWindows.contains( videoStreamSubWindow ) )
{
for( int i = 0; i < m_acPlayingSubWindows.size(); i++ )
{
m_acPlayingSubWindows.at( i )->pause();
}
}
else
{
videoStreamSubWindow->pause();
}
}
setTimerStatus();
emit changed();
}
void VideoHandle::stop()
{
if( m_acPlayingSubWindows.size() > 0 )
{
for( auto playingSubWindow : m_acPlayingSubWindows )
{
playingSubWindow->stop();
}
m_acPlayingSubWindows.clear();
setTimerStatus();
}
else
{
if( m_pcCurrentVideoSubWindow )
{
m_pcCurrentVideoSubWindow->stop();
}
}
emit changed();
m_arrayActions[VIDEO_LOCK_ACT]->setVisible( false );
}
void VideoHandle::calculateRealFrameRate()
{
double average_time_between_frames_ms = m_uiNumberPlayedFrames > 0 ? 1000.0 / m_uiRealAverageFrameRate : 0;
auto time_elapsed_since_last_frame_ms = static_cast<double>( m_pcFrameRateFeedbackTimer->elapsed() );
average_time_between_frames_ms =
double( average_time_between_frames_ms * m_uiNumberPlayedFrames + time_elapsed_since_last_frame_ms ) /
double( m_uiNumberPlayedFrames + 1 );
m_uiRealAverageFrameRate =
std::round<unsigned int>( static_cast<unsigned int>( 1000.0 / average_time_between_frames_ms ) );
m_uiNumberPlayedFrames++;
m_pcFrameRateFeedbackTimer->restart();
}
void VideoHandle::playEvent()
{
// auto start = std::chrono::steady_clock::now();
// This value should only be true if it is really needed to refresh the UI after this play event
// Lets assume the current window is not playing. Nothing changes in the UI expect the playing windows
// So no changed is emmited
bool bShouldRefreshUI = false;
calculateRealFrameRate();
try
{
bool bEndOfSequence = false;
for( auto playingSubWindow : m_acPlayingSubWindows )
{
bEndOfSequence |= playingSubWindow->playEvent();
// If the current selected windows is playing then we need to refresh the UI
if( playingSubWindow == m_pcCurrentVideoSubWindow )
{
bShouldRefreshUI = true;
}
}
if( bEndOfSequence )
{
if( !m_arrayActions[VIDEO_REPEAT_ACT]->isChecked() )
{
stop();
}
else
{
for( auto playingSubWindow : m_acPlayingSubWindows )
{
playingSubWindow->seekAbsoluteEvent( 0 );
}
}
}
}
catch( const char* msg )
{
QString warningMsg =
"Error while playing " + m_pcCurrentVideoSubWindow->getWindowName() + " with the following error: \n" + msg;
QMessageBox::warning( m_pcParent, QApplication::applicationName(), warningMsg );
qDebug() << warningMsg;
stop();
m_pcCurrentVideoSubWindow->close();
}
if( bShouldRefreshUI ) emit changed();
// auto end = std::chrono::steady_clock::now();
// std::cout << "Elapsed time going to next frame: "
// << std::chrono::duration_cast<std::chrono::milliseconds>( end - start ).count()
// << " ms" << std::endl;
}
void VideoHandle::seekEvent( int direction )
{
if( auto* videoStreamSubWindow = qobject_cast<VideoStreamSubWindow*>( m_pcCurrentVideoSubWindow ) )
{
if( m_acPlayingSubWindows.contains( videoStreamSubWindow ) )
{
if( !( (unsigned int)( videoStreamSubWindow->getInputStream()->getCurrFrameNum() + 1 ) >= getMaxFrameNumber() &&
direction > 0 ) )
{
for( auto playingSubWindow : m_acPlayingSubWindows )
{
playingSubWindow->seekRelativeEvent( direction > 0 ? true : false );
}
}
}
else
{
videoStreamSubWindow->seekRelativeEvent( direction > 0 ? true : false );
}
emit changed();
}
}
void VideoHandle::seekVideo()
{
if( auto* videoStreamSubWindow = qobject_cast<VideoStreamSubWindow*>( m_pcCurrentVideoSubWindow ) )
{
SeekStreamDialog* dialogSeekVideo = new SeekStreamDialog( videoStreamSubWindow->getInputStream(), m_pcParent );
int newFrameNum = dialogSeekVideo->runDialog();
if( newFrameNum >= 0 )
{
if( m_acPlayingSubWindows.contains( videoStreamSubWindow ) )
{
for( auto playingSubWindow : m_acPlayingSubWindows )
{
playingSubWindow->seekAbsoluteEvent( (unsigned int)newFrameNum );
}
}
else
{
videoStreamSubWindow->seekAbsoluteEvent( (unsigned int)newFrameNum );
}
emit changed();
}
}
}
void VideoHandle::seekSliderEvent( int new_frame_num )
{
// TODO: Fix this slot
if( m_bIsPlaying ) return;
if( auto* videoStreamSubWindow = qobject_cast<VideoStreamSubWindow*>( m_pcCurrentVideoSubWindow ) )
{
if( m_acPlayingSubWindows.contains( videoStreamSubWindow ) )
{
for( auto playingSubWindow : m_acPlayingSubWindows )
{
playingSubWindow->seekAbsoluteEvent( (unsigned int)new_frame_num );
}
}
else
{
videoStreamSubWindow->seekAbsoluteEvent( (unsigned int)new_frame_num );
}
emit changed();
}
}
void VideoHandle::videoSelectionButtonEvent()
{
Qt::KeyboardModifiers keyModifiers = QApplication::keyboardModifiers();
if( keyModifiers & Qt::ControlModifier )
{
QList<SubWindowAbstract*> subWindowList =
m_pcMainWindowManager->findSubWindow( SubWindowAbstract::VIDEO_STREAM_SUBWINDOW );
for( auto subWindow : subWindowList )
{
auto* videoSubWindow = qobject_cast<VideoStreamSubWindow*>( subWindow );
m_acPlayingSubWindows.append( videoSubWindow );
}
}
else
{
SubWindowSelectorDialog dialogWindowsSelection( m_pcParent, m_pcMainWindowManager,
SubWindowAbstract::VIDEO_STREAM_SUBWINDOW );
QStringList cWindowListNames;
for( auto playingSubWindow : m_acPlayingSubWindows )
{
dialogWindowsSelection.selectSubWindow( playingSubWindow );
}
if( dialogWindowsSelection.exec() == QDialog::Accepted )
{
m_acPlayingSubWindows.clear();
QList<SubWindowAbstract*> selectedSubWindowList = dialogWindowsSelection.getSelectedWindows();
for( auto window : selectedSubWindowList )
{
auto* videoSubWindow = qobject_cast<VideoStreamSubWindow*>( window );
m_acPlayingSubWindows.append( videoSubWindow );
}
}
else
{
return;
}
}
if( m_acPlayingSubWindows.size() > 0 )
{
m_acPlayingSubWindows.front()->activateWindow();
if( m_acPlayingSubWindows.size() > 1 )
{
m_arrayActions[VIDEO_LOCK_ACT]->setVisible( true );
}
if( m_bIsPlaying )
{
for( int i = 0; i < m_acPlayingSubWindows.size(); i++ )
{
m_acPlayingSubWindows.at( i )->play();
}
}
}
else
{
stop();
}
}
void VideoHandle::setComponent( int component )
{
if( m_pcCurrentVideoSubWindow == nullptr ) return;
if( component < 0 )
{
m_pcCurrentVideoSubWindow->getViewArea()->clearFilteredChannel();
}
else
{
m_pcCurrentVideoSubWindow->getViewArea()->setFilteredChannel( static_cast<std::size_t>( component ) );
}
}
void VideoHandle::setTool( int tool )
{
m_uiViewTool = tool;
m_actionGroupTools->actions().at( m_uiViewTool )->setChecked( true );
QList<SubWindowAbstract*> subWindowList = m_pcMainWindowManager->findSubWindow( SubWindowAbstract::VIDEO_SUBWINDOW );
for( int i = 0; i < subWindowList.size(); i++ )
{
qobject_cast<VideoSubWindow*>( subWindowList.at( i ) )->getViewArea()->setTool( m_uiViewTool );
}
}
void VideoHandle::toggleGrid( bool checked )
{
QList<SubWindowAbstract*> subWindowList = m_pcMainWindowManager->findSubWindow( SubWindowAbstract::VIDEO_SUBWINDOW );
for( int i = 0; i < subWindowList.size(); i++ )
{
qobject_cast<VideoSubWindow*>( subWindowList.at( i ) )->getViewArea()->setGridVisible( checked );
}
}
void VideoHandle::setGridSize( int size )
{
QList<SubWindowAbstract*> subWindowList = m_pcMainWindowManager->findSubWindow( SubWindowAbstract::VIDEO_SUBWINDOW );
for( int i = 0; i < subWindowList.size(); i++ )
{
qobject_cast<VideoSubWindow*>( subWindowList.at( i ) )->getViewArea()->setGridSize( size );
}
}