Skip to content

Commit

Permalink
keys and key ranges editable with a midi keyboard
Browse files Browse the repository at this point in the history
In the tables, possibly to edit with a midi keyboard:
* rootkey
* fixed note
* key range

Fix #10
  • Loading branch information
davy7125 committed Jul 8, 2019
1 parent ebe3cee commit 923da0e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
1 change: 1 addition & 0 deletions sources/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ polyphone (2.1) unstable; urgency=low
* (improvement) better ergonomics for editing modulators
* (improvement) multi selection is allowed in the range editor
* (improvement) show some of the sample information at the instrument level
* (improvement) keys and key ranges can be edited with a midi keyboard in the table
* (improvement) drag & drop of divisions in the tree can create instruments and presets
* (improvement) drag & drop in the tree can duplicate elements within instruments and presets
* (fix) several modulators can target the same modulator
Expand Down
2 changes: 1 addition & 1 deletion sources/context/mididevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void midiCallback(double deltatime, std::vector<unsigned char> *message, void *u
{
case 0x80: case 0x90: // NOTE ON or NOTE OFF
// First message is the note, second is velocity
if (status == 0x90 || message->at(2) == 0)
if (status == 0x80 || message->at(2) == 0)
ev = new NoteEvent(message->at(1), 0);
else
ev = new NoteEvent(message->at(1), message->at(2));
Expand Down
8 changes: 8 additions & 0 deletions sources/editor/widgets/spinboxkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ SpinBoxKey::SpinBoxKey(QWidget *parent) :
this->setMinimum(0);
this->setMaximum(127);
this->setValue(60);

connect(ContextManager::midi(), SIGNAL(keyPlayed(int,int)), this, SLOT(onKeyPlayed(int,int)));
}

QValidator::State SpinBoxKey::validate(QString &input, int &pos) const
Expand All @@ -51,3 +53,9 @@ QString SpinBoxKey::textFromValue(int val) const
{
return ContextManager::keyName()->getKeyName(val);
}

void SpinBoxKey::onKeyPlayed(int key, int vel)
{
if (vel > 0)
this->setValue(key);
}
3 changes: 3 additions & 0 deletions sources/editor/widgets/spinboxkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class SpinBoxKey : public QSpinBox
virtual QValidator::State validate(QString &input, int &pos) const;
virtual int valueFromText(const QString &text) const;
virtual QString textFromValue(int val) const;

private slots:
void onKeyPlayed(int key, int vel);
};

#endif // SPINBOXKEY_H
27 changes: 26 additions & 1 deletion sources/editor/widgets/spinboxrange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ int SpinBoxRange::MAXI = 127;

SpinBoxRange::SpinBoxRange(QWidget *parent) : QAbstractSpinBox(parent),
_valMin(MINI),
_valMax(MAXI)
_valMax(MAXI),
_firstMidiKey(-1)
{
connect(this, SIGNAL(editingFinished()), this, SLOT(updateValue()));
connect(ContextManager::midi(), SIGNAL(keyPlayed(int,int)), this, SLOT(onKeyPlayed(int,int)));
}

void SpinBoxRange::stepBy(int steps)
Expand Down Expand Up @@ -126,6 +128,29 @@ void SpinBoxRange::clear()
emit(valueChanged());
}

void SpinBoxRange::onKeyPlayed(int key, int vel)
{
if (vel > 0)
{
if (_firstMidiKey == -1)
{
// Single note for now
_firstMidiKey = key;
_valMin = _valMax = key;
}
else
{
// The second creates a range
_valMin = qMin(_firstMidiKey, key);
_valMax = qMax(_firstMidiKey, key);
_firstMidiKey = -1;
}
formatText();
}
else
_firstMidiKey = -1;
}

QAbstractSpinBox::StepEnabled SpinBoxRange::stepEnabled() const
{
StepEnabled stepEnabled = 0;
Expand Down
2 changes: 2 additions & 0 deletions sources/editor/widgets/spinboxrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public slots:

private slots:
void updateValue();
void onKeyPlayed(int key, int vel);

private:
enum SpinboxSection
Expand All @@ -72,6 +73,7 @@ private slots:
static int MINI;
static int MAXI;
int _valMin, _valMax;
int _firstMidiKey;
};

class SpinBoxVelocityRange : public SpinBoxRange
Expand Down
22 changes: 11 additions & 11 deletions sources/editor/widgets/tabledelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ QWidget * TableDelegate::createEditor(QWidget *parent, const QStyleOptionViewIte
{
if (isLoop)
{
// Remove the icon in the model
// Remove the icon from the model
QVariant previousDecoration = index.data(Qt::DecorationRole);
QAbstractItemModel * model = const_cast<QAbstractItemModel *>(index.model());
model->blockSignals(true);
Expand All @@ -79,17 +79,17 @@ QWidget * TableDelegate::createEditor(QWidget *parent, const QStyleOptionViewIte
SpinBoxKey * spin = new SpinBoxKey(parent);
spin->setMinimum(0);
spin->setMaximum(127);
spin->setStyleSheet("SpinBoxKey{ border: 3px solid " + highlightColor.name() + "; }"
"SpinBoxKey::down-button{width:0px;} SpinBoxKey::up-button{width:0px;} ");
spin->setStyleSheet("SpinBoxKey{ border: 3px solid " + highlightColor.name() + "; }" +
"SpinBoxKey::down-button{width:0px;} SpinBoxKey::up-button{width:0px;} ");
widget = spin;
}
else if (nbDecimales == 0)
{
QSpinBox * spin = new QSpinBox(parent);
spin->setMinimum(-2147483647);
spin->setMaximum(2147483647);
spin->setStyleSheet("QSpinBox{ border: 3px solid " + highlightColor.name() + "; }"
"QSpinBox::down-button{width:0px;} QSpinBox::up-button{width:0px;} ");
spin->setStyleSheet("QSpinBox{ border: 3px solid " + highlightColor.name() + "; }" +
"QSpinBox::down-button{width:0px;} QSpinBox::up-button{width:0px;} ");
widget = spin;
}
else
Expand All @@ -98,8 +98,8 @@ QWidget * TableDelegate::createEditor(QWidget *parent, const QStyleOptionViewIte
spin->setMinimum(-1000000);
spin->setMaximum(1000000);
spin->setSingleStep(.1);
spin->setStyleSheet("QDoubleSpinBox{ border: 3px solid " + highlightColor.name() + "; }"
"QDoubleSpinBox::down-button{width:0px;} QDoubleSpinBox::up-button{width:0px;} ");
spin->setStyleSheet("QDoubleSpinBox{ border: 3px solid " + highlightColor.name() + "; }" +
"QDoubleSpinBox::down-button{width:0px;} QDoubleSpinBox::up-button{width:0px;} ");
spin->setDecimals(nbDecimales);
widget = spin;
}
Expand All @@ -112,8 +112,8 @@ QWidget * TableDelegate::createEditor(QWidget *parent, const QStyleOptionViewIte
spin = new SpinBoxKeyRange(parent);
else
spin = new SpinBoxVelocityRange(parent);
spin->setStyleSheet("SpinBoxRange{ border: 3px solid " + highlightColor.name() + "; }"
"SpinBoxRange::down-button{width:0px;} SpinBoxRange::up-button{width:0px;} ");
spin->setStyleSheet("SpinBoxRange{ border: 3px solid " + highlightColor.name() + "; }" +
"SpinBoxRange::down-button{width:0px;} SpinBoxRange::up-button{width:0px;} ");
widget = spin;
}

Expand Down Expand Up @@ -297,9 +297,9 @@ void TableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
if (_modDisplay.contains(index.column()) && _modDisplay[index.column()].contains(index.row()))
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
QStyleOptionViewItem opt(option);
QStyleOptionViewItem opt(option);
#else
QStyleOptionViewItemV4 opt(option);
QStyleOptionViewItemV4 opt(option);
#endif
initStyleOption(&opt, index);
QRect rect1 = opt.rect;
Expand Down

0 comments on commit 923da0e

Please sign in to comment.