Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Glue notes in Piano-Roll #5482

Closed
wants to merge 10 commits into from
Binary file added data/themes/default/glue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/themes/default/tool.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions include/PianoRoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ protected slots:
void selectRegionFromPixels( int xStart, int xEnd );

void clearGhostPattern();
void glueNotes();


signals:
Expand Down
82 changes: 82 additions & 0 deletions src/gui/editors/PianoRoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <QScrollBar>
#include <QStyleOption>
#include <QtMath>
#include <QToolButton>

#ifndef __USE_XOPEN
#define __USE_XOPEN
Expand Down Expand Up @@ -627,6 +628,72 @@ void PianoRoll::clearGhostPattern()
}


void PianoRoll::glueNotes()
{
if (hasValidPattern())
{
NoteVector selectedNotes = getSelectedNotes();
if (selectedNotes.empty())
{
TextFloat::displayMessage( tr( "Glue notes failed" ),
tr( "Please select notes to glue first." ),
embed::getIconPixmap( "glue", 24, 24 ),
3000 );
return;
}

// Make undo possible
m_pattern->addJournalCheckPoint();

// Sort notes on key and then pos.
std::sort(selectedNotes.begin(), selectedNotes.end(),
[](const Note * note, const Note * compareNote) -> bool
{
if (note->key() == compareNote->key())
{
return note->pos() < compareNote->pos();
}
return note->key() < compareNote->key();
});

QList<Note *> noteToRemove;

NoteVector::iterator note = selectedNotes.begin();
auto nextNote = note+1;
NoteVector::iterator end = selectedNotes.end();

while (note != end && nextNote != end)
{
// key and position match for glue
if ((*note)->key() == (*nextNote)->key()
&& (*nextNote)->pos() >= (*note)->pos()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will that conditional ever be false? Since the notes were previously ordered by key and by pos, if two notes have the same key isn't it always true that the next note will have a position equal or greater than the previous note?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment highlighted all 4 lines, so to make it clearer the conditional I meant is (*nextNote)->pos() >= (*note)->pos() which will be evaluated if the keys are the same on the two notes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is redundant, but I think there should be a comment which explains what you said.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, what needs to be done here? Remove && (*nextNote)->pos() >= (*note)->pos() and improve the comment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think just removing that part of the conditional and having the comment explaining that you don't need to check if the next note is after the first because they are ordered already. Removes an unnecessary comparison but leaves an explanation why.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Hopefully fixed in d86ba8c

&& (*nextNote)->pos() <= (*note)->pos()
+ qMax(MidiTime(0), (*note)->length()))
{
(*note)->setLength(qMax((*note)->length(),
MidiTime((*nextNote)->endPos() - (*note)->pos())));
noteToRemove.push_back(*nextNote);
++nextNote;
}
// key or position doesn't match
else
{
note = nextNote;
nextNote = note+1;
}
}

// Remove old notes
for (int i = 0; i < noteToRemove.count(); ++i)
{
m_pattern->removeNote(noteToRemove[i]);
}

update();
}
}


void PianoRoll::loadMarkedSemiTones(const QDomElement & de)
{
// clear marked semitones to prevent leftover marks
Expand Down Expand Up @@ -4333,6 +4400,21 @@ PianoRollWindow::PianoRollWindow() :
DropToolBar *timeLineToolBar = addDropToolBarToTop( tr( "Timeline controls" ) );
m_editor->m_timeLine->addToolButtons( timeLineToolBar );

// -- Note modifier tools
// Toolbar
QToolButton * noteToolsButton = new QToolButton(m_toolBar);
noteToolsButton->setIcon(embed::getIconPixmap("tool"));
noteToolsButton->setPopupMode(QToolButton::InstantPopup);

// Glue
QAction * glueAction = new QAction(embed::getIconPixmap("glue"),
tr("Glue"), noteToolsButton);
connect(glueAction, SIGNAL(triggered()), m_editor, SLOT(glueNotes()));
glueAction->setShortcut( Qt::SHIFT | Qt::Key_G );

noteToolsButton->addAction(glueAction);

notesActionsToolBar->addWidget(noteToolsButton);

addToolBarBreak();

Expand Down