]> git.sesse.net Git - nageru/blobdiff - mainwindow.cpp
Move the audio level logic into MainWindow, where it has a much better home than...
[nageru] / mainwindow.cpp
index 65db8cf96bc1e17e0138515e7cd32ba4bc71083c..4dc06084a0e93bd51ef8e065607affd347bb6ffd 100644 (file)
@@ -1,10 +1,11 @@
 #include "mainwindow.h"
-#include "window.h"
 #include <thread>
 #include <vector>
 #include <string>
 #include <QSignalMapper>
 #include <QMetaType>
+#include <QShortcut>
+#include <QResizeEvent>
 
 #include "context.h"
 #include "mixer.h"
@@ -15,9 +16,12 @@ using namespace std;
 
 Q_DECLARE_METATYPE(std::vector<std::string>);
 
+MainWindow *global_mainwindow = nullptr;
+
 MainWindow::MainWindow()
+       : ui(new Ui::MainWindow)
 {
-       Ui::MainWindow *ui = new Ui::MainWindow;
+       global_mainwindow = this;
        ui->setupUi(this);
 
        ui->me_live->set_output(Mixer::OUTPUT_LIVE);
@@ -37,6 +41,23 @@ MainWindow::MainWindow()
                connect(ui->preview1, SIGNAL(clicked()), mapper, SLOT(map()));
                connect(ui->preview2, SIGNAL(clicked()), mapper, SLOT(map()));
                connect(ui->preview3, SIGNAL(clicked()), mapper, SLOT(map()));
+
+               connect(mapper, SIGNAL(mapped(int)), this, SLOT(channel_clicked(int)));
+       }
+
+       // Hook up the preview keyboard keys.
+       {
+               QSignalMapper *mapper = new QSignalMapper(this);
+               QShortcut *shortcut1 = new QShortcut(QKeySequence(Qt::Key_1), this);
+               connect(shortcut1, SIGNAL(activated()), mapper, SLOT(map()));
+               QShortcut *shortcut2 = new QShortcut(QKeySequence(Qt::Key_2), this);
+               connect(shortcut2, SIGNAL(activated()), mapper, SLOT(map()));
+               QShortcut *shortcut3 = new QShortcut(QKeySequence(Qt::Key_3), this);
+               connect(shortcut3, SIGNAL(activated()), mapper, SLOT(map()));
+               mapper->setMapping(shortcut1, 0),
+               mapper->setMapping(shortcut2, 1);
+               mapper->setMapping(shortcut3, 2);
+
                connect(mapper, SIGNAL(mapped(int)), this, SLOT(channel_clicked(int)));
        }
 
@@ -62,6 +83,59 @@ MainWindow::MainWindow()
                this, SLOT(set_transition_names(std::vector<std::string>)));
 }
 
+void MainWindow::resizeEvent(QResizeEvent* event)
+{
+       QMainWindow::resizeEvent(event);
+
+       // Ask for a relayout, but only after the event loop is done doing relayout
+       // on everything else.
+       QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
+}
+
+void MainWindow::mixer_created(Mixer *mixer)
+{
+       mixer->set_audio_level_callback([this](float level_lufs, float peak_db){
+               ui->vu_meter->set_level(level_lufs);
+
+               char buf[256];
+               snprintf(buf, sizeof(buf), "%.1f", peak_db);
+               ui->peak_display->setText(buf);
+       });
+}
+
+void MainWindow::relayout()
+{
+       int width = size().width();
+       int height = size().height();
+
+       // Allocate the height; the most important part is to keep the main displays
+       // at 16:9 if at all possible.
+       double me_width = ui->me_preview->width();
+       double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height();
+
+       // TODO: Scale the widths when we need to do this.
+       if (me_height / double(height) > 0.8) {
+               me_height = height * 0.8;
+       }
+
+       // The previews will be constrained by the remaining height, and the width.
+       // FIXME: spacing?
+       double preview_height = std::min(height - me_height, (width / 4.0) * 9.0 / 16.0);
+
+       ui->vertical_layout->setStretch(0, lrintf(me_height));
+       ui->vertical_layout->setStretch(1, std::max<int>(1, lrintf(height - me_height - preview_height)));
+       ui->vertical_layout->setStretch(2, lrintf(preview_height));
+
+       // Set the widths for the previews.
+       double preview_width = preview_height * 16.0 / 9.0;  // FIXME: spacing?
+
+       ui->preview_displays->setStretch(0, lrintf(preview_width));
+       ui->preview_displays->setStretch(1, lrintf(preview_width));
+       ui->preview_displays->setStretch(2, lrintf(preview_width));
+       ui->preview_displays->setStretch(3, lrintf(preview_width));
+       ui->preview_displays->setStretch(4, lrintf(width - 4.0 * preview_width));
+}
+
 void MainWindow::set_transition_names(vector<string> transition_names)
 {
        if (transition_names.size() < 1) {