]> git.sesse.net Git - nageru/commitdiff
Make remove/up/down buttons work.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 2 Aug 2016 22:10:27 +0000 (00:10 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 19 Oct 2016 22:55:44 +0000 (00:55 +0200)
input_mapping_dialog.cpp
input_mapping_dialog.h

index f56ab62e2d0939a67bb96c69a18d831c40cc6b48..a34b6cb955ec8705d1108bca47d60bdc871e0741 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)
@@ -126,6 +133,9 @@ 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;
@@ -135,4 +145,55 @@ void InputMappingDialog::add_clicked()
        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);
 }
index 05bc223ee03efc991fbbdd275b5433e6341ccc25..90be52acffa8bb82dddea0d8ef891cd1c88cd3b6 100644 (file)
@@ -29,6 +29,9 @@ private:
        void ok_clicked();
        void cancel_clicked();
        void add_clicked();
+       void remove_clicked();
+       void updown_clicked(int direction);
+       void update_button_state();
 
        Ui::InputMappingDialog *ui;
        InputMapping mapping;  // Under edit. Will be committed on OK.