]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Fix repeated WB clicks.
[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 = size().width();
111         int height = size().height();
112
113         // Allocate the height; the most important part is to keep the main displays
114         // at 16:9 if at all possible.
115         double me_width = ui->me_preview->width();
116         double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height();
117
118         // TODO: Scale the widths when we need to do this.
119         if (me_height / double(height) > 0.8) {
120                 me_height = height * 0.8;
121         }
122
123         // The previews will be constrained by the remaining height, and the width.
124         // FIXME: spacing?
125         double preview_label_height = previews[0]->label->height();
126         double preview_height = std::min(height - me_height - preview_label_height, (width / double(previews.size())) * 9.0 / 16.0);
127
128         ui->vertical_layout->setStretch(0, lrintf(me_height));
129         ui->vertical_layout->setStretch(1, std::max<int>(1, lrintf(height - me_height - preview_height)));
130         ui->vertical_layout->setStretch(2, lrintf(preview_height + preview_label_height));
131
132         // Set the widths for the previews.
133         double preview_width = preview_height * 16.0 / 9.0;  // FIXME: spacing?
134
135         for (unsigned i = 0; i < previews.size(); ++i) {
136                 ui->preview_displays->setStretch(i, lrintf(preview_width));
137         }
138
139         // The spacer.
140         ui->preview_displays->setStretch(previews.size(), lrintf(width - previews.size() * preview_width));
141 }
142
143 void MainWindow::set_transition_names(vector<string> transition_names)
144 {
145         if (transition_names.size() < 1) {
146                 transition_btn1->setText(QString(""));
147         } else {
148                 transition_btn1->setText(QString::fromStdString(transition_names[0]));
149         }
150         if (transition_names.size() < 2) {
151                 transition_btn2->setText(QString(""));
152         } else {
153                 transition_btn2->setText(QString::fromStdString(transition_names[1]));
154         }
155         if (transition_names.size() < 3) {
156                 transition_btn3->setText(QString(""));
157         } else {
158                 transition_btn3->setText(QString::fromStdString(transition_names[2]));
159         }
160 }
161
162 void MainWindow::transition_clicked(int transition_number)
163 {
164         global_mixer->transition_clicked(transition_number);
165 }
166
167 void MainWindow::channel_clicked(int channel_number)
168 {
169         if (current_wb_pick_display == channel_number) {
170                 // The picking was already done from eventFilter(), since we don't get
171                 // the mouse pointer here.
172         } else {
173                 global_mixer->channel_clicked(channel_number);
174         }
175 }
176
177 void MainWindow::wb_button_clicked(int channel_number)
178 {
179         current_wb_pick_display = channel_number;
180         QApplication::setOverrideCursor(Qt::CrossCursor);
181 }
182
183 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
184 {
185         if (current_wb_pick_display != -1 &&
186             event->type() == QEvent::MouseButtonRelease &&
187             watched->isWidgetType()) {
188                 QApplication::restoreOverrideCursor();
189                 if (watched == previews[current_wb_pick_display]->display) {
190                         const QMouseEvent *mouse_event = (QMouseEvent *)event;
191                         set_white_balance(current_wb_pick_display, mouse_event->x(), mouse_event->y());
192                 } else {
193                         // The user clicked on something else, give up.
194                         // (The click goes through, which might not be ideal, but, yes.)
195                         current_wb_pick_display = -1;
196                 }
197         }
198         return false;
199 }
200
201 namespace {
202
203 double srgb_to_linear(double x)
204 {
205         if (x < 0.04045) {
206                 return x / 12.92;
207         } else {
208                 return pow((x + 0.055) / 1.055, 2.4);
209         }
210 }
211
212 }  // namespace
213
214 void MainWindow::set_white_balance(int channel_number, int x, int y)
215 {
216         // Set the white balance to neutral for the grab. It's probably going to
217         // flicker a bit, but hopefully this display is not live anyway.
218         global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, 0.5, 0.5, 0.5);
219         previews[channel_number]->display->updateGL();
220         QRgb reference_color = previews[channel_number]->display->grabFrameBuffer().pixel(x, y);
221
222         double r = srgb_to_linear(qRed(reference_color) / 255.0);
223         double g = srgb_to_linear(qGreen(reference_color) / 255.0);
224         double b = srgb_to_linear(qBlue(reference_color) / 255.0);
225         global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, r, g, b);
226         previews[channel_number]->display->updateGL();
227 }