allow dragging and dropping to rearrange map groups

This commit is contained in:
garak 2024-01-12 14:39:49 -05:00
parent 99eb92c3b2
commit abc433bc78
2 changed files with 71 additions and 27 deletions

View file

@ -72,7 +72,7 @@ public:
public: public:
void setMap(QString mapName) { this->openMap = mapName; } void setMap(QString mapName) { this->openMap = mapName; }
QStandardItem *createGroupItem(QString groupName, int groupIndex); QStandardItem *createGroupItem(QString groupName, int groupIndex, QStandardItem *fromItem = nullptr);
QStandardItem *createMapItem(QString mapName, QStandardItem *fromItem = nullptr); QStandardItem *createMapItem(QString mapName, QStandardItem *fromItem = nullptr);
QStandardItem *insertMapItem(QString mapName, QString groupName); QStandardItem *insertMapItem(QString mapName, QString groupName);

View file

@ -59,7 +59,9 @@ Qt::DropActions MapGroupModel::supportedDropActions() const {
QStringList MapGroupModel::mimeTypes() const { QStringList MapGroupModel::mimeTypes() const {
QStringList types; QStringList types;
types << "application/porymap.mapgroupmodel.map" types << "application/porymap.mapgroupmodel.map"
<< "application/porymap.mapgroupmodel.group"; << "application/porymap.mapgroupmodel.group"
<< "application/porymap.mapgroupmodel.source.row"
<< "application/porymap.mapgroupmodel.source.column";
return types; return types;
} }
@ -69,6 +71,17 @@ QMimeData *MapGroupModel::mimeData(const QModelIndexList &indexes) const {
QDataStream stream(&encodedData, QIODevice::WriteOnly); QDataStream stream(&encodedData, QIODevice::WriteOnly);
// if dropping a selection containing a group(s) and map(s), clear all selection but first group.
for (const QModelIndex &index : indexes) {
if (index.isValid() && data(index, MapListRoles::TypeRole).toString() == "map_group") {
QString groupName = data(index, Qt::UserRole).toString();
stream << groupName;
mimeData->setData("application/porymap.mapgroupmodel.group", encodedData);
mimeData->setData("application/porymap.mapgroupmodel.source.row", QByteArray::number(index.row()));
return mimeData;
}
}
for (const QModelIndex &index : indexes) { for (const QModelIndex &index : indexes) {
if (index.isValid()) { if (index.isValid()) {
QString mapName = data(index, Qt::UserRole).toString(); QString mapName = data(index, Qt::UserRole).toString();
@ -83,11 +96,8 @@ QMimeData *MapGroupModel::mimeData(const QModelIndexList &indexes) const {
bool MapGroupModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parentIndex) { bool MapGroupModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parentIndex) {
if (action == Qt::IgnoreAction) if (action == Qt::IgnoreAction)
return true; return true;
if (!data->hasFormat("application/porymap.mapgroupmodel.map"))
return false;
if (!parentIndex.isValid()) if (!parentIndex.isValid() && !data->hasFormat("application/porymap.mapgroupmodel.group"))
return false; return false;
int firstRow = 0; int firstRow = 0;
@ -99,34 +109,68 @@ bool MapGroupModel::dropMimeData(const QMimeData *data, Qt::DropAction action, i
firstRow = rowCount(parentIndex); firstRow = rowCount(parentIndex);
} }
QByteArray encodedData = data->data("application/porymap.mapgroupmodel.map"); if (data->hasFormat("application/porymap.mapgroupmodel.group")) {
QDataStream stream(&encodedData, QIODevice::ReadOnly); if (parentIndex.row() != -1 || parentIndex.column() != -1) {
QStringList droppedMaps; return false;
int rowCount = 0; }
QByteArray encodedData = data->data("application/porymap.mapgroupmodel.group");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QString groupName;
int rowCount = 1;
QList<QStandardItem *> newItems; while (!stream.atEnd()) {
stream >> groupName;
}
while (!stream.atEnd()) { this->insertRow(row, parentIndex);
QString mapName;
stream >> mapName; // copy children to new node
droppedMaps << mapName; int sourceRow = data->data("application/porymap.mapgroupmodel.source.row").toInt();
rowCount++; QModelIndex originIndex = this->index(sourceRow, 0);
QModelIndexList children;
QStringList mapsToMove;
for (int i = 0; i < this->rowCount(originIndex); ++i ) {
children << this->index( i, 0, originIndex);
mapsToMove << this->index( i, 0 , originIndex).data(Qt::UserRole).toString();
}
QModelIndex groupIndex = index(row, 0, parentIndex);
QStandardItem *groupItem = this->itemFromIndex(groupIndex);
createGroupItem(groupName, row, groupItem);
for (QString mapName : mapsToMove) {
QStandardItem *mapItem = createMapItem(mapName);
groupItem->appendRow(mapItem);
}
} }
else if (data->hasFormat("application/porymap.mapgroupmodel.map")) {
QByteArray encodedData = data->data("application/porymap.mapgroupmodel.map");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QStringList droppedMaps;
int rowCount = 0;
this->insertRows(firstRow, rowCount, parentIndex); while (!stream.atEnd()) {
QString mapName;
stream >> mapName;
droppedMaps << mapName;
rowCount++;
}
int newItemIndex = 0; this->insertRows(firstRow, rowCount, parentIndex);
for (QString mapName : droppedMaps) {
QModelIndex mapIndex = index(firstRow, 0, parentIndex); int newItemIndex = 0;
QStandardItem *mapItem = this->itemFromIndex(mapIndex); for (QString mapName : droppedMaps) {
createMapItem(mapName, mapItem); QModelIndex mapIndex = index(firstRow, 0, parentIndex);
firstRow++; QStandardItem *mapItem = this->itemFromIndex(mapIndex);
createMapItem(mapName, mapItem);
firstRow++;
}
} }
emit dragMoveCompleted(); emit dragMoveCompleted();
updateProject(); updateProject();
return false; return true;
} }
void MapGroupModel::updateProject() { void MapGroupModel::updateProject() {
@ -158,13 +202,13 @@ void MapGroupModel::updateProject() {
this->project->mapNames = mapNames; this->project->mapNames = mapNames;
} }
QStandardItem *MapGroupModel::createGroupItem(QString groupName, int groupIndex) { QStandardItem *MapGroupModel::createGroupItem(QString groupName, int groupIndex, QStandardItem *group) {
QStandardItem *group = new QStandardItem; if (!group) group = new QStandardItem;
group->setText(groupName); group->setText(groupName);
group->setData(groupName, Qt::UserRole); group->setData(groupName, Qt::UserRole);
group->setData("map_group", MapListRoles::TypeRole); group->setData("map_group", MapListRoles::TypeRole);
group->setData(groupIndex, MapListRoles::GroupRole); group->setData(groupIndex, MapListRoles::GroupRole);
group->setFlags(Qt::ItemIsEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEditable); group->setFlags(Qt::ItemIsEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsSelectable);
this->groupItems.insert(groupName, group); this->groupItems.insert(groupName, group);
return group; return group;
} }