]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Re-run IWYU, again with lots of manual cleanup.
[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_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         // TODO: Ask for the real number.
43         ui->preview1->set_output(Mixer::OUTPUT_INPUT0);
44         ui->preview2->set_output(Mixer::OUTPUT_INPUT1);
45         ui->preview3->set_output(Mixer::OUTPUT_INPUT2);
46
47         // Hook up the preview clicks.
48         {
49                 QSignalMapper *mapper = new QSignalMapper(this);
50                 mapper->setMapping(ui->preview1, 0),
51                 mapper->setMapping(ui->preview2, 1);
52                 mapper->setMapping(ui->preview3, 2);
53                 connect(ui->preview1, SIGNAL(clicked()), mapper, SLOT(map()));
54                 connect(ui->preview2, SIGNAL(clicked()), mapper, SLOT(map()));
55                 connect(ui->preview3, SIGNAL(clicked()), mapper, SLOT(map()));
56
57                 connect(mapper, SIGNAL(mapped(int)), this, SLOT(channel_clicked(int)));
58         }
59
60         // Hook up the preview keyboard keys.
61         {
62                 QSignalMapper *mapper = new QSignalMapper(this);
63                 QShortcut *shortcut1 = new QShortcut(QKeySequence(Qt::Key_1), this);
64                 connect(shortcut1, SIGNAL(activated()), mapper, SLOT(map()));
65                 QShortcut *shortcut2 = new QShortcut(QKeySequence(Qt::Key_2), this);
66                 connect(shortcut2, SIGNAL(activated()), mapper, SLOT(map()));
67                 QShortcut *shortcut3 = new QShortcut(QKeySequence(Qt::Key_3), this);
68                 connect(shortcut3, SIGNAL(activated()), mapper, SLOT(map()));
69                 mapper->setMapping(shortcut1, 0),
70                 mapper->setMapping(shortcut2, 1);
71                 mapper->setMapping(shortcut3, 2);
72
73                 connect(mapper, SIGNAL(mapped(int)), this, SLOT(channel_clicked(int)));
74         }
75
76         // Hook up the transition buttons.
77         // TODO: Make them dynamic.
78         {
79                 QSignalMapper *mapper = new QSignalMapper(this);
80                 mapper->setMapping(ui->transition_btn1, 0),
81                 mapper->setMapping(ui->transition_btn2, 1);
82                 mapper->setMapping(ui->transition_btn3, 2);
83                 connect(ui->transition_btn1, SIGNAL(clicked()), mapper, SLOT(map()));
84                 connect(ui->transition_btn2, SIGNAL(clicked()), mapper, SLOT(map()));
85                 connect(ui->transition_btn3, SIGNAL(clicked()), mapper, SLOT(map()));
86                 connect(mapper, SIGNAL(mapped(int)), this, SLOT(transition_clicked(int)));
87         }
88
89         // Aiee...
90         transition_btn1 = ui->transition_btn1;
91         transition_btn2 = ui->transition_btn2;
92         transition_btn3 = ui->transition_btn3;
93         qRegisterMetaType<std::vector<std::string>>("std::vector<std::string>");
94         connect(ui->preview1, SIGNAL(transition_names_updated(std::vector<std::string>)),
95                 this, SLOT(set_transition_names(std::vector<std::string>)));
96 }
97
98 void MainWindow::resizeEvent(QResizeEvent* event)
99 {
100         QMainWindow::resizeEvent(event);
101
102         // Ask for a relayout, but only after the event loop is done doing relayout
103         // on everything else.
104         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
105 }
106
107 void MainWindow::mixer_created(Mixer *mixer)
108 {
109         mixer->set_audio_level_callback([this](float level_lufs, float peak_db, float global_level_lufs, float range_low_lufs, float range_high_lufs){
110                 ui->vu_meter->set_level(level_lufs);
111                 ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
112
113                 char buf[256];
114                 snprintf(buf, sizeof(buf), "%.1f", peak_db);
115                 ui->peak_display->setText(buf);
116                 if (peak_db > -0.1f) {  // -0.1 dBFS is EBU peak limit.
117                         ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
118                 } else {
119                         ui->peak_display->setStyleSheet("");
120                 }
121         });
122 }
123
124 void MainWindow::relayout()
125 {
126         int width = size().width();
127         int height = size().height();
128
129         // Allocate the height; the most important part is to keep the main displays
130         // at 16:9 if at all possible.
131         double me_width = ui->me_preview->width();
132         double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height();
133
134         // TODO: Scale the widths when we need to do this.
135         if (me_height / double(height) > 0.8) {
136                 me_height = height * 0.8;
137         }
138
139         // The previews will be constrained by the remaining height, and the width.
140         // FIXME: spacing?
141         double preview_height = std::min(height - me_height, (width / 4.0) * 9.0 / 16.0);
142
143         ui->vertical_layout->setStretch(0, lrintf(me_height));
144         ui->vertical_layout->setStretch(1, std::max<int>(1, lrintf(height - me_height - preview_height)));
145         ui->vertical_layout->setStretch(2, lrintf(preview_height));
146
147         // Set the widths for the previews.
148         double preview_width = preview_height * 16.0 / 9.0;  // FIXME: spacing?
149
150         ui->preview_displays->setStretch(0, lrintf(preview_width));
151         ui->preview_displays->setStretch(1, lrintf(preview_width));
152         ui->preview_displays->setStretch(2, lrintf(preview_width));
153         ui->preview_displays->setStretch(3, lrintf(preview_width));
154         ui->preview_displays->setStretch(4, lrintf(width - 4.0 * preview_width));
155 }
156
157 void MainWindow::set_transition_names(vector<string> transition_names)
158 {
159         if (transition_names.size() < 1) {
160                 transition_btn1->setText(QString(""));
161         } else {
162                 transition_btn1->setText(QString::fromStdString(transition_names[0]));
163         }
164         if (transition_names.size() < 2) {
165                 transition_btn2->setText(QString(""));
166         } else {
167                 transition_btn2->setText(QString::fromStdString(transition_names[1]));
168         }
169         if (transition_names.size() < 3) {
170                 transition_btn3->setText(QString(""));
171         } else {
172                 transition_btn3->setText(QString::fromStdString(transition_names[2]));
173         }
174 }
175
176 void MainWindow::transition_clicked(int transition_number)
177 {
178         global_mixer->transition_clicked(transition_number);
179 }
180
181 void MainWindow::channel_clicked(int channel_number)
182 {
183         global_mixer->channel_clicked(channel_number);
184 }