]> git.sesse.net Git - nageru/blob - input_mapping_dialog.cpp
Make most of the input mapping dialog actually work.
[nageru] / input_mapping_dialog.cpp
1 #include "input_mapping_dialog.h"
2
3 #include "ui_input_mapping.h"
4
5 #include <QComboBox>
6
7 using namespace std;
8 using namespace std::placeholders;
9
10 InputMappingDialog::InputMappingDialog()
11         : ui(new Ui::InputMappingDialog),
12           mapping(global_mixer->get_audio_mixer()->get_input_mapping()),
13           old_mapping(mapping),
14           card_names(global_mixer->get_audio_mixer()->get_names())
15 {
16         ui->setupUi(this);
17
18         fill_ui_from_mapping(mapping);
19         connect(ui->table, &QTableWidget::cellChanged, this, &InputMappingDialog::cell_changed);
20         connect(ui->ok_cancel_buttons, &QDialogButtonBox::accepted, this, &InputMappingDialog::ok_clicked);
21         connect(ui->ok_cancel_buttons, &QDialogButtonBox::rejected, this, &InputMappingDialog::cancel_clicked);
22         connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
23         //connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
24 }
25
26 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
27 {
28         ui->table->verticalHeader()->hide();
29         ui->table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
30         ui->table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
31         ui->table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
32         ui->table->horizontalHeader()->setSectionsClickable(false);
33
34         ui->table->setRowCount(mapping.inputs.size());
35         for (unsigned row = 0; row < mapping.inputs.size(); ++row) {
36                 fill_row_from_input(row, mapping.inputs[row]);
37         }
38 }
39
40 void InputMappingDialog::fill_row_from_input(unsigned row, const InputMapping::Input &input)
41 {
42         QString name(QString::fromStdString(input.name));
43         ui->table->setItem(row, 0, new QTableWidgetItem(name));
44
45         // Card choices.
46         QComboBox *card_combo = new QComboBox;
47         card_combo->addItem(QString("(none)   "));
48         for (const string &name : card_names) {
49                 card_combo->addItem(QString::fromStdString(name + "   "));
50         }
51         switch (input.input_source_type) {
52         case InputSourceType::SILENCE:
53                 card_combo->setCurrentIndex(0);
54                 break;
55         case InputSourceType::CAPTURE_CARD:
56                 card_combo->setCurrentIndex(mapping.inputs[row].input_source_index + 1);
57                 break;
58         default:
59                 assert(false);
60         }
61         connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
62                 bind(&InputMappingDialog::card_selected, this, row, _1));
63         ui->table->setCellWidget(row, 1, card_combo);
64
65         setup_channel_choices_from_input(row, input);
66 }
67
68 void InputMappingDialog::setup_channel_choices_from_input(unsigned row, const InputMapping::Input &input)
69 {
70         // Left and right channel.
71         for (unsigned channel = 0; channel < 2; ++channel) {
72                 QComboBox *channel_combo = new QComboBox;
73                 channel_combo->addItem(QString("(none)"));
74                 if (input.input_source_type == InputSourceType::CAPTURE_CARD) {
75                         for (unsigned source = 0; source < 8; ++source) {  // TODO: Ask the card about number of channels, and names.
76                                 char buf[256];
77                                 snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
78                                 channel_combo->addItem(QString(buf));
79                         }
80                         channel_combo->setCurrentIndex(input.source_channel[channel] + 1);
81                 } else {
82                         channel_combo->setCurrentIndex(0);
83                 }
84                 connect(channel_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
85                         bind(&InputMappingDialog::channel_selected, this, row, channel, _1));
86                 ui->table->setCellWidget(row, 2 + channel, channel_combo);
87         }
88 }
89
90 void InputMappingDialog::ok_clicked()
91 {
92         global_mixer->get_audio_mixer()->set_input_mapping(mapping);
93         accept();
94 }
95
96 void InputMappingDialog::cancel_clicked()
97 {
98         global_mixer->get_audio_mixer()->set_input_mapping(old_mapping);
99         reject();
100 }
101
102 void InputMappingDialog::cell_changed(int row, int column)
103 {
104         if (column != 0) {
105                 // Spurious; only really the name column should fire these.
106                 return;
107         }
108         mapping.inputs[row].name = ui->table->item(row, column)->text().toStdString();
109 }
110
111 void InputMappingDialog::card_selected(unsigned row, int index)
112 {
113         if (index == 0) {
114                 mapping.inputs[row].input_source_type = InputSourceType::SILENCE;
115         } else {
116                 mapping.inputs[row].input_source_type = InputSourceType::CAPTURE_CARD;
117                 mapping.inputs[row].input_source_index = index - 1;
118         }
119         setup_channel_choices_from_input(row, mapping.inputs[row]);
120 }
121
122 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
123 {
124         mapping.inputs[row].source_channel[channel] = index - 1;
125 }
126
127 void InputMappingDialog::add_clicked()
128 {
129         InputMapping::Input new_input;
130         new_input.name = "New input";
131         new_input.input_source_type = InputSourceType::SILENCE;
132         mapping.inputs.push_back(new_input);
133         ui->table->setRowCount(mapping.inputs.size());
134
135         unsigned row = mapping.inputs.size() - 1;
136         fill_row_from_input(row, new_input);
137         ui->table->editItem(ui->table->item(row, 0));  // Start editing the name.
138 }