From 7e07d7b95422f2c7a22a3d468fae76a965641e1a Mon Sep 17 00:00:00 2001 From: Hiro Protagonist Date: Tue, 4 Apr 2017 17:06:48 +1200 Subject: [PATCH] A bit more refactoring. --- src/cameramanager.cpp | 64 +++++++++++++++++++-------------------- src/cameramanager.h | 28 +++++++++-------- src/mainwindow.cpp | 3 ++ src/recorder.cpp | 70 +++++++++++++++++++++---------------------- src/recorder.h | 41 +++++++++++++------------ 5 files changed, 107 insertions(+), 99 deletions(-) diff --git a/src/cameramanager.cpp b/src/cameramanager.cpp index 049e35b..6600d55 100644 --- a/src/cameramanager.cpp +++ b/src/cameramanager.cpp @@ -57,38 +57,6 @@ void CameraManager::connectCamera( int index ) { camera_index = index; } -void CameraManager::camConnectEvent( void * pParameter, unsigned int serialNumber ) { - static_cast( pParameter )->cameraConnected(); -} - -void CameraManager::camDisconnectEvent( void * pParameter, unsigned int serialNumber ) { - CameraManager * cam = static_cast( pParameter ); - FlyCapture2::CameraInfo info; - - cam->camera.GetCameraInfo( &info ); - bool current = ( serialNumber == info.serialNumber ); - - cam->cameraDisconnected( current ); -} - -void CameraManager::stopCapture() { - if ( !is_capturing ) - return; - - grabber->stopCapturing(); - - Error error = camera.StopCapture(); - if ( error != PGRERROR_OK ) { - throw error; - return; - } - - grabber->wait(); - - - is_capturing = false; -} - void CameraManager::startCapture() { // Don't capture twice! if ( is_capturing ) @@ -113,3 +81,35 @@ void CameraManager::startCapture() { grabber = tmpG; is_capturing = true; } + +void CameraManager::stopCapture() { + if ( !is_capturing ) + return; + + grabber->stopCapturing(); + + Error error = camera.StopCapture(); + if ( error != PGRERROR_OK ) { + throw error; + return; + } + + grabber->wait(); + + + is_capturing = false; +} + +void CameraManager::camConnectEvent( void * pParameter, unsigned int serialNumber ) { + static_cast( pParameter )->cameraConnected(); +} + +void CameraManager::camDisconnectEvent( void * pParameter, unsigned int serialNumber ) { + CameraManager * cam = static_cast( pParameter ); + FlyCapture2::CameraInfo info; + + cam->camera.GetCameraInfo( &info ); + bool current = ( serialNumber == info.serialNumber ); + + cam->cameraDisconnected( current ); +} diff --git a/src/cameramanager.h b/src/cameramanager.h index 602e1b2..aebf59d 100644 --- a/src/cameramanager.h +++ b/src/cameramanager.h @@ -20,6 +20,10 @@ class CameraManager : public QObject { explicit CameraManager( QObject * parent = 0 ); ~CameraManager(); + // Connect camera from index. Once successfull it emits the cameraConnected signal. + // Emits Error signal in case of an error. + void connectCamera( int ); + // Starts capturing images. When an image has been captured, the signal frameCaptured is emited. // Throws FlyCapture2::Error void startCapture(); @@ -40,22 +44,24 @@ class CameraManager : public QObject { return num_cameras; } - // Connect camera from index. Once successfull it emits the cameraConnected signal. - // Emits Error signal in case of an error. - void connectCamera( int ); - bool isCapturing() { return is_capturing; } - FlyCapture2::CallbackHandle handleC; - FlyCapture2::CallbackHandle handleD; - private: FlyCapture2::Camera camera; FlyCapture2::FC2Config cam_config; FlyCapture2::BusManager bus_mgr; + FlyCapture2::CallbackHandle handleC; + FlyCapture2::CallbackHandle handleD; + + // Capture Thread + ImageGrabber * grabber; + + // Status Variable + bool is_capturing; + unsigned int num_cameras; // GUID of the camera, which is currently being used @@ -64,12 +70,7 @@ class CameraManager : public QObject { // Index of the current camera int camera_index; - // State Variable - bool is_capturing; - - // Capture Thread - ImageGrabber * grabber; - + // Event Handlers static void camConnectEvent( void * pParameter, unsigned int serialNumber ); static void camDisconnectEvent( void * pParameter, unsigned int serialNumber ); @@ -77,6 +78,7 @@ class CameraManager : public QObject { void frameCaptured( FlyCapture2::Image * image ) const; void cameraConnected(); void cameraDisconnected( bool current ); + void captureError( FlyCapture2::Error err ); }; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0bfea13..9c1f03a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -295,6 +295,9 @@ void MainWindow::startStopRecording() { return; } + // If changed through corrections. + ui->projectName->setText( recorder.getRecName() ); + if ( !camMan.isCapturing() ) { try { camMan.startCapture(); diff --git a/src/recorder.cpp b/src/recorder.cpp index 577faa7..e78ab6e 100644 --- a/src/recorder.cpp +++ b/src/recorder.cpp @@ -25,17 +25,6 @@ Recorder::~Recorder() { stopRecording(); } -void Recorder::setProjectDir( QString &p_dir ) { - QFileInfo pDirInfo( p_dir ); - if ( !pDirInfo.isDir() || !pDirInfo.isWritable() ) { - throw RecorderError( RecorderError::INVALID_PROJECT_DIRECTORY ); - return; - } - - baseDir = QDir( p_dir ); - pDirSet = true; -} - void Recorder::newRecording( QString r_name ) { stopRecording(); @@ -99,6 +88,31 @@ void Recorder::newRecording( QString r_name ) { is_recording = true; } +void Recorder::stopRecording() { + if ( is_recording ) { + // Stop Recorder + Error err = recorder.AVIClose(); + if ( err != PGRERROR_OK ) { + throw err; + return; + } + + is_recording = false; + cleanup(); + } +} + +void Recorder::setProjectDir( QString &p_dir ) { + QFileInfo pDirInfo( p_dir ); + if ( !pDirInfo.isDir() || !pDirInfo.isWritable() ) { + throw RecorderError( RecorderError::INVALID_PROJECT_DIRECTORY ); + return; + } + + baseDir = QDir( p_dir ); + pDirSet = true; +} + bool Recorder::restoreRecording() { bool ok; @@ -138,30 +152,6 @@ RecorderError Recorder::verifyRecDir() { return RecorderError( RecorderError::OK ); } -void Recorder::stopRecording() { - if ( is_recording ) { - // Stop Recorder - Error err = recorder.AVIClose(); - if ( err != PGRERROR_OK ) { - throw err; - return; - } - - is_recording = false; - cleanup(); - } -} - -void Recorder::cleanup() { - // Reset Everything - if ( !is_recording ) { - frame_n = 0; - time_c = 0; - append = false; - delete statFile; - } -} - void Recorder::appendFrame( FlyCapture2::Image * image ) { write_lock.lock(); qDebug() << "Writing Frame..."; @@ -199,3 +189,13 @@ void Recorder::appendFrame( FlyCapture2::Image * image ) { emit frameSaved( image ); write_lock.unlock(); } + +void Recorder::cleanup() { + // Reset Everything + if ( !is_recording ) { + frame_n = 0; + time_c = 0; + append = false; + delete statFile; + } +} diff --git a/src/recorder.h b/src/recorder.h index 9b1c41f..25b31f5 100644 --- a/src/recorder.h +++ b/src/recorder.h @@ -68,23 +68,31 @@ class Recorder : public QObject { // stops the recording, cleans up, throws FlyCapture2::Error void stopRecording(); + void setProjectDir( QString &p_dir ); QString getProjectDir() { return baseDir.absolutePath(); } - QString getRecName() { - return recName; - } - bool isRecording() { return is_recording; } + int frameNumber() const { + return frame_n; + } + + double timeCaptured() const { + return time_c; + } + + QString getRecName() { + return recName; + } + unsigned int getFrameRate() { return options.frameRate; } - void setProjectDir( QString &p_dir ); void setFrameRate( unsigned int frame_rate ) { options.frameRate = frame_rate; } @@ -97,14 +105,6 @@ class Recorder : public QObject { return capture_frames; } - int frameNumber() const { - return frame_n; - } - - double timeCaptured() const { - return time_c; - } - public slots: // Append a frame to the recording, void appendFrame( FlyCapture2::Image * image ); @@ -118,7 +118,14 @@ class Recorder : public QObject { FlyCapture2::AVIOption options; FlyCapture2::TIFFOption frame_options; - // Basic state Variable, because AVIRecorder doesn't provide it. + // lock the appendFrame function. + QMutex write_lock; + + // Indicates wether the project directorie is set. + bool pDirSet; + + // basic state Variable, because AVIRecorder doesn't provide it. + // Well it does, but it is boring to look it up. bool is_recording; // append to existing files @@ -131,6 +138,7 @@ class Recorder : public QObject { int frame_n; double time_c; + // recording config QDir baseDir; QDir record_dir; QString recName; @@ -147,11 +155,6 @@ class Recorder : public QObject { // reset Status, only if not recording (is_recording == false). void cleanup(); - // lock the appendFrame function. - QMutex write_lock; - - bool pDirSet; - signals: void frameSaved( FlyCapture2::Image * image ); void writeError( FlyCapture2::Error err );