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