/* This file is a part of plaYUVer project * Copyright (C) 2014-2015 by Luis Lucas (luisfrlucas@gmail.com) * Joao Carreira (jfmcarreira@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 ModulesHandle.cpp * \brief PlaYUVer modules handle */ #include #include "ModulesHandle.h" #include "PlaYUVerSubWindowHandle.h" #include "VideoHandle.h" #include "PlaYUVerModuleFactory.h" #include "DialogSubWindowSelector.h" namespace plaYUVer { ModulesHandle::ModulesHandle( QWidget* parent, PlaYUVerSubWindowHandle *windowManager, VideoHandle *moduleVideo ) : QWidget( parent ), m_pcParent( parent ), m_pcMainWindowManager( windowManager ), m_appModuleVideo( moduleVideo ) { setParent( m_pcParent ); } ModulesHandle::~ModulesHandle() { } Void ModulesHandle::createActions() { m_arrayActions.resize( MODULES_TOTAL_ACT ); m_pcActionMapper = new QSignalMapper( this ); connect( m_pcActionMapper, SIGNAL( mapped(int) ), this, SLOT( processOpt(int) ) ); m_arrayActions[FORCE_NEW_WINDOW_ACT] = new QAction( "Use New Window", parent() ); m_arrayActions[FORCE_NEW_WINDOW_ACT]->setStatusTip( "Show module result in a new window. Some modules already force this feature" ); m_arrayActions[FORCE_NEW_WINDOW_ACT]->setCheckable( true ); m_arrayActions[FORCE_PLAYING_REFRESH_ACT] = new QAction( "Refresh while playing", parent() ); m_arrayActions[FORCE_PLAYING_REFRESH_ACT]->setStatusTip( "Force module refreshing while playing" ); m_arrayActions[FORCE_PLAYING_REFRESH_ACT]->setCheckable( true ); m_arrayActions[APPLY_ALL_ACT] = new QAction( "Apply to All", parent() ); m_arrayActions[APPLY_ALL_ACT]->setStatusTip( "Apply module to all frames and save the result" ); connect( m_arrayActions[APPLY_ALL_ACT], SIGNAL( triggered() ), m_pcActionMapper, SLOT( map() ) ); m_pcActionMapper->setMapping( m_arrayActions[APPLY_ALL_ACT], APPLY_ALL_ACT ); m_arrayActions[SWAP_FRAMES_ACT] = new QAction( "Swap frames", parent() ); m_arrayActions[SWAP_FRAMES_ACT]->setStatusTip( "Swap Sub Window order" ); connect( m_arrayActions[SWAP_FRAMES_ACT], SIGNAL( triggered() ), m_pcActionMapper, SLOT( map() ) ); m_pcActionMapper->setMapping( m_arrayActions[SWAP_FRAMES_ACT], SWAP_FRAMES_ACT ); m_arrayActions[DISABLE_ALL_ACT] = new QAction( "Disable All Modules", parent() ); connect( m_arrayActions[DISABLE_ALL_ACT], SIGNAL( triggered() ), this, SLOT( destroyAllModulesIf() ) ); } QMenu* ModulesHandle::createMenu() { PlaYUVerModuleIf* pcCurrModuleIf; QString ModuleIfInternalName; QAction* currAction; QMenu* currSubMenu; m_pcModulesMenu = new QMenu( "&Modules", this ); m_pcModulesActionMapper = new QSignalMapper( this ); //connect( m_pcModulesActionMapper, SIGNAL( mapped(int) ), this, SLOT( activateModule(int) ) ); UInt i = 0; PlaYUVerModuleFactoryMap& PlaYUVerModuleFactoryMap = PlaYUVerModuleFactory::Get()->getMap(); PlaYUVerModuleFactoryMap::iterator it = PlaYUVerModuleFactoryMap.begin(); for( ; it != PlaYUVerModuleFactoryMap.end(); ++it, i++ ) { pcCurrModuleIf = it->second(); ModuleIfInternalName = QString::fromLocal8Bit( it->first ); currSubMenu = NULL; if( pcCurrModuleIf->m_pchModuleCategory ) { for( Int j = 0; j < m_pcModulesSubMenuList.size(); j++ ) { if( m_pcModulesSubMenuList.at( j )->title() == QString( pcCurrModuleIf->m_pchModuleCategory ) ) { currSubMenu = m_pcModulesSubMenuList.at( j ); break; } } if( !currSubMenu ) { currSubMenu = m_pcModulesMenu->addMenu( pcCurrModuleIf->m_pchModuleCategory ); m_pcModulesSubMenuList.append( currSubMenu ); } } currAction = new QAction( pcCurrModuleIf->m_pchModuleName, parent() ); currAction->setStatusTip( pcCurrModuleIf->m_pchModuleTooltip ); currAction->setData( ModuleIfInternalName ); currAction->setCheckable( true ); connect( currAction, SIGNAL( triggered() ), this, SLOT( activateModule() ) ); //connect( currAction, SIGNAL( triggered() ), m_pcModulesActionMapper, SLOT( map() ) ); m_pcModulesActionMapper->setMapping( currAction, i ); m_arrayModulesActions.append( currAction ); if( currSubMenu ) currSubMenu->addAction( currAction ); else m_pcModulesMenu->addAction( currAction ); pcCurrModuleIf->Delete(); } m_pcModulesMenu->addSeparator(); m_pcModulesMenu->addAction( m_arrayActions[APPLY_ALL_ACT] ); m_pcModulesMenu->addAction( m_arrayActions[SWAP_FRAMES_ACT] ); m_pcModulesMenu->addAction( m_arrayActions[DISABLE_ALL_ACT] ); m_pcModulesMenu->addAction( m_arrayActions[FORCE_NEW_WINDOW_ACT] ); return m_pcModulesMenu; } Void ModulesHandle::updateMenus() { VideoSubWindow* pcSubWindow = qobject_cast( m_pcMainWindowManager->activeSubWindow() ); Bool hasSubWindow = pcSubWindow ? true : false; QAction* currModuleAction; for( Int i = 0; i < m_arrayModulesActions.size(); i++ ) { currModuleAction = m_arrayModulesActions.at( i ); currModuleAction->setEnabled( hasSubWindow ); currModuleAction->setChecked( false ); } for( Int i = 0; i < m_arrayActions.size(); i++ ) { m_arrayActions.at( i )->setEnabled( false ); } m_arrayActions[DISABLE_ALL_ACT]->setEnabled( hasSubWindow ); m_arrayActions[FORCE_NEW_WINDOW_ACT]->setEnabled( hasSubWindow ); if( pcSubWindow ) { QList apcCurrentModule = pcSubWindow->getModuleArray(); for( Int i = 0; i < apcCurrentModule.size(); i++ ) { currModuleAction = apcCurrentModule.at( i )->m_pcModuleAction; currModuleAction->setChecked( true ); } if( apcCurrentModule.size() ) { m_arrayActions[APPLY_ALL_ACT]->setEnabled( true ); m_arrayActions[SWAP_FRAMES_ACT]->setEnabled( true ); } } } Void ModulesHandle::readSettings() { QSettings appSettings; m_arrayActions[FORCE_NEW_WINDOW_ACT]->setChecked( appSettings.value( "ModulesHandle/UseNewWindow", false ).toBool() ); } Void ModulesHandle::writeSettings() { QSettings appSettings; appSettings.setValue( "ModulesHandle/UseNewWindow", m_arrayActions[FORCE_NEW_WINDOW_ACT]->isChecked() ); } Void ModulesHandle::processOpt( Int index ) { VideoSubWindow* pcSubWindow = qobject_cast( m_pcMainWindowManager->activeSubWindow() ); if( pcSubWindow ) { QList apcCurrentModule = pcSubWindow->getModuleArray(); if( apcCurrentModule.size() > 1 ) { PlaYUVerAppModuleIf* pcCurrModule = apcCurrentModule.at( 0 ); switch( index ) { case INVALID_OPT: break; case SWAP_FRAMES_ACT: swapModulesWindowsIf( pcCurrModule ); break; case APPLY_ALL_ACT: applyAllModuleIf( pcCurrModule ); break; default: Q_ASSERT( 0 ); } } emit changed(); } } Void ModulesHandle::activateModule() { QAction *pcAction = qobject_cast( sender() ); VideoSubWindow* pcVideoSubWindow = qobject_cast( m_pcMainWindowManager->activeSubWindow() ); if( pcVideoSubWindow ) { if( pcAction->isChecked() ) { QString ModuleIfName = pcAction->data().toString(); PlaYUVerModuleIf* pcCurrModuleIf = PlaYUVerModuleFactory::Get()->CreateModule( ModuleIfName.toLocal8Bit().constData() ); PlaYUVerAppModuleIf* pcCurrAppModuleIf = new PlaYUVerAppModuleIf( this, pcAction, pcCurrModuleIf ); enableModuleIf( pcCurrAppModuleIf ); m_pcPlaYUVerAppModuleIfList.append( pcCurrAppModuleIf ); } else { pcVideoSubWindow->disableModule(); } } emit changed(); } Void ModulesHandle::enableModuleIf( PlaYUVerAppModuleIf *pcCurrModuleIf ) { VideoSubWindow* pcModuleSubWindow = NULL; Bool bShowModulesNewWindow = m_arrayActions[FORCE_NEW_WINDOW_ACT]->isChecked(); VideoSubWindow* pcVideoSubWindow = qobject_cast( m_pcMainWindowManager->activeSubWindow() ); QString windowName; QList videoSubWindowList; UInt numberOfFrames = pcCurrModuleIf->m_pcModule->m_uiNumberOfFrames; if( numberOfFrames > MODULE_REQUIRES_ONE_FRAME ) // Show dialog to select sub windows { DialogSubWindowSelector dialogWindowsSelection( m_pcParent, m_pcMainWindowManager, SubWindowHandle::VIDEO_SUBWINDOW, numberOfFrames, numberOfFrames ); if( dialogWindowsSelection.exec() == QDialog::Accepted ) { QStringList selectedWindows = dialogWindowsSelection.getSelectedWindows(); VideoSubWindow *videoSubWindow; QList subWindowList = m_pcMainWindowManager->findSubWindow(); for( Int j = 0; j < selectedWindows.size(); j++ ) { for( Int i = 0; i < subWindowList.size(); i++ ) { videoSubWindow = qobject_cast( subWindowList.at( i ) ); if( videoSubWindow->getWindowName() == selectedWindows.at( j ) ) { videoSubWindowList.append( videoSubWindow ); pcCurrModuleIf->m_pcSubWindow[j] = videoSubWindow; } } } } // Check for same fmt in more than one frame modules for( Int i = 1; i < videoSubWindowList.size(); i++ ) { if( !videoSubWindowList.at( i )->getCurrFrame()->haveSameFmt( videoSubWindowList.at( 0 )->getCurrFrame() ) ) { videoSubWindowList.clear(); break; } } } else { windowName.append( pcVideoSubWindow->windowTitle() ); videoSubWindowList.append( pcVideoSubWindow ); } if( videoSubWindowList.size() == 0 ) { qobject_cast( m_pcParent )->statusBar()->showMessage( "Error! Module cannot be applied", 2000 ); destroyModuleIf( pcCurrModuleIf ); return; } windowName.append( " <" ); windowName.append( pcCurrModuleIf->m_pcModule->m_pchModuleName ); windowName.append( ">" ); for( Int i = 0; i < videoSubWindowList.size(); i++ ) { pcCurrModuleIf->m_pcSubWindow[i] = videoSubWindowList.at( i ); } pcCurrModuleIf->m_pcDisplaySubWindow = NULL; if( pcCurrModuleIf->m_pcModule->m_iModuleType == FRAME_PROCESSING_MODULE ) { if( ( pcCurrModuleIf->m_pcModule->m_uiModuleRequirements & MODULE_REQUIRES_NEW_WINDOW ) || bShowModulesNewWindow ) { pcModuleSubWindow = new VideoSubWindow( VideoSubWindow::MODULE_SUBWINDOW, this ); pcModuleSubWindow->setWindowShortName( windowName ); pcModuleSubWindow->setWindowName( windowName ); connect( pcModuleSubWindow->getViewArea(), SIGNAL( selectionChanged( QRect ) ), m_appModuleVideo, SLOT( updateSelectionArea( QRect ) ) ); connect( pcModuleSubWindow, SIGNAL( zoomFactorChanged_SWindow( const double, const QPoint ) ), m_appModuleVideo, SLOT( zoomToFactorAll( double, QPoint ) ) ); connect( pcModuleSubWindow, SIGNAL( scrollBarMoved_SWindow( const QPoint ) ), m_appModuleVideo, SLOT( moveAllScrollBars( const QPoint ) ) ); connect( pcModuleSubWindow, SIGNAL( updateStatusBar( const QString& ) ), this, SLOT( updateStatusBar( const QString& ) ) ); connect( pcModuleSubWindow, SIGNAL( zoomFactorChanged_SWindow( const double, const QPoint ) ), this, SLOT( updateZoomFactorSBox() ) ); pcCurrModuleIf->m_pcDisplaySubWindow = pcModuleSubWindow; //m_pcMdiArea->addSubWindow( pcModuleSubWindow ); } } else if( pcCurrModuleIf->m_pcModule->m_iModuleType == FRAME_MEASUREMENT_MODULE ) { if( pcCurrModuleIf->m_pcModule->m_uiModuleRequirements & MODULE_REQUIRES_SIDEBAR ) { pcCurrModuleIf->m_pcModuleDock = new ModuleHandleDock( m_pcParent, pcCurrModuleIf ); QString titleDockWidget; titleDockWidget.append( pcCurrModuleIf->m_pcModule->m_pchModuleName ); titleDockWidget.append( " Information" ); pcCurrModuleIf->m_pcDockWidget = new QDockWidget( titleDockWidget, m_pcParent ); pcCurrModuleIf->m_pcDockWidget->setFeatures( QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable ); pcCurrModuleIf->m_pcDockWidget->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea ); pcCurrModuleIf->m_pcDockWidget->setWidget( pcCurrModuleIf->m_pcModuleDock ); qobject_cast( m_pcParent )->addDockWidget( Qt::RightDockWidgetArea, pcCurrModuleIf->m_pcDockWidget ); } } // Create Module pcCurrModuleIf->m_pcModule->create( pcCurrModuleIf->m_pcSubWindow[0]->getCurrFrame() ); // Associate module with subwindows if( pcCurrModuleIf->m_pcDisplaySubWindow ) { pcCurrModuleIf->m_pcDisplaySubWindow->enableModule( pcCurrModuleIf, true ); } else { for( UInt i = 0; i < numberOfFrames; i++ ) { pcCurrModuleIf->m_pcSubWindow[i]->associateModule( pcCurrModuleIf ); } } applyModuleIf( pcCurrModuleIf, false ); QCoreApplication::processEvents(); if( pcModuleSubWindow ) { m_pcMainWindowManager->addSubWindow( pcModuleSubWindow ); pcModuleSubWindow->show(); } return; } Void ModulesHandle::destroyModuleIf( PlaYUVerAppModuleIf *pcCurrModuleIf ) { if( pcCurrModuleIf ) { if( pcCurrModuleIf->m_pcModuleDock ) { pcCurrModuleIf->m_pcModuleDock->close(); pcCurrModuleIf->m_pcModuleDock = NULL; } if( pcCurrModuleIf->m_pcDockWidget ) { pcCurrModuleIf->m_pcDockWidget->close(); pcCurrModuleIf->m_pcDockWidget = NULL; } if( pcCurrModuleIf->m_pcDisplaySubWindow ) pcCurrModuleIf->m_pcDisplaySubWindow->close(); pcCurrModuleIf->m_pcDisplaySubWindow = NULL; for( Int i = 0; i < MAX_NUMBER_FRAMES; i++ ) { if( pcCurrModuleIf->m_pcSubWindow[i] ) { pcCurrModuleIf->m_pcSubWindow[i]->disableModule( pcCurrModuleIf ); pcCurrModuleIf->m_pcSubWindow[i] = NULL; } } if( pcCurrModuleIf->m_pcModuleStream ) { pcCurrModuleIf->m_pcModuleStream->close(); delete pcCurrModuleIf->m_pcModuleStream; pcCurrModuleIf->m_pcModuleStream = NULL; } if( pcCurrModuleIf->m_pcModule ) { pcCurrModuleIf->m_pcModule->destroy(); pcCurrModuleIf->m_pcModule->Delete(); pcCurrModuleIf->m_pcModule = NULL; } } } Void ModulesHandle::destroyAllModulesIf() { for( Int i = 0; i < m_pcPlaYUVerAppModuleIfList.size(); i++ ) { destroyModuleIf( m_pcPlaYUVerAppModuleIfList.at( i ) ); } } Bool ModulesHandle::applyModuleIf( PlaYUVerAppModuleIf *pcCurrModuleIf, Bool isPlaying, Bool disableThreads ) { Bool bRet = false; if( !isPlaying || ( isPlaying && pcCurrModuleIf->m_pcModule->m_bApplyWhilePlaying ) ) { #ifdef PLAYUVER_THREADED_MODULES if( !disableThreads ) pcCurrModuleIf->start(); else #endif pcCurrModuleIf->run(); if( pcCurrModuleIf->m_pcDisplaySubWindow || pcCurrModuleIf->m_pcModule->m_iModuleType == FRAME_MEASUREMENT_MODULE ) { bRet = true; } } else { bRet = true; } return bRet; } Void ModulesHandle::applyAllModuleIf( PlaYUVerAppModuleIf *pcCurrModuleIf ) { if( pcCurrModuleIf ) { if( !pcCurrModuleIf->m_pcModuleStream ) { UInt Width = 0, Height = 0, FrameRate = 30; Int InputFormat = -1; QString supported = tr( "Supported Files (" ); QStringList formatsList; QStringList formatsExt = PlaYUVerStream::supportedWriteFormatsExt(); QStringList formatsName = PlaYUVerStream::supportedWriteFormatsName(); for( Int i = 0; i < formatsName.size(); i++ ) { QString currFmt; supported.append( " *." ); supported.append( formatsExt[i] ); currFmt.append( formatsName[i] ); currFmt.append( " (*." ); currFmt.append( formatsExt[i] ); currFmt.append( ")" ); formatsList << currFmt; } supported.append( " )" ); QStringList filter; filter << supported << formatsList << tr( "All Files (*)" ); QString fileName = QFileDialog::getSaveFileName( m_pcParent, tr( "Open File" ), QString(), filter.join( ";;" ) ); Width = pcCurrModuleIf->m_pcProcessedFrame->getWidth(); Height = pcCurrModuleIf->m_pcProcessedFrame->getHeight(); InputFormat = pcCurrModuleIf->m_pcProcessedFrame->getPelFormat(); FrameRate = pcCurrModuleIf->m_pcSubWindow[0]->getInputStream()->getFrameRate(); pcCurrModuleIf->m_pcModuleStream = new PlaYUVerStream; if( !pcCurrModuleIf->m_pcModuleStream->open( fileName.toStdString(), Width, Height, InputFormat, FrameRate, false ) ) { delete pcCurrModuleIf->m_pcModuleStream; pcCurrModuleIf->m_pcModuleStream = NULL; return; } } } if( pcCurrModuleIf->m_pcModuleStream ) { UInt numberOfWindows = pcCurrModuleIf->m_pcModule->m_uiNumberOfFrames; UInt64 currFrames = 0; UInt64 numberOfFrames = INT_MAX; for( UInt i = 0; i < numberOfWindows; i++ ) { currFrames = pcCurrModuleIf->m_pcSubWindow[i]->getInputStream()->getFrameNum(); if( currFrames < numberOfFrames ) numberOfFrames = currFrames; pcCurrModuleIf->m_pcSubWindow[i]->stop(); } QApplication::setOverrideCursor( Qt::WaitCursor ); for( UInt f = 1; f < numberOfFrames; f++ ) { applyModuleIf( pcCurrModuleIf, false, true ); QCoreApplication::processEvents(); pcCurrModuleIf->m_pcModuleStream->writeFrame( pcCurrModuleIf->m_pcProcessedFrame ); for( UInt i = 0; i < numberOfWindows; i++ ) { pcCurrModuleIf->m_pcSubWindow[i]->play(); pcCurrModuleIf->m_pcSubWindow[i]->playEvent(); } } for( UInt i = 0; i < numberOfWindows; i++ ) { pcCurrModuleIf->m_pcSubWindow[i]->stop(); } QApplication::restoreOverrideCursor(); } } Bool ModulesHandle::showModuleIf( PlaYUVerAppModuleIf *pcCurrModuleIf, PlaYUVerFrame* processedFrame ) { Bool bRet = false; //if( processedFrame ) { if( pcCurrModuleIf->m_pcDisplaySubWindow ) { pcCurrModuleIf->m_pcDisplaySubWindow->setCurrFrame( processedFrame ); bRet = true; } else { pcCurrModuleIf->m_pcSubWindow[0]->setCurrFrame( processedFrame ); } } return bRet; } Bool ModulesHandle::showModuleIf( PlaYUVerAppModuleIf *pcCurrModuleIf, Double moduleResult ) { Bool bRet = false; pcCurrModuleIf->m_pcModuleDock->setModulueReturnValue( moduleResult ); return bRet; } Void ModulesHandle::swapModulesWindowsIf( PlaYUVerAppModuleIf *pcCurrModuleIf ) { if( pcCurrModuleIf->m_pcModule->m_uiNumberOfFrames == MODULE_REQUIRES_TWO_FRAMES ) { VideoSubWindow* auxWindowHandle = pcCurrModuleIf->m_pcSubWindow[0]; pcCurrModuleIf->m_pcSubWindow[0] = pcCurrModuleIf->m_pcSubWindow[1]; pcCurrModuleIf->m_pcSubWindow[1] = auxWindowHandle; applyModuleIf( pcCurrModuleIf ); } } Void ModulesHandle::customEvent( QEvent *event ) { if( !event ) { return; } PlaYUVerAppModuleIf::EventData *eventData = ( PlaYUVerAppModuleIf::EventData* )event; if( eventData->m_bSuccess ) { switch( eventData->m_pcModule->m_pcModule->m_iModuleType ) { case FRAME_PROCESSING_MODULE: showModuleIf( eventData->m_pcModule, eventData->m_pcModule->m_pcProcessedFrame ); break; case FRAME_MEASUREMENT_MODULE: showModuleIf( eventData->m_pcModule, eventData->m_pcModule->m_dMeasurementResult ); break; } } } } // NAMESPACE