]> git.sesse.net Git - nageru/blobdiff - input_mapping_dialog.cpp
Make AudioMixer ready for indexing on multiple types of devices.
[nageru] / input_mapping_dialog.cpp
index f56ab62e2d0939a67bb96c69a18d831c40cc6b48..076cc2fb2202c6ae222cf4838f2e3d8d2c26e066 100644 (file)
@@ -14,13 +14,20 @@ InputMappingDialog::InputMappingDialog()
          card_names(global_mixer->get_audio_mixer()->get_names())
 {
        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)
@@ -48,12 +55,12 @@ void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus
        for (const string &name : card_names) {
                card_combo->addItem(QString::fromStdString(name + "   "));
        }
-       switch (bus.input_source_type) {
+       switch (bus.device.type) {
        case InputSourceType::SILENCE:
                card_combo->setCurrentIndex(0);
                break;
        case InputSourceType::CAPTURE_CARD:
-               card_combo->setCurrentIndex(mapping.buses[row].input_source_index + 1);
+               card_combo->setCurrentIndex(mapping.buses[row].device.index + 1);
                break;
        default:
                assert(false);
@@ -71,7 +78,7 @@ 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) {
+               if (bus.device.type == InputSourceType::CAPTURE_CARD) {
                        for (unsigned source = 0; source < 8; ++source) {  // TODO: Ask the card about number of channels, and names.
                                char buf[256];
                                snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
@@ -111,10 +118,10 @@ void InputMappingDialog::cell_changed(int row, int column)
 void InputMappingDialog::card_selected(unsigned row, int index)
 {
        if (index == 0) {
-               mapping.buses[row].input_source_type = InputSourceType::SILENCE;
+               mapping.buses[row].device.type = InputSourceType::SILENCE;
        } else {
-               mapping.buses[row].input_source_type = InputSourceType::CAPTURE_CARD;
-               mapping.buses[row].input_source_index = index - 1;
+               mapping.buses[row].device.type = InputSourceType::CAPTURE_CARD;
+               mapping.buses[row].device.index = index - 1;
        }
        setup_channel_choices_from_bus(row, mapping.buses[row]);
 }
@@ -126,13 +133,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);
 }