aboutsummaryrefslogtreecommitdiff
path: root/src/main_window.cc
diff options
context:
space:
mode:
authorccolin2020-12-22 13:15:23 +0100
committerccolin2020-12-22 13:15:23 +0100
commit7fbe0814d52ba861a02b0560d4e6872845ef241e (patch)
tree1a8f6fd67b5d1606a58c0df00fbab285f1206d36 /src/main_window.cc
initial commit
Diffstat (limited to 'src/main_window.cc')
-rw-r--r--src/main_window.cc73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/main_window.cc b/src/main_window.cc
new file mode 100644
index 0000000..5e58660
--- /dev/null
+++ b/src/main_window.cc
@@ -0,0 +1,73 @@
+#include "main_window.hh"
+
+#include <QApplication>
+#include <QFile>
+#include <QFileDialog>
+#include <QJsonDocument>
+#include <QStyle>
+
+
+MainWindow::MainWindow(QWidget *parent) {
+ (void) parent;
+ connect(&glw, &OpenGLWidget::initialized,
+ this, &MainWindow::onOpenGLWidgetInitialized);
+ // connect(&timer, &QTimer::timeout, this, &MainWindow::step);
+ setCentralWidget(&glw);
+ addToolBar(Qt::TopToolBarArea, &top_tb);
+ open_action = top_tb.addAction("Ouvrir…", [&]() {
+ open(QFileDialog::getOpenFileName(this, "Ouvrir un fichier JSON"));
+ });
+ open_action->setEnabled(false);
+ addToolBar(Qt::BottomToolBarArea, &bottom_tb);
+ playpause_action = bottom_tb.addAction(style()->standardIcon(QStyle::SP_MediaPlay), "",
+ this, &MainWindow::play);
+ playpause_action->setEnabled(false);
+ slider = new QSlider(Qt::Horizontal);
+ bottom_tb.addWidget(slider);
+ slider->setEnabled(false);
+}
+
+
+void MainWindow::play() {
+ dc->play();
+ playpause_action->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
+ disconnect(playpause_action, &QAction::triggered, nullptr, nullptr);
+ connect(playpause_action, &QAction::triggered, this, &MainWindow::pause);
+}
+
+
+void MainWindow::pause() {
+ dc->pause();
+ playpause_action->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
+ disconnect(playpause_action, &QAction::triggered, nullptr, nullptr);
+ connect(playpause_action, &QAction::triggered, this, &MainWindow::play);
+}
+
+
+void MainWindow::open(const QString &path) {
+ QFile file(path);
+ if (!file.open(QIODevice::ReadOnly)) {
+ qWarning("Impossible d’ouvrir le fichier.");
+ return;
+ }
+ QByteArray data = file.readAll();
+ QJsonDocument json_doc = QJsonDocument::fromJson(data);
+ if (dc) delete dc;
+ dc = new DroneController(json_doc.object());
+ playpause_action->setEnabled(true);
+ slider->setMinimum(0);
+ slider->setMaximum(dc->getDuration());
+ connect(slider, &QSlider::sliderPressed, dc, &DroneController::suspend);
+ connect(slider, &QSlider::sliderReleased, dc, &DroneController::resume);
+ connect(slider, &QSlider::valueChanged, dc, &DroneController::seek);
+ connect(dc, &DroneController::frameChanged, slider, &QSlider::setValue);
+ slider->setEnabled(true);
+}
+
+
+void MainWindow::onOpenGLWidgetInitialized() {
+ if (qApp->arguments().size() == 2) {
+ open(qApp->arguments().at(1));
+ }
+ open_action->setEnabled(true);
+}