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 devices(global_mixer->get_audio_mixer()->get_devices())
17 ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
18 ui->table->setSelectionMode(QAbstractItemView::SingleSelection); // Makes implementing moving easier for now.
20 fill_ui_from_mapping(mapping);
21 connect(ui->table, &QTableWidget::cellChanged, this, &InputMappingDialog::cell_changed);
22 connect(ui->ok_cancel_buttons, &QDialogButtonBox::accepted, this, &InputMappingDialog::ok_clicked);
23 connect(ui->ok_cancel_buttons, &QDialogButtonBox::rejected, this, &InputMappingDialog::cancel_clicked);
24 connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
25 connect(ui->remove_button, &QPushButton::clicked, this, &InputMappingDialog::remove_clicked);
26 connect(ui->up_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, -1));
27 connect(ui->down_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, 1));
29 update_button_state();
30 connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
33 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
35 ui->table->verticalHeader()->hide();
36 ui->table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
37 ui->table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
38 ui->table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
39 ui->table->horizontalHeader()->setSectionsClickable(false);
41 ui->table->setRowCount(mapping.buses.size());
42 for (unsigned row = 0; row < mapping.buses.size(); ++row) {
43 fill_row_from_bus(row, mapping.buses[row]);
47 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
49 QString name(QString::fromStdString(bus.name));
50 ui->table->setItem(row, 0, new QTableWidgetItem(name));
53 QComboBox *card_combo = new QComboBox;
54 unsigned current_index = 0;
55 card_combo->addItem(QString("(none) "));
56 for (const auto &spec_and_info : devices) {
59 QString::fromStdString(spec_and_info.second.name + " "),
60 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
61 if (bus.device == spec_and_info.first) {
62 card_combo->setCurrentIndex(current_index);
65 connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
66 bind(&InputMappingDialog::card_selected, this, card_combo, row, _1));
67 ui->table->setCellWidget(row, 1, card_combo);
69 setup_channel_choices_from_bus(row, bus);
72 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
74 // Left and right channel.
75 for (unsigned channel = 0; channel < 2; ++channel) {
76 QComboBox *channel_combo = new QComboBox;
77 channel_combo->addItem(QString("(none)"));
78 if (bus.device.type == InputSourceType::CAPTURE_CARD ||
79 bus.device.type == InputSourceType::ALSA_INPUT) {
80 auto device_it = devices.find(bus.device);
81 assert(device_it != devices.end());
82 unsigned num_device_channels = device_it->second.num_channels;
83 for (unsigned source = 0; source < num_device_channels; ++source) {
85 snprintf(buf, sizeof(buf), "Channel %u ", source + 1);
86 channel_combo->addItem(QString(buf));
88 channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
90 channel_combo->setCurrentIndex(0);
92 connect(channel_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
93 bind(&InputMappingDialog::channel_selected, this, row, channel, _1));
94 ui->table->setCellWidget(row, 2 + channel, channel_combo);
98 void InputMappingDialog::ok_clicked()
100 global_mixer->get_audio_mixer()->set_input_mapping(mapping);
104 void InputMappingDialog::cancel_clicked()
106 global_mixer->get_audio_mixer()->set_input_mapping(old_mapping);
110 void InputMappingDialog::cell_changed(int row, int column)
113 // Spurious; only really the name column should fire these.
116 mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
119 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
121 uint64_t key = card_combo->itemData(index).toULongLong();
122 mapping.buses[row].device = key_to_DeviceSpec(key);
123 setup_channel_choices_from_bus(row, mapping.buses[row]);
126 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
128 mapping.buses[row].source_channel[channel] = index - 1;
131 void InputMappingDialog::add_clicked()
133 QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
134 ui->table->setRangeSelected(all, false);
136 InputMapping::Bus new_bus;
137 new_bus.name = "New input";
138 new_bus.device.type = InputSourceType::SILENCE;
139 mapping.buses.push_back(new_bus);
140 ui->table->setRowCount(mapping.buses.size());
142 unsigned row = mapping.buses.size() - 1;
143 fill_row_from_bus(row, new_bus);
144 ui->table->editItem(ui->table->item(row, 0)); // Start editing the name.
145 update_button_state();
148 void InputMappingDialog::remove_clicked()
150 assert(ui->table->rowCount() != 0);
152 set<int, greater<int>> rows_to_delete; // Need to remove in reverse order.
153 for (const QTableWidgetSelectionRange &range : ui->table->selectedRanges()) {
154 for (int row = range.topRow(); row <= range.bottomRow(); ++row) {
155 rows_to_delete.insert(row);
158 if (rows_to_delete.empty()) {
159 rows_to_delete.insert(ui->table->rowCount() - 1);
162 for (int row : rows_to_delete) {
163 ui->table->removeRow(row);
164 mapping.buses.erase(mapping.buses.begin() + row);
166 update_button_state();
169 void InputMappingDialog::updown_clicked(int direction)
171 assert(ui->table->selectedRanges().size() == 1);
172 const QTableWidgetSelectionRange &range = ui->table->selectedRanges()[0];
173 int a_row = range.bottomRow();
174 int b_row = range.bottomRow() + direction;
176 swap(mapping.buses[a_row], mapping.buses[b_row]);
177 fill_row_from_bus(a_row, mapping.buses[a_row]);
178 fill_row_from_bus(b_row, mapping.buses[b_row]);
180 QTableWidgetSelectionRange a_sel(a_row, 0, a_row, ui->table->columnCount() - 1);
181 QTableWidgetSelectionRange b_sel(b_row, 0, b_row, ui->table->columnCount() - 1);
182 ui->table->setRangeSelected(a_sel, false);
183 ui->table->setRangeSelected(b_sel, true);
186 void InputMappingDialog::update_button_state()
188 ui->add_button->setDisabled(mapping.buses.size() >= MAX_BUSES);
189 ui->remove_button->setDisabled(mapping.buses.size() == 0);
190 ui->up_button->setDisabled(
191 ui->table->selectedRanges().empty() ||
192 ui->table->selectedRanges()[0].bottomRow() == 0);
193 ui->down_button->setDisabled(
194 ui->table->selectedRanges().empty() ||
195 ui->table->selectedRanges()[0].bottomRow() == ui->table->rowCount() - 1);