From 807fec5d76753063ea523135d7693fc490a7e231 Mon Sep 17 00:00:00 2001
From: tobiglaser <76131623+tobiglaser@users.noreply.github.com>
Date: Fri, 5 Aug 2022 17:09:58 +0200
Subject: [PATCH] Worksheet 4: 3.4 done

---
 CMakeLists.txt               |  4 +++
 src/ControlUI.cpp            | 35 +++++++++++++++++--
 src/ControlUI.h              |  8 +++--
 src/PanelCommandConfig.h     | 14 ++++++++
 src/PanelConfigDefault.cpp   | 10 ++++++
 src/PanelConfigDefault.h     | 16 +++++++++
 src/PanelConfigDirection.cpp | 56 ++++++++++++++++++++++++++++++
 src/PanelConfigDirection.h   | 25 ++++++++++++++
 src/PanelConfigGear.cpp      | 66 ++++++++++++++++++++++++++++++++++++
 src/PanelConfigGear.h        | 26 ++++++++++++++
 src/PanelConfigPause.cpp     | 59 ++++++++++++++++++++++++++++++++
 src/PanelConfigPause.h       | 25 ++++++++++++++
 12 files changed, 339 insertions(+), 5 deletions(-)
 create mode 100644 src/PanelCommandConfig.h
 create mode 100644 src/PanelConfigDefault.cpp
 create mode 100644 src/PanelConfigDefault.h
 create mode 100644 src/PanelConfigDirection.cpp
 create mode 100644 src/PanelConfigDirection.h
 create mode 100644 src/PanelConfigGear.cpp
 create mode 100644 src/PanelConfigGear.h
 create mode 100644 src/PanelConfigPause.cpp
 create mode 100644 src/PanelConfigPause.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7e736ad..e606ce6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,6 +23,10 @@ set(PROJECT_SOURCES
         src/ControlModel.cpp
         src/ControlModelRegistry.cpp
         src/PanelCommandTypes.cpp
+        src/PanelConfigDirection.cpp
+        src/PanelConfigPause.cpp
+        src/PanelConfigGear.cpp
+        src/PanelConfigDefault.cpp
         src/CommandType.cpp
         src/CommandListOWN.cpp
         include/ComHandler.cpp
diff --git a/src/ControlUI.cpp b/src/ControlUI.cpp
index 06eee0d..48be779 100644
--- a/src/ControlUI.cpp
+++ b/src/ControlUI.cpp
@@ -1,10 +1,41 @@
+#include <QSplitter>
+
 #include "ControlUI.h"
 #include "PanelCommandTypes.h"
 
+#include "PanelConfigDirection.h"
+#include "PanelConfigPause.h"
+#include "PanelConfigGear.h"
+#include "PanelConfigDefault.h"
+
+#include <iostream>
+
 ControlUI::ControlUI(QWidget *parent)
     : QMainWindow(parent)
 {
+    ControlModel::getInstance().readCommands("../testCommands.txt");
+
     setWindowTitle("ControlDeveloper");
-    PanelCommandTypes *pct = new PanelCommandTypes(&ControlModel::getInstance());
-    setCentralWidget(pct);
+    QSplitter *splitterH = new QSplitter(Qt::Horizontal);
+    setCentralWidget(splitterH);
+    PanelCommandTypes *pct = new PanelCommandTypes(&ControlModel::getInstance(), this);
+    splitterH->addWidget(pct);
+
+    PanelConfigDirection *pcd = new PanelConfigDirection(this);
+    splitterH->addWidget(pcd);
+    PanelConfigPause *pcp = new PanelConfigPause(this);
+    splitterH->addWidget(pcp);
+    PanelConfigDefault *pcdef = new PanelConfigDefault(this);
+    splitterH->addWidget(pcdef);
+    PanelConfigGear *pcg = new PanelConfigGear(this);
+    splitterH->addWidget(pcg);
+    pcg->update(ControlModel::getInstance().getCommandList().getCommand(1));
+    pcd->update(ControlModel::getInstance().getCommandList().getCommand(2));
+    pcp->update(ControlModel::getInstance().getCommandList().getCommand(3));
+}
+
+void ControlUI::updateTableView()
+{
+    std::cout << "\n\nTable:\n";
+    ControlModel::getInstance().getCommandList().printCommands();
 }
diff --git a/src/ControlUI.h b/src/ControlUI.h
index ec91558..c46ddba 100644
--- a/src/ControlUI.h
+++ b/src/ControlUI.h
@@ -1,7 +1,7 @@
-#ifndef MAINWINDOW_H
-#define MAINWINDOW_H
+#pragma once
 
 #include <QMainWindow>
+#include <QTableView>
 
 class ControlUI : public QMainWindow
 {
@@ -11,5 +11,7 @@ private:
 
 public:
     ControlUI(QWidget *parent = nullptr);
+
+public slots:
+    void updateTableView();
 };
-#endif // MAINWINDOW_H
diff --git a/src/PanelCommandConfig.h b/src/PanelCommandConfig.h
new file mode 100644
index 0000000..62e099b
--- /dev/null
+++ b/src/PanelCommandConfig.h
@@ -0,0 +1,14 @@
+#pragma once
+#include <QWidget>
+#include <memory>
+#include "commandlib.h"
+
+class PanelCommandConfig : public QWidget
+{
+
+protected:
+    std::shared_ptr<ICommand> command;
+
+public:
+    virtual void update(std::shared_ptr<ICommand>) = 0;
+};
diff --git a/src/PanelConfigDefault.cpp b/src/PanelConfigDefault.cpp
new file mode 100644
index 0000000..a820e4e
--- /dev/null
+++ b/src/PanelConfigDefault.cpp
@@ -0,0 +1,10 @@
+#include "PanelConfigDefault.h"
+
+PanelConfigDefault::PanelConfigDefault(ControlUI *_cui)
+{
+    cui = _cui;
+}
+
+void PanelConfigDefault::update(std::shared_ptr<ICommand> icom)
+{
+}
\ No newline at end of file
diff --git a/src/PanelConfigDefault.h b/src/PanelConfigDefault.h
new file mode 100644
index 0000000..5efaa07
--- /dev/null
+++ b/src/PanelConfigDefault.h
@@ -0,0 +1,16 @@
+#pragma once
+#include "PanelCommandConfig.h"
+#include "ControlUI.h"
+
+class PanelConfigDefault : public PanelCommandConfig
+{
+
+Q_OBJECT
+
+private:
+    ControlUI *cui;
+
+public:
+    PanelConfigDefault(ControlUI *_cui);
+    void update(std::shared_ptr<ICommand>);
+};
\ No newline at end of file
diff --git a/src/PanelConfigDirection.cpp b/src/PanelConfigDirection.cpp
new file mode 100644
index 0000000..74def63
--- /dev/null
+++ b/src/PanelConfigDirection.cpp
@@ -0,0 +1,56 @@
+#include "PanelConfigDirection.h"
+#include "Direction.h"
+#include <QLayout>
+#include <QFormLayout>
+#include <QLabel>
+#include <memory>
+
+PanelConfigDirection::PanelConfigDirection(ControlUI *_cui)
+{
+    cui = _cui;
+    setView();
+    setController();
+}
+
+void PanelConfigDirection::setView()
+{
+    QVBoxLayout *vl = new QVBoxLayout();
+    setLayout(vl);
+    QWidget *fw = new QWidget();
+    QFormLayout *fl = new QFormLayout();
+    fw->setLayout(fl);
+    vl->addWidget(fw);
+    bSave = new QPushButton("Save");
+    vl->addWidget(bSave);
+
+    tDegree = new QLineEdit();
+    fl->addRow("Degree:", tDegree);
+    fl->addRow("", new QLabel("-90 to +90 degree"));
+}
+
+void PanelConfigDirection::setController()
+{
+    connect(bSave, SIGNAL(clicked()), this, SLOT(onSaveButton()));
+}
+
+void PanelConfigDirection::update(std::shared_ptr<ICommand> icom)
+{
+    command = icom;
+    std::shared_ptr<Direction> d = dynamic_pointer_cast<Direction>(command);
+    if(d)
+    {
+        int deg = d->getDegree();
+        tDegree->setText(QString::number(deg));
+    }
+}
+
+void PanelConfigDirection::onSaveButton()
+{
+    int deg = tDegree->text().toInt();
+    std::shared_ptr<Direction> d = dynamic_pointer_cast<Direction>(command);
+    if(d)
+    {
+        d->setDegree(deg);
+        cui->updateTableView();
+    }
+}
\ No newline at end of file
diff --git a/src/PanelConfigDirection.h b/src/PanelConfigDirection.h
new file mode 100644
index 0000000..797d06c
--- /dev/null
+++ b/src/PanelConfigDirection.h
@@ -0,0 +1,25 @@
+#pragma once
+#include <QLineEdit>
+#include <QPushButton>
+#include "PanelCommandConfig.h"
+#include "ControlUI.h"
+
+class PanelConfigDirection : public PanelCommandConfig
+{
+
+Q_OBJECT
+
+private:
+    ControlUI *cui;
+    void setView();
+    void setController();
+    QPushButton *bSave;
+    QLineEdit *tDegree;
+
+public:
+    PanelConfigDirection(ControlUI *_cui);
+    void update(std::shared_ptr<ICommand>);
+
+private slots:
+    void onSaveButton();
+};
\ No newline at end of file
diff --git a/src/PanelConfigGear.cpp b/src/PanelConfigGear.cpp
new file mode 100644
index 0000000..b0b11b7
--- /dev/null
+++ b/src/PanelConfigGear.cpp
@@ -0,0 +1,66 @@
+#include "PanelConfigGear.h"
+#include "Gear.h"
+#include <QLayout>
+#include <QFormLayout>
+#include <QLabel>
+#include <memory>
+
+PanelConfigGear::PanelConfigGear(ControlUI *_cui)
+{
+    cui = _cui;
+    setView();
+    setController();
+}
+
+void PanelConfigGear::setView()
+{
+    QVBoxLayout *vl = new QVBoxLayout();
+    setLayout(vl);
+    QWidget *fw = new QWidget();
+    QFormLayout *fl = new QFormLayout();
+    fw->setLayout(fl);
+    vl->addWidget(fw);
+    bSave = new QPushButton("Save");
+    vl->addWidget(bSave);
+
+    tSpeed = new QLineEdit();
+    fl->addRow("Speed:", tSpeed);
+    fl->addRow("", new QLabel("-100 to +100 %"));
+    tDuration = new QLineEdit();
+    fl->addRow("Duration:", tDuration);
+    fl->addRow("", new QLabel("max 8s"));
+}
+
+void PanelConfigGear::setController()
+{
+    connect(bSave, SIGNAL(clicked()), this, SLOT(onSaveButton()));
+}
+
+void PanelConfigGear::update(std::shared_ptr<ICommand> icom)
+{
+    command = icom;
+    std::shared_ptr<Gear> g = dynamic_pointer_cast<Gear>(command);
+    if(g)
+    {
+        int speed = g->getSpeed();
+        tSpeed->setText(QString::number(speed));
+        double dur = g->getDuration();
+        tDuration->setText(QString::number(dur));
+    }
+}
+
+void PanelConfigGear::onSaveButton()
+{
+    int speed = tSpeed->text().toInt();
+    double dur = tDuration->text().toDouble();
+    std::shared_ptr<Gear> g = dynamic_pointer_cast<Gear>(command);
+    if(g)
+    {
+        if (dur > 0 && dur < 8)
+        {
+            g->setDuration(dur);
+            g->setSpeed(speed);
+            cui->updateTableView();
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/PanelConfigGear.h b/src/PanelConfigGear.h
new file mode 100644
index 0000000..e99ea08
--- /dev/null
+++ b/src/PanelConfigGear.h
@@ -0,0 +1,26 @@
+#pragma once
+#include <QLineEdit>
+#include <QPushButton>
+#include "PanelCommandConfig.h"
+#include "ControlUI.h"
+
+class PanelConfigGear : public PanelCommandConfig
+{
+
+Q_OBJECT
+
+private:
+    ControlUI *cui;
+    void setView();
+    void setController();
+    QPushButton *bSave;
+    QLineEdit *tSpeed;
+    QLineEdit *tDuration;
+
+public:
+    PanelConfigGear(ControlUI *_cui);
+    void update(std::shared_ptr<ICommand>);
+
+private slots:
+    void onSaveButton();
+};
\ No newline at end of file
diff --git a/src/PanelConfigPause.cpp b/src/PanelConfigPause.cpp
new file mode 100644
index 0000000..e0d8b3c
--- /dev/null
+++ b/src/PanelConfigPause.cpp
@@ -0,0 +1,59 @@
+#include "PanelConfigPause.h"
+#include "Pause.h"
+#include <QLayout>
+#include <QFormLayout>
+#include <QLabel>
+#include <memory>
+
+PanelConfigPause::PanelConfigPause(ControlUI *_cui)
+{
+    cui = _cui;
+    setView();
+    setController();
+}
+
+void PanelConfigPause::setView()
+{
+    QVBoxLayout *vl = new QVBoxLayout();
+    setLayout(vl);
+    QWidget *fw = new QWidget();
+    QFormLayout *fl = new QFormLayout();
+    fw->setLayout(fl);
+    vl->addWidget(fw);
+    bSave = new QPushButton("Save");
+    vl->addWidget(bSave);
+
+    tDuration = new QLineEdit();
+    fl->addRow("Duration:", tDuration);
+    fl->addRow("", new QLabel("max 8s"));
+}
+
+void PanelConfigPause::setController()
+{
+    connect(bSave, SIGNAL(clicked()), this, SLOT(onSaveButton()));
+}
+
+void PanelConfigPause::update(std::shared_ptr<ICommand> icom)
+{
+    command = icom;
+    std::shared_ptr<Pause> p = dynamic_pointer_cast<Pause>(command);
+    if(p)
+    {
+        double dur = p->getDuration();
+        tDuration->setText(QString::number(dur));
+    }
+}
+
+void PanelConfigPause::onSaveButton()
+{
+    double dur = tDuration->text().toDouble();
+    std::shared_ptr<Pause> p = dynamic_pointer_cast<Pause>(command);
+    if(p)
+    {
+        if (dur > 0 && dur < 8)
+        {
+            p->setDuration(dur);
+            cui->updateTableView();
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/PanelConfigPause.h b/src/PanelConfigPause.h
new file mode 100644
index 0000000..7210acf
--- /dev/null
+++ b/src/PanelConfigPause.h
@@ -0,0 +1,25 @@
+#pragma once
+#include <QLineEdit>
+#include <QPushButton>
+#include "PanelCommandConfig.h"
+#include "ControlUI.h"
+
+class PanelConfigPause : public PanelCommandConfig
+{
+
+Q_OBJECT
+
+private:
+    ControlUI *cui;
+    void setView();
+    void setController();
+    QPushButton *bSave;
+    QLineEdit *tDuration;
+
+public:
+    PanelConfigPause(ControlUI *_cui);
+    void update(std::shared_ptr<ICommand>);
+
+private slots:
+    void onSaveButton();
+};
\ No newline at end of file
-- 
GitLab