Skip to content

Commit

Permalink
Adds Remove Note action (LMMS#4)
Browse files Browse the repository at this point in the history
* Changes mousePressEvent variables to camelCase

	Changes variable names from PianoRoll::mousePressEvent to
camelCase and changes 2 comments.

* Style fixes on mouseDoubleClickEvent

* Removes magic number and costy loop

	Removes a loop that removed every note except one and replaced
it with a clear() call plus appending the note.
	Removes a magic number used for finding the closest note.

* Small fixes + Code style

	Fixes small bug with the vertical selection mode scrolling.
	Improves logic on the cursor selection of ActionNone.
	Adds update() + return statements for the actions that didn't
have them, and removed the update() from outside the switch.
	Fixes the code style.

* Adds Remove Note action

	Adds a action to remove notes on the PianoRoll, and remove an
unnecessary member variable that was only being used to handle that
action (m_mouseDownRight).
	Also removes unnecessary handling of the play key action on the
ActionNone case switch from the mouseMoveEvent. Adds a update() call on
the ActionPlayKeys (so it updates the cursor) and a break; statement
for consistency. Also fixes the calculation of the velocity.
  • Loading branch information
IanCaio authored and Veratil committed Jun 19, 2021
1 parent aa40f20 commit f9cc20d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 60 deletions.
3 changes: 1 addition & 2 deletions include/PianoRoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ protected slots:
enum Actions
{
ActionNone,
ActionRemoveNote,
ActionMoveNote,
ActionResizeNote,
ActionSelectNotes,
Expand Down Expand Up @@ -435,8 +436,6 @@ protected slots:
EditModes m_ctrlMode; // mode they were in before they hit ctrl
EditModes m_knifeMode; // mode they where in before entering knife mode

bool m_mouseDownRight; //true if right click is being held down

TimeLineWidget * m_timeLine;
bool m_scrollBack;

Expand Down
101 changes: 43 additions & 58 deletions src/gui/editors/PianoRoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ PianoRoll::PianoRoll() :
m_lastKey( 0 ),
m_editMode( ModeDraw ),
m_ctrlMode( ModeDraw ),
m_mouseDownRight( false ),
m_scrollBack( false ),
m_stepRecorderWidget(this, DEFAULT_PR_PPB, PR_TOP_MARGIN, PR_BOTTOM_MARGIN + m_notesEditHeight, WHITE_KEY_WIDTH, 0),
m_stepRecorder(*this, m_stepRecorderWidget),
Expand Down Expand Up @@ -1870,7 +1869,7 @@ void PianoRoll::mousePressEvent(QMouseEvent * me)
}
else if (rightButton) // erase single note
{
m_mouseDownRight = true;
m_action = ActionRemoveNote;
if (n != nullptr)
{
m_pattern->addJournalCheckPoint();
Expand All @@ -1885,6 +1884,7 @@ void PianoRoll::mousePressEvent(QMouseEvent * me)
case EditModes::ModeErase:
{
// erase single note
m_action = ActionRemoveNote;
if (n != nullptr)
{
m_pattern->addJournalCheckPoint();
Expand Down Expand Up @@ -2634,12 +2634,13 @@ void PianoRoll::computeSelectedNotes(bool shift)



void PianoRoll::mouseReleaseEvent( QMouseEvent * me )
void PianoRoll::mouseReleaseEvent(QMouseEvent* me)
{
const bool shiftDown = me->modifiers() & Qt::ShiftModifier;
const bool rightUp = me->button() & Qt::RightButton;
const bool leftUp = me->button() & Qt::LeftButton;
bool mustRepaint = false;
// Both right and left releases should trigger a repaint
bool mustRepaint = rightUp || leftUp;

s_textFloat->hide();

Expand All @@ -2651,15 +2652,13 @@ void PianoRoll::mouseReleaseEvent( QMouseEvent * me )

if (leftUp)
{
mustRepaint = true;

if( m_action == ActionSelectNotes && m_editMode == ModeSelect )
if (m_action == ActionSelectNotes && m_editMode == ModeSelect)
{
// select the notes within the selection rectangle and
// then destroy the selection rectangle
computeSelectedNotes(shiftDown);
}
else if( m_action == ActionMoveNote )
else if (m_action == ActionMoveNote)
{
// we moved one or more notes so they have to be
// moved properly according to new starting-
Expand All @@ -2668,42 +2667,36 @@ void PianoRoll::mouseReleaseEvent( QMouseEvent * me )

}

if( m_action == ActionMoveNote || m_action == ActionResizeNote )
if (m_action == ActionMoveNote || m_action == ActionResizeNote)
{
// if we only moved one note, deselect it so we can
// edit the notes in the note edit area
if( selectionCount() == 1 )
if (selectionCount() == 1)
{
clearSelectedNotes();
}
}
}

if (rightUp)
{
m_mouseDownRight = false;
mustRepaint = true;
}

if( hasValidPattern() )
if (hasValidPattern())
{
// turn off all notes that are playing
stopNotes();
}

m_currentNote = NULL;
m_currentNote = nullptr;

if (m_action != ActionKnife)
{
m_action = ActionNone;
}

if( m_editMode == ModeDraw )
if (m_editMode == ModeDraw)
{
setCursor( Qt::ArrowCursor );
setCursor(Qt::ArrowCursor);
}

if( mustRepaint )
if (mustRepaint)
{
repaint();
}
Expand Down Expand Up @@ -2742,41 +2735,7 @@ void PianoRoll::mouseMoveEvent(QMouseEvent * me)
{
case ActionNone:
{
const int keyNum = getKey(mey);

// See if they clicked on the keyboard on the left
if (pra == PianoRollArea::Keys && keyNum != m_lastKey && leftButton)
{
// Clicked on a key, play the note
testPlayKey(keyNum, ((float) mex) / ((float) m_whiteKeyWidth) * MidiDefaultVelocity, 0);
update();
return;
}

Note* const n = noteUnderMouse();
// TODO: Removing notes should have an action of its own
// If we're in ModeDraw and holding right button or ModeErase and any
if (n != nullptr)
{
switch (m_editMode)
{
case ModeDraw:
{
// Don't continue if we're not holding right button
if (!rightButton) { break; }
}
case ModeErase:
{
m_pattern->removeNote(n);
update();
return;
}
case ModeSelect:
case ModeEditDetuning:
case ModeEditKnife:
break;
}
}

// Set the appropriate cursor
// Default is Arrow Cursor
Expand All @@ -2802,10 +2761,33 @@ void PianoRoll::mouseMoveEvent(QMouseEvent * me)

break;
}
case ActionRemoveNote:
{
Note* const n = noteUnderMouse();
// If we're in ModeDraw and holding right button or ModeErase and any
if (n != nullptr)
{
if ((m_editMode == ModeDraw && rightButton) ||
(m_editMode == ModeErase))
{
m_pattern->removeNote(n);
}
}

update();
return;

break;
}
case ActionPlayKeys:
{
const int keyNum = getKey(mey);
const int v = static_cast<int>((static_cast<float>(mex) / m_whiteKeyWidth) * MidiDefaultVelocity);
// Calculate the velocity to play the key
// TODO: Use std::clamp when we have C++17
auto vx = std::min(mex, m_whiteKeyWidth);
vx = std::max(vx, 0);
const int v = static_cast<int>((static_cast<float>(vx) / m_whiteKeyWidth) * MidiDefaultVelocity);

if (keyNum != m_lastKey)
{
testPlayKey(keyNum, v, 0);
Expand All @@ -2817,7 +2799,10 @@ void PianoRoll::mouseMoveEvent(QMouseEvent * me)
m_pattern->instrumentTrack()->pianoModel()->handleKeyPress(keyNum, v);
playChordNotes(keyNum, v);
}
update();
return;

break;
}
case ActionMoveNote:
case ActionResizeNote:
Expand Down Expand Up @@ -4400,11 +4385,11 @@ void PianoRoll::paintEvent(QPaintEvent * pe )
switch( m_editMode )
{
case ModeDraw:
if( m_mouseDownRight )
if (m_action == ActionRemoveNote)
{
cursor = s_toolErase;
}
else if( m_action == ActionMoveNote )
else if (m_action == ActionMoveNote)
{
cursor = s_toolMove;
}
Expand Down

0 comments on commit f9cc20d

Please sign in to comment.