1 #include "mainwindow.h"
9 #include <QKeySequence>
12 #include <QPushButton>
13 #include <QResizeEvent>
21 #include "ui_display.h"
22 #include "ui_mainwindow.h"
29 Q_DECLARE_METATYPE(std::vector<std::string>);
31 MainWindow *global_mainwindow = nullptr;
33 MainWindow::MainWindow()
34 : ui(new Ui::MainWindow)
36 global_mainwindow = this;
39 ui->me_live->set_output(Mixer::OUTPUT_LIVE);
40 ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
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));
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);
56 void MainWindow::resizeEvent(QResizeEvent* event)
58 QMainWindow::resizeEvent(event);
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);
65 void MainWindow::mixer_created(Mixer *mixer)
68 unsigned num_previews = mixer->get_num_channels();
70 for (unsigned i = 0; i < num_previews; ++i) {
71 Mixer::Output output = Mixer::Output(Mixer::OUTPUT_INPUT0 + i);
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);
82 connect(ui_display->display, &GLWidget::clicked, std::bind(&MainWindow::channel_clicked, this, i));
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));
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));
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);
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; }");
103 ui->peak_display->setStyleSheet("");
108 void MainWindow::relayout()
110 int width = size().width();
111 int height = size().height();
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();
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;
123 // The previews will be constrained by the remaining height, and the width.
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);
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));
132 // Set the widths for the previews.
133 double preview_width = preview_height * 16.0 / 9.0; // FIXME: spacing?
135 for (unsigned i = 0; i < previews.size(); ++i) {
136 ui->preview_displays->setStretch(i, lrintf(preview_width));
140 ui->preview_displays->setStretch(previews.size(), lrintf(width - previews.size() * preview_width));
143 void MainWindow::set_transition_names(vector<string> transition_names)
145 if (transition_names.size() < 1) {
146 transition_btn1->setText(QString(""));
148 transition_btn1->setText(QString::fromStdString(transition_names[0]));
150 if (transition_names.size() < 2) {
151 transition_btn2->setText(QString(""));
153 transition_btn2->setText(QString::fromStdString(transition_names[1]));
155 if (transition_names.size() < 3) {
156 transition_btn3->setText(QString(""));
158 transition_btn3->setText(QString::fromStdString(transition_names[2]));
162 void MainWindow::transition_clicked(int transition_number)
164 global_mixer->transition_clicked(transition_number);
167 void MainWindow::channel_clicked(int channel_number)
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.
173 global_mixer->channel_clicked(channel_number);
177 void MainWindow::wb_button_clicked(int channel_number)
179 current_wb_pick_display = channel_number;
180 QApplication::setOverrideCursor(Qt::CrossCursor);
183 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
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());
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;
203 double srgb_to_linear(double x)
208 return pow((x + 0.055) / 1.055, 2.4);
214 void MainWindow::set_white_balance(int channel_number, int x, int y)
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);
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();