]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Create buses in the expanded view. (Still inert.)
[nageru] / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include <math.h>
4 #include <stdio.h>
5 #include <signal.h>
6 #include <algorithm>
7 #include <chrono>
8 #include <string>
9 #include <vector>
10 #include <QBoxLayout>
11 #include <QInputDialog>
12 #include <QKeySequence>
13 #include <QLabel>
14 #include <QMetaType>
15 #include <QPushButton>
16 #include <QResizeEvent>
17 #include <QShortcut>
18 #include <QSize>
19 #include <QString>
20
21 #include "aboutdialog.h"
22 #include "disk_space_estimator.h"
23 #include "flags.h"
24 #include "glwidget.h"
25 #include "input_mapping_dialog.h"
26 #include "lrameter.h"
27 #include "mixer.h"
28 #include "post_to_main_thread.h"
29 #include "ui_audio_miniview.h"
30 #include "ui_audio_expanded_view.h"
31 #include "ui_display.h"
32 #include "ui_mainwindow.h"
33 #include "vumeter.h"
34
35 class QResizeEvent;
36
37 using namespace std;
38 using namespace std::chrono;
39 using namespace std::placeholders;
40
41 Q_DECLARE_METATYPE(std::string);
42 Q_DECLARE_METATYPE(std::vector<std::string>);
43
44 MainWindow *global_mainwindow = nullptr;
45
46 namespace {
47
48 void schedule_cut_signal(int ignored)
49 {
50         global_mixer->schedule_cut();
51 }
52
53 void quit_signal(int ignored)
54 {
55         global_mainwindow->close();
56 }
57
58 void slave_knob(QDial *master, QDial *slave)
59 {
60         QWidget::connect(master, &QDial::valueChanged, [slave](int value){
61                 slave->blockSignals(true);
62                 slave->setValue(value);
63                 slave->blockSignals(false);
64         });
65         QWidget::connect(slave, &QDial::valueChanged, [master](int value){
66                 master->setValue(value);
67         });
68 }
69
70 void slave_checkbox(QCheckBox *master, QCheckBox *slave)
71 {
72         QWidget::connect(master, &QCheckBox::stateChanged, [slave](int state){
73                 slave->blockSignals(true);
74                 slave->setCheckState(Qt::CheckState(state));
75                 slave->blockSignals(false);
76         });
77         QWidget::connect(slave, &QCheckBox::stateChanged, [master](int state){
78                 master->setCheckState(Qt::CheckState(state));
79         });
80 }
81
82 constexpr unsigned DB_NO_FLAGS = 0x0;
83 constexpr unsigned DB_WITH_SIGN = 0x1;
84 constexpr unsigned DB_BARE = 0x2;
85
86 string format_db(double db, unsigned flags)
87 {
88         string text;
89         if (flags & DB_WITH_SIGN) {
90                 if (isfinite(db)) {
91                         char buf[256];
92                         snprintf(buf, sizeof(buf), "%+.1f", db);
93                         text = buf;
94                 } else if (db < 0.0) {
95                         text = "-∞";
96                 } else {
97                         // Should never happen, really.
98                         text = "+∞";
99                 }
100         } else {
101                 if (isfinite(db)) {
102                         char buf[256];
103                         snprintf(buf, sizeof(buf), "%.1f", db);
104                         text = buf;
105                 } else if (db < 0.0) {
106                         text = "-∞";
107                 } else {
108                         // Should never happen, really.
109                         text = "∞";
110                 }
111         }
112         if (!(flags & DB_BARE)) {
113                 text += " dB";
114         }
115         return text;
116 }
117
118 }  // namespace
119
120 MainWindow::MainWindow()
121         : ui(new Ui::MainWindow)
122 {
123         global_mainwindow = this;
124         ui->setupUi(this);
125
126         global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
127         disk_free_label = new QLabel(this);
128         disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
129         ui->menuBar->setCornerWidget(disk_free_label);
130
131         ui->me_live->set_output(Mixer::OUTPUT_LIVE);
132         ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
133
134         // The menus.
135         connect(ui->cut_action, &QAction::triggered, this, &MainWindow::cut_triggered);
136         connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
137         connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered);
138         connect(ui->input_mapping_action, &QAction::triggered, this, &MainWindow::input_mapping_triggered);
139
140         if (global_flags.x264_video_to_http) {
141                 connect(ui->x264_bitrate_action, &QAction::triggered, this, &MainWindow::x264_bitrate_triggered);
142         } else {
143                 ui->x264_bitrate_action->setEnabled(false);
144         }
145
146         // Hook up the transition buttons. (Keyboard shortcuts are set in set_transition_names().)
147         // TODO: Make them dynamic.
148         connect(ui->transition_btn1, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 0));
149         connect(ui->transition_btn2, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 1));
150         connect(ui->transition_btn3, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 2));
151
152         // Aiee...
153         transition_btn1 = ui->transition_btn1;
154         transition_btn2 = ui->transition_btn2;
155         transition_btn3 = ui->transition_btn3;
156         qRegisterMetaType<string>("std::string");
157         qRegisterMetaType<vector<string>>("std::vector<std::string>");
158         connect(ui->me_live, &GLWidget::transition_names_updated, this, &MainWindow::set_transition_names);
159         qRegisterMetaType<Mixer::Output>("Mixer::Output");
160
161         // Hook up the prev/next buttons on the audio views.
162         connect(ui->compact_prev_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 1));
163         connect(ui->compact_next_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 1));
164         connect(ui->full_prev_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 0));
165         connect(ui->full_next_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 0));
166
167         last_audio_level_callback = steady_clock::now() - seconds(1);
168 }
169
170 void MainWindow::resizeEvent(QResizeEvent* event)
171 {
172         QMainWindow::resizeEvent(event);
173
174         // Ask for a relayout, but only after the event loop is done doing relayout
175         // on everything else.
176         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
177 }
178
179 void MainWindow::mixer_created(Mixer *mixer)
180 {
181         // Make the previews.
182         unsigned num_previews = mixer->get_num_channels();
183
184         for (unsigned i = 0; i < num_previews; ++i) {
185                 Mixer::Output output = Mixer::Output(Mixer::OUTPUT_INPUT0 + i);
186
187                 QWidget *preview = new QWidget(this);
188                 Ui::Display *ui_display = new Ui::Display;
189                 ui_display->setupUi(preview);
190                 ui_display->label->setText(mixer->get_channel_name(output).c_str());
191                 ui_display->display->set_output(output);
192                 ui->preview_displays->insertWidget(previews.size(), preview, 1);
193                 previews.push_back(ui_display);
194
195                 // Hook up the click.
196                 connect(ui_display->display, &GLWidget::clicked, bind(&MainWindow::channel_clicked, this, i));
197
198                 // Let the theme update the text whenever the resolution or color changed.
199                 connect(ui_display->display, &GLWidget::name_updated, this, &MainWindow::update_channel_name);
200                 connect(ui_display->display, &GLWidget::color_updated, this, &MainWindow::update_channel_color);
201
202                 // Hook up the keyboard key.
203                 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
204                 connect(shortcut, &QShortcut::activated, bind(&MainWindow::channel_clicked, this, i));
205
206                 // Hook up the white balance button (irrelevant if invisible).
207                 ui_display->wb_button->setVisible(mixer->get_supports_set_wb(output));
208                 connect(ui_display->wb_button, &QPushButton::clicked, bind(&MainWindow::wb_button_clicked, this, i));
209         }
210
211         setup_audio_miniview();
212         setup_audio_expanded_view();
213
214         slave_knob(ui->locut_cutoff_knob, ui->locut_cutoff_knob_2);
215         slave_knob(ui->limiter_threshold_knob, ui->limiter_threshold_knob_2);
216         slave_knob(ui->makeup_gain_knob, ui->makeup_gain_knob_2);
217         slave_checkbox(ui->makeup_gain_auto_checkbox, ui->makeup_gain_auto_checkbox_2);
218         slave_checkbox(ui->limiter_enabled, ui->limiter_enabled_2);
219
220         // TODO: Fetch all of the values these for completeness,
221         // not just the enable knobs implied by flags.
222         ui->locut_enabled->setChecked(global_mixer->get_audio_mixer()->get_locut_enabled());
223         ui->gainstaging_knob->setValue(global_mixer->get_audio_mixer()->get_gain_staging_db());
224         ui->gainstaging_auto_checkbox->setChecked(global_mixer->get_audio_mixer()->get_gain_staging_auto());
225         ui->compressor_enabled->setChecked(global_mixer->get_audio_mixer()->get_compressor_enabled());
226         ui->limiter_enabled->setChecked(global_mixer->get_audio_mixer()->get_limiter_enabled());
227         ui->makeup_gain_auto_checkbox->setChecked(global_mixer->get_audio_mixer()->get_final_makeup_gain_auto());
228
229         QString limiter_threshold_label(
230                 QString::fromStdString(format_db(mixer->get_audio_mixer()->get_limiter_threshold_dbfs(), DB_WITH_SIGN)));
231         ui->limiter_threshold_db_display->setText(limiter_threshold_label);
232         ui->limiter_threshold_db_display_2->setText(limiter_threshold_label);
233         ui->compressor_threshold_db_display->setText(
234                 QString::fromStdString(format_db(mixer->get_audio_mixer()->get_compressor_threshold_dbfs(), DB_WITH_SIGN)));
235
236         connect(ui->locut_cutoff_knob, &QDial::valueChanged, this, &MainWindow::cutoff_knob_changed);
237         cutoff_knob_changed(ui->locut_cutoff_knob->value());
238         connect(ui->locut_enabled, &QCheckBox::stateChanged, [this](int state){
239                 global_mixer->get_audio_mixer()->set_locut_enabled(state == Qt::Checked);
240         });
241
242         connect(ui->gainstaging_knob, &QAbstractSlider::valueChanged, this, &MainWindow::gain_staging_knob_changed);
243         connect(ui->gainstaging_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
244                 global_mixer->get_audio_mixer()->set_gain_staging_auto(state == Qt::Checked);
245         });
246         connect(ui->makeup_gain_knob, &QAbstractSlider::valueChanged, this, &MainWindow::final_makeup_gain_knob_changed);
247         connect(ui->makeup_gain_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
248                 global_mixer->get_audio_mixer()->set_final_makeup_gain_auto(state == Qt::Checked);
249         });
250
251         connect(ui->limiter_threshold_knob, &QDial::valueChanged, this, &MainWindow::limiter_threshold_knob_changed);
252         connect(ui->compressor_threshold_knob, &QDial::valueChanged, this, &MainWindow::compressor_threshold_knob_changed);
253         connect(ui->limiter_enabled, &QCheckBox::stateChanged, [this](int state){
254                 global_mixer->get_audio_mixer()->set_limiter_enabled(state == Qt::Checked);
255         });
256         connect(ui->compressor_enabled, &QCheckBox::stateChanged, [this](int state){
257                 global_mixer->get_audio_mixer()->set_compressor_enabled(state == Qt::Checked);
258         });
259         connect(ui->reset_meters_button, &QPushButton::clicked, this, &MainWindow::reset_meters_button_clicked);
260         mixer->get_audio_mixer()->set_audio_level_callback(bind(&MainWindow::audio_level_callback, this, _1, _2, _3, _4, _5, _6, _7, _8, _9));
261
262         struct sigaction act;
263         memset(&act, 0, sizeof(act));
264         act.sa_handler = schedule_cut_signal;
265         act.sa_flags = SA_RESTART;
266         sigaction(SIGHUP, &act, nullptr);
267
268         // Mostly for debugging. Don't override SIGINT, that's so evil if
269         // shutdown isn't instant.
270         memset(&act, 0, sizeof(act));
271         act.sa_handler = quit_signal;
272         act.sa_flags = SA_RESTART;
273         sigaction(SIGUSR1, &act, nullptr);
274 }
275
276 void MainWindow::setup_audio_miniview()
277 {
278         // Remove any existing channels.
279         for (QLayoutItem *item; (item = ui->faders->takeAt(0)) != nullptr; ) {
280                 delete item->widget();
281                 delete item;
282         }
283         audio_miniviews.clear();
284
285         // Set up brand new ones from the input mapping.
286         InputMapping mapping = global_mixer->get_audio_mixer()->get_input_mapping();
287         audio_miniviews.resize(mapping.buses.size());
288         for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
289                 QWidget *channel = new QWidget(this);
290                 Ui::AudioMiniView *ui_audio_miniview = new Ui::AudioMiniView;
291                 ui_audio_miniview->setupUi(channel);
292                 ui_audio_miniview->bus_desc_label->setFullText(
293                         QString::fromStdString(mapping.buses[bus_index].name));
294                 audio_miniviews[bus_index] = ui_audio_miniview;
295                 // TODO: Set the fader position.
296                 ui->faders->addWidget(channel);
297
298                 connect(ui_audio_miniview->fader, &NonLinearFader::dbValueChanged,
299                         bind(&MainWindow::mini_fader_changed, this, ui_audio_miniview, bus_index, _1));
300         }
301 }
302
303 void MainWindow::setup_audio_expanded_view()
304 {
305         // Remove any existing channels.
306         for (QLayoutItem *item; (item = ui->buses->takeAt(0)) != nullptr; ) {
307                 delete item->widget();
308                 delete item;
309         }
310         audio_expanded_views.clear();
311
312         // Set up brand new ones from the input mapping.
313         InputMapping mapping = global_mixer->get_audio_mixer()->get_input_mapping();
314         audio_expanded_views.resize(mapping.buses.size());
315         for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
316                 QWidget *channel = new QWidget(this);
317                 Ui::AudioExpandedView *ui_audio_expanded_view = new Ui::AudioExpandedView;
318                 ui_audio_expanded_view->setupUi(channel);
319                 ui_audio_expanded_view->bus_desc_label->setFullText(
320                         QString::fromStdString(mapping.buses[bus_index].name));
321                 audio_expanded_views[bus_index] = ui_audio_expanded_view;
322                 // TODO: Set the fader position.
323                 ui->buses->addWidget(channel);
324
325                 //connect(ui_audio_expanded_view->fader, &NonLinearFader::dbValueChanged,
326                 //      bind(&MainWindow::mini_fader_changed, this, ui_audio_expanded_view, bus_index, _1));
327         }
328 }
329
330 void MainWindow::mixer_shutting_down()
331 {
332         ui->me_live->clean_context();
333         ui->me_preview->clean_context();
334         for (Ui::Display *display : previews) {
335                 display->display->clean_context();
336         }
337 }
338
339 void MainWindow::cut_triggered()
340 {
341         global_mixer->schedule_cut();
342 }
343
344 void MainWindow::x264_bitrate_triggered()
345 {
346         bool ok;
347         int new_bitrate = QInputDialog::getInt(this, "Change x264 bitrate", "Choose new bitrate for x264 HTTP output (from 100–100,000 kbit/sec):", global_flags.x264_bitrate, /*min=*/100, /*max=*/100000, /*step=*/100, &ok);
348         if (ok && new_bitrate >= 100 && new_bitrate <= 100000) {
349                 global_flags.x264_bitrate = new_bitrate;
350                 global_mixer->change_x264_bitrate(new_bitrate);
351         }
352 }
353
354 void MainWindow::exit_triggered()
355 {
356         close();
357 }
358
359 void MainWindow::about_triggered()
360 {
361         AboutDialog().exec();
362 }
363
364 void MainWindow::input_mapping_triggered()
365 {
366         if (InputMappingDialog().exec() == QDialog::Accepted) {
367                 setup_audio_miniview();
368                 setup_audio_expanded_view();
369         }
370 }
371
372 void MainWindow::gain_staging_knob_changed(int value)
373 {
374         ui->gainstaging_auto_checkbox->setCheckState(Qt::Unchecked);
375
376         float gain_db = value * 0.1f;
377         global_mixer->get_audio_mixer()->set_gain_staging_db(gain_db);
378
379         // The label will be updated by the audio level callback.
380 }
381
382 void MainWindow::final_makeup_gain_knob_changed(int value)
383 {
384         ui->makeup_gain_auto_checkbox->setCheckState(Qt::Unchecked);
385
386         float gain_db = value * 0.1f;
387         global_mixer->get_audio_mixer()->set_final_makeup_gain_db(gain_db);
388
389         // The label will be updated by the audio level callback.
390 }
391
392 void MainWindow::cutoff_knob_changed(int value)
393 {
394         float octaves = value * 0.1f;
395         float cutoff_hz = 20.0 * pow(2.0, octaves);
396         global_mixer->get_audio_mixer()->set_locut_cutoff(cutoff_hz);
397
398         char buf[256];
399         snprintf(buf, sizeof(buf), "%ld Hz", lrintf(cutoff_hz));
400         ui->locut_cutoff_display->setText(buf);
401         ui->locut_cutoff_display_2->setText(buf);
402 }
403
404 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
405 {
406         char time_str[256];
407         if (estimated_seconds_left < 60.0) {
408                 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
409         } else if (estimated_seconds_left < 1800.0) {  // Less than half an hour: Xm Ys (red).
410                 int s = lrintf(estimated_seconds_left);
411                 int m = s / 60;
412                 s %= 60;
413                 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
414         } else if (estimated_seconds_left < 3600.0) {  // Less than an hour: Xm.
415                 int m = lrintf(estimated_seconds_left / 60.0);
416                 snprintf(time_str, sizeof(time_str), "%dm", m);
417         } else if (estimated_seconds_left < 36000.0) {  // Less than ten hours: Xh Ym.
418                 int m = lrintf(estimated_seconds_left / 60.0);
419                 int h = m / 60;
420                 m %= 60;
421                 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
422         } else {  // More than ten hours: Xh.
423                 int h = lrintf(estimated_seconds_left / 3600.0);
424                 snprintf(time_str, sizeof(time_str), "%dh", h);
425         }
426         char buf[256];
427         snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
428
429         std::string label = buf;
430
431         post_to_main_thread([this, label]{
432                 disk_free_label->setText(QString::fromStdString(label));
433                 ui->menuBar->setCornerWidget(disk_free_label);  // Need to set this again for the sizing to get right.
434         });
435 }
436
437 void MainWindow::limiter_threshold_knob_changed(int value)
438 {
439         float threshold_dbfs = value * 0.1f;
440         global_mixer->get_audio_mixer()->set_limiter_threshold_dbfs(threshold_dbfs);
441         ui->limiter_threshold_db_display->setText(
442                 QString::fromStdString(format_db(threshold_dbfs, DB_WITH_SIGN)));
443         ui->limiter_threshold_db_display_2->setText(
444                 QString::fromStdString(format_db(threshold_dbfs, DB_WITH_SIGN)));
445 }
446
447 void MainWindow::compressor_threshold_knob_changed(int value)
448 {
449         float threshold_dbfs = value * 0.1f;
450         global_mixer->get_audio_mixer()->set_compressor_threshold_dbfs(threshold_dbfs);
451         ui->compressor_threshold_db_display->setText(
452                 QString::fromStdString(format_db(threshold_dbfs, DB_WITH_SIGN)));
453 }
454
455 void MainWindow::mini_fader_changed(Ui::AudioMiniView *ui, int channel, double volume_db)
456 {
457         char buf[256];
458         if (isfinite(volume_db)) {
459                 snprintf(buf, sizeof(buf), "%+.1f dB", volume_db);
460                 ui->fader_label->setText(buf);
461         } else {
462                 ui->fader_label->setText("-∞ dB");
463         }
464
465         global_mixer->get_audio_mixer()->set_fader_volume(channel, volume_db);
466 }
467
468 void MainWindow::reset_meters_button_clicked()
469 {
470         global_mixer->get_audio_mixer()->reset_meters();
471         ui->peak_display->setText(QString::fromStdString(format_db(-HUGE_VAL, DB_WITH_SIGN | DB_BARE)));
472         ui->peak_display->setStyleSheet("");
473 }
474
475 void MainWindow::audio_level_callback(float level_lufs, float peak_db, vector<float> bus_level_lufs,
476                                       float global_level_lufs,
477                                       float range_low_lufs, float range_high_lufs,
478                                       float gain_staging_db, float final_makeup_gain_db,
479                                       float correlation)
480 {
481         steady_clock::time_point now = steady_clock::now();
482
483         // The meters are somewhat inefficient to update. Only update them
484         // every 100 ms or so (we get updates every 5–20 ms).
485         double last_update_age = duration<double>(now - last_audio_level_callback).count();
486         if (last_update_age < 0.100) {
487                 return;
488         }
489         last_audio_level_callback = now;
490
491         post_to_main_thread([=]() {
492                 ui->vu_meter->set_level(level_lufs);
493                 for (unsigned bus_index = 0; bus_index < bus_level_lufs.size(); ++bus_index) {
494                         if (bus_index < audio_miniviews.size()) {
495                                 audio_miniviews[bus_index]->vu_meter_meter->set_level(
496                                         bus_level_lufs[bus_index]);
497                         }
498                 }
499                 ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
500                 ui->correlation_meter->set_correlation(correlation);
501
502                 ui->peak_display->setText(QString::fromStdString(format_db(peak_db, DB_BARE)));
503                 if (peak_db > -0.1f) {  // -0.1 dBFS is EBU peak limit.
504                         ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
505                 } else {
506                         ui->peak_display->setStyleSheet("");
507                 }
508
509                 ui->gainstaging_knob->blockSignals(true);
510                 ui->gainstaging_knob->setValue(lrintf(gain_staging_db * 10.0f));
511                 ui->gainstaging_knob->blockSignals(false);
512                 ui->gainstaging_db_display->setText(
513                         QString::fromStdString(format_db(gain_staging_db, DB_WITH_SIGN)));
514
515                 ui->makeup_gain_knob->blockSignals(true);
516                 ui->makeup_gain_knob->setValue(lrintf(final_makeup_gain_db * 10.0f));
517                 ui->makeup_gain_knob->blockSignals(false);
518                 ui->makeup_gain_db_display->setText(
519                         QString::fromStdString(format_db(final_makeup_gain_db, DB_WITH_SIGN)));
520                 ui->makeup_gain_db_display_2->setText(
521                         QString::fromStdString(format_db(final_makeup_gain_db, DB_WITH_SIGN)));
522         });
523 }
524
525 void MainWindow::relayout()
526 {
527         int height = ui->vertical_layout->geometry().height();
528
529         double remaining_height = height;
530
531         // Allocate the height; the most important part is to keep the main displays
532         // at 16:9 if at all possible.
533         double me_width = ui->me_preview->width();
534         double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height() + ui->preview_vertical_layout->spacing();
535
536         // TODO: Scale the widths when we need to do this.
537         if (me_height / double(height) > 0.8) {
538                 me_height = height * 0.8;
539         }
540         remaining_height -= me_height + ui->vertical_layout->spacing();
541
542         // Space between the M/E displays and the audio strip.
543         remaining_height -= ui->vertical_layout->spacing();
544
545         // The label above the audio strip.
546         double compact_label_height = ui->compact_label->geometry().height() +
547                 ui->compact_audio_layout->spacing();
548         remaining_height -= compact_label_height;
549
550         // The previews will be constrained by the remaining height, and the width.
551         double preview_label_height = previews[0]->title_bar->geometry().height() +
552                 previews[0]->main_vertical_layout->spacing();
553         int preview_total_width = ui->preview_displays->geometry().width() - (previews.size() - 1) * ui->preview_displays->spacing();
554         double preview_height = min(remaining_height - preview_label_height, (preview_total_width / double(previews.size())) * 9.0 / 16.0);
555         remaining_height -= preview_height + preview_label_height + ui->vertical_layout->spacing();
556
557         ui->vertical_layout->setStretch(0, lrintf(me_height));
558         ui->vertical_layout->setStretch(1,
559                 lrintf(compact_label_height) +
560                 lrintf(remaining_height) +
561                 lrintf(preview_height + preview_label_height));  // Audio strip and previews together.
562
563         ui->compact_audio_layout->setStretch(0, lrintf(compact_label_height));
564         ui->compact_audio_layout->setStretch(1, lrintf(remaining_height));  // Audio strip.
565         ui->compact_audio_layout->setStretch(2, lrintf(preview_height + preview_label_height));
566
567         // Set the widths for the previews.
568         double preview_width = preview_height * 16.0 / 9.0;
569         for (unsigned i = 0; i < previews.size(); ++i) {
570                 ui->preview_displays->setStretch(i, lrintf(preview_width));
571         }
572
573         // The preview horizontal spacer.
574         double remaining_preview_width = preview_total_width - previews.size() * preview_width;
575         ui->preview_displays->setStretch(previews.size(), lrintf(remaining_preview_width));
576 }
577
578 void MainWindow::set_transition_names(vector<string> transition_names)
579 {
580         if (transition_names.size() < 1 || transition_names[0].empty()) {
581                 transition_btn1->setText(QString(""));
582         } else {
583                 transition_btn1->setText(QString::fromStdString(transition_names[0] + " (J)"));
584                 ui->transition_btn1->setShortcut(QKeySequence("J"));
585         }
586         if (transition_names.size() < 2 || transition_names[1].empty()) {
587                 transition_btn2->setText(QString(""));
588         } else {
589                 transition_btn2->setText(QString::fromStdString(transition_names[1] + " (K)"));
590                 ui->transition_btn2->setShortcut(QKeySequence("K"));
591         }
592         if (transition_names.size() < 3 || transition_names[2].empty()) {
593                 transition_btn3->setText(QString(""));
594         } else {
595                 transition_btn3->setText(QString::fromStdString(transition_names[2] + " (L)"));
596                 ui->transition_btn3->setShortcut(QKeySequence("L"));
597         }
598 }
599
600 void MainWindow::update_channel_name(Mixer::Output output, const string &name)
601 {
602         if (output >= Mixer::OUTPUT_INPUT0) {
603                 unsigned channel = output - Mixer::OUTPUT_INPUT0;
604                 previews[channel]->label->setText(name.c_str());
605         }
606 }
607
608 void MainWindow::update_channel_color(Mixer::Output output, const string &color)
609 {
610         if (output >= Mixer::OUTPUT_INPUT0) {
611                 unsigned channel = output - Mixer::OUTPUT_INPUT0;
612                 previews[channel]->frame->setStyleSheet(QString::fromStdString("background-color:" + color));
613         }
614 }
615
616 void MainWindow::transition_clicked(int transition_number)
617 {
618         global_mixer->transition_clicked(transition_number);
619 }
620
621 void MainWindow::channel_clicked(int channel_number)
622 {
623         if (current_wb_pick_display == channel_number) {
624                 // The picking was already done from eventFilter(), since we don't get
625                 // the mouse pointer here.
626         } else {
627                 global_mixer->channel_clicked(channel_number);
628         }
629 }
630
631 void MainWindow::wb_button_clicked(int channel_number)
632 {
633         current_wb_pick_display = channel_number;
634         QApplication::setOverrideCursor(Qt::CrossCursor);
635 }
636
637 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
638 {
639         if (current_wb_pick_display != -1 &&
640             event->type() == QEvent::MouseButtonRelease &&
641             watched->isWidgetType()) {
642                 QApplication::restoreOverrideCursor();
643                 if (watched == previews[current_wb_pick_display]->display) {
644                         const QMouseEvent *mouse_event = (QMouseEvent *)event;
645                         set_white_balance(current_wb_pick_display, mouse_event->x(), mouse_event->y());
646                 } else {
647                         // The user clicked on something else, give up.
648                         // (The click goes through, which might not be ideal, but, yes.)
649                         current_wb_pick_display = -1;
650                 }
651         }
652         return false;
653 }
654
655 namespace {
656
657 double srgb_to_linear(double x)
658 {
659         if (x < 0.04045) {
660                 return x / 12.92;
661         } else {
662                 return pow((x + 0.055) / 1.055, 2.4);
663         }
664 }
665
666 }  // namespace
667
668 void MainWindow::set_white_balance(int channel_number, int x, int y)
669 {
670         // Set the white balance to neutral for the grab. It's probably going to
671         // flicker a bit, but hopefully this display is not live anyway.
672         global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, 0.5, 0.5, 0.5);
673         previews[channel_number]->display->updateGL();
674         QRgb reference_color = previews[channel_number]->display->grabFrameBuffer().pixel(x, y);
675
676         double r = srgb_to_linear(qRed(reference_color) / 255.0);
677         double g = srgb_to_linear(qGreen(reference_color) / 255.0);
678         double b = srgb_to_linear(qBlue(reference_color) / 255.0);
679         global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, r, g, b);
680         previews[channel_number]->display->updateGL();
681 }