]> git.sesse.net Git - nageru/commitdiff
Make most of the input mapping dialog actually work.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 31 Jul 2016 11:25:07 +0000 (13:25 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 19 Oct 2016 22:55:44 +0000 (00:55 +0200)
You can now add new mappings and edit them, but not delete existing ones
or move them around. OK/Cancel works and actually sets the mapping
in the AudioMixer, but the AudioMixer doesn't care (and neither does the UI).

input_mapping_dialog.cpp
input_mapping_dialog.h
ui_input_mapping.ui

index e411318f7ae2636020f19156aabf63a00d66a223..424e32049431b8dea7cbd8798bb842397d120f60 100644 (file)
@@ -5,18 +5,25 @@
 #include <QComboBox>
 
 using namespace std;
+using namespace std::placeholders;
 
 InputMappingDialog::InputMappingDialog()
-       : ui(new Ui::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())
 {
        ui->setupUi(this);
 
-       //connect(ui->button_box, &QDialogButtonBox::accepted, [this]{ this->close(); });
-       vector<string> card_names = global_mixer->get_audio_mixer()->get_names();
-       fill_ui_from_mapping(global_mixer->get_audio_mixer()->get_input_mapping(), card_names);
+       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);
 }
 
-void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping, const vector<string> &card_names)
+void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
 {
        ui->table->verticalHeader()->hide();
        ui->table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
@@ -26,35 +33,41 @@ void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping, const
 
        ui->table->setRowCount(mapping.inputs.size());
        for (unsigned row = 0; row < mapping.inputs.size(); ++row) {
-               // TODO: Mark as some sort of header (by means of background color, probably).
-               QString name(QString::fromStdString(mapping.inputs[row].name));
-               ui->table->setItem(row, 0, new QTableWidgetItem(name));
-
-               // Card choices.
-               QComboBox *card_combo = new QComboBox;
-               card_combo->addItem(QString("(none)   "));
-               for (const string &name : card_names) {
-                       card_combo->addItem(QString::fromStdString(name + "   "));
-               }
-               switch (mapping.inputs[row].input_source_type) {
-               case InputSourceType::SILENCE:
-                       card_combo->setCurrentIndex(0);
-                       break;
-               case InputSourceType::CAPTURE_CARD:
-                       card_combo->setCurrentIndex(mapping.inputs[row].input_source_index + 1);
-                       break;
-               default:
-                       assert(false);
-               }
-               ui->table->setCellWidget(row, 1, card_combo);
+               fill_row_from_input(row, mapping.inputs[row]);
+       }
+}
 
-               // Left and right channel.
-               fill_channel_ui_from_mapping(row, mapping.inputs[row]);
+void InputMappingDialog::fill_row_from_input(unsigned row, const InputMapping::Input &input)
+{
+       QString name(QString::fromStdString(input.name));
+       ui->table->setItem(row, 0, new QTableWidgetItem(name));
+
+       // Card choices.
+       QComboBox *card_combo = new QComboBox;
+       card_combo->addItem(QString("(none)   "));
+       for (const string &name : card_names) {
+               card_combo->addItem(QString::fromStdString(name + "   "));
        }
+       switch (input.input_source_type) {
+       case InputSourceType::SILENCE:
+               card_combo->setCurrentIndex(0);
+               break;
+       case InputSourceType::CAPTURE_CARD:
+               card_combo->setCurrentIndex(mapping.inputs[row].input_source_index + 1);
+               break;
+       default:
+               assert(false);
+       }
+       connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
+               bind(&InputMappingDialog::card_selected, this, row, _1));
+       ui->table->setCellWidget(row, 1, card_combo);
+
+       setup_channel_choices_from_input(row, input);
 }
 
-void InputMappingDialog::fill_channel_ui_from_mapping(unsigned row, const InputMapping::Input &input)
+void InputMappingDialog::setup_channel_choices_from_input(unsigned row, const InputMapping::Input &input)
 {
+       // Left and right channel.
        for (unsigned channel = 0; channel < 2; ++channel) {
                QComboBox *channel_combo = new QComboBox;
                channel_combo->addItem(QString("(none)"));
@@ -64,8 +77,62 @@ void InputMappingDialog::fill_channel_ui_from_mapping(unsigned row, const InputM
                                snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
                                channel_combo->addItem(QString(buf));
                        }
+                       channel_combo->setCurrentIndex(input.source_channel[channel] + 1);
+               } else {
+                       channel_combo->setCurrentIndex(0);
                }
-               channel_combo->setCurrentIndex(input.source_channel[channel] + 1);
+               connect(channel_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
+                       bind(&InputMappingDialog::channel_selected, this, row, channel, _1));
                ui->table->setCellWidget(row, 2 + channel, channel_combo);
        }
 }
+
+void InputMappingDialog::ok_clicked()
+{
+       global_mixer->get_audio_mixer()->set_input_mapping(mapping);
+       accept();
+}
+
+void InputMappingDialog::cancel_clicked()
+{
+       global_mixer->get_audio_mixer()->set_input_mapping(old_mapping);
+       reject();
+}
+
+void InputMappingDialog::cell_changed(int row, int column)
+{
+       if (column != 0) {
+               // Spurious; only really the name column should fire these.
+               return;
+       }
+       mapping.inputs[row].name = ui->table->item(row, column)->text().toStdString();
+}
+
+void InputMappingDialog::card_selected(unsigned row, int index)
+{
+       if (index == 0) {
+               mapping.inputs[row].input_source_type = InputSourceType::SILENCE;
+       } else {
+               mapping.inputs[row].input_source_type = InputSourceType::CAPTURE_CARD;
+               mapping.inputs[row].input_source_index = index - 1;
+       }
+       setup_channel_choices_from_input(row, mapping.inputs[row]);
+}
+
+void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
+{
+       mapping.inputs[row].source_channel[channel] = index - 1;
+}
+
+void InputMappingDialog::add_clicked()
+{
+       InputMapping::Input new_input;
+       new_input.name = "New input";
+       new_input.input_source_type = InputSourceType::SILENCE;
+       mapping.inputs.push_back(new_input);
+       ui->table->setRowCount(mapping.inputs.size());
+
+       unsigned row = mapping.inputs.size() - 1;
+       fill_row_from_input(row, new_input);
+       ui->table->editItem(ui->table->item(row, 0));  // Start editing the name.
+}
index 4eb5e4cd75b62674855f8c1ad298cb8536c9b1b4..91246f563f05cddb5212f5737244297295d9959a 100644 (file)
@@ -20,10 +20,25 @@ public:
        InputMappingDialog();
 
 private:
-       void fill_ui_from_mapping(const InputMapping &mapping, const std::vector<std::string> &card_names);
-       void fill_channel_ui_from_mapping(unsigned row, const InputMapping::Input &input);
+       void fill_ui_from_mapping(const InputMapping &mapping);
+       void fill_row_from_input(unsigned row, const InputMapping::Input &input);
+       void setup_channel_choices_from_input(unsigned row, const InputMapping::Input &input);
+       void cell_changed(int row, int column);
+       void card_selected(unsigned row, int index);
+       void channel_selected(unsigned row, unsigned channel, int index);
+       void ok_clicked();
+       void cancel_clicked();
+       void add_clicked();
 
        Ui::InputMappingDialog *ui;
+       InputMapping mapping;  // Under edit. Will be committed on OK.
+
+       // The old mapping. Will be re-committed on cancel, so that we
+       // unhold all the unused devices (otherwise they would be
+       // held forever).
+       InputMapping old_mapping;
+
+       const std::vector<std::string> card_names;
 };
 
 #endif  // !defined(_INPUT_MAPPING_DIALOG_H)
index f2c28ab911a6793e2184acc16f4e2b36d4a73031..7b45338c856b035e0e134c5732dad3cbc489c146 100644 (file)
@@ -41,7 +41,7 @@
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout_2" stretch="0,0,1,0,0,1,0">
      <item>
-      <widget class="QPushButton" name="pushButton">
+      <widget class="QPushButton" name="add_button">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
          <horstretch>0</horstretch>
@@ -64,7 +64,7 @@
       </widget>
      </item>
      <item>
-      <widget class="QPushButton" name="pushButton_2">
+      <widget class="QPushButton" name="remove_button">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
          <horstretch>0</horstretch>
       </spacer>
      </item>
      <item>
-      <widget class="QPushButton" name="pushButton_3">
+      <widget class="QPushButton" name="up_button">
        <property name="maximumSize">
         <size>
          <width>30</width>
       </widget>
      </item>
      <item>
-      <widget class="QPushButton" name="pushButton_4">
+      <widget class="QPushButton" name="down_button">
        <property name="maximumSize">
         <size>
          <width>30</width>
       </spacer>
      </item>
      <item>
-      <widget class="QDialogButtonBox" name="buttonBox">
+      <widget class="QDialogButtonBox" name="ok_cancel_buttons">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
   </layout>
  </widget>
  <resources/>
- <connections>
-  <connection>
-   <sender>buttonBox</sender>
-   <signal>accepted()</signal>
-   <receiver>Dialog</receiver>
-   <slot>accept()</slot>
-   <hints>
-    <hint type="sourcelabel">
-     <x>248</x>
-     <y>254</y>
-    </hint>
-    <hint type="destinationlabel">
-     <x>157</x>
-     <y>274</y>
-    </hint>
-   </hints>
-  </connection>
-  <connection>
-   <sender>buttonBox</sender>
-   <signal>rejected()</signal>
-   <receiver>Dialog</receiver>
-   <slot>reject()</slot>
-   <hints>
-    <hint type="sourcelabel">
-     <x>316</x>
-     <y>260</y>
-    </hint>
-    <hint type="destinationlabel">
-     <x>286</x>
-     <y>274</y>
-    </hint>
-   </hints>
-  </connection>
- </connections>
+ <connections/>
 </ui>