]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Add the beginnings of a very simple VU meter, based on libebur128.
[nageru] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "window.h"
3 #include <thread>
4 #include <vector>
5 #include <string>
6 #include <QSignalMapper>
7 #include <QMetaType>
8 #include <QShortcut>
9 #include <QResizeEvent>
10
11 #include "context.h"
12 #include "mixer.h"
13
14 #include "ui_mainwindow.h"
15
16 using namespace std;
17
18 Q_DECLARE_METATYPE(std::vector<std::string>);
19
20 MainWindow::MainWindow()
21         : ui(new Ui::MainWindow)
22 {
23         ui->setupUi(this);
24
25         ui->me_live->set_output(Mixer::OUTPUT_LIVE);
26         ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
27
28         // TODO: Ask for the real number.
29         ui->preview1->set_output(Mixer::OUTPUT_INPUT0);
30         ui->preview2->set_output(Mixer::OUTPUT_INPUT1);
31         ui->preview3->set_output(Mixer::OUTPUT_INPUT2);
32
33         // Hook up the preview clicks.
34         {
35                 QSignalMapper *mapper = new QSignalMapper(this);
36                 mapper->setMapping(ui->preview1, 0),
37                 mapper->setMapping(ui->preview2, 1);
38                 mapper->setMapping(ui->preview3, 2);
39                 connect(ui->preview1, SIGNAL(clicked()), mapper, SLOT(map()));
40                 connect(ui->preview2, SIGNAL(clicked()), mapper, SLOT(map()));
41                 connect(ui->preview3, SIGNAL(clicked()), mapper, SLOT(map()));
42
43                 connect(mapper, SIGNAL(mapped(int)), this, SLOT(channel_clicked(int)));
44         }
45
46         // Hook up the preview keyboard keys.
47         {
48                 QSignalMapper *mapper = new QSignalMapper(this);
49                 QShortcut *shortcut1 = new QShortcut(QKeySequence(Qt::Key_1), this);
50                 connect(shortcut1, SIGNAL(activated()), mapper, SLOT(map()));
51                 QShortcut *shortcut2 = new QShortcut(QKeySequence(Qt::Key_2), this);
52                 connect(shortcut2, SIGNAL(activated()), mapper, SLOT(map()));
53                 QShortcut *shortcut3 = new QShortcut(QKeySequence(Qt::Key_3), this);
54                 connect(shortcut3, SIGNAL(activated()), mapper, SLOT(map()));
55                 mapper->setMapping(shortcut1, 0),
56                 mapper->setMapping(shortcut2, 1);
57                 mapper->setMapping(shortcut3, 2);
58
59                 connect(mapper, SIGNAL(mapped(int)), this, SLOT(channel_clicked(int)));
60         }
61
62         // Hook up the transition buttons.
63         // TODO: Make them dynamic.
64         {
65                 QSignalMapper *mapper = new QSignalMapper(this);
66                 mapper->setMapping(ui->transition_btn1, 0),
67                 mapper->setMapping(ui->transition_btn2, 1);
68                 mapper->setMapping(ui->transition_btn3, 2);
69                 connect(ui->transition_btn1, SIGNAL(clicked()), mapper, SLOT(map()));
70                 connect(ui->transition_btn2, SIGNAL(clicked()), mapper, SLOT(map()));
71                 connect(ui->transition_btn3, SIGNAL(clicked()), mapper, SLOT(map()));
72                 connect(mapper, SIGNAL(mapped(int)), this, SLOT(transition_clicked(int)));
73         }
74
75         // Aiee...
76         transition_btn1 = ui->transition_btn1;
77         transition_btn2 = ui->transition_btn2;
78         transition_btn3 = ui->transition_btn3;
79         qRegisterMetaType<std::vector<std::string>>("std::vector<std::string>");
80         connect(ui->preview1, SIGNAL(transition_names_updated(std::vector<std::string>)),
81                 this, SLOT(set_transition_names(std::vector<std::string>)));
82
83         global_vu_meter = ui->vu_meter;  // global_mixer does not exist yet, so need to delay the hookup.
84 }
85
86 void MainWindow::resizeEvent(QResizeEvent* event)
87 {
88         QMainWindow::resizeEvent(event);
89
90         // Ask for a relayout, but only after the event loop is done doing relayout
91         // on everything else.
92         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
93 }
94
95 void MainWindow::relayout()
96 {
97         int width = size().width();
98         int height = size().height();
99
100         // Allocate the height; the most important part is to keep the main displays
101         // at 16:9 if at all possible.
102         double me_width = ui->me_preview->width();
103         double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height();
104
105         // TODO: Scale the widths when we need to do this.
106         if (me_height / double(height) > 0.8) {
107                 me_height = height * 0.8;
108         }
109
110         // The previews will be constrained by the remaining height, and the width.
111         // FIXME: spacing?
112         double preview_height = std::min(height - me_height, (width / 4.0) * 9.0 / 16.0);
113
114         ui->vertical_layout->setStretch(0, lrintf(me_height));
115         ui->vertical_layout->setStretch(1, std::max<int>(1, lrintf(height - me_height - preview_height)));
116         ui->vertical_layout->setStretch(2, lrintf(preview_height));
117
118         // Set the widths for the previews.
119         double preview_width = preview_height * 16.0 / 9.0;  // FIXME: spacing?
120
121         ui->preview_displays->setStretch(0, lrintf(preview_width));
122         ui->preview_displays->setStretch(1, lrintf(preview_width));
123         ui->preview_displays->setStretch(2, lrintf(preview_width));
124         ui->preview_displays->setStretch(3, lrintf(preview_width));
125         ui->preview_displays->setStretch(4, lrintf(width - 4.0 * preview_width));
126 }
127
128 void MainWindow::set_transition_names(vector<string> transition_names)
129 {
130         if (transition_names.size() < 1) {
131                 transition_btn1->setText(QString(""));
132         } else {
133                 transition_btn1->setText(QString::fromStdString(transition_names[0]));
134         }
135         if (transition_names.size() < 2) {
136                 transition_btn2->setText(QString(""));
137         } else {
138                 transition_btn2->setText(QString::fromStdString(transition_names[1]));
139         }
140         if (transition_names.size() < 3) {
141                 transition_btn3->setText(QString(""));
142         } else {
143                 transition_btn3->setText(QString::fromStdString(transition_names[2]));
144         }
145 }
146
147 void MainWindow::transition_clicked(int transition_number)
148 {
149         global_mixer->transition_clicked(transition_number);
150 }
151
152 void MainWindow::channel_clicked(int channel_number)
153 {
154         global_mixer->channel_clicked(channel_number);
155 }