]> git.sesse.net Git - nageru/blob - midi_mapping_dialog.h
6a895ba081455911cb4578e7acdc96d159bb7461
[nageru] / midi_mapping_dialog.h
1 #ifndef _MIDI_MAPPING_DIALOG_H
2 #define _MIDI_MAPPING_DIALOG_H
3
4 #include <QDialog>
5 #include <string>
6 #include <utility>
7 #include <vector>
8 #include <sys/time.h>
9
10 #include "audio_mixer.h"
11 #include "midi_mapper.h"
12 #include "mixer.h"
13
14 namespace Ui {
15 class MIDIMappingDialog;
16 }  // namespace Ui
17
18 class MIDIMapper;
19 class MIDIMappingProto;
20 class QComboBox;
21 class QSpinBox;
22 class QTreeWidgetItem;
23
24 class MIDIMappingDialog : public QDialog, public ControllerReceiver
25 {
26         Q_OBJECT
27
28 public:
29         MIDIMappingDialog(MIDIMapper *mapper);
30         ~MIDIMappingDialog();
31
32         bool eventFilter(QObject *obj, QEvent *event) override;
33
34         // For use in midi_mapping_dialog.cpp only.
35         struct Control {
36                 std::string label;
37                 int field_number;  // In MIDIMappingBusProto.
38                 int bank_field_number;  // In MIDIMappingProto.
39         };
40
41         // ControllerReceiver interface. We only implement the raw events.
42         // All values are [0.0, 1.0].
43         void set_locut(float value) override {}
44         void set_limiter_threshold(float value) override {}
45         void set_makeup_gain(float value) override {}
46
47         void set_treble(unsigned bus_idx, float value) override {}
48         void set_mid(unsigned bus_idx, float value) override {}
49         void set_bass(unsigned bus_idx, float value) override {}
50         void set_gain(unsigned bus_idx, float value) override {}
51         void set_compressor_threshold(unsigned bus_idx, float value) override {}
52         void set_fader(unsigned bus_idx, float value) override {}
53
54         void toggle_locut(unsigned bus_idx) override {}
55         void toggle_auto_gain_staging(unsigned bus_idx) override {}
56         void toggle_compressor(unsigned bus_idx) override {}
57         void clear_peak(unsigned bus_idx) override {}
58         void toggle_limiter() override {}
59         void toggle_auto_makeup_gain() override {}
60
61         void clear_all_highlights() override {}
62
63         void highlight_locut(bool highlight) override {}
64         void highlight_limiter_threshold(bool highlight) override {}
65         void highlight_makeup_gain(bool highlight) override {}
66
67         void highlight_treble(unsigned bus_idx, bool highlight) override {}
68         void highlight_mid(unsigned bus_idx, bool highlight) override {}
69         void highlight_bass(unsigned bus_idx, bool highlight) override {}
70         void highlight_gain(unsigned bus_idx, bool highlight) override {}
71         void highlight_compressor_threshold(unsigned bus_idx, bool highlight) override {}
72         void highlight_fader(unsigned bus_idx, bool highlight) override {}
73
74         void highlight_toggle_locut(unsigned bus_idx, bool highlight) override {}
75         void highlight_toggle_auto_gain_staging(unsigned bus_idx, bool highlight) override {}
76         void highlight_toggle_compressor(unsigned bus_idx, bool highlight) override {}
77         void highlight_clear_peak(unsigned bus_idx, bool highlight) override {}
78         void highlight_toggle_limiter(bool highlight) override {}
79         void highlight_toggle_auto_makeup_gain(bool highlight) override {}
80
81         // Raw events; used for the editor dialog only.
82         void controller_changed(unsigned controller) override;
83         void note_on(unsigned note) override;
84
85 public slots:
86         void guess_clicked(bool limit_to_group);
87         void ok_clicked();
88         void cancel_clicked();
89         void save_clicked();
90         void load_clicked();
91
92 private:
93         static constexpr unsigned num_buses = 8;
94
95         // Each spinner belongs to exactly one group, corresponding to the
96         // subheadings in the UI. This is so that we can extrapolate data
97         // across only single groups if need be.
98         enum class SpinnerGroup {
99                 ALL_GROUPS = -1,
100                 PER_BUS_CONTROLLERS,
101                 PER_BUS_BUTTONS,
102                 GLOBAL_CONTROLLERS,
103                 GLOBAL_BUTTONS
104         };
105
106         void add_bank_selector(QTreeWidgetItem *item, const MIDIMappingProto &mapping_proto, int bank_field_number);
107         
108         enum class ControlType { CONTROLLER, BUTTON };
109         void add_controls(const std::string &heading, ControlType control_type,
110                           SpinnerGroup spinner_group,
111                           const MIDIMappingProto &mapping_proto, const std::vector<Control> &controls);
112         void fill_controls_from_mapping(const MIDIMappingProto &mapping_proto);
113
114         // Tries to find a source bus and an offset to it that would give
115         // a consistent offset for the rest of the mappings in this bus.
116         // Returns -1 for the bus if no consistent offset can be found.
117         std::pair<int, int> guess_offset(unsigned bus_idx, SpinnerGroup spinner_group);
118         bool bus_is_empty(unsigned bus_idx, SpinnerGroup spinner_group);
119
120         void update_guess_button_state();
121         struct FocusInfo {
122                 int bus_idx;  // -1 for none.
123                 SpinnerGroup spinner_group;
124                 int field_number;
125         };
126         FocusInfo find_focus() const;
127
128         std::unique_ptr<MIDIMappingProto> construct_mapping_proto_from_ui();
129
130         Ui::MIDIMappingDialog *ui;
131         MIDIMapper *mapper;
132         ControllerReceiver *old_receiver;
133         FocusInfo last_focus{-1, SpinnerGroup::ALL_GROUPS, -1};
134
135         // All controllers actually laid out on the grid (we need to store them
136         // so that we can move values back and forth between the controls and
137         // the protobuf on save/load).
138         struct InstantiatedSpinner {
139                 QSpinBox *spinner;
140                 unsigned bus_idx;
141                 SpinnerGroup spinner_group;
142                 int field_number;  // In MIDIMappingBusProto.
143         };
144         struct InstantiatedComboBox {
145                 QComboBox *combo_box;
146                 int field_number;  // In MIDIMappingProto.
147         };
148         std::vector<InstantiatedSpinner> controller_spinners;
149         std::vector<InstantiatedSpinner> button_spinners;
150         std::vector<InstantiatedComboBox> bank_combo_boxes;
151
152         // Keyed on bus index, then field number.
153         struct SpinnerAndGroup {
154                 QSpinBox *spinner;
155                 SpinnerGroup group;
156         };
157         std::map<unsigned, std::map<unsigned, SpinnerAndGroup>> spinners;
158 };
159
160 #endif  // !defined(_MIDI_MAPPING_DIALOG_H)