]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Start an audio strip, and rework some of the layout stuff, as it was getting painfull...
[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 <QSize>
16 #include <QString>
17
18 #include "glwidget.h"
19 #include "lrameter.h"
20 #include "mixer.h"
21 #include "ui_display.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         connect(ui->transition_btn1, &QPushButton::clicked, std::bind(&MainWindow::transition_clicked, this, 0));
45         connect(ui->transition_btn2, &QPushButton::clicked, std::bind(&MainWindow::transition_clicked, this, 1));
46         connect(ui->transition_btn3, &QPushButton::clicked, std::bind(&MainWindow::transition_clicked, this, 2));
47
48         // Aiee...
49         transition_btn1 = ui->transition_btn1;
50         transition_btn2 = ui->transition_btn2;
51         transition_btn3 = ui->transition_btn3;
52         qRegisterMetaType<std::vector<std::string>>("std::vector<std::string>");
53         connect(ui->me_preview, &GLWidget::transition_names_updated, this, &MainWindow::set_transition_names);
54 }
55
56 void MainWindow::resizeEvent(QResizeEvent* event)
57 {
58         QMainWindow::resizeEvent(event);
59
60         // Ask for a relayout, but only after the event loop is done doing relayout
61         // on everything else.
62         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
63 }
64
65 void MainWindow::mixer_created(Mixer *mixer)
66 {
67         // Make the previews.
68         unsigned num_previews = mixer->get_num_channels();
69
70         for (unsigned i = 0; i < num_previews; ++i) {
71                 Mixer::Output output = Mixer::Output(Mixer::OUTPUT_INPUT0 + i);
72
73                 QWidget *preview = new QWidget(this);
74                 Ui::Display *ui_display = new Ui::Display;
75                 ui_display->setupUi(preview);
76                 ui_display->label->setText(mixer->get_channel_name(output).c_str());
77                 ui_display->display->set_output(output);
78                 ui->preview_displays->insertWidget(previews.size(), preview, 1);
79                 previews.push_back(ui_display);
80
81                 // Hook up the click.
82                 connect(ui_display->display, &GLWidget::clicked, std::bind(&MainWindow::channel_clicked, this, i));
83
84                 // Hook up the keyboard key.
85                 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
86                 connect(shortcut, &QShortcut::activated, std::bind(&MainWindow::channel_clicked, this, i));
87
88                 // Hook up the white balance button (irrelevant if invisible).
89                 ui_display->wb_button->setVisible(mixer->get_supports_set_wb(output));
90                 connect(ui_display->wb_button, &QPushButton::clicked, std::bind(&MainWindow::wb_button_clicked, this, i));
91         }
92
93         mixer->set_audio_level_callback([this](float level_lufs, float peak_db, float global_level_lufs, float range_low_lufs, float range_high_lufs){
94                 ui->vu_meter->set_level(level_lufs);
95                 ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
96
97                 char buf[256];
98                 snprintf(buf, sizeof(buf), "%.1f", peak_db);
99                 ui->peak_display->setText(buf);
100                 if (peak_db > -0.1f) {  // -0.1 dBFS is EBU peak limit.
101                         ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
102                 } else {
103                         ui->peak_display->setStyleSheet("");
104                 }
105         });
106 }
107
108 void MainWindow::relayout()
109 {
110         int width = ui->vertical_layout->geometry().width();
111         int height = ui->vertical_layout->geometry().height();
112
113         double remaining_height = height;
114
115         // Allocate the height; the most important part is to keep the main displays
116         // at 16:9 if at all possible.
117         double me_width = ui->me_preview->width();
118         double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height() + ui->preview_vertical_layout->spacing();
119
120         // TODO: Scale the widths when we need to do this.
121         if (me_height / double(height) > 0.8) {
122                 me_height = height * 0.8;
123         }
124         remaining_height -= me_height + ui->vertical_layout->spacing();
125
126         double audiostrip_height = ui->audiostrip->geometry().height();
127         remaining_height -= audiostrip_height + ui->vertical_layout->spacing();
128
129         // The previews will be constrained by the remaining height, and the width.
130         double preview_label_height = previews[0]->title_bar->geometry().height() + ui->preview_displays->spacing();  // Wrong spacing?
131         double preview_height = std::min(remaining_height - preview_label_height, (width / double(previews.size())) * 9.0 / 16.0);
132         remaining_height -= preview_height + preview_label_height + ui->vertical_layout->spacing();
133
134         ui->vertical_layout->setStretch(0, lrintf(me_height));
135         ui->vertical_layout->setStretch(1, 0);  // Don't stretch the audiostrip.
136         ui->vertical_layout->setStretch(2, std::max<int>(1, remaining_height));  // Spacer.
137         ui->vertical_layout->setStretch(3, lrintf(preview_height + preview_label_height));
138
139         // Set the widths for the previews.
140         double preview_width = preview_height * 16.0 / 9.0;
141
142         for (unsigned i = 0; i < previews.size(); ++i) {
143                 ui->preview_displays->setStretch(i, lrintf(preview_width));
144         }
145
146         // The preview horizontal spacer.
147         ui->preview_displays->setStretch(previews.size(), lrintf(width - (previews.size() + ui->preview_displays->spacing()) * preview_width));
148 }
149
150 void MainWindow::set_transition_names(vector<string> transition_names)
151 {
152         if (transition_names.size() < 1) {
153                 transition_btn1->setText(QString(""));
154         } else {
155                 transition_btn1->setText(QString::fromStdString(transition_names[0]));
156         }
157         if (transition_names.size() < 2) {
158                 transition_btn2->setText(QString(""));
159         } else {
160                 transition_btn2->setText(QString::fromStdString(transition_names[1]));
161         }
162         if (transition_names.size() < 3) {
163                 transition_btn3->setText(QString(""));
164         } else {
165                 transition_btn3->setText(QString::fromStdString(transition_names[2]));
166         }
167 }
168
169 void MainWindow::transition_clicked(int transition_number)
170 {
171         global_mixer->transition_clicked(transition_number);
172 }
173
174 void MainWindow::channel_clicked(int channel_number)
175 {
176         if (current_wb_pick_display == channel_number) {
177                 // The picking was already done from eventFilter(), since we don't get
178                 // the mouse pointer here.
179         } else {
180                 global_mixer->channel_clicked(channel_number);
181         }
182 }
183
184 void MainWindow::wb_button_clicked(int channel_number)
185 {
186         current_wb_pick_display = channel_number;
187         QApplication::setOverrideCursor(Qt::CrossCursor);
188 }
189
190 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
191 {
192         if (current_wb_pick_display != -1 &&
193             event->type() == QEvent::MouseButtonRelease &&
194             watched->isWidgetType()) {
195                 QApplication::restoreOverrideCursor();
196                 if (watched == previews[current_wb_pick_display]->display) {
197                         const QMouseEvent *mouse_event = (QMouseEvent *)event;
198                         set_white_balance(current_wb_pick_display, mouse_event->x(), mouse_event->y());
199                 } else {
200                         // The user clicked on something else, give up.
201                         // (The click goes through, which might not be ideal, but, yes.)
202                         current_wb_pick_display = -1;
203                 }
204         }
205         return false;
206 }
207
208 namespace {
209
210 double srgb_to_linear(double x)
211 {
212         if (x < 0.04045) {
213                 return x / 12.92;
214         } else {
215                 return pow((x + 0.055) / 1.055, 2.4);
216         }
217 }
218
219 }  // namespace
220
221 void MainWindow::set_white_balance(int channel_number, int x, int y)
222 {
223         // Set the white balance to neutral for the grab. It's probably going to
224         // flicker a bit, but hopefully this display is not live anyway.
225         global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, 0.5, 0.5, 0.5);
226         previews[channel_number]->display->updateGL();
227         QRgb reference_color = previews[channel_number]->display->grabFrameBuffer().pixel(x, y);
228
229         double r = srgb_to_linear(qRed(reference_color) / 255.0);
230         double g = srgb_to_linear(qGreen(reference_color) / 255.0);
231         double b = srgb_to_linear(qBlue(reference_color) / 255.0);
232         global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, r, g, b);
233         previews[channel_number]->display->updateGL();
234 }