]> git.sesse.net Git - nageru/blobdiff - midi_mapping_dialog.h
Make the guess button work.
[nageru] / midi_mapping_dialog.h
index 17cdc180c0b4ffd96bf9c86fdc2deb1fb544a48f..efc770d28a1cc81433bd73cda828acd2e138062e 100644 (file)
@@ -3,10 +3,12 @@
 
 #include <QDialog>
 #include <string>
+#include <utility>
 #include <vector>
 #include <sys/time.h>
 
 #include "audio_mixer.h"
+#include "midi_mapper.h"
 #include "mixer.h"
 
 namespace Ui {
@@ -19,7 +21,7 @@ class QComboBox;
 class QSpinBox;
 class QTreeWidgetItem;
 
-class MIDIMappingDialog : public QDialog
+class MIDIMappingDialog : public QDialog, public ControllerReceiver
 {
        Q_OBJECT
 
@@ -27,6 +29,8 @@ public:
        MIDIMappingDialog(MIDIMapper *mapper);
        ~MIDIMappingDialog();
 
+       bool eventFilter(QObject *obj, QEvent *event) override;
+
        // For use in midi_mapping_dialog.cpp only.
        struct Control {
                std::string label;
@@ -34,9 +38,34 @@ public:
                int bank_field_number;  // In MIDIMappingProto.
        };
 
+       // ControllerReceiver interface. We only implement the raw events.
+       // All values are [0.0, 1.0].
+       void set_locut(float value) override {}
+       void set_limiter_threshold(float value) override {}
+       void set_makeup_gain(float value) override {}
+
+       void set_treble(unsigned bus_idx, float value) override {}
+       void set_mid(unsigned bus_idx, float value) override {}
+       void set_bass(unsigned bus_idx, float value) override {}
+       void set_gain(unsigned bus_idx, float value) override {}
+       void set_compressor_threshold(unsigned bus_idx, float value) override {}
+       void set_fader(unsigned bus_idx, float value) override {}
+
+       void toggle_locut(unsigned bus_idx) override {}
+       void toggle_auto_gain_staging(unsigned bus_idx) override {}
+       void toggle_compressor(unsigned bus_idx) override {}
+       void clear_peak(unsigned bus_idx) override {}
+
+       // Raw events; used for the editor dialog only.
+       void controller_changed(unsigned controller) override;
+       void note_on(unsigned note) override;
+
 public slots:
+       void guess_clicked();
        void ok_clicked();
        void cancel_clicked();
+       void save_clicked();
+       void load_clicked();
 
 private:
        static constexpr unsigned num_buses = 8;
@@ -46,15 +75,28 @@ private:
        enum class ControlType { CONTROLLER, BUTTON };
        void add_controls(const std::string &heading, ControlType control_type,
                          const MIDIMappingProto &mapping_proto, const std::vector<Control> &controls);
+       void fill_controls_from_mapping(const MIDIMappingProto &mapping_proto);
 
-       std::unique_ptr<MIDIMappingProto> construct_mapping_proto_from_ui();
+       // Tries to find a source bus and an offset to it that would give
+       // a consistent offset for the rest of the mappings in this bus.
+       // Returns -1 for the bus (the first element) if no consistent offset
+       // can be found.
+       std::pair<int, int> guess_offset(unsigned bus_idx);
+       bool bus_is_empty(unsigned bus_idx);
+
+       void update_guess_button_state();
+       int find_focus_bus();
 
+       std::unique_ptr<MIDIMappingProto> construct_mapping_proto_from_ui();
 
        Ui::MIDIMappingDialog *ui;
        MIDIMapper *mapper;
+       ControllerReceiver *old_receiver;
+       int last_focus_bus_idx{-1};
 
        // All controllers actually laid out on the grid (we need to store them
-       // so that we can read its values back into the new protobuf).
+       // so that we can move values back and forth between the controls and
+       // the protobuf on save/load).
        struct InstantiatedSpinner {
                QSpinBox *spinner;
                unsigned bus_idx;
@@ -67,6 +109,9 @@ private:
        std::vector<InstantiatedSpinner> controller_spinners;
        std::vector<InstantiatedSpinner> button_spinners;
        std::vector<InstantiatedComboBox> bank_combo_boxes;
+
+       // Keyed on bus index, then field number.
+       std::map<unsigned, std::map<unsigned, QSpinBox *>> spinners;
 };
 
 #endif  // !defined(_MIDI_MAPPING_DIALOG_H)