]> git.sesse.net Git - nageru/blob - input_mapping_dialog.cpp
Make remove/up/down buttons work.
[nageru] / input_mapping_dialog.cpp
1 #include "input_mapping_dialog.h"
2
3 #include "ui_input_mapping.h"
4
5 #include <QComboBox>
6
7 using namespace std;
8 using namespace std::placeholders;
9
10 InputMappingDialog::InputMappingDialog()
11         : ui(new Ui::InputMappingDialog),
12           mapping(global_mixer->get_audio_mixer()->get_input_mapping()),
13           old_mapping(mapping),
14           card_names(global_mixer->get_audio_mixer()->get_names())
15 {
16         ui->setupUi(this);
17         ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
18         ui->table->setSelectionMode(QAbstractItemView::SingleSelection);  // Makes implementing moving easier for now.
19
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));
28
29         update_button_state();
30         connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
31 }
32
33 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
34 {
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);
40
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]);
44         }
45 }
46
47 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
48 {
49         QString name(QString::fromStdString(bus.name));
50         ui->table->setItem(row, 0, new QTableWidgetItem(name));
51
52         // Card choices.
53         QComboBox *card_combo = new QComboBox;
54         card_combo->addItem(QString("(none)   "));
55         for (const string &name : card_names) {
56                 card_combo->addItem(QString::fromStdString(name + "   "));
57         }
58         switch (bus.input_source_type) {
59         case InputSourceType::SILENCE:
60                 card_combo->setCurrentIndex(0);
61                 break;
62         case InputSourceType::CAPTURE_CARD:
63                 card_combo->setCurrentIndex(mapping.buses[row].input_source_index + 1);
64                 break;
65         default:
66                 assert(false);
67         }
68         connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
69                 bind(&InputMappingDialog::card_selected, this, row, _1));
70         ui->table->setCellWidget(row, 1, card_combo);
71
72         setup_channel_choices_from_bus(row, bus);
73 }
74
75 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
76 {
77         // Left and right channel.
78         for (unsigned channel = 0; channel < 2; ++channel) {
79                 QComboBox *channel_combo = new QComboBox;
80                 channel_combo->addItem(QString("(none)"));
81                 if (bus.input_source_type == InputSourceType::CAPTURE_CARD) {
82                         for (unsigned source = 0; source < 8; ++source) {  // TODO: Ask the card about number of channels, and names.
83                                 char buf[256];
84                                 snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
85                                 channel_combo->addItem(QString(buf));
86                         }
87                         channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
88                 } else {
89                         channel_combo->setCurrentIndex(0);
90                 }
91                 connect(channel_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
92                         bind(&InputMappingDialog::channel_selected, this, row, channel, _1));
93                 ui->table->setCellWidget(row, 2 + channel, channel_combo);
94         }
95 }
96
97 void InputMappingDialog::ok_clicked()
98 {
99         global_mixer->get_audio_mixer()->set_input_mapping(mapping);
100         accept();
101 }
102
103 void InputMappingDialog::cancel_clicked()
104 {
105         global_mixer->get_audio_mixer()->set_input_mapping(old_mapping);
106         reject();
107 }
108
109 void InputMappingDialog::cell_changed(int row, int column)
110 {
111         if (column != 0) {
112                 // Spurious; only really the name column should fire these.
113                 return;
114         }
115         mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
116 }
117
118 void InputMappingDialog::card_selected(unsigned row, int index)
119 {
120         if (index == 0) {
121                 mapping.buses[row].input_source_type = InputSourceType::SILENCE;
122         } else {
123                 mapping.buses[row].input_source_type = InputSourceType::CAPTURE_CARD;
124                 mapping.buses[row].input_source_index = index - 1;
125         }
126         setup_channel_choices_from_bus(row, mapping.buses[row]);
127 }
128
129 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
130 {
131         mapping.buses[row].source_channel[channel] = index - 1;
132 }
133
134 void InputMappingDialog::add_clicked()
135 {
136         QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
137         ui->table->setRangeSelected(all, false);
138
139         InputMapping::Bus new_bus;
140         new_bus.name = "New input";
141         new_bus.input_source_type = InputSourceType::SILENCE;
142         mapping.buses.push_back(new_bus);
143         ui->table->setRowCount(mapping.buses.size());
144
145         unsigned row = mapping.buses.size() - 1;
146         fill_row_from_bus(row, new_bus);
147         ui->table->editItem(ui->table->item(row, 0));  // Start editing the name.
148         update_button_state();
149 }
150
151 void InputMappingDialog::remove_clicked()
152 {
153         assert(ui->table->rowCount() != 0);
154
155         set<int, greater<int>> rows_to_delete;  // Need to remove in reverse order.
156         for (const QTableWidgetSelectionRange &range : ui->table->selectedRanges()) {
157                 for (int row = range.topRow(); row <= range.bottomRow(); ++row) {
158                         rows_to_delete.insert(row);
159                 }
160         }
161         if (rows_to_delete.empty()) {
162                 rows_to_delete.insert(ui->table->rowCount() - 1);
163         }
164
165         for (int row : rows_to_delete) {
166                 ui->table->removeRow(row);
167                 mapping.buses.erase(mapping.buses.begin() + row);
168         }
169         update_button_state();
170 }
171
172 void InputMappingDialog::updown_clicked(int direction)
173 {
174         assert(ui->table->selectedRanges().size() == 1);
175         const QTableWidgetSelectionRange &range = ui->table->selectedRanges()[0];
176         int a_row = range.bottomRow();
177         int b_row = range.bottomRow() + direction;
178
179         swap(mapping.buses[a_row], mapping.buses[b_row]);
180         fill_row_from_bus(a_row, mapping.buses[a_row]);
181         fill_row_from_bus(b_row, mapping.buses[b_row]);
182
183         QTableWidgetSelectionRange a_sel(a_row, 0, a_row, ui->table->columnCount() - 1);
184         QTableWidgetSelectionRange b_sel(b_row, 0, b_row, ui->table->columnCount() - 1);
185         ui->table->setRangeSelected(a_sel, false);
186         ui->table->setRangeSelected(b_sel, true);
187 }
188
189 void InputMappingDialog::update_button_state()
190 {
191         ui->add_button->setDisabled(mapping.buses.size() >= MAX_BUSES);
192         ui->remove_button->setDisabled(mapping.buses.size() == 0);
193         ui->up_button->setDisabled(
194                 ui->table->selectedRanges().empty() ||
195                 ui->table->selectedRanges()[0].bottomRow() == 0);
196         ui->down_button->setDisabled(
197                 ui->table->selectedRanges().empty() ||
198                 ui->table->selectedRanges()[0].bottomRow() == ui->table->rowCount() - 1);
199 }