From 8f5311e1d786c38df327fcd57d507718edfc637b Mon Sep 17 00:00:00 2001
From: am0ebe <am0ebe@gmx.de>
Date: Tue, 2 Jul 2024 16:51:50 +0200
Subject: [PATCH] [not tested] progressbar for recordings: add functions to
 iprinter to print progressbar, get width from console and calc chars, get
 times and format mm:ss.

---
 src/cam.cpp         | 10 ++++++
 src/cam.h           |  1 +
 src/cmd/console.cpp |  2 +-
 src/iprinter.cpp    | 88 +++++++++++++++++++++++++++++++++++----------
 src/iprinter.h      | 11 ++++++
 5 files changed, 93 insertions(+), 19 deletions(-)

diff --git a/src/cam.cpp b/src/cam.cpp
index f82d882..cc5c0f2 100644
--- a/src/cam.cpp
+++ b/src/cam.cpp
@@ -256,6 +256,16 @@ void Cam::showRecordingStats()
 
 }
 
+void Cam::showRecordingProgress()
+{
+	if( _state != recording )
+		return; // error("cant show progress, when cam is " + stateToString(_state));
+
+	//TODO test
+	//fps?
+	progressBar(_timer);
+}
+
 void Cam::printFeatures( QString feature)
 {
 	printFeatures(QStringList(feature));
diff --git a/src/cam.h b/src/cam.h
index 6d565bd..f98d6d7 100644
--- a/src/cam.h
+++ b/src/cam.h
@@ -61,6 +61,7 @@ public:
 	QString printCamInfo();
 
 	void showRecordingStats();
+	void showRecordingProgress();
 	void printFeatures(QStringList);
 	void printFeatures(QString);
 
diff --git a/src/cmd/console.cpp b/src/cmd/console.cpp
index 019dbb1..ae25116 100644
--- a/src/cmd/console.cpp
+++ b/src/cmd/console.cpp
@@ -10,7 +10,7 @@
 #include <QTimer>
 
 #include <iostream>
-#include <math.h>
+#include <cmath>
 
 using utils::DELIM;
 
diff --git a/src/iprinter.cpp b/src/iprinter.cpp
index cca322b..b9de8a8 100644
--- a/src/iprinter.cpp
+++ b/src/iprinter.cpp
@@ -1,10 +1,16 @@
 #include "iprinter.h"
+
 #include <QDebug>
+#include <QTimer>
+
 #ifdef CONSOLE
 #include "cmd/console.h"
+#include <cstdlib>
 #endif
 
 
+
+
 IPrinter::IPrinter(QObject *parent) : QObject(parent)
 {
 #ifdef CONSOLE //defined in .pro
@@ -22,6 +28,36 @@ IPrinter::~IPrinter()
 {
 }
 
+
+bool IPrinter::f(const VmbErrorType& ret, const QString& msg)
+{
+	// vimba function wrapper to test return type and potentially emit error message or successmsg
+	// returns true on error
+	// qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << " ret:" << ret << " msg:" << msg;
+
+	if ( VmbErrorSuccess == ret )
+	{
+		if (! msg.isEmpty())
+			emit info(msg);
+
+		return false;
+	}
+	else
+	{
+		if (! msg.isEmpty())
+			emit info("Fail! " + msg);
+		emit error(ret);
+		return true;
+	}
+}
+
+void IPrinter::g(const VmbErrorType& ret, const QString& msg)
+{
+	//vimba function wrapper - throws exception on error - to declutter error handling code
+	if( f(ret,msg) )
+		throw std::runtime_error("Error: " + msg.toStdString());
+}
+
 void IPrinter::print(const QStringList& name)
 {
 	emit info(name);
@@ -70,31 +106,47 @@ void IPrinter::print(const char* name, const QString& val)
 	emit info(QString::fromLocal8Bit(name) + ": " + val);
 }
 
-bool IPrinter::f(const VmbErrorType& ret, const QString& msg)
+void IPrinter::progressBar(QTimer* timer)
 {
-	// vimba function wrapper to test return type and potentially emit error message or successmsg
-	// returns true on error
-	// qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << " ret:" << ret << " msg:" << msg;
+	int total_ms = timer->interval();
+	int remaining_ms = timer->remainingTime();
+	auto elapsed_ms = total_ms - remaining_ms;
+	int progressInPercent = (double) elapsed_ms / total_ms;
+	//print in minutes and secs
+	print(QString::number(progressInPercent) + "% | " + formatTime(elapsed_ms) + " < " + formatTime(remaining_ms));
+	progressBar(progressInPercent);
+}
 
-	if ( VmbErrorSuccess == ret )
-	{
-		if (! msg.isEmpty())
-			emit info(msg);
+void IPrinter::progressBar(int percentage)
+{
 
-		return false;
+	int cols = 42;
+	if (const char* env_p = std::getenv("COLUMNS"))
+	{
+		cols = atoi(env_p);
 	}
-	else
+	qDebug() << "#col: " << cols;
+
+
+	// print without appending newline
+	QDebug deb = qDebug();
+	deb << "[";
+	int pos_progress = cols*(percentage/100.0);
+	for(int pos=1; pos<cols-1; pos++)
 	{
-		if (! msg.isEmpty())
-			emit info("Fail! " + msg);
-		emit error(ret);
-		return true;
+		if( pos < pos_progress )
+			deb << "=";
+		else
+			deb << " ";
 	}
+	deb << "]";
 }
 
-void IPrinter::g(const VmbErrorType& ret, const QString& msg)
+
+QString IPrinter::formatTime(const int& milliseconds)
 {
-	//vimba function wrapper - throws exception on error - to declutter error handling code
-	if( f(ret,msg) )
-		throw std::runtime_error("Error: " + msg.toStdString());
+	int seconds = milliseconds / 1000;
+	int minutes = seconds / 60;
+	seconds %= 60;
+	return QString::asprintf("%02d:%02d", minutes, seconds);
 }
diff --git a/src/iprinter.h b/src/iprinter.h
index 6572d7f..b66d470 100644
--- a/src/iprinter.h
+++ b/src/iprinter.h
@@ -6,6 +6,8 @@
 #include <QString>
 #include <QStringList>
 
+class QTimer;
+
 // common functionality "interface" for printing, error handling and connecting messages to CMD
 class IPrinter : public QObject
 {
@@ -35,4 +37,13 @@ public:
 	void print(const char*);
 	void print(const char*, const VmbInt64_t&);
 	void print(const char*, const QString&);
+
+
+	void progressBar(QTimer*);
+
+private:
+	void progressBar(int);
+	QString formatTime(const int&);
+
+
 };
-- 
GitLab