some little tweaks

This commit is contained in:
Hiro Protagonist 2017-03-26 16:28:01 +13:00
parent 3e3c103c93
commit 28703fe31b
4 changed files with 2 additions and 498 deletions

View file

@ -36,7 +36,6 @@ MainWindow::MainWindow( QWidget * parent ) :
// Connect Frame Counts, Time Captured LCD
connect( &recorder, &Recorder::frameSaved, this, [this] {
ui->framesCaptured->display( recorder.frameNumber() );
qDebug() << recorder.timeCaptured();
ui->timeCaptured->display( QString( "%1:%2" ).arg( ( ( ( int )recorder.timeCaptured() ) / 60 ) ).arg( ( int )recorder.timeCaptured() % 60 ) );
} );
@ -264,6 +263,7 @@ void MainWindow::startStopRecording() {
}
setStatus( RECORDING );
} else {
// Stop Capture!
try {

View file

@ -1,307 +0,0 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QImage>
#include <QGraphicsItem>
#include <QDebug>
#include <QTime>
#include <QErrorMessage>
#include <QFileDialog>
// TODO: Handle Errors!!
// TODO: Handle Disconnect!!
MainWindow::MainWindow( QWidget * parent ) :
QMainWindow( parent ),
ui( new Ui::MainWindow ),
camMan( this ),
recorder( this, 18, false ),
resize { false } {
ui->setupUi( this );
// Set Scene and Hide Preview Widget
ui->preview_widget->hide();
fit();
// Set status
setStatus( WAITING );
// Try to connect to first cam
scanAndUpdateCameras();
// Set default name
ui->projectName->setText( "Taevitas_Rec_" + QDateTime::currentDateTime().toString( "dd_MM_yyyy_hh_mm_ss" ) );
// Connect Frame Counts, Time Captured LCD
connect( &recorder, &Recorder::frameSaved, this, [this] {
ui->framesCaptured->display( recorder.frameNumber() );
qDebug() << recorder.timeCaptured();
ui->timeCaptured->display( QString( "%1:%2" ).arg( ( ( ( int )recorder.timeCaptured() ) / 60 ) ).arg( ( int )recorder.timeCaptured() % 60 ) );
} );
// Connect Events
connect( ui->preview_button, &QPushButton::clicked, this, &MainWindow::togglePreview );
// Cam ComboBox clicked
connect( ui->camScanButton, &QPushButton::clicked, this, &MainWindow::scanAndUpdateCameras );
// Camera selected
connect( ui->cameraSelector, static_cast<void( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &MainWindow::cameraSelected );
// File Selector
connect( ui->directorySelector, &QPushButton::clicked, this, &MainWindow::directorySelection );
// Frame Captured
connect( &camMan, &CameraManager::frameCaptured, this, &MainWindow::frameCaptured, Qt::DirectConnection );
// Change Project Name
connect( ui->projectName, &QLineEdit::textChanged, this, &MainWindow::enableStart );
// Change Record Frames
connect( ui->saveFrames, &QCheckBox::toggled, &recorder, &Recorder::setCaptureFrames );
// Start recording
connect( ui->startButton, &QPushButton::clicked, this, &MainWindow::startStopRecording );
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::setStatus( STATUS status ) {
switch ( status ) {
case WAITING:
disableRecOptions();
ui->statusLabel->setText( "Waiting..." );
ui->startButton->setText( "Start" );
ui->recStats->hide();
break;
case CONNECTED:
enableRecOptions();
ui->statusLabel->setText( "Connected." );
ui->startButton->setText( "Start" );
ui->recStats->hide();
qDebug( "Connected...." );
break;
case RECORDING:
ui->timeCaptured->display( QString( "00:00" ) );
ui->framesCaptured->display( 0 );
ui->statusLabel->setText( "Recording!" );
ui->startButton->setText( "Stop" );
ui->recStats->show();
break;
}
}
// FIXME
void MainWindow::fit() {
setMinimumSize( 0, 0 );
setMaximumSize( 5000, 5000 );
adjustSize();
//setFixedSize( this->size() );
}
void MainWindow::updateCameraList( unsigned int num_cameras ) {
ui->cameraSelector->clear();
// Fill Combo Box with cameras
for ( unsigned int i = 0; i < num_cameras; i++ ) {
ui->cameraSelector->addItem( QString( i + '0' ) );
}
}
void MainWindow::scanAndUpdateCameras() {
unsigned int num_cameras = camMan.numCameras();
updateCameraList( num_cameras );
if ( num_cameras > 0 && !camMan.isConnected() )
cameraSelectnnnned( 0 );
}
void MainWindow::disableRecOptions() {
ui->recOptions->setProperty( "enabled", false );
ui->startButton->setProperty( "enabled", false );
}
void MainWindow::enableRecOptions() {
ui->recOptions->setProperty( "enabled", true );
enableStart();
}
void MainWindow::enableStart() {
if ( ui->projectName->text() != "t" && recorder.dirSet() && camMan.isConnected() ) {
ui->startButton->setProperty( "enabled", true );
} else {
ui->startButton->setProperty( "enabled", true );
}
}
void MainWindow::showError( QString error ) {
QMessageBox errBox;
errBox.critical( 0, "Error", "An Error has occured:\n" + error );
errBox.setFixedSize( 500, 200 );
errBox.show();
}
void MainWindow::showError( FlyCapture2::Error error ) {
showError( error.GetDescription() );
}
void MainWindow::cameraSelected( int index ) {
if ( recorder.isRecording() || index < 0 )
return;
try {
camMan.connectCamera( index );
} catch ( FlyCapture2::Error e ) {
showError( e );
return;
}
setStatus( CONNECTED );
}
// Show/Hide Preview
void MainWindow::togglePreview( bool checked ) {
// skip if there is no camera
if ( !camMan.isConnected() ) {
ui->preview_button->setProperty( "checked", false );
return;
}
if ( checked ) {
ui->preview_widget->setProperty( "enabled", true );
resize = true;
// Start Capturing for preview
try {
camMan.startCapture();
} catch ( FlyCapture2::Error e ) {
showError( e );
}
} else {
ui->preview_widget->setProperty( "enabled", false );
ui->preview_widget->hide();
//Stop capture
if ( !recorder.isRecording() )
camMan.stopCapture();
}
}
void MainWindow::frameCaptured( FlyCapture2::Image * image ) {
qDebug() << "Image Captured!";
// If preview is activated...
if ( ui->preview_widget->isEnabled() )
displayPreview( image );
if ( recorder.isRecording() )
recorder.appendFrame( image );
return;
}
// Render Preview
void MainWindow::displayPreview( FlyCapture2::Image * last_capture ) {
ui->preview_widget->show();
FlyCapture2::Image last_image;
QImage last_preview_image;
QPixmap last_preview;
// Convert Pixel Format to RGB
FlyCapture2::Error e = last_capture->Convert( FlyCapture2::PIXEL_FORMAT_RGB, &last_image );
last_preview_image = QImage( last_image.GetData(), last_image.GetCols(), last_image.GetRows(), QImage::Format_RGB888 );
last_preview_image = last_preview_image.scaledToHeight( 500 );
ui->preview_widget->setFixedSize( last_preview_image.width(), last_preview_image.height() );
last_preview.convertFromImage( last_preview_image );
ui->preview_widget->setPixmap( last_preview );
if ( resize ) {
fit();
resize = false;
}
}
void MainWindow::directorySelection() {
QString dir = QFileDialog::getExistingDirectory( this, tr( "Choose the working Directory." ), ( recorder.dirSet() ? recorder.getProjectDir() : QStandardPaths::writableLocation( QStandardPaths::DocumentsLocation ) ), QFileDialog::ShowDirsOnly );
try {
recorder.setProjectDir( dir );
enableStart();
} catch ( FlyCapture2::Error e ) {
showError( e );
}
}
void MainWindow::startStopRecording() {
// TODO: Errors
if ( !recorder.isRecording() ) {
// TODO: Maybe allow dynamic setting...
ui->saveFrames->setProperty( "enabled", false );
if ( !camMan.isCapturing() ) {
try {
camMan.startCapture();
} catch ( FlyCapture2::Error e ) {
showError( e );
ui->saveFrames->setProperty( "enabled", true );
return;
}
}
try {
recorder.newRecording( ui->projectName->text() );
} catch ( RecorderError ) {
showError( "Could not start Recording!" );
ui->saveFrames->setProperty( "enabled", true );
stopCapture();
return;
}
setStatus( RECORDING );
} else {
// Stop Capture!
try {
camMan.stopCapture();
} catch ( FlyCapture2::Error e ) {
showError( e );
return;
}
try {
recorder.stopRecording();
} catch ( FlyCapture2::Error e ) {
showError( e );
return;
}
// Restart Preview
if ( ui->preview_button->isChecked() ) {
try {
camMan.startCapture();
} catch ( FlyCapture2::Error e ) {
showError( e );
}
}
ui->saveFrames->setProperty( "enabled", true );
setStatus( CONNECTED );
}
}
void MainWindow::stopCapture() {
if ( !ui->preview_button->isChecked() ) {
try {
camMan.stopCapture();
} catch ( FlyCapture2::Error e ) {
showError( e );
return;
}
}
}

View file

@ -129,7 +129,6 @@ RecorderError Recorder::verifyRecDir() {
return RecorderError::OK;
}
// TODO: Capture Status...
void Recorder::stopRecording() {
if ( is_recording ) {
// Stop Recorder
@ -179,6 +178,7 @@ void Recorder::appendFrame( FlyCapture2::Image * image ) {
frame_n++;
time_c = frame_n / options.frameRate;
qDebug() << time_c;
emit frameSaved();
// Go to begining.

View file

@ -1,189 +0,0 @@
#include "recorder.h"
#include <QDebug>
#include <QRegExp>
using namespace FlyCapture2;
Recorder::Recorder( QObject * parent, unsigned int frame_rate, bool cap_frames ) : QObject( parent ), is_recording {false}, frame_n {0}, time_c {0}, pDirSet { false } {
// No Compression for frame_captures
frame_options.compression = frame_options.NONE;
options.frameRate = frame_rate;
capture_frames = cap_frames;
}
Recorder::~Recorder() {
stopRecording();
}
void Recorder::setProjectDir( QString &p_dir ) {
QFileInfo pDirInfo( p_dir );
if ( !pDirInfo.isDir() || !pDirInfo.isWritable() ) {
throw RecorderError::INVALID_PROJECT_DIRECTORY;
return;
}
baseDir = QDir( p_dir );
pDirSet = true;
}
void Recorder::newRecording( QString r_name ) {
stopRecording();
// If unset.
if ( !dirSet() ) {
throw RecorderError::INVALID_PROJECT_DIRECTORY;
return;
}
if ( r_name.length() == 0 ) {
throw RecorderError::INVALID_RECORDING_NAME;
return;
}
// Clean r_name // TODO: Maybe Visual Feedback for that...
r_name.replace(QRegExp("[^\w\s]", Qt::CaseInsensitive), "");
recName = r_name;
// Verify Recdir... create directories...
record_dir = QDir( baseDir.path() + "/" + recName );
// Change to try/catch
RecorderError err = verifyRecDir();
if ( err != RecorderError::OK ) {
throw err;
return;
}
bool statExists = QFile( record_dir.path() + "/" + ".stat" ).exists();
// get Status file
statFile = new QFile( record_dir.path() + "/" + ".stat" );
if ( !statFile->open( QIODevice::ReadWrite | QIODevice::Text ) ) {
throw RecorderError::CANT_OPEN_STATFILE;
return;
}
if ( !statExists ) {
statFile->write( "0" );
}
frame_n = 0;
time_c = 0;
// If append, figure out Frame count etc...
if ( append ) {
if ( !restoreRecording() ) {
throw RecorderError::STATFILE_ERROR;
return;
}
}
Error f_err;
qDebug() << ( record_dir.path() + "/" + recName ).toStdString().c_str();
// open AVI in recorder
f_err = recorder.AVIOpen( ( record_dir.path() + "/" + recName + "_" + QString::number( frame_n ) + ".avi" ).toStdString().c_str(), &options );
if ( f_err != PGRERROR_OK ) {
throw f_err;
return;
}
is_recording = true;
}
bool Recorder::restoreRecording() {
bool ok;
QByteArray tmp = statFile->readLine();
frame_n = QString( tmp ).toUInt( &ok );
return ok;
}
RecorderError Recorder::verifyRecDir() {
// check if project with same name exists and append to that
if ( record_dir.exists() ) {
QMessageBox msgBox;
msgBox.setText( tr( "A recording with this name already exists. Which action should be taken?" ) );
void * appendButton = msgBox.addButton( tr( "Append the recording." ), QMessageBox::ActionRole );
void * removeButton = msgBox.addButton( tr( "Overwrite recording." ), QMessageBox::ActionRole );
void * abortButton = msgBox.addButton( QMessageBox::Abort );
msgBox.exec();
if ( msgBox.clickedButton() == appendButton ) {
append = true;
} else if ( msgBox.clickedButton() == removeButton ) {
record_dir.removeRecursively();
} else if ( msgBox.clickedButton() == abortButton ) {
return RecorderError::CANCELED;
}
}
baseDir.mkdir( recName );
if ( capture_frames )
record_dir.mkdir( "frames" );
return 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();
// If not recording, just stop.
if ( !is_recording )
return;
Error app_err = recorder.AVIAppend( image );
if ( app_err != PGRERROR_OK ) {
write_lock.unlock();
throw app_err;
return;
}
// save image as frame
if ( capture_frames ) {
app_err = image->Save( ( record_dir.path() + "/frames/" + recName + "_" + QString::number( frame_n ) + ".tiff" ).toStdString().c_str(), &frame_options );
if ( app_err != PGRERROR_OK ) {
write_lock.unlock();
throw app_err;
return;
}
}
frame_n++;
time_c = frame_n / options.frameRate;
emit frameSaved();
// Go to begining.
statFile->seek( 0 );
// Write Frames to Stat File.
statFile->write( QByteArray::number( frame_n ) );
write_lock.unlock();
}