1 #include "input_mapping_dialog.h"
3 #include "ui_input_mapping.h"
8 using namespace std::placeholders;
10 InputMappingDialog::InputMappingDialog()
11 : ui(new Ui::InputMappingDialog),
12 mapping(global_mixer->get_audio_mixer()->get_input_mapping()),
14 card_names(global_mixer->get_audio_mixer()->get_names())
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);
26 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
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);
34 ui->table->setRowCount(mapping.buses.size());
35 for (unsigned row = 0; row < mapping.buses.size(); ++row) {
36 fill_row_from_bus(row, mapping.buses[row]);
40 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
42 QString name(QString::fromStdString(bus.name));
43 ui->table->setItem(row, 0, new QTableWidgetItem(name));
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 + " "));
51 switch (bus.input_source_type) {
52 case InputSourceType::SILENCE:
53 card_combo->setCurrentIndex(0);
55 case InputSourceType::CAPTURE_CARD:
56 card_combo->setCurrentIndex(mapping.buses[row].input_source_index + 1);
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);
65 setup_channel_choices_from_bus(row, bus);
68 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
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 (bus.input_source_type == InputSourceType::CAPTURE_CARD) {
75 for (unsigned source = 0; source < 8; ++source) { // TODO: Ask the card about number of channels, and names.
77 snprintf(buf, sizeof(buf), "Channel %u ", source + 1);
78 channel_combo->addItem(QString(buf));
80 channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
82 channel_combo->setCurrentIndex(0);
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);
90 void InputMappingDialog::ok_clicked()
92 global_mixer->get_audio_mixer()->set_input_mapping(mapping);
96 void InputMappingDialog::cancel_clicked()
98 global_mixer->get_audio_mixer()->set_input_mapping(old_mapping);
102 void InputMappingDialog::cell_changed(int row, int column)
105 // Spurious; only really the name column should fire these.
108 mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
111 void InputMappingDialog::card_selected(unsigned row, int index)
114 mapping.buses[row].input_source_type = InputSourceType::SILENCE;
116 mapping.buses[row].input_source_type = InputSourceType::CAPTURE_CARD;
117 mapping.buses[row].input_source_index = index - 1;
119 setup_channel_choices_from_bus(row, mapping.buses[row]);
122 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
124 mapping.buses[row].source_channel[channel] = index - 1;
127 void InputMappingDialog::add_clicked()
129 InputMapping::Bus new_bus;
130 new_bus.name = "New input";
131 new_bus.input_source_type = InputSourceType::SILENCE;
132 mapping.buses.push_back(new_bus);
133 ui->table->setRowCount(mapping.buses.size());
135 unsigned row = mapping.buses.size() - 1;
136 fill_row_from_bus(row, new_bus);
137 ui->table->editItem(ui->table->item(row, 0)); // Start editing the name.