From d5c25742fc80b89ac047037172e1a0120cd97244 Mon Sep 17 00:00:00 2001
From: am0ebe <am0ebe@gmx.de>
Date: Thu, 18 Jan 2024 18:24:39 +0100
Subject: [PATCH] new architecture: 2 apps.

1. console app: recorder-cmd
2. gui app: recorder-gui

> set up build system for both apps
> console: read commandline arguments
> update gitignore
---
 .gitignore                             |   2 +-
 README                                 |  11 +++
 obj/recorder-cmd.pro                   |  35 +++++++++
 obj/{recorder.pro => recorder-gui.pro} |  12 +--
 recorder.sublime-project               |  73 +++++++++++++-----
 src/cmd/main.cpp                       | 100 +++++++++++++++++++++++++
 src/{ => gui}/main.cpp                 |   3 +-
 src/{ => gui}/mainwindow.cpp           |  26 ++++---
 src/{ => gui}/mainwindow.h             |   0
 src/{ => gui}/mainwindow.ui            |   0
 src/recorder.cpp                       |   2 +-
 src/utils.cpp                          |   6 +-
 12 files changed, 228 insertions(+), 42 deletions(-)
 create mode 100644 obj/recorder-cmd.pro
 rename obj/{recorder.pro => recorder-gui.pro} (91%)
 create mode 100644 src/cmd/main.cpp
 rename src/{ => gui}/main.cpp (96%)
 rename src/{ => gui}/mainwindow.cpp (93%)
 rename src/{ => gui}/mainwindow.h (100%)
 rename src/{ => gui}/mainwindow.ui (100%)

diff --git a/.gitignore b/.gitignore
index 09d9bb9..f8b7e4b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,7 +2,7 @@ data
 bin
 obj/*
 obj/Makefile
-!obj/recorder.pro
+!obj/recorder*.pro
 
 .cache
 *.autosave
diff --git a/README b/README
index 5eaea05..f157c42 100644
--- a/README
+++ b/README
@@ -6,3 +6,14 @@ Based of VimbaX SDK from:
 -> expanded on VimbaCPP/Example/AsyncOpenCVRecorder !
 
 does: acquire images and store them as jpeg along with timestamps
+
+-------------------------------
+
+Software B actually consists of 2 apps:
+
+  1. recorder-cmd
+    a commandline app that is meant to be standalone and communicates with the camera
+
+  2. recorder-gui
+    a Qt GUI app, which calls the commandline app and displays the images
+
diff --git a/obj/recorder-cmd.pro b/obj/recorder-cmd.pro
new file mode 100644
index 0000000..47d1779
--- /dev/null
+++ b/obj/recorder-cmd.pro
@@ -0,0 +1,35 @@
+TEMPLATE = app
+
+QT += core
+CONFIG += c++20 warn_on console
+
+SOURCES += \
+	../src/cmd/main.cpp \
+	../src/utils.cpp
+
+HEADERS += \
+	../src/utils.h
+
+DEFINES *= QT_USE_QSTRINGBUILDER		#converts + to % when building strings
+
+# opencv
+CONFIG += link_pkgconfig
+PKGCONFIG += opencv4
+
+# Vimba
+INCLUDEPATH += ../vimbax/api/include
+VIMBA_LIB_DIR = ../lib
+LIBS += -L$${VIMBA_LIB_DIR} -lVmbC -lVmbCPP -Wl,-rpath,\'\$$ORIGIN\'
+
+#-Wl,-rpath,.
+QMAKE_POST_LINK += cp $${VIMBA_LIB_DIR}/lib*.so ../bin  # copy vmb-libs to bin
+
+#PRE_LINK: cp vimba libs once
+# cp ../b/vimbax/api/lib/GenICam/*.so $${VIMBA_LIB_DIR}
+# cp ../b/vimbax/api/lib/libVmb{C,CPP,ImageTransform}.so $${VIMBA_LIB_DIR}
+
+TARGET = recorder-cmd
+DESTDIR = ../bin
+
+QMAKE_CLEAN += .cache/clangd/index/* .qmake.stash
+#cant rm dir here, so added in project file
diff --git a/obj/recorder.pro b/obj/recorder-gui.pro
similarity index 91%
rename from obj/recorder.pro
rename to obj/recorder-gui.pro
index 411c678..d87299d 100644
--- a/obj/recorder.pro
+++ b/obj/recorder-gui.pro
@@ -4,17 +4,17 @@ QT += core gui widgets
 CONFIG += c++20 warn_on
 
 SOURCES += \
-	../src/main.cpp \
-	../src/mainwindow.cpp \
+	../src/gui/main.cpp \
+	../src/gui/mainwindow.cpp \
 	../src/utils.cpp
 
 HEADERS += \
 	ui_mainwindow.h \
-	../src/mainwindow.h \
+	../src/gui/mainwindow.h \
 	../src/utils.h
 
 FORMS += \
-	../src/mainwindow.ui
+	../src/gui/mainwindow.ui
 
 DEFINES *= QT_USE_QSTRINGBUILDER		#converts + to % when building strings
 
@@ -34,11 +34,11 @@ QMAKE_POST_LINK += cp $${VIMBA_LIB_DIR}/lib*.so ../bin  # copy vmb-libs to bin
 # cp ../b/vimbax/api/lib/GenICam/*.so $${VIMBA_LIB_DIR}
 # cp ../b/vimbax/api/lib/libVmb{C,CPP,ImageTransform}.so $${VIMBA_LIB_DIR}
 
-TARGET = recorder
+TARGET = recorder-gui
 DESTDIR = ../bin
 
 QMAKE_CLEAN += .cache/clangd/index/* .qmake.stash
-#cant rm dir here, so added in project file
+#cant rm dir here, so added cmd in sublime project file
 
 
 
diff --git a/recorder.sublime-project b/recorder.sublime-project
index c5f3498..de7f6b1 100644
--- a/recorder.sublime-project
+++ b/recorder.sublime-project
@@ -16,9 +16,11 @@
 	"build_systems":
 	[
 		{
-			"name": "recorder",
+			"name": "recorder", // default is clean
 			"working_dir": "$project_path/obj",
-			"shell_cmd": "qmake6 && make -j`nproc` && cd ../bin && recorder",
+			"shell_cmd": "make -j`nproc` clean && rmdir -p .cache/clangd/index",
+
+			"env": {"run_args": "-o blubb/bla"}, // pass args to the program
 
 			// COLORS in output - HOW??
 			// "env": {"GCC_COLORS": "error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01"}, //force color for some CLI programs
@@ -39,38 +41,70 @@
 			"variants":
 			[
 				{
-					"name": "rebuild && run",
-					"shell_cmd": "make -j`nproc` clean && qmake6 && make -j`nproc` -B && LD_LIBRARY_PATH=../bin",
+					"name": "gui: build & run",
+					"shell_cmd": "qmake6 recorder-gui.pro && make -j`nproc` && cd ../bin && recorder-gui",
 				},
 				{
-					"name": "rebuild",
-					"shell_cmd": "make -j`nproc` clean && qmake6 && make -j`nproc` -B",
+					"name": "gui: run",
+					"shell_cmd": "cd ../bin && recorder-gui",
 				},
 				{
-					"name": "build",
-					"shell_cmd": "qmake6 && make -j`nproc` ",
+					"name": "gui: build",
+					"shell_cmd": "qmake6 recorder-gui.pro && make -j`nproc` ",
 				},
 				{
-					"name": "run",
-					"shell_cmd": "cd ../bin && recorder",
+					"name": "gui: rebuild",
+					"shell_cmd": "make -j`nproc` clean && qmake6 recorder-gui.pro && make -j`nproc` -B",
 				},
 				{
-					"name": "qmake6",
-					"shell_cmd": "qmake6",
+					"name": "gui: rebuild && run",
+					"shell_cmd": "make -j`nproc` clean && qmake6 recorder-gui.pro && make -j`nproc` -B && LD_LIBRARY_PATH=../bin && recorder-gui",
 				},
 				{
-					"name": "clean",
-					"shell_cmd": "make -j`nproc` clean && rmdir -p .cache/clangd/index",
+					"name": "gui: designer",
+					"shell_cmd": "designer ../src/gui/mainwindow.ui",
 				},
 				{
-					"name": "bear -- create new compile_commands.json",
-					"shell_cmd": "bear -- make -j`nproc` && mv compile_commands.json ..",
+					"name": "-------------------------------",
+					"shell_cmd": "",
+				},
+				{
+					"name": "cmd: build & run",
+					"shell_cmd": "qmake6 recorder-cmd.pro && make -j`nproc` && cd ../bin && recorder-cmd \\$run_args",
+				},
+				{
+					"name": "cmd: run",
+					"shell_cmd": "cd ../bin && recorder-cmd \\$run_args",
+				},
+				{
+					"name": "cmd: build",
+					"shell_cmd": "qmake6 recorder-cmd.pro && make -j`nproc` ",
+				},
+				{
+					"name": "cmd: rebuild",
+					"shell_cmd": "make -j`nproc` clean && qmake6 recorder-cmd.pro && make -j`nproc` -B",
 				},
 				{
-					"name": "designer",
-					"shell_cmd": "designer ../src/mainwindow.ui",
+					"name": "cmd: rebuild && run",
+					"shell_cmd": "make -j`nproc` clean && qmake6 recorder-cmd.pro && make -j`nproc` -B && LD_LIBRARY_PATH=../bin && recorder-cmd \\$run_args",
+				},
+				{
+					"name": "-------------------------------",
+					"shell_cmd": "",
+				},
+				{
+					"name": "bear -- create new compile_commands.json",
+					"shell_cmd": "bear -- make -j`nproc` && mv compile_commands.json ..",
 				},
 				// {
+				// 	"name": "qmake6",
+				// 	"shell_cmd": "qmake6",
+				// },
+				// {
+				// 	"name": "clean",
+				// 	"shell_cmd": "make -j`nproc` clean && rmdir -p .cache/clangd/index",
+				// },
+				// {
 				// 	"name": "distclean ",
 				// 	"shell_cmd": "make -j`nproc` distclean",
 				// },
@@ -80,5 +114,8 @@
 	]
 }
 
+// for more info see:
+// https://www.sublimetext.com/docs/build_systems.html
+
 //build sys @ ~/.config/sublime-text/Packages/User/C++.sublime-build
 
diff --git a/src/cmd/main.cpp b/src/cmd/main.cpp
new file mode 100644
index 0000000..1a9fbf0
--- /dev/null
+++ b/src/cmd/main.cpp
@@ -0,0 +1,100 @@
+#include "../utils.h"
+
+#include <iostream>
+
+#include <QCoreApplication>
+#include <QCommandLineParser>
+#include <QDir>
+
+using Qt::endl;
+using namespace std;
+
+int main(int argc, char *argv[])
+{
+
+    QCoreApplication a(argc, argv);
+    QCoreApplication::setApplicationVersion(getVersionString());
+    QCoreApplication::setApplicationName("recorder (cmd-line)");
+
+    QTextStream qout(stdout); //2d make global to access from anywhere
+    QTextStream qin(stdin);
+    qin.skipWhiteSpace(); // Remove white characters that interrupt
+    QTextStream qerr(stderr);
+
+    // parse arguments
+    QCommandLineParser parser;
+    parser.setApplicationDescription("a commandline app that is meant to be standalone and communicates with the camera");
+    parser.addHelpOption();
+    parser.addVersionOption();
+    parser.addOptions({
+        {{"o", "out-dir"}, "store frames to <out-dir>", "out_dir"},
+        {{"l", "list-cams"}, "list available cams"},
+        {{"e", "export-cam-config"}, "export camera config" "settings.xml"},
+        {{"i", "import-cam-config"}, "import camera config" "settings.xml"},
+        {{"c", "calibrate"}, "calibrate cam"},
+        {{"r", "record"}, "record"},
+        {{"s", "stop"}, "stop recording"},
+        // {{"f", "force"}, "Overwrite existing files."},
+        // {{"n", "dry-run"}, "Do nothing. For testing purposes."},
+        // {{"p", "progress"}, "Show progress during copy."},
+        // {{"v", "verbose"}, "verbose"},
+    });
+
+    parser.process(a);
+
+    const QStringList args = parser.positionalArguments();
+    // source is args.at(0), destination is args.at(1)
+    qDebug() << "args: " << args.join(", ") << "\n";
+
+
+    qout << "Please enter your name: ";
+    qout.flush(); // clears the keyboard buffer
+
+    QString inpu;
+
+    // QString inpu = qin.readLine();
+    qin >> inpu;
+
+    qout << "u typed " << inpu.toLower() << endl;
+
+    return 3;
+
+    cout << "__LINE__" << __LINE__ << endl; //2d make snippet HERE for dbg
+    if( parser.isSet("o") )
+    {
+        QDir out_dir(parser.value("o"));
+        if( !out_dir.exists() )
+        {
+            qDebug() << "out_dir (" << out_dir.absolutePath() << ") doesn't exist. Should I Create it? [y|yes or other]\n";
+            QString yn ="";
+            qin >> yn;
+
+            QStringList yes{"y", "yes", "1", "ja", "j", "oui", "si"};
+            if( yes.contains(yn.toLower())  )
+            {
+                // out_dir.mkpath(out_dir.absolutePath());
+                if( !out_dir.mkdir(".") ){
+                    cerr << "failed to create out_dir '" << out_dir.absolutePath().toStdString()  << "'" << endl;
+                    return 1;
+                }
+            }
+            else
+            {
+                cout << "u pressed no" << endl;
+                return 0;
+            }
+        }
+
+
+
+        qDebug() << "out_dir: " << out_dir << "\n";
+    }
+    // bool force = parser.isSet(forceOption);
+    // QString targetDir = parser.value(targetDirectoryOption);
+
+
+
+    // return a.exec();
+    return 0;
+}
+
diff --git a/src/main.cpp b/src/gui/main.cpp
similarity index 96%
rename from src/main.cpp
rename to src/gui/main.cpp
index 69283e4..4d09e9d 100644
--- a/src/main.cpp
+++ b/src/gui/main.cpp
@@ -2,7 +2,7 @@
 
 #include <QApplication>
 #include <QFile>
-#include <QDir>
+// #include <QDir>
 
 
 void loadStyleSheets(const QString& name, MainWindow& mw)
@@ -21,6 +21,7 @@ void loadStyleSheets(const QString& name, MainWindow& mw)
 
 int main(int argc, char *argv[])
 {
+
     QApplication a(argc, argv);
     MainWindow w;
     loadStyleSheets(QCoreApplication::applicationDirPath()+"/style.qss", w);
diff --git a/src/mainwindow.cpp b/src/gui/mainwindow.cpp
similarity index 93%
rename from src/mainwindow.cpp
rename to src/gui/mainwindow.cpp
index 28504dc..86a5014 100644
--- a/src/mainwindow.cpp
+++ b/src/gui/mainwindow.cpp
@@ -33,11 +33,11 @@ MainWindow::MainWindow(QWidget *parent)
     {
       log( " Started Vimba C++ Api" );
       updateCams();
-  }
-  else
-  {
-    log( " Failed to start Vimba C++ Api" );
-}
+    }
+    else
+    {
+        log( " Failed to start Vimba C++ Api" );
+    }
 }
 
 MainWindow::~MainWindow()
@@ -45,11 +45,11 @@ MainWindow::~MainWindow()
     if ( VmbErrorSuccess == sys.Shutdown() )
     {
       log( " Shutdown Vimba Api" );
-  }
-  else
-  {
-      log( " Failed to Shutdown Vimba Api" );
-  }
+    }
+    else
+    {
+        log( " Failed to Shutdown Vimba Api" );
+    }
   delete ui;
 }
 
@@ -159,8 +159,10 @@ void MainWindow::getCamInfo( const CameraPtr &camera )
     //QTableWidgetItem(const QIcon &icon, const QString &text, int type = Type)
 
     addRow(infos);
-    QStringList sl= {randomString(),randomString(),randomString(),randomString(),randomString(),randomString()};
-    addRow(sl);
+
+    // random rows for mockup testing when no cam is avaiable
+    // QStringList sl= {randomString(),randomString(),randomString(),randomString(),randomString(),randomString()};
+    // addRow(sl);
 }
 
 ///add row to listwidget
diff --git a/src/mainwindow.h b/src/gui/mainwindow.h
similarity index 100%
rename from src/mainwindow.h
rename to src/gui/mainwindow.h
diff --git a/src/mainwindow.ui b/src/gui/mainwindow.ui
similarity index 100%
rename from src/mainwindow.ui
rename to src/gui/mainwindow.ui
diff --git a/src/recorder.cpp b/src/recorder.cpp
index c8b89b2..e60c56f 100644
--- a/src/recorder.cpp
+++ b/src/recorder.cpp
@@ -1,4 +1,4 @@
-#include "VmbCPP.h"
+#include <VmbCPP/VmbCPP.h>
 
 #include <iostream>
 #include <vector>
diff --git a/src/utils.cpp b/src/utils.cpp
index d2b9bb3..890f152 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -2,7 +2,7 @@
 
 #include <random>
 
-#include "mainwindow.h"
+// #include "mainwindow.h"
 #include "VmbCPP/VmbCPP.h"
 #include <QString>
 #include <QDebug>
@@ -10,7 +10,7 @@
 
 const int APP_VERSION_MAJOR = 0;
 const int APP_VERSION_MINOR = 0;
-const int APP_VERSION_PATCH = 4;
+const int APP_VERSION_PATCH = 5;
 
 using namespace VmbCPP;
 
@@ -19,7 +19,7 @@ const QString getVersionString()
     VmbVersionInfo_t version;
     VmbSystem::GetInstance().QueryVersion(version);
 
-    QString s = "### Versions\n";
+    QString s = "\n### Versions\n";
     s += QStringLiteral("# Recorder: %1.%2.%3\n").arg(APP_VERSION_MAJOR).arg(APP_VERSION_MINOR).arg(APP_VERSION_PATCH);
     s += QStringLiteral("#    Vimba: %1.%2.%3\n").arg(version.major).arg(version.minor).arg(version.patch);
     s += QStringLiteral("#       Qt: %1.%2.%3\n").arg(QT_VERSION_MAJOR).arg(QT_VERSION_MINOR).arg(QT_VERSION_PATCH);
-- 
GitLab