+ update_button_state();
+}
+
+void InputMappingDialog::remove_clicked()
+{
+ assert(ui->table->rowCount() != 0);
+
+ set<int, greater<int>> rows_to_delete; // Need to remove in reverse order.
+ for (const QTableWidgetSelectionRange &range : ui->table->selectedRanges()) {
+ for (int row = range.topRow(); row <= range.bottomRow(); ++row) {
+ rows_to_delete.insert(row);
+ }
+ }
+ if (rows_to_delete.empty()) {
+ rows_to_delete.insert(ui->table->rowCount() - 1);
+ }
+
+ for (int row : rows_to_delete) {
+ ui->table->removeRow(row);
+ mapping.buses.erase(mapping.buses.begin() + row);
+ bus_settings.erase(bus_settings.begin() + row);
+ }
+ update_button_state();
+}
+
+void InputMappingDialog::updown_clicked(int direction)
+{
+ assert(ui->table->selectedRanges().size() == 1);
+ const QTableWidgetSelectionRange &range = ui->table->selectedRanges()[0];
+ int a_row = range.bottomRow();
+ int b_row = range.bottomRow() + direction;
+
+ swap(mapping.buses[a_row], mapping.buses[b_row]);
+ swap(bus_settings[a_row], bus_settings[b_row]);
+ fill_row_from_bus(a_row, mapping.buses[a_row]);
+ fill_row_from_bus(b_row, mapping.buses[b_row]);
+
+ QTableWidgetSelectionRange a_sel(a_row, 0, a_row, ui->table->columnCount() - 1);
+ QTableWidgetSelectionRange b_sel(b_row, 0, b_row, ui->table->columnCount() - 1);
+ ui->table->setRangeSelected(a_sel, false);
+ ui->table->setRangeSelected(b_sel, true);
+}
+
+void InputMappingDialog::save_clicked()
+{
+ QString filename = QFileDialog::getSaveFileName(this,
+ "Save input mapping", QString(), tr("Mapping files (*.mapping)"));
+ if (!filename.endsWith(".mapping")) {
+ filename += ".mapping";
+ }
+ if (!save_input_mapping_to_file(devices, mapping, filename.toStdString())) {
+ QMessageBox box;
+ box.setText("Could not save mapping to '" + filename + "'. Check that you have the right permissions and try again.");
+ box.exec();
+ }
+}
+
+void InputMappingDialog::load_clicked()
+{
+ QString filename = QFileDialog::getOpenFileName(this,
+ "Load input mapping", QString(), tr("Mapping files (*.mapping)"));
+ InputMapping new_mapping;
+ if (!load_input_mapping_from_file(devices, filename.toStdString(), &new_mapping)) {
+ QMessageBox box;
+ box.setText("Could not load mapping from '" + filename + "'. Check that the file exists, has the right permissions and is valid.");
+ box.exec();
+ return;
+ }
+
+ mapping = new_mapping;
+ bus_settings.clear();
+ for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
+ bus_settings.push_back(global_audio_mixer->get_bus_settings(bus_index));
+ }
+ devices = global_audio_mixer->get_devices(); // New dead cards may have been made.
+ fill_ui_from_mapping(mapping);
+}
+
+void InputMappingDialog::update_button_state()
+{
+ ui->add_button->setDisabled(mapping.buses.size() >= MAX_BUSES);
+ ui->remove_button->setDisabled(mapping.buses.size() == 0);
+ ui->up_button->setDisabled(
+ ui->table->selectedRanges().empty() ||
+ ui->table->selectedRanges()[0].bottomRow() == 0);
+ ui->down_button->setDisabled(
+ ui->table->selectedRanges().empty() ||
+ ui->table->selectedRanges()[0].bottomRow() == ui->table->rowCount() - 1);