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