From ee76b5a07c553afcce7362e2db9c5e99e89ba994 Mon Sep 17 00:00:00 2001 From: bmanga Date: Mon, 15 Apr 2019 23:31:21 +0100 Subject: [PATCH] gui: added working levels --- tools/QtGAITGui/CMakeLists.txt | 2 - tools/QtGAITGui/CircleWidget.cpp | 6 +- tools/QtGAITGui/CircleWidget.hpp | 7 +- tools/QtGAITGui/FinalWindow.hpp | 3 +- tools/QtGAITGui/Level_weight_shifting.cpp | 103 ------------------- tools/QtGAITGui/Level_weight_shifting.hpp | 120 ++++++++++++++++++---- tools/QtGAITGui/MainWindow.cpp | 4 +- tools/QtGAITGui/TargetBase.h | 8 ++ tools/QtGAITGui/Training_mode.cpp | 15 --- tools/QtGAITGui/Training_mode.hpp | 12 ++- tools/QtGAITGui/progressbar.cpp | 6 +- tools/QtGAITGui/progressbar.hpp | 9 +- 12 files changed, 141 insertions(+), 154 deletions(-) delete mode 100644 tools/QtGAITGui/Level_weight_shifting.cpp create mode 100644 tools/QtGAITGui/TargetBase.h delete mode 100644 tools/QtGAITGui/Training_mode.cpp diff --git a/tools/QtGAITGui/CMakeLists.txt b/tools/QtGAITGui/CMakeLists.txt index 2183df9..0f08784 100644 --- a/tools/QtGAITGui/CMakeLists.txt +++ b/tools/QtGAITGui/CMakeLists.txt @@ -19,7 +19,6 @@ find_package(Qt5Charts CONFIG REQUIRED) add_executable(qt-gait-gui main.cpp WindowBase.hpp - Training_mode.cpp Training_mode.hpp CircleWidget.cpp CircleWidget.hpp @@ -27,7 +26,6 @@ add_executable(qt-gait-gui progressbar.hpp intro_window.cpp intro_window.hpp - Level_weight_shifting.cpp Level_weight_shifting.hpp MainWindow.cpp MainWindow.hpp diff --git a/tools/QtGAITGui/CircleWidget.cpp b/tools/QtGAITGui/CircleWidget.cpp index 9905ff2..1661155 100644 --- a/tools/QtGAITGui/CircleWidget.cpp +++ b/tools/QtGAITGui/CircleWidget.cpp @@ -26,10 +26,10 @@ float CircleWidget::getDiameter() return this->diameter; } -void CircleWidget::setTarget(float lower, float upper) +void CircleWidget::setTarget(Target t) { - m_targetLow = lower; - m_targetHigh = upper; + m_targetLow = t.low; + m_targetHigh = t.high; } void CircleWidget::checkStatus() diff --git a/tools/QtGAITGui/CircleWidget.hpp b/tools/QtGAITGui/CircleWidget.hpp index d3e02dd..ec4d303 100644 --- a/tools/QtGAITGui/CircleWidget.hpp +++ b/tools/QtGAITGui/CircleWidget.hpp @@ -5,6 +5,7 @@ #include #include "MainWindow.hpp" +#include "TargetBase.h" #include "common_types.h" class CircleWidget : public QWidget { @@ -28,8 +29,10 @@ class CircleWidget : public QWidget { public slots: void setDiameter(float d); float getDiameter(); - void setTarget(float lower, float upper); // NEW - void checkStatus(); // NEW + + void setTarget(Target t); + void checkStatus(); + void onNewFSRData(fsr_data p) { setDiameter(p.toe + p.heel); } private: diff --git a/tools/QtGAITGui/FinalWindow.hpp b/tools/QtGAITGui/FinalWindow.hpp index 7ef4729..e0573cf 100644 --- a/tools/QtGAITGui/FinalWindow.hpp +++ b/tools/QtGAITGui/FinalWindow.hpp @@ -2,8 +2,9 @@ #define FinalWindow_hpp #include #include +#include "WindowBase.hpp" -class FinalWindow : public QWidget { +class FinalWindow : public WindowBase { public: FinalWindow(); ~FinalWindow(); diff --git a/tools/QtGAITGui/Level_weight_shifting.cpp b/tools/QtGAITGui/Level_weight_shifting.cpp deleted file mode 100644 index 9f93ffb..0000000 --- a/tools/QtGAITGui/Level_weight_shifting.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include "Level_weight_shifting.hpp" -#include -#include -#include -#include -#include -#include -#include -#include -#include "progressbar.hpp" - -LevelWS::LevelWS(ProgressBar *left, ProgressBar *right) - : m_leftBar(left), m_rightBar(right) -{ - connect(left, &ProgressBar::onTarget, this, &LevelWS::onLeftFSRChanged); - connect(right, &ProgressBar::onTarget, this, &LevelWS::onRightFSRChanged); - - m_timer.setSingleShot(true); - - connect(&m_timer, SIGNAL(timeout()), this, SIGNAL(levelComplete())); - connect(this, &LevelWS::levelComplete, []() { qDebug() << "success"; }); -} - -void LevelWS::onLeftFSRChanged(bool onTarget) -{ - m_leftOnTgt = onTarget; - checkTgtStatus(); -} - -void LevelWS::onRightFSRChanged(bool onTarget) -{ - m_rightOnTgt = onTarget; - checkTgtStatus(); -} - -void LevelWS::setLvlRequirements(Target left, - Target right, - std::chrono::seconds timeRequirement) -{ - m_leftBar->setTarget(left.low, left.high); - m_rightBar->setTarget(right.low, right.high); - m_timeRequirement = timeRequirement; -} - -void LevelWS::checkTgtStatus() -{ - if (m_rightOnTgt && m_leftOnTgt) { - m_timer.start(m_timeRequirement); - } - else { - m_timer.stop(); - } -} - -void LevelWS::loadJsonFile(QString file) -{ - QFile dataFile(file); - - auto jsonDoc = QJsonDocument::fromJson(dataFile.readAll()); - - levelsRoot = jsonDoc.object(); -} - -void LevelWS::start() -{ - currentLevel = levelsRoot.keys()[0]; - - runLevel(); -} -void LevelWS::runLevel() -{ - auto levelData = levelsRoot[currentLevel].toObject(); - - long duration = levelData["Time"].toDouble(); - - auto leftArray = levelData["Left"].toArray(); - auto rightArray = levelData["RIght"].toArray(); - - setLvlRequirements({leftArray[0].toDouble(), leftArray[1].toDouble()}, , - {rightArray[0].toDouble(), rightArray[1].toDouble()}, - std::chrono::seconds(duration)); - - auto seriesNames = root[timepoints.first()].toObject().keys(); - - for (auto timepointStr : timepoints) { - auto timepoint = timepointStr.toDouble(); - auto tpData = root[timepointStr].toObject(); - for (auto seriesStr : seriesNames) { - auto points = tpData[seriesStr].toArray(); - auto *series_x = - m_chart->getSeries((seriesStr + "-X").toStdString().c_str()); - series_x->append({timepoint, points[0].toDouble()}); - - auto *series_y = - m_chart->getSeries((seriesStr + "-Y").toStdString().c_str()); - series_y->append({timepoint, points[1].toDouble()}); - - auto *series_z = - m_chart->getSeries((seriesStr + "-Z").toStdString().c_str()); - series_z->append({timepoint, points[2].toDouble()}); - } - } -} diff --git a/tools/QtGAITGui/Level_weight_shifting.hpp b/tools/QtGAITGui/Level_weight_shifting.hpp index fbf8b00..373c116 100644 --- a/tools/QtGAITGui/Level_weight_shifting.hpp +++ b/tools/QtGAITGui/Level_weight_shifting.hpp @@ -2,47 +2,129 @@ #define Level_weight_shifting_hpp #include +#include +#include #include +#include #include #include #include +#include "TargetBase.h" -class ProgressBar; - -struct Target { - float low; - float high; -}; +template +class TrainingMode; +template class LevelWS : public QObject { - Q_OBJECT public: - LevelWS(ProgressBar *left, ProgressBar *right); + LevelWS(TrainingMode *parent, + TargetT *left, + TargetT *right, + QLabel *label) + : m_parent(parent), m_leftBar(left), m_rightBar(right), m_label(label) + { + connect(left, &TargetT::onTarget, this, &LevelWS::onLeftFSRChanged); + connect(right, &TargetT::onTarget, this, &LevelWS::onRightFSRChanged); + + m_timer.setSingleShot(true); + + connect(&m_timer, &QTimer::timeout, this, &LevelWS::levelComplete); + } + void setLvlRequirements(Target left, Target right, - std::chrono::seconds timeRequirement); - void loadJsonFile(QString file); + std::chrono::seconds timeRequirement) + { + m_leftBar->setTarget(left); + m_rightBar->setTarget(right); + m_timeRequirement = timeRequirement; + } + + void loadJsonFile(QString file) + { + QFile dataFile(file); + + if (!dataFile.open(QFile::ReadOnly)) { + QMessageBox().critical(0, "File Error", "Data file does not exist"); + return; + } + + auto jsonDoc = QJsonDocument::fromJson(dataFile.readAll()); + + levelsRoot = jsonDoc.object(); + } + public slots: - void onLeftFSRChanged(bool onTarget); - void onRightFSRChanged(bool onTarget); + void onLeftFSRChanged(bool onTarget) + { + m_leftOnTgt = onTarget; + checkTgtStatus(); + } - void start(); + void onRightFSRChanged(bool onTarget) + { + m_rightOnTgt = onTarget; + checkTgtStatus(); + } - void runLevel(); + void levelComplete() + { + auto levelIdx = levelsRoot.keys().indexOf(currentLevel); + if (levelIdx == levelsRoot.keys().size() - 1) { + // This was the last level + m_parent->emitWindowDone(); + return; + } + currentLevel = levelsRoot.keys()[levelIdx + 1]; + runLevel(); + } - private: - void levelComplete(); + void start() + { + currentLevel = levelsRoot.keys()[0]; + + runLevel(); + } + + void runLevel() + { + m_label->setText(currentLevel); + auto levelData = levelsRoot[currentLevel].toObject(); + + long duration = levelData["Time"].toDouble(); + + auto leftArray = levelData["Left"].toArray(); + auto rightArray = levelData["Right"].toArray(); + + setLvlRequirements({leftArray[0].toDouble(), leftArray[1].toDouble()}, + {rightArray[0].toDouble(), rightArray[1].toDouble()}, + std::chrono::seconds(duration)); + + m_timer.start(m_timeRequirement); + } private: - void checkTgtStatus(); + void checkTgtStatus() + { + if (m_rightOnTgt && m_leftOnTgt) { + m_timer.start(m_timeRequirement); + } + else { + m_timer.stop(); + } + } private: QJsonObject levelsRoot; QString currentLevel; QTimer m_timer; - ProgressBar *m_leftBar; - ProgressBar *m_rightBar; + TargetT *m_leftBar; + TargetT *m_rightBar; + + QLabel *m_label; + + TrainingMode *m_parent; Target m_tgtLeft; Target m_tgtRight; diff --git a/tools/QtGAITGui/MainWindow.cpp b/tools/QtGAITGui/MainWindow.cpp index b9389af..e38fe44 100644 --- a/tools/QtGAITGui/MainWindow.cpp +++ b/tools/QtGAITGui/MainWindow.cpp @@ -52,7 +52,9 @@ void MainWindow::onWindowDone(WindowKind wk, int extra) this); ; } - + break; + case WindowKind::Training: + nextWindow = new FinalWindow(); break; } connect(nextWindow, &WindowBase::windowDone, this, &MainWindow::onWindowDone); diff --git a/tools/QtGAITGui/TargetBase.h b/tools/QtGAITGui/TargetBase.h new file mode 100644 index 0000000..f29b919 --- /dev/null +++ b/tools/QtGAITGui/TargetBase.h @@ -0,0 +1,8 @@ +#ifndef RTEP_TEAM18_TARGETBASE_H +#define RTEP_TEAM18_TARGETBASE_H + +struct Target { + float low, high; +}; + +#endif // RTEP_TEAM18_TARGETBASE_H diff --git a/tools/QtGAITGui/Training_mode.cpp b/tools/QtGAITGui/Training_mode.cpp deleted file mode 100644 index 60fd1d8..0000000 --- a/tools/QtGAITGui/Training_mode.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "Training_mode.hpp" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "CircleWidget.hpp" -#include "Level_weight_shifting.hpp" -#include "progressbar.hpp" diff --git a/tools/QtGAITGui/Training_mode.hpp b/tools/QtGAITGui/Training_mode.hpp index 45d0880..73d8904 100644 --- a/tools/QtGAITGui/Training_mode.hpp +++ b/tools/QtGAITGui/Training_mode.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -13,6 +14,7 @@ #include #include #include +#include "Level_weight_shifting.hpp" #include "WindowBase.hpp" #include "progressbar.hpp" #include "telemetry/client.h" @@ -27,7 +29,8 @@ class TrainingMode : public WindowBase { : widget_r(new WidgetT()), widget_l(new WidgetT()), level_label(new QLabel()), - instruction_label(new QLabel(instructions)) + instruction_label(new QLabel(instructions)), + level_manager(this, widget_l, widget_r, level_label) { QPalette pal = palette(); @@ -73,8 +76,13 @@ class TrainingMode : public WindowBase { widget_l, &WidgetT::onNewFSRData); connect(static_cast(parent), &MainWindow::newFSRDataR, widget_r, &WidgetT::onNewFSRData); + + level_manager.loadJsonFile("levels.json"); + level_manager.start(); } + void emitWindowDone() { emit windowDone(WindowKind::Training, 0); } + private: WidgetT *widget_r; WidgetT *widget_l; @@ -84,6 +92,8 @@ class TrainingMode : public WindowBase { QLabel *level_label; QLabel *instruction_label; + + LevelWS level_manager; }; #endif /* Training_mode_hpp */ diff --git a/tools/QtGAITGui/progressbar.cpp b/tools/QtGAITGui/progressbar.cpp index b2585b9..c0ef40f 100644 --- a/tools/QtGAITGui/progressbar.cpp +++ b/tools/QtGAITGui/progressbar.cpp @@ -26,10 +26,10 @@ ProgressBar::ProgressBar(QWidget *parent) ProgressBar::~ProgressBar() {} -void ProgressBar::setTarget(float lower, float upper) +void ProgressBar::setTarget(Target t) { - m_targetLow = lower; - m_targetHigh = upper; + m_targetLow = t.low; + m_targetHigh = t.high; } void ProgressBar::checkStatus() diff --git a/tools/QtGAITGui/progressbar.hpp b/tools/QtGAITGui/progressbar.hpp index d231d6c..f85c211 100644 --- a/tools/QtGAITGui/progressbar.hpp +++ b/tools/QtGAITGui/progressbar.hpp @@ -11,6 +11,7 @@ * Widget which displays a the current fill state of the Engine's internal * buffer, and the current play/record position within that buffer. */ + class ProgressBar : public QProgressBar { Q_OBJECT @@ -34,7 +35,7 @@ class ProgressBar : public QProgressBar { void recordPositionChanged(float recordPosition); void playPositionChanged(float playPosition); void windowChanged(float position, float length); - void setTarget(float lower, float upper); + void setTarget(Target t); void checkStatus(); void onNewFSRData(fsr_data packet) @@ -42,9 +43,6 @@ class ProgressBar : public QProgressBar { playPositionChanged(packet.toe + packet.heel); } - signals: - void onTarget(bool); - private: bool isPositionOnTarget(float val) const; @@ -58,6 +56,9 @@ class ProgressBar : public QProgressBar { float max; bool m_prevOnTarget = false; + + signals: + void onTarget(bool); }; #endif /* progressbar_hpp */