]> git.sesse.net Git - nageru/blob - mainwindow.cpp
430ec0f0a8b5af09231308451ec1e5a1f5b2eaf9
[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_mainwindow.h"
23 #include "vumeter.h"
24
25 class QResizeEvent;
26
27 using namespace std;
28
29 Q_DECLARE_METATYPE(std::vector<std::string>);
30
31 MainWindow *global_mainwindow = nullptr;
32
33 MainWindow::MainWindow()
34         : ui(new Ui::MainWindow)
35 {
36         global_mainwindow = this;
37         ui->setupUi(this);
38
39         ui->me_live->set_output(Mixer::OUTPUT_LIVE);
40         ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
41
42         // Hook up the transition buttons.
43         // TODO: Make them dynamic.
44         {
45                 QSignalMapper *mapper = new QSignalMapper(this);
46                 mapper->setMapping(ui->transition_btn1, 0),
47                 mapper->setMapping(ui->transition_btn2, 1);
48                 mapper->setMapping(ui->transition_btn3, 2);
49                 connect(ui->transition_btn1, SIGNAL(clicked()), mapper, SLOT(map()));
50                 connect(ui->transition_btn2, SIGNAL(clicked()), mapper, SLOT(map()));
51                 connect(ui->transition_btn3, SIGNAL(clicked()), mapper, SLOT(map()));
52                 connect(mapper, SIGNAL(mapped(int)), this, SLOT(transition_clicked(int)));
53         }
54
55         // Aiee...
56         transition_btn1 = ui->transition_btn1;
57         transition_btn2 = ui->transition_btn2;
58         transition_btn3 = ui->transition_btn3;
59         qRegisterMetaType<std::vector<std::string>>("std::vector<std::string>");
60         connect(ui->me_preview, SIGNAL(transition_names_updated(std::vector<std::string>)),
61                 this, SLOT(set_transition_names(std::vector<std::string>)));
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                 GLWidget *preview = new GLWidget(this);
81                 preview->set_output(Mixer::Output(Mixer::OUTPUT_INPUT0 + i));
82                 ui->preview_displays->insertWidget(previews.size(), preview, 1);
83                 previews.push_back(preview);
84
85                 // Hook up the click.
86                 mapper->setMapping(preview, i);
87                 connect(preview, SIGNAL(clicked()), mapper, SLOT(map()));
88
89                 // Hook up the keyboard key.
90                 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
91                 mapper->setMapping(shortcut, i);
92                 connect(shortcut, SIGNAL(activated()), mapper, SLOT(map()));
93         }
94
95         connect(mapper, SIGNAL(mapped(int)), this, SLOT(channel_clicked(int)));
96
97         mixer->set_audio_level_callback([this](float level_lufs, float peak_db, float global_level_lufs, float range_low_lufs, float range_high_lufs){
98                 ui->vu_meter->set_level(level_lufs);
99                 ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
100
101                 char buf[256];
102                 snprintf(buf, sizeof(buf), "%.1f", peak_db);
103                 ui->peak_display->setText(buf);
104                 if (peak_db > -0.1f) {  // -0.1 dBFS is EBU peak limit.
105                         ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
106                 } else {
107                         ui->peak_display->setStyleSheet("");
108                 }
109         });
110 }
111
112 void MainWindow::relayout()
113 {
114         int width = size().width();
115         int height = size().height();
116
117         // Allocate the height; the most important part is to keep the main displays
118         // at 16:9 if at all possible.
119         double me_width = ui->me_preview->width();
120         double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height();
121
122         // TODO: Scale the widths when we need to do this.
123         if (me_height / double(height) > 0.8) {
124                 me_height = height * 0.8;
125         }
126
127         // The previews will be constrained by the remaining height, and the width.
128         // FIXME: spacing?
129         double preview_height = std::min(height - me_height, (width / double(previews.size())) * 9.0 / 16.0);
130
131         ui->vertical_layout->setStretch(0, lrintf(me_height));
132         ui->vertical_layout->setStretch(1, std::max<int>(1, lrintf(height - me_height - preview_height)));
133         ui->vertical_layout->setStretch(2, lrintf(preview_height));
134
135         // Set the widths for the previews.
136         double preview_width = preview_height * 16.0 / 9.0;  // FIXME: spacing?
137
138         for (unsigned i = 0; i < previews.size(); ++i) {
139                 ui->preview_displays->setStretch(i, lrintf(preview_width));
140         }
141
142         // The spacer.
143         ui->preview_displays->setStretch(previews.size(), lrintf(width - previews.size() * preview_width));
144 }
145
146 void MainWindow::set_transition_names(vector<string> transition_names)
147 {
148         if (transition_names.size() < 1) {
149                 transition_btn1->setText(QString(""));
150         } else {
151                 transition_btn1->setText(QString::fromStdString(transition_names[0]));
152         }
153         if (transition_names.size() < 2) {
154                 transition_btn2->setText(QString(""));
155         } else {
156                 transition_btn2->setText(QString::fromStdString(transition_names[1]));
157         }
158         if (transition_names.size() < 3) {
159                 transition_btn3->setText(QString(""));
160         } else {
161                 transition_btn3->setText(QString::fromStdString(transition_names[2]));
162         }
163 }
164
165 void MainWindow::transition_clicked(int transition_number)
166 {
167         global_mixer->transition_clicked(transition_number);
168 }
169
170 void MainWindow::channel_clicked(int channel_number)
171 {
172         global_mixer->channel_clicked(channel_number);
173 }