From d01a91651bf1abdc393e7e329995acd7e025ac66 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Sun, 26 Dec 2021 12:09:18 +0000 Subject: [PATCH] Finish refactor of load and format stream --- app/MainWindow.cpp | 117 +++++++++++++++-------------- app/MainWindow.h | 4 +- app/VideoStreamSubWindow.h | 1 + app/models/VideostreamResource.cpp | 21 ------ app/models/VideostreamResource.h | 1 - 5 files changed, 63 insertions(+), 81 deletions(-) diff --git a/app/MainWindow.cpp b/app/MainWindow.cpp index 7d14e43..3a51026 100644 --- a/app/MainWindow.cpp +++ b/app/MainWindow.cpp @@ -185,14 +185,14 @@ void MainWindow::closeAll() m_pcWindowHandle->removeAllSubWindow(); } -void MainWindow::loadFile( CalypFileInfo pStreamInfo ) +void MainWindow::loadFile( const CalypFileInfo& pStreamInfo ) { - QString filename{ pStreamInfo.m_cFilename }; - loadFile( std::move( filename ), std::move( pStreamInfo ) ); + loadFile( pStreamInfo.m_cFilename, pStreamInfo ); } -void MainWindow::loadFile( QString fileName, std::optional streamInfoOpt ) +void MainWindow::loadFile( const QString& fileName, std::optional streamInfoOpt, bool forceDialog ) { + qDebug() << fileName; if( !QFileInfo( fileName ).exists() ) { printMessage( "File " + fileName + " do not exist!", CLP_LOG_ERROR ); @@ -212,44 +212,45 @@ void MainWindow::loadFile( QString fileName, std::optional stream streamInfoOpt = m_aRecentFileStreamInfo.at( idx ); } - auto streamInfo = streamInfoOpt.has_value() ? *streamInfoOpt : CalypFileInfo{ .m_cFilename = std::move( fileName ) }; + auto streamInfo = streamInfoOpt.has_value() ? *streamInfoOpt : CalypFileInfo{ .m_cFilename = fileName }; auto videoResource = std::make_unique(); bool opened = false; try { - opened = videoResource->loadFile( streamInfo, false ); + [[maybe_unused]] auto guessed = video_resource_guess_format( + streamInfo.m_cFilename, streamInfo.m_uiWidth, streamInfo.m_uiHeight, streamInfo.m_iPelFormat, + streamInfo.m_uiBitsPelPixel, streamInfo.m_iEndianness, streamInfo.m_uiFrameRate ); + + if( !forceDialog ) + { + opened = videoResource->loadFile( streamInfo, false ); + } if( !opened ) { - auto guessed = video_resource_guess_format( streamInfo.m_cFilename, streamInfo.m_uiWidth, streamInfo.m_uiHeight, - streamInfo.m_iPelFormat, streamInfo.m_uiBitsPelPixel, - streamInfo.m_iEndianness, streamInfo.m_uiFrameRate ); - - if( !guessed ) + ConfigureFormatDialog formatDialog( this ); + if( streamInfo.m_uiWidth == 0 ) { - ConfigureFormatDialog formatDialog( this ); - if( streamInfo.m_uiWidth == 0 ) - { - streamInfo.m_uiWidth = m_lastOpenWidth; - } - if( streamInfo.m_uiHeight == 0 ) - { - streamInfo.m_uiHeight = m_lastOpenHeight; - } - if( streamInfo.m_uiBitsPelPixel == 0 ) - { - streamInfo.m_uiBitsPelPixel = m_lastOpenBitPerPixel; - } - if( formatDialog.runConfigureFormatDialog( QFileInfo( streamInfo.m_cFilename ).fileName(), streamInfo.m_uiWidth, - streamInfo.m_uiHeight, streamInfo.m_iPelFormat, - streamInfo.m_uiBitsPelPixel, streamInfo.m_iEndianness, - streamInfo.m_uiFrameRate ) != QDialog::Accepted ) - { - return; - } + streamInfo.m_uiWidth = m_lastOpenWidth; } + if( streamInfo.m_uiHeight == 0 ) + { + streamInfo.m_uiHeight = m_lastOpenHeight; + } + if( streamInfo.m_uiBitsPelPixel == 0 ) + { + streamInfo.m_uiBitsPelPixel = m_lastOpenBitPerPixel; + } + if( formatDialog.runConfigureFormatDialog( QFileInfo( streamInfo.m_cFilename ).fileName(), streamInfo.m_uiWidth, + streamInfo.m_uiHeight, streamInfo.m_iPelFormat, + streamInfo.m_uiBitsPelPixel, streamInfo.m_iEndianness, + streamInfo.m_uiFrameRate ) != QDialog::Accepted ) + { + return; + } + opened = videoResource->loadFile( streamInfo, false ); } @@ -269,13 +270,13 @@ void MainWindow::loadFile( QString fileName, std::optional stream updateZoomFactorSBox(); + m_lastOpenPath = QFileInfo( openedStreamInfo.m_cFilename ).path(); + m_lastOpenWidth = openedStreamInfo.m_uiWidth; + m_lastOpenHeight = openedStreamInfo.m_uiHeight; + m_lastOpenBitPerPixel = openedStreamInfo.m_uiBitsPelPixel; addStreamInfoToRecentList( std::move( openedStreamInfo ) ); printMessage( "File loaded", CLP_LOG_INFO ); - m_lastOpenPath = QFileInfo( streamInfo.m_cFilename ).path(); - m_lastOpenWidth = streamInfo.m_uiWidth; - m_lastOpenHeight = streamInfo.m_uiHeight; - m_lastOpenBitPerPixel = streamInfo.m_uiBitsPelPixel; } } catch( CalypFailure& e ) @@ -422,28 +423,30 @@ void MainWindow::saveStream() void MainWindow::format() { - // if( auto* pcVideoSubWindow = qobject_cast( m_pcCurrentVideoSubWindow ) ) - // { - // try - // { - // if( pcVideoSubWindow->loadFile( pcVideoSubWindow->getCurrentFileName(), true ) ) - // { - // addStreamInfoToRecentList( pcVideoSubWindow->getStreamInfo() ); - // } - // } - // catch( CalypFailure& e ) - // { - // QString warningMsg = "Cannot change format of " + QFileInfo( pcVideoSubWindow->getCurrentFileName() - // ).fileName() + - // " with the following error: \n" + e.what(); - // QMessageBox::warning( this, QApplication::applicationName(), warningMsg ); - // printMessage( warningMsg, CLP_LOG_ERROR ); - // qDebug() << warningMsg; - // m_pcCurrentSubWindow->close(); - // } - // m_pcCurrentSubWindow = NULL; - // updateMainWindow(); - // } + auto* activeSubWindow = m_pcWindowHandle->activeSubWindow(); + if( auto* pcVideoSubWindow = qobject_cast( activeSubWindow ) ) + { + try + { + auto resourceId = pcVideoSubWindow->getResourceId(); + CalypFileInfo streamInfo; + m_appResourceHandle->executeResourceAction( resourceId, [&streamInfo]( auto* resource ) { + streamInfo = resource->getStreamInfo(); + return true; + } ); + m_pcWindowHandle->removeSubWindow( activeSubWindow ); + loadFile( streamInfo.m_cFilename, streamInfo, true ); + } + catch( CalypFailure& e ) + { + QString warningMsg = "Cannot change format of " + QFileInfo( pcVideoSubWindow->getCurrentFileName() ).fileName() + + " with the following error: \n" + e.what(); + QMessageBox::warning( this, QApplication::applicationName(), warningMsg ); + printMessage( warningMsg, CLP_LOG_ERROR ); + qDebug() << warningMsg; + } + updateMainWindow(); + } } void MainWindow::reload() diff --git a/app/MainWindow.h b/app/MainWindow.h index cdbd126..b5b345b 100644 --- a/app/MainWindow.h +++ b/app/MainWindow.h @@ -78,8 +78,8 @@ class MainWindow : public QMainWindow public: MainWindow(); bool parseArgs( int argc, char* argv[] ); - void loadFile( CalypFileInfo pStreamInfo ); - void loadFile( QString fileName, std::optional pStreamInfo = {} ); + void loadFile( const CalypFileInfo& pStreamInfo ); + void loadFile( const QString& fileName, std::optional streamInfoOpt = {}, bool forceDialog = false ); public Q_SLOTS: void printMessage( const QString& msg ); diff --git a/app/VideoStreamSubWindow.h b/app/VideoStreamSubWindow.h index 9fcf324..50a7f86 100644 --- a/app/VideoStreamSubWindow.h +++ b/app/VideoStreamSubWindow.h @@ -64,6 +64,7 @@ public: bool supportsFormatConfiguration() const { return m_pCurrStream->supportsFormatConfiguration(); }; void setResource( std::size_t id ); + auto getResourceId() const -> std::size_t { return m_uiResourceId; }; // bool loadFile( QString cFilename, bool bForceDialog = false ); // bool loadFile( CalypFileInfo streamInfo ); void loadAll(); diff --git a/app/models/VideostreamResource.cpp b/app/models/VideostreamResource.cpp index 2ca2ec9..7e0b392 100644 --- a/app/models/VideostreamResource.cpp +++ b/app/models/VideostreamResource.cpp @@ -97,27 +97,6 @@ auto VideostreamResource::isReady() -> bool return m_currStream.hasWritingSlot(); } -bool VideostreamResource::loadFile( const QString& cFilename ) -{ - CalypFileInfo streamInfo; - - m_currStream.getFormat( streamInfo.m_uiWidth, streamInfo.m_uiHeight, streamInfo.m_iPelFormat, - streamInfo.m_uiBitsPelPixel, streamInfo.m_iEndianness, streamInfo.m_uiFrameRate ); - - [[maybe_unused]] auto guessed = - video_resource_guess_format( cFilename, streamInfo.m_uiWidth, streamInfo.m_uiHeight, streamInfo.m_iPelFormat, - streamInfo.m_uiBitsPelPixel, streamInfo.m_iEndianness, streamInfo.m_uiFrameRate ); - - if( streamInfo.m_iPelFormat == ClpPixelFormats::Invalid ) - { - streamInfo.m_iPelFormat = ClpPixelFormats::YUV420p; - } - - streamInfo.m_cFilename = cFilename; - - return loadFile( streamInfo ); -} - bool VideostreamResource::loadFile( const CalypFileInfo& streamInfo, bool optimistic ) { m_sStreamInfo = streamInfo; diff --git a/app/models/VideostreamResource.h b/app/models/VideostreamResource.h index b654f05..def1440 100644 --- a/app/models/VideostreamResource.h +++ b/app/models/VideostreamResource.h @@ -67,7 +67,6 @@ public: auto iteration() -> bool override; auto isReady() -> bool override; - auto loadFile( const QString& cFilename ) -> bool; auto loadFile( const CalypFileInfo& streamInfo, bool optimistic = true ) -> bool; auto getStreamInfo() const -> CalypFileInfo { return m_sStreamInfo; }