Skip to content

Commit

Permalink
Percussion panel - allow deletion of non-empty rows
Browse files Browse the repository at this point in the history
  • Loading branch information
mathesoncalum committed Feb 21, 2025
1 parent 0587b0a commit 3b87493
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 37 deletions.
15 changes: 2 additions & 13 deletions src/notation/qml/MuseScore/NotationScene/PercussionPanel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Item {
onNavigationEvent: {
// Use the last known "pad navigation row" and tab to the associated delete button if it exists
var padNavigationRow = navigationPrv.currentPadNavigationIndex[0]
if (padGrid.model.rowIsEmpty(padNavigationRow)) {
if (padGrid.numRows > 1) {
event.setData("controlIndex", [padNavigationRow, 0])
}
}
Expand Down Expand Up @@ -200,7 +200,7 @@ Item {
anchors.verticalCenter: parent.verticalCenter
anchors.right: parent.right

visible: padGrid.numRows > 1 && padGrid.model.rowIsEmpty(model.index)
visible: padGrid.numRows > 1

icon: IconCode.DELETE_TANK
backgroundRadius: deleteButton.width / 2
Expand All @@ -214,17 +214,6 @@ Item {
onClicked: {
padGrid.model.deleteRow(model.index)
}

Connections {
target: padGrid.model

function onRowIsEmptyChanged(row, isEmpty) {
if (row !== model.index) {
return
}
deleteButton.visible = padGrid.numRows > 1 && isEmpty
}
}
}
}
}
Expand Down
33 changes: 12 additions & 21 deletions src/notation/view/percussionpanel/percussionpanelpadlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,17 @@ void PercussionPanelPadListModel::addEmptyRow(bool focusFirstInNewRow)

void PercussionPanelPadListModel::deleteRow(int row)
{
// Update the drumset...
const int startIdx = row * NUM_COLUMNS;
for (int i = startIdx; i < startIdx + NUM_COLUMNS; ++i) {
if (const PercussionPanelPadModel* model = m_padModels.at(i)) {
m_drumset->setDrum(model->pitch(), mu::engraving::DrumInstrument());
}
}

// Then remove the row...
m_padModels.remove(row * NUM_COLUMNS, NUM_COLUMNS);

emit layoutChanged();
emit numPadsChanged();
}
Expand All @@ -95,7 +105,8 @@ void PercussionPanelPadListModel::removeEmptyRows()
const int lastRowIndex = numPads() / NUM_COLUMNS - 1;
for (int i = lastRowIndex; i >= 0; --i) {
const int numRows = numPads() / NUM_COLUMNS;
if (rowIsEmpty(i) && numRows > 1) { // never delete the first row
const bool rowIsEmpty = numEmptySlotsAtRow(i) == NUM_COLUMNS;
if (rowIsEmpty && numRows > 1) { // never delete the first row
m_padModels.remove(i * NUM_COLUMNS, NUM_COLUMNS);
rowsRemoved = true;
}
Expand All @@ -106,11 +117,6 @@ void PercussionPanelPadListModel::removeEmptyRows()
}
}

bool PercussionPanelPadListModel::rowIsEmpty(int row) const
{
return numEmptySlotsAtRow(row) == NUM_COLUMNS;
}

void PercussionPanelPadListModel::startPadSwap(int startIndex)
{
m_padSwapStartIndex = startIndex;
Expand Down Expand Up @@ -424,23 +430,8 @@ int PercussionPanelPadListModel::getModelIndexForPitch(int pitch) const

void PercussionPanelPadListModel::movePad(int fromIndex, int toIndex)
{
const int fromRow = fromIndex / NUM_COLUMNS;
const int toRow = toIndex / NUM_COLUMNS;

// fromRow will become empty if there's only 1 "occupied" slot, toRow will no longer be empty if it was previously...
const bool fromRowEmptyChanged = numEmptySlotsAtRow(fromRow) == NUM_COLUMNS - 1;
const bool toRowEmptyChanged = rowIsEmpty(toRow);

m_padModels.swapItemsAt(fromIndex, toIndex);
emit layoutChanged();

if (fromRowEmptyChanged) {
emit rowIsEmptyChanged(fromRow, /*isEmpty*/ true);
}

if (toRowEmptyChanged) {
emit rowIsEmptyChanged(toRow, /*isEmpty*/ false);
}
}

int PercussionPanelPadListModel::numEmptySlotsAtRow(int row) const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ class PercussionPanelPadListModel : public QAbstractListModel, public muse::Inje

void removeEmptyRows();

Q_INVOKABLE bool rowIsEmpty(int row) const;

Q_INVOKABLE void startPadSwap(int startIndex);
Q_INVOKABLE void endPadSwap(int endIndex);
bool swapInProgress() const { return indexIsValid(m_padSwapStartIndex); }
Expand All @@ -89,7 +87,6 @@ class PercussionPanelPadListModel : public QAbstractListModel, public muse::Inje

signals:
void numPadsChanged();
void rowIsEmptyChanged(int row, bool empty);
void padFocusRequested(int padIndex); //! NOTE: This won't work if it is called immediately before a layoutChange

private:
Expand Down

0 comments on commit 3b87493

Please sign in to comment.