]> git.sesse.net Git - nageru/blobdiff - input_mapping_dialog.cpp
Communicate number of channels to the input mapping dialog.
[nageru] / input_mapping_dialog.cpp
index f56ab62e2d0939a67bb96c69a18d831c40cc6b48..2d40cc2a500a839557e46aef541fa1c52677a86f 100644 (file)
@@ -11,16 +11,23 @@ InputMappingDialog::InputMappingDialog()
        : ui(new Ui::InputMappingDialog),
          mapping(global_mixer->get_audio_mixer()->get_input_mapping()),
          old_mapping(mapping),
-         card_names(global_mixer->get_audio_mixer()->get_names())
+         devices(global_mixer->get_audio_mixer()->get_devices())
 {
        ui->setupUi(this);
+       ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
+       ui->table->setSelectionMode(QAbstractItemView::SingleSelection);  // Makes implementing moving easier for now.
 
        fill_ui_from_mapping(mapping);
        connect(ui->table, &QTableWidget::cellChanged, this, &InputMappingDialog::cell_changed);
        connect(ui->ok_cancel_buttons, &QDialogButtonBox::accepted, this, &InputMappingDialog::ok_clicked);
        connect(ui->ok_cancel_buttons, &QDialogButtonBox::rejected, this, &InputMappingDialog::cancel_clicked);
        connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
-       //connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
+       connect(ui->remove_button, &QPushButton::clicked, this, &InputMappingDialog::remove_clicked);
+       connect(ui->up_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, -1));
+       connect(ui->down_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, 1));
+
+       update_button_state();
+       connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
 }
 
 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
@@ -44,22 +51,19 @@ void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus
 
        // Card choices.
        QComboBox *card_combo = new QComboBox;
+       unsigned current_index = 0;
        card_combo->addItem(QString("(none)   "));
-       for (const string &name : card_names) {
-               card_combo->addItem(QString::fromStdString(name + "   "));
-       }
-       switch (bus.input_source_type) {
-       case InputSourceType::SILENCE:
-               card_combo->setCurrentIndex(0);
-               break;
-       case InputSourceType::CAPTURE_CARD:
-               card_combo->setCurrentIndex(mapping.buses[row].input_source_index + 1);
-               break;
-       default:
-               assert(false);
+       for (const auto &spec_and_info : devices) {
+               ++current_index;
+               card_combo->addItem(
+                       QString::fromStdString(spec_and_info.second.name + "   "),
+                       qulonglong(DeviceSpec_to_key(spec_and_info.first)));
+               if (bus.device == spec_and_info.first) {
+                       card_combo->setCurrentIndex(current_index);
+               }
        }
        connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
-               bind(&InputMappingDialog::card_selected, this, row, _1));
+               bind(&InputMappingDialog::card_selected, this, card_combo, row, _1));
        ui->table->setCellWidget(row, 1, card_combo);
 
        setup_channel_choices_from_bus(row, bus);
@@ -71,8 +75,11 @@ void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const Inpu
        for (unsigned channel = 0; channel < 2; ++channel) {
                QComboBox *channel_combo = new QComboBox;
                channel_combo->addItem(QString("(none)"));
-               if (bus.input_source_type == InputSourceType::CAPTURE_CARD) {
-                       for (unsigned source = 0; source < 8; ++source) {  // TODO: Ask the card about number of channels, and names.
+               if (bus.device.type == InputSourceType::CAPTURE_CARD) {
+                       auto device_it = devices.find(bus.device);
+                       assert(device_it != devices.end());
+                       unsigned num_device_channels = device_it->second.num_channels;
+                       for (unsigned source = 0; source < num_device_channels; ++source) {
                                char buf[256];
                                snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
                                channel_combo->addItem(QString(buf));
@@ -108,14 +115,10 @@ void InputMappingDialog::cell_changed(int row, int column)
        mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
 }
 
-void InputMappingDialog::card_selected(unsigned row, int index)
+void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
 {
-       if (index == 0) {
-               mapping.buses[row].input_source_type = InputSourceType::SILENCE;
-       } else {
-               mapping.buses[row].input_source_type = InputSourceType::CAPTURE_CARD;
-               mapping.buses[row].input_source_index = index - 1;
-       }
+       uint64_t key = card_combo->itemData(index).toULongLong();
+       mapping.buses[row].device = key_to_DeviceSpec(key);
        setup_channel_choices_from_bus(row, mapping.buses[row]);
 }
 
@@ -126,13 +129,67 @@ void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int in
 
 void InputMappingDialog::add_clicked()
 {
+       QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
+       ui->table->setRangeSelected(all, false);
+
        InputMapping::Bus new_bus;
        new_bus.name = "New input";
-       new_bus.input_source_type = InputSourceType::SILENCE;
+       new_bus.device.type = InputSourceType::SILENCE;
        mapping.buses.push_back(new_bus);
        ui->table->setRowCount(mapping.buses.size());
 
        unsigned row = mapping.buses.size() - 1;
        fill_row_from_bus(row, new_bus);
        ui->table->editItem(ui->table->item(row, 0));  // Start editing the name.
+       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);
+       }
+       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]);
+       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::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);
 }