Skip to content

Commit

Permalink
Merge pull request #3880 from Holzhaus/midi-enums
Browse files Browse the repository at this point in the history
Replace MidiOpCode/MidiOption enums with scoped enum classes
  • Loading branch information
uklotzde authored May 30, 2021
2 parents 24ae20b + f487cbc commit 744e4bd
Show file tree
Hide file tree
Showing 20 changed files with 979 additions and 735 deletions.
12 changes: 6 additions & 6 deletions src/control/controlbehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,15 @@ ControlPushButtonBehavior::ControlPushButtonBehavior(ButtonMode buttonMode,
void ControlPushButtonBehavior::setValueFromMidi(
MidiOpCode o, double dParam, ControlDoublePrivate* pControl) {
// Calculate pressed State of the midi Button
// Some controller like the RMX2 are sending always MIDI_NOTE_ON
// Some controller like the RMX2 are sending always MidiOpCode::NoteOn
// with a changed dParam 127 for pressed an 0 for released.
// Other controller like the VMS4 are using MIDI_NOTE_ON
// And MIDI_NOTE_OFF and a velocity value like a piano keyboard
// Other controller like the VMS4 are using MidiOpCode::NoteOn
// And MidiOpCode::NoteOff and a velocity value like a piano keyboard
bool pressed = true;
if (o == MIDI_NOTE_OFF || dParam == 0) {
// MIDI_NOTE_ON + 0 should be interpreted a released according to
if (o == MidiOpCode::NoteOff || dParam == 0) {
// MidiOpCode::NoteOn + 0 should be interpreted a released according to
// http://de.wikipedia.org/wiki/Musical_Instrument_Digital_Interface
// looking for MIDI_NOTE_ON doesn't seem to work...
// looking for MidiOpCode::NoteOn doesn't seem to work...
pressed = false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/controllerinputmappingtablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ QVariant ControllerInputMappingTableModel::data(const QModelIndex& index,
case MIDI_COLUMN_CHANNEL:
return MidiUtils::channelFromStatus(mapping.key.status);
case MIDI_COLUMN_OPCODE:
return MidiUtils::opCodeFromStatus(mapping.key.status);
return MidiUtils::opCodeValue(MidiUtils::opCodeFromStatus(mapping.key.status));
case MIDI_COLUMN_CONTROL:
return mapping.key.control;
case MIDI_COLUMN_OPTIONS:
// UserRole is used for sorting.
if (role == Qt::UserRole) {
return mapping.options.all;
return QVariant(mapping.options);
}
return QVariant::fromValue(mapping.options);
case MIDI_COLUMN_ACTION:
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/controlleroutputmappingtablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ QVariant ControllerOutputMappingTableModel::data(const QModelIndex& index,
case MIDI_COLUMN_CHANNEL:
return MidiUtils::channelFromStatus(mapping.output.status);
case MIDI_COLUMN_OPCODE:
return MidiUtils::opCodeFromStatus(mapping.output.status);
return MidiUtils::opCodeValue(MidiUtils::opCodeFromStatus(mapping.output.status));
case MIDI_COLUMN_CONTROL:
return mapping.output.control;
case MIDI_COLUMN_ON:
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/delegates/controldelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void ControlDelegate::paint(QPainter* painter,
QModelIndex optionsColumn = index.sibling(index.row(),
m_iMidiOptionsColumn);
MidiOptions options = optionsColumn.data().value<MidiOptions>();
m_bIsIndexScript = options.script;
m_bIsIndexScript = options.testFlag(MidiOption::Script);
}

QStyledItemDelegate::paint(painter, option, index);
Expand Down
13 changes: 7 additions & 6 deletions src/controllers/delegates/midiopcodedelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ QWidget* MidiOpCodeDelegate::createEditor(QWidget* parent,
QComboBox* pComboBox = new QComboBox(parent);

QList<MidiOpCode> choices;
choices.append(MIDI_NOTE_ON);
choices.append(MIDI_NOTE_OFF);
choices.append(MIDI_CC);
choices.append(MIDI_PITCH_BEND);
choices.append(MidiOpCode::NoteOn);
choices.append(MidiOpCode::NoteOff);
choices.append(MidiOpCode::ControlChange);
choices.append(MidiOpCode::PitchBendChange);

foreach (MidiOpCode choice, choices) {
pComboBox->addItem(MidiUtils::opCodeToTranslatedString(choice), choice);
for (const MidiOpCode choice : qAsConst(choices)) {
pComboBox->addItem(MidiUtils::opCodeToTranslatedString(choice),
static_cast<uint8_t>(choice));
}
return pComboBox;
}
Expand Down
54 changes: 28 additions & 26 deletions src/controllers/delegates/midioptionsdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@
#include "controllers/midi/midiutils.h"
#include "moc_midioptionsdelegate.cpp"

namespace {

const QList<MidiOption> kMidiOptions = {
MidiOption::None,
MidiOption::Invert,
MidiOption::Rot64,
MidiOption::Rot64Invert,
MidiOption::Rot64Fast,
MidiOption::Diff,
MidiOption::Button,
MidiOption::Switch,
MidiOption::Spread64,
MidiOption::HercJog,
MidiOption::SelectKnob,
MidiOption::SoftTakeover,
MidiOption::Script,
MidiOption::FourteenBitMSB,
MidiOption::FourteenBitLSB,
};

}

MidiOptionsDelegate::MidiOptionsDelegate(QObject* pParent)
: QStyledItemDelegate(pParent) {
}
Expand All @@ -23,27 +45,9 @@ QWidget* MidiOptionsDelegate::createEditor(QWidget* parent,
Q_UNUSED(index);
QComboBox* pComboBox = new QComboBox(parent);

QList<MidiOption> choices;
choices.append(MIDI_OPTION_NONE);
choices.append(MIDI_OPTION_INVERT);
choices.append(MIDI_OPTION_ROT64);
choices.append(MIDI_OPTION_ROT64_INV);
choices.append(MIDI_OPTION_ROT64_FAST);
choices.append(MIDI_OPTION_DIFF);
choices.append(MIDI_OPTION_BUTTON);
choices.append(MIDI_OPTION_SWITCH);
choices.append(MIDI_OPTION_SPREAD64);
choices.append(MIDI_OPTION_HERC_JOG);
choices.append(MIDI_OPTION_SELECTKNOB);
choices.append(MIDI_OPTION_SOFT_TAKEOVER);
choices.append(MIDI_OPTION_SCRIPT);
choices.append(MIDI_OPTION_14BIT_MSB);
choices.append(MIDI_OPTION_14BIT_LSB);

for (int i = 0; i < choices.size(); ++i) {
MidiOption choice = choices.at(i);
for (const MidiOption choice : kMidiOptions) {
pComboBox->addItem(MidiUtils::midiOptionToTranslatedString(choice),
choice);
static_cast<uint16_t>(choice));
}

return pComboBox;
Expand All @@ -54,12 +58,10 @@ QString MidiOptionsDelegate::displayText(const QVariant& value,
Q_UNUSED(locale);
MidiOptions options = value.value<MidiOptions>();
QStringList optionStrs;
MidiOption option = static_cast<MidiOption>(1);
while (option < MIDI_OPTION_MASK) {
if (options.all & option) {
for (const MidiOption option : kMidiOptions) {
if (options.testFlag(option)) {
optionStrs.append(MidiUtils::midiOptionToTranslatedString(option));
}
option = static_cast<MidiOption>(option << 1);
}
return optionStrs.join(", ");
}
Expand All @@ -73,7 +75,7 @@ void MidiOptionsDelegate::setEditorData(QWidget* editor,
return;
}
for (int i = 0; i < pComboBox->count(); ++i) {
if (pComboBox->itemData(i).toInt() & options.all) {
if (MidiOptions(pComboBox->itemData(i).toInt()) & options) {
pComboBox->setCurrentIndex(i);
return;
}
Expand All @@ -88,6 +90,6 @@ void MidiOptionsDelegate::setModelData(QWidget* editor,
if (pComboBox == nullptr) {
return;
}
options.all = pComboBox->itemData(pComboBox->currentIndex()).toInt();
options = MidiOptions(pComboBox->itemData(pComboBox->currentIndex()).toInt());
model->setData(index, QVariant::fromValue(options), Qt::EditRole);
}
56 changes: 31 additions & 25 deletions src/controllers/dlgcontrollerlearning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ DlgControllerLearning::DlgControllerLearning(QWidget* parent,
connect(pushButtonFakeControl,
&QAbstractButton::clicked,
this,
&DlgControllerLearning::DEBUGFakeMidiMessage);
&DlgControllerLearning::DEBUGFakeMidiMessage;
connect(pushButtonFakeControl2,
&QAbstractButton::clicked,
this,
Expand Down Expand Up @@ -258,11 +258,11 @@ void DlgControllerLearning::slotStartLearningPressed() {

#ifdef CONTROLLERLESSTESTING
void DlgControllerLearning::DEBUGFakeMidiMessage() {
slotMessageReceived(MIDI_CC, 0x20, 0x41);
slotMessageReceived(MidiOpCode::ControlChange, 0x20, 0x41);
}

void DlgControllerLearning::DEBUGFakeMidiMessage2() {
slotMessageReceived(MIDI_CC, 0x20, 0x3F);
slotMessageReceived(MidiOpCode::ControlChange, 0x20, 0x3F);
}
#endif

Expand Down Expand Up @@ -312,11 +312,11 @@ void DlgControllerLearning::slotMessageReceived(unsigned char status,
// We got a message, so we can cancel the taking-too-long timeout.
m_firstMessageTimer.stop();

// Unless this is a MIDI_CC and the progress bar is full, restart the
// Unless this is a MidiOpCode::ControlChange and the progress bar is full, restart the
// timer. That way the user won't just push buttons forever and wonder
// why the wizard never advances.
unsigned char opCode = MidiUtils::opCodeFromStatus(status);
if (opCode != MIDI_CC || progressBarWiggleFeedback->value() != 10) {
MidiOpCode opCode = MidiUtils::opCodeFromStatus(status);
if (opCode != MidiOpCode::ControlChange || progressBarWiggleFeedback->value() != 10) {
m_lastMessageTimer.start();
}
}
Expand Down Expand Up @@ -362,28 +362,34 @@ void DlgControllerLearning::slotTimerExpired() {
QString midiControl = "";
bool first = true;
foreach (const MidiInputMapping& mapping, m_mappings) {
unsigned char opCode = MidiUtils::opCodeFromStatus(mapping.key.status);
MidiOpCode opCode = MidiUtils::opCodeFromStatus(mapping.key.status);
bool twoBytes = MidiUtils::isMessageTwoBytes(opCode);
QString mappingStr = twoBytes ? QString("Status: 0x%1 Control: 0x%2 Options: 0x%03")
.arg(QString::number(mapping.key.status, 16).toUpper(),
QString::number(mapping.key.control, 16).toUpper()
.rightJustified(2, '0'),
QString::number(mapping.options.all, 16).toUpper()
.rightJustified(2, '0')) :
QString("0x%1 0x%2")
.arg(QString::number(mapping.key.status, 16).toUpper(),
QString::number(mapping.options.all, 16).toUpper()
.rightJustified(2, '0'));
QString mappingStr = twoBytes
? QString("Status: 0x%1 Control: 0x%2 Options: 0x%03")
.arg(QString::number(mapping.key.status, 16)
.toUpper(),
QString::number(mapping.key.control, 16)
.toUpper()
.rightJustified(2, '0'),
QString::number(mapping.options, 16)
.toUpper()
.rightJustified(2, '0'))
: QString("0x%1 0x%2")
.arg(QString::number(mapping.key.status, 16)
.toUpper(),
QString::number(mapping.options, 16)
.toUpper()
.rightJustified(2, '0'));

// Set the debug string and "Advanced MIDI Options" group using the
// first mapping.
if (first) {
midiControl = mappingStr;
MidiOptions options = mapping.options;
midiOptionInvert->setChecked(options.invert);
midiOptionSelectKnob->setChecked(options.selectknob);
midiOptionSoftTakeover->setChecked(options.soft_takeover);
midiOptionSwitchMode->setChecked(options.sw);
midiOptionInvert->setChecked(options.testFlag(MidiOption::Invert));
midiOptionSelectKnob->setChecked(options.testFlag(MidiOption::SelectKnob));
midiOptionSoftTakeover->setChecked(options.testFlag(MidiOption::SoftTakeover));
midiOptionSwitchMode->setChecked(options.testFlag(MidiOption::Switch));
first = false;
}

Expand Down Expand Up @@ -417,10 +423,10 @@ void DlgControllerLearning::slotMidiOptionsChanged() {
for (MidiInputMappings::iterator it = m_mappings.begin();
it != m_mappings.end(); ++it) {
MidiOptions& options = it->options;
options.sw = midiOptionSwitchMode->isChecked();
options.soft_takeover = midiOptionSoftTakeover->isChecked();
options.invert = midiOptionInvert->isChecked();
options.selectknob = midiOptionSelectKnob->isChecked();
options.setFlag(MidiOption::Switch, midiOptionSwitchMode->isChecked());
options.setFlag(MidiOption::SoftTakeover, midiOptionSoftTakeover->isChecked());
options.setFlag(MidiOption::Invert, midiOptionInvert->isChecked());
options.setFlag(MidiOption::SelectKnob, midiOptionSelectKnob->isChecked());
}

emit learnTemporaryInputMappings(m_mappings);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/dlgcontrollerlearning.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class DlgControllerLearning : public QDialog,
void showControlMenu();
#ifdef CONTROLLERLESSTESTING
void DEBUGFakeMidiMessage();
void DEBUGFakeMidiMessage2();
void DEBUGFakeMidiMessage();
#endif

private:
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/keyboard/keyboardeventfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ bool KeyboardEventFilter::eventFilter(QObject*, QEvent* e) {
if (configKey.group != "[KeyboardShortcuts]") {
ControlObject* control = ControlObject::getControl(configKey);
if (control) {
//qDebug() << configKey << "MIDI_NOTE_ON" << 1;
//qDebug() << configKey << "MidiOpCode::NoteOn" << 1;
// Add key to active key list
m_qActiveKeyList.append(KeyDownInformation(
keyId, ke->modifiers(), control));
// Since setting the value might cause us to go down
// a route that would eventually clear the active
// key list, do that last.
control->setValueFromMidi(MIDI_NOTE_ON, 1);
control->setValueFromMidi(MidiOpCode::NoteOn, 1);
result = true;
} else {
qDebug() << "Warning: Keyboard key is configured for nonexistent control:"
Expand Down Expand Up @@ -108,8 +108,8 @@ bool KeyboardEventFilter::eventFilter(QObject*, QEvent* e) {
if (keyDownInfo.keyId == keyId ||
(clearModifiers > 0 && keyDownInfo.modifiers == clearModifiers)) {
if (!autoRepeat) {
//qDebug() << pControl->getKey() << "MIDI_NOTE_OFF" << 0;
pControl->setValueFromMidi(MIDI_NOTE_OFF, 0);
//qDebug() << pControl->getKey() << "MidiOpCode::NoteOff" << 0;
pControl->setValueFromMidi(MidiOpCode::NoteOff, 0);
m_qActiveKeyList.removeAt(i);
}
// Due to the modifier clearing workaround we might match multiple keys for
Expand Down
Loading

0 comments on commit 744e4bd

Please sign in to comment.