]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Switch to typesafe signals and slots, even though the syntax with QSignalMapper is...
[nageru] / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include <math.h>
4 #include <stdio.h>
5 #include <algorithm>
6 #include <string>
7 #include <vector>
8 #include <QBoxLayout>
9 #include <QKeySequence>
10 #include <QLabel>
11 #include <QMetaType>
12 #include <QPushButton>
13 #include <QResizeEvent>
14 #include <QShortcut>
15 #include <QSignalMapper>
16 #include <QSize>
17 #include <QString>
18
19 #include "glwidget.h"
20 #include "lrameter.h"
21 #include "mixer.h"
22 #include "ui_display.h"
23 #include "ui_mainwindow.h"
24 #include "vumeter.h"
25
26 class QResizeEvent;
27
28 using namespace std;
29
30 Q_DECLARE_METATYPE(std::vector<std::string>);
31
32 MainWindow *global_mainwindow = nullptr;
33
34 MainWindow::MainWindow()
35         : ui(new Ui::MainWindow)
36 {
37         global_mainwindow = this;
38         ui->setupUi(this);
39
40         ui->me_live->set_output(Mixer::OUTPUT_LIVE);
41         ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
42
43         // Hook up the transition buttons.
44         // TODO: Make them dynamic.
45         {
46                 QSignalMapper *mapper = new QSignalMapper(this);
47                 mapper->setMapping(ui->transition_btn1, 0),
48                 mapper->setMapping(ui->transition_btn2, 1);
49                 mapper->setMapping(ui->transition_btn3, 2);
50                 connect(ui->transition_btn1, &QPushButton::clicked, mapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
51                 connect(ui->transition_btn2, &QPushButton::clicked, mapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
52                 connect(ui->transition_btn3, &QPushButton::clicked, mapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
53                 connect(mapper, static_cast<void (QSignalMapper::*)(int)>(&QSignalMapper::mapped), this, &MainWindow::transition_clicked);
54         }
55
56         // Aiee...
57         transition_btn1 = ui->transition_btn1;
58         transition_btn2 = ui->transition_btn2;
59         transition_btn3 = ui->transition_btn3;
60         qRegisterMetaType<std::vector<std::string>>("std::vector<std::string>");
61         connect(ui->me_preview, &GLWidget::transition_names_updated, this, &MainWindow::set_transition_names);
62 }
63
64 void MainWindow::resizeEvent(QResizeEvent* event)
65 {
66         QMainWindow::resizeEvent(event);
67
68         // Ask for a relayout, but only after the event loop is done doing relayout
69         // on everything else.
70         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
71 }
72
73 void MainWindow::mixer_created(Mixer *mixer)
74 {
75         // Make the previews.
76         unsigned num_previews = mixer->get_num_channels();
77         QSignalMapper *mapper = new QSignalMapper(this);
78
79         for (unsigned i = 0; i < num_previews; ++i) {
80                 Mixer::Output output = Mixer::Output(Mixer::OUTPUT_INPUT0 + i);
81
82                 QWidget *preview = new QWidget(this);
83                 Ui::Display *ui_display = new Ui::Display;
84                 ui_display->setupUi(preview);
85                 ui_display->label->setText(mixer->get_channel_name(output).c_str());
86                 ui_display->wb_button->setVisible(mixer->get_supports_set_wb(output));
87                 ui_display->display->set_output(output);
88                 ui->preview_displays->insertWidget(previews.size(), preview, 1);
89                 previews.push_back(ui_display);
90
91                 // Hook up the click.
92                 mapper->setMapping(ui_display->display, i);
93                 connect(ui_display->display, &GLWidget::clicked, mapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
94
95                 // Hook up the keyboard key.
96                 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
97                 mapper->setMapping(shortcut, i);
98                 connect(shortcut, &QShortcut::activated, mapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
99         }
100
101         connect(mapper, static_cast<void (QSignalMapper::*)(int)>(&QSignalMapper::mapped), this, &MainWindow::channel_clicked);
102
103         mixer->set_audio_level_callback([this](float level_lufs, float peak_db, float global_level_lufs, float range_low_lufs, float range_high_lufs){
104                 ui->vu_meter->set_level(level_lufs);
105                 ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
106
107                 char buf[256];
108                 snprintf(buf, sizeof(buf), "%.1f", peak_db);
109                 ui->peak_display->setText(buf);
110                 if (peak_db > -0.1f) {  // -0.1 dBFS is EBU peak limit.
111                         ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
112                 } else {
113                         ui->peak_display->setStyleSheet("");
114                 }
115         });
116 }
117
118 void MainWindow::relayout()
119 {
120         int width = size().width();
121         int height = size().height();
122
123         // Allocate the height; the most important part is to keep the main displays
124         // at 16:9 if at all possible.
125         double me_width = ui->me_preview->width();
126         double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height();
127
128         // TODO: Scale the widths when we need to do this.
129         if (me_height / double(height) > 0.8) {
130                 me_height = height * 0.8;
131         }
132
133         // The previews will be constrained by the remaining height, and the width.
134         // FIXME: spacing?
135         double preview_label_height = previews[0]->label->height();
136         double preview_height = std::min(height - me_height - preview_label_height, (width / double(previews.size())) * 9.0 / 16.0);
137
138         ui->vertical_layout->setStretch(0, lrintf(me_height));
139         ui->vertical_layout->setStretch(1, std::max<int>(1, lrintf(height - me_height - preview_height)));
140         ui->vertical_layout->setStretch(2, lrintf(preview_height + preview_label_height));
141
142         // Set the widths for the previews.
143         double preview_width = preview_height * 16.0 / 9.0;  // FIXME: spacing?
144
145         for (unsigned i = 0; i < previews.size(); ++i) {
146                 ui->preview_displays->setStretch(i, lrintf(preview_width));
147         }
148
149         // The spacer.
150         ui->preview_displays->setStretch(previews.size(), lrintf(width - previews.size() * preview_width));
151 }
152
153 void MainWindow::set_transition_names(vector<string> transition_names)
154 {
155         if (transition_names.size() < 1) {
156                 transition_btn1->setText(QString(""));
157         } else {
158                 transition_btn1->setText(QString::fromStdString(transition_names[0]));
159         }
160         if (transition_names.size() < 2) {
161                 transition_btn2->setText(QString(""));
162         } else {
163                 transition_btn2->setText(QString::fromStdString(transition_names[1]));
164         }
165         if (transition_names.size() < 3) {
166                 transition_btn3->setText(QString(""));
167         } else {
168                 transition_btn3->setText(QString::fromStdString(transition_names[2]));
169         }
170 }
171
172 void MainWindow::transition_clicked(int transition_number)
173 {
174         global_mixer->transition_clicked(transition_number);
175 }
176
177 void MainWindow::channel_clicked(int channel_number)
178 {
179         global_mixer->channel_clicked(channel_number);
180 }