From 1d0d72785bb0e3046b034342f5c420fe3d11c0dc Mon Sep 17 00:00:00 2001
From: am0ebe <am0ebe@gmx.de>
Date: Thu, 22 Feb 2024 12:50:44 +0100
Subject: [PATCH] connect signals/slots in iPrinter ctor level. use Console
 singleton, use conditional compilation for console

---
 obj/recorder-cmd.pro |  3 ++-
 src/cam.cpp          | 36 ++++++++++++------------------------
 src/cam.h            |  2 +-
 src/cmd/console.cpp  | 15 +++++++++++----
 src/cmd/console.h    | 43 ++++++++++++++++++++++++++-----------------
 src/cmd/main.cpp     | 11 -----------
 src/core.cpp         |  6 +++---
 src/core.h           |  1 +
 src/iprinter.cpp     | 13 +++++++++++++
 9 files changed, 69 insertions(+), 61 deletions(-)

diff --git a/obj/recorder-cmd.pro b/obj/recorder-cmd.pro
index 26b22bd..232d796 100644
--- a/obj/recorder-cmd.pro
+++ b/obj/recorder-cmd.pro
@@ -9,7 +9,8 @@ SOURCES += 	$$SRC_DIR/cmd/*.cpp \
 HEADERS += 	$$SRC_DIR/cmd/*.h \
 				$$SRC_DIR/*.h
 
-DEFINES *= QT_USE_QSTRINGBUILDER		#converts + to % when building strings
+DEFINES *= QT_USE_QSTRINGBUILDER		#converts + to % when building strings 	#append macro
+DEFINES += CONSOLE 	#add macro
 
 # opencv
 CONFIG += link_pkgconfig
diff --git a/src/cam.cpp b/src/cam.cpp
index b5f4f5e..65cdc24 100644
--- a/src/cam.cpp
+++ b/src/cam.cpp
@@ -1,8 +1,9 @@
 #include "cam.h"
-#include "VmbCPP/SharedPointerDefines.h"
 #include "utils.h"
 #include "frameobserver.h"
 
+#include "VmbCPP/SharedPointerDefines.h"
+
 #include <chrono>
 #include <memory> //shared_ptr
 #include <unistd.h>
@@ -13,8 +14,7 @@
 using State = Cam::State;
 using namespace VmbCPP;
 
-Cam::Cam(CameraPtr pCamera) :
-IPrinter(),
+Cam::Cam(CameraPtr pCamera) : IPrinter(),
 _cam(pCamera),
 _name(""),
 _state(disconnected),
@@ -23,12 +23,6 @@ _payloadSize(0LL),
 _pixelFormat("")
 {
 	name();
-
-	//vmb (core) -> ui (controller.console)
-	// QObject::connect(&core, SIGNAL(error(const QString&, const int&)), controller.console, SLOT(error(const QString&, const int&)), Qt::DirectConnection);
-	// QObject::connect(&core, SIGNAL(error(const int&)), controller.console, SLOT(error(const int&)), Qt::DirectConnection);
-	// QObject::connect(&core, SIGNAL(info(const QString&)), controller.console, SLOT(print(const QString&)), Qt::DirectConnection);
-	// QObject::connect(&core, SIGNAL(info(const QStringList&)), controller.console, SLOT(print(const QStringList&)), Qt::DirectConnection);
 }
 
 // Copy constructor
@@ -45,16 +39,12 @@ _pixelFormat(other._pixelFormat)
 Cam::~Cam()
 {
 	qDebug() << __FUNCTION__ << "():" << __LINE__ << "name:" << _name;
-	// if( _state == recording )
-		// stopRecording();
+	if( _state == recording )
+		stopRecording();
 
-	// if( _state == opened )
-		// close();
+	if( _state == opened )
+		close();
 	// close() should be called automatically, when the last shared_ptr to the camera is destroyed
-
-	// for ( auto cam : cameras )
-	// 	cam->Close();
-
 }
 
 
@@ -128,10 +118,12 @@ void Cam::startRecording(std::chrono::seconds dur)
 
 		g( _cam->GetFeatureByName ("AcquisitionStart", pFeature ),"get AcquisitionStart");
 		g( pFeature->RunCommand(),"run AcquisitionStart");
+
 	}
 	catch (const std::runtime_error& xx)
 	{
 		qDebug() << "Exception caught: " << xx.what();
+		return;
 	}
 
 	_state = recording;
@@ -166,20 +158,16 @@ void Cam::stopRecording()
 		g(_cam->RevokeAllFrames(),"revoke all frames");
 		for( FramePtr frame : _frames )
 			g(frame->UnregisterObserver(),"unregister observer");
+
+		_state = opened;
 	}
 	catch (const std::runtime_error& xx)
-	{
-		qDebug() << "Exception caught: " << xx.what();
-	}
-
-	_state = opened;
+	{} //ignore
 }
 
 // id,name,model,serial,interfaceID,state
 QString Cam::info()
 {
-	// qDebug() << "Camera Name: " << _name;
-	// qDebug() << "Camera State: " << _state;
 	QString camInfo;
 	try
 	{
diff --git a/src/cam.h b/src/cam.h
index 7ec7a19..4a82eb6 100644
--- a/src/cam.h
+++ b/src/cam.h
@@ -45,11 +45,11 @@ public:
 	void close();
 	void startRecording(std::chrono::minutes dur);
 	void startRecording(std::chrono::seconds dur); //use as literals "1s, 1min"
-	void stopRecording();
 	QString info();
 	inline CameraPtr getCameraPtr() const { return _cam; }
 
 public slots:
+	void stopRecording();
 	void onCameraDisconnected();
 
 private:
diff --git a/src/cmd/console.cpp b/src/cmd/console.cpp
index bec3933..6ea2616 100644
--- a/src/cmd/console.cpp
+++ b/src/cmd/console.cpp
@@ -6,9 +6,16 @@
 
 #include <QDebug>
 #include <QCoreApplication>
+#include <QTimer>
+
 #include <iostream>
 #include <math.h>
 
+Console* Console::getInstance() {
+	static Console* instance = new Console; // Static local variable ensures single instance
+	return instance;
+}
+
 void Console::listenKeys()
 {
 	// listen to keyevents in endless loop
@@ -105,13 +112,15 @@ void Console::printHelp()
 
 // ###############################
 // Controller
-Controller::Controller() : console(new Console())
+Controller::Controller()
 {
+	auto console = Console::getInstance();
 	console->moveToThread(&thread);
 	connect(&thread, &QThread::finished, console, &QObject::deleteLater);
 	connect(this, &Controller::operate, console, &Console::listenKeys);
 	connect(console, &Console::keyPressed, this, &Controller::keyPress);
 
+	QTimer::singleShot(0, this, SIGNAL(operate()));
 	thread.start();
 }
 
@@ -124,7 +133,7 @@ Controller::~Controller()
 
 void Controller::keyPress(const QChar& key)
 {
-
+	auto console = Console::getInstance();
 	qDebug().noquote() << __FUNCTION__ << ": " << key;
 	if ( key.isDigit() )
 	{
@@ -171,6 +180,4 @@ void Controller::keyPress(const QChar& key)
 				break;
 		}
 	}
-
 }
-
diff --git a/src/cmd/console.h b/src/cmd/console.h
index b0d1c09..9946186 100644
--- a/src/cmd/console.h
+++ b/src/cmd/console.h
@@ -11,23 +11,33 @@ class Console : public QObject
 {
 	Q_OBJECT
 
-	public slots:
-		void listenKeys();
-		QString getLine();
-		int getNumber();
-		void error(const QString&,const int&);
-		void error(const int&);
-		void print(const QString&);
-		void print(const QStringList&);
-
-		void printVersion();
-		void printHelp();
-		// ...
+public:
+	static Console* getInstance();
+
+	Console(const Console&) = delete; // Delete copy constructor and assignment operator to prevent copying
+	Console& operator=(const Console&) = delete;
+
+private:
+	Console() : QObject(){}; //// Private constructor to prevent instantiation
+
+public slots:
+	void listenKeys();
+	QString getLine();
+	int getNumber();
+	void error(const QString&,const int&);
+	void error(const int&);
+	void print(const QString&);
+	void print(const QStringList&);
+
+	void printVersion();
+	void printHelp();
+	// ...
+
+signals:
+	void keyPressed(const QChar &key);
+	// void lineEntered(const QString&);
+	// void numberEntered(const int&);
 
-	signals:
-		void keyPressed(const QChar &key);
-		// void lineEntered(const QString&);
-		// void numberEntered(const int&);
 
 };
 
@@ -39,7 +49,6 @@ class Controller : public QObject
 	public:
 		Controller();
 		~Controller();
-		Console* console;
 
 	public slots:
 		void keyPress(const QChar &);
diff --git a/src/cmd/main.cpp b/src/cmd/main.cpp
index ff19440..8b9c5ea 100644
--- a/src/cmd/main.cpp
+++ b/src/cmd/main.cpp
@@ -24,19 +24,8 @@ int main(int argc, char *argv[])
 	QObject::connect(&controller, &Controller::startRecording, 	&core, &Core::startRecording);
 	QObject::connect(&controller, &Controller::stopRecording,	&core, &Core::stopRecording);
 	QObject::connect(&controller, &Controller::setDuration, 		&core, &Core::setDuration);
-
-
-
 	// XXX add new func
 
-	//vmb (core) -> ui (controller.console)
-	QObject::connect(&core, SIGNAL(error(const QString&, const int&)), controller.console, SLOT(error(const QString&, const int&)), Qt::DirectConnection);
-	QObject::connect(&core, SIGNAL(error(const int&)), controller.console, SLOT(error(const int&)), Qt::DirectConnection);
-	QObject::connect(&core, SIGNAL(info(const QString&)), controller.console, SLOT(print(const QString&)), Qt::DirectConnection);
-	QObject::connect(&core, SIGNAL(info(const QStringList&)), controller.console, SLOT(print(const QStringList&)), Qt::DirectConnection);
-	// sa Controller() for delegating to Console
-
-	QTimer::singleShot(0, &controller, SIGNAL(operate()));
 	// QTimer::singleShot(10, controller.console, SLOT(printVersion()), Qt::DirectConnection); // for testing
 	// QTimer::singleShot(200, &core, SLOT(openCam())); // for testing
 
diff --git a/src/core.cpp b/src/core.cpp
index 4ba5ace..9f0110c 100644
--- a/src/core.cpp
+++ b/src/core.cpp
@@ -21,8 +21,7 @@
 using namespace VmbCPP;
 using namespace std::chrono_literals; //seconds, minutes, hours
 
-Core::Core() :
-	IPrinter(),
+Core::Core() : IPrinter(),
 	_apiStarted( false ),
 	_sys( VmbSystem::GetInstance() ), // create and get Vimba singleton
 	_cameras( CamPtrVector() ),
@@ -82,7 +81,8 @@ void Core::updateCameras()
 		}
 		if (!found)
 		{
-			_cameras.push_back(std::make_shared<Cam>(vcam));
+
+			_cameras.push_back(std::make_shared<Cam>(vcam)); //create Cam() from CameraPtr vcam
 			print("cam connected");
 			//vcam->GetName(), threadsPerCam
 		}
diff --git a/src/core.h b/src/core.h
index cb539ba..4e7aec9 100644
--- a/src/core.h
+++ b/src/core.h
@@ -14,6 +14,7 @@ using std::chrono::seconds;
 
 class Core : public IPrinter
 {
+
 Q_OBJECT
 
 public:
diff --git a/src/iprinter.cpp b/src/iprinter.cpp
index 5ba8515..e93fe63 100644
--- a/src/iprinter.cpp
+++ b/src/iprinter.cpp
@@ -1,7 +1,20 @@
 #include "iprinter.h"
+#ifdef CONSOLE
+#include "cmd/console.h"
+#endif
+
 
 IPrinter::IPrinter(QObject *parent) : QObject(parent)
 {
+#ifdef CONSOLE //defined in .pro
+	//vmb (core, cam) -> ui (controller.console)
+	Console* console = Console::getInstance();
+	connect(this, SIGNAL(error(const QString&, const int&)), console, SLOT(error(const QString&, const int&)), Qt::DirectConnection);
+	connect(this, SIGNAL(error(const int&)), console, SLOT(error(const int&)), Qt::DirectConnection);
+	connect(this, SIGNAL(info(const QString&)), console, SLOT(print(const QString&)), Qt::DirectConnection);
+	connect(this, SIGNAL(info(const QStringList&)), console, SLOT(print(const QStringList&)), Qt::DirectConnection);
+#endif
+
 }
 
 IPrinter::~IPrinter()
-- 
GitLab