Implement layer move functionality using buttons

This commit is contained in:
Dan Paulat 2023-10-16 22:29:35 -05:00
parent b45ec9dfa5
commit 5d06f6bc21
3 changed files with 190 additions and 39 deletions

View file

@ -529,6 +529,62 @@ bool LayerModel::removeRows(int row, int count, const QModelIndex& parent)
return true;
}
bool LayerModel::moveRows(const QModelIndex& sourceParent,
int sourceRow,
int count,
const QModelIndex& destinationParent,
int destinationChild)
{
bool moved = false;
if (sourceParent != destinationParent || // Only accept internal moves
count < 1 || // Minimum selection size of 1
sourceRow < 0 || // Valid source row (start)
sourceRow + count > p->layers_.size() || // Valid source row (end)
destinationChild < 0 || // Valid destination row
destinationChild > p->layers_.size())
{
return false;
}
if (destinationChild < sourceRow)
{
// Move up
auto first = p->layers_.begin() + destinationChild;
auto middle = p->layers_.begin() + sourceRow;
auto last = middle + count;
beginMoveRows(sourceParent,
sourceRow,
sourceRow + count - 1,
destinationParent,
destinationChild);
std::rotate(first, middle, last);
endMoveRows();
moved = true;
}
else if (sourceRow + count < destinationChild)
{
// Move down
auto first = p->layers_.begin() + sourceRow;
auto middle = first + count;
auto last = p->layers_.begin() + destinationChild;
beginMoveRows(sourceParent,
sourceRow,
sourceRow + count - 1,
destinationParent,
destinationChild);
std::rotate(first, middle, last);
endMoveRows();
moved = true;
}
return moved;
}
void LayerModel::HandlePlacefileRemoved(const std::string& name)
{
auto it =

View file

@ -66,6 +66,11 @@ public:
bool removeRows(int row,
int count,
const QModelIndex& parent = QModelIndex()) override;
bool moveRows(const QModelIndex& sourceParent,
int sourceRow,
int count,
const QModelIndex& destinationParent,
int destinationChild) override;
public slots:
void HandlePlacefileRemoved(const std::string& name);