save changes to layouts

This commit is contained in:
garak 2023-02-14 12:09:22 -05:00
parent 5d98f8e2f8
commit 2ea0590f6e
6 changed files with 87 additions and 80 deletions

View file

@ -19,8 +19,6 @@ class Layout : public QObject {
public:
Layout() {}
void copyAttributesFrom(Layout *other);
static QString layoutConstantFromName(QString mapName);
bool loaded = false;
@ -72,6 +70,9 @@ public:
QUndoStack editHistory;
public:
Layout *copy();
void copyFrom(Layout *other);
int getWidth();
int getHeight();
int getBorderWidth();

View file

@ -58,7 +58,7 @@ public:
QString layoutsLabel;
QMap<QString, QString> layoutIdsToNames;
QMap<QString, Layout*> mapLayouts;
// QMap<QString, Layout*> mapLayoutsMaster;
QMap<QString, Layout*> mapLayoutsMaster;
QMap<QString, QString> mapSecToMapHoverName;
QMap<QString, int> mapSectionNameToValue;
QMap<int, QString> mapSectionValueToName;
@ -157,11 +157,12 @@ public:
void loadTilesetPalettes(Tileset*);
void readTilesetPaths(Tileset* tileset);
void saveLayoutBlockdata(Map*);
void saveLayoutBorder(Map*);
void saveLayout(Layout *);
void saveLayoutBlockdata(Layout *);
void saveLayoutBorder(Layout *);
void writeBlockdata(QString, const Blockdata &);
void saveAllMaps();
void saveMap(Map*);
void saveMap(Map *);
void saveAllDataStructures();
void saveMapLayouts();
void saveMapGroups();
@ -238,7 +239,7 @@ public:
static int getMaxObjectEvents();
private:
void updateMapLayout(Map*);
void updateLayout(Layout *);
void setNewMapBlockdata(Map* map);
void setNewMapBorder(Map *map);

View file

@ -50,8 +50,27 @@
// BorderMetatilesPixmapItem *borderItem = nullptr;
// QUndoStack editHistory;
void Layout::copyAttributesFrom(Layout *other) {
//
Layout *Layout::copy() {
Layout *layout = new Layout;
layout->copyFrom(this);
return layout;
}
void Layout::copyFrom(Layout *other) {
this->id = other->id;
this->name = other->name;
this->width = other->width;
this->height = other->height;
this->border_width = other->border_width;
this->border_height = other->border_height;
this->border_path = other->border_path;
this->blockdata_path = other->blockdata_path;
this->tileset_primary_label = other->tileset_primary_label;
this->tileset_secondary_label = other->tileset_secondary_label;
this->tileset_primary = other->tileset_primary;
this->tileset_secondary = other->tileset_secondary;
this->blockdata = other->blockdata;
this->border = other->border;
}
QString Layout::layoutConstantFromName(QString mapName) {

View file

@ -62,10 +62,13 @@ void Editor::saveProject() {
}
void Editor::save() {
if (project && map) {
if (this->project && this->map) {
saveUiFields();
project->saveMap(map);
project->saveAllDataStructures();
this->project->saveMap(this->map);
this->project->saveAllDataStructures();
}
else if (this->project && this->layout) {
this->project->saveLayout(this->layout);
}
}

View file

@ -684,10 +684,6 @@ void MainWindow::on_action_Reload_Project_triggered() {
}
void MainWindow::unsetMap() {
//
logInfo("Disabling map-related edits");
//
this->editor->unsetMap();
// disable other tabs
@ -696,7 +692,6 @@ void MainWindow::unsetMap() {
this->ui->mainTabBar->setTabEnabled(3, false);
this->ui->mainTabBar->setTabEnabled(4, false);
//
this->ui->comboBox_LayoutSelector->setEnabled(false);
}
@ -752,8 +747,12 @@ bool MainWindow::setMap(QString map_name, bool scroll) {
}
bool MainWindow::setLayout(QString layoutId) {
// if this->editor->setLayout(layoutName);
// this->editor->layout = layout;
if (this->editor->map)
logInfo("Switching to a layout-only editing mode. Disabling map-related edits.");
setMap(QString());
logInfo(QString("Setting layout to '%1'").arg(layoutId));
if (!this->editor->setLayout(layoutId)) {
return false;
@ -762,17 +761,7 @@ bool MainWindow::setLayout(QString layoutId) {
layoutTreeModel->setLayout(layoutId);
refreshMapScene();
// if (scrollTreeView) {
// // Make sure we clear the filter first so we actually have a scroll target
// /// !TODO: make this onto a function that scrolls the current view taking a map name or layout name
// groupListProxyModel->setFilterRegularExpression(QString());
// ui->mapList->setCurrentIndex(groupListProxyModel->mapFromSource(mapGroupModel->indexOfMap(map_name)));
// ui->mapList->scrollTo(ui->mapList->currentIndex(), QAbstractItemView::PositionAtCenter);
// }
showWindowTitle();
updateMapList();
// !TODO: make sure these connections are not duplicated / cleared later
@ -780,15 +769,6 @@ bool MainWindow::setLayout(QString layoutId) {
connect(editor->layout, &Layout::needsRedrawing, this, &MainWindow::onLayoutNeedsRedrawing);
// connect(editor->map, &Map::modified, [this](){ this->markMapEdited(); });
// displayMapProperties
//
// connect(editor->layout, &Layout::mapChanged, this, &MainWindow::onMapChanged);
// connect(editor->layout, &Layout::mapNeedsRedrawing, this, &MainWindow::onMapNeedsRedrawing);
// connect(editor->layout, &Layout::modified, [this](){ this->markMapEdited(); });
//
updateTilesetEditor();
return true;
@ -1116,15 +1096,6 @@ bool MainWindow::populateMapList() {
ui->mapList->setModel(groupListProxyModel);
this->ui->mapList->setItemDelegateForColumn(0, new GroupNameDelegate(this->editor->project, this));
//
// connect(this->mapGroupModel, &QStandardItemModel::dataChanged, [=](const QModelIndex &, const QModelIndex &, const QList<int> &){
// qDebug() << "mapGroupModel dataChanged";
// });
// connect(this->mapGroupModel, &MapGroupModel::edited, [=, this](){
// qDebug() << "model edited with" << this->ui->mapList->selectionModel()->selection().size() << "items";
// }); removeSelected
connect(this->mapGroupModel, &MapGroupModel::dragMoveCompleted, this->ui->mapList, &MapTree::removeSelected);
this->mapAreaModel = new MapAreaModel(editor->project);
@ -1497,12 +1468,6 @@ void MainWindow::on_layoutList_activated(const QModelIndex &index) {
QVariant data = index.data(Qt::UserRole);
if (index.data(MapListRoles::TypeRole) == "map_layout" && !data.isNull()) {
QString layoutId = data.toString();
//
logInfo("Switching to a layout-only editing mode");
setMap(QString());
//setLayout(layoutId);
// setLayout(layout)
qDebug() << "set layout" << layoutId;
if (!setLayout(layoutId)) {
QMessageBox msgBox(this);
@ -1517,22 +1482,28 @@ void MainWindow::on_layoutList_activated(const QModelIndex &index) {
void MainWindow::updateMapList() {
if (this->editor->map) {
mapGroupModel->setMap(this->editor->map->name);
groupListProxyModel->layoutChanged();
mapAreaModel->setMap(this->editor->map->name);
areaListProxyModel->layoutChanged();
this->mapGroupModel->setMap(this->editor->map->name);
this->groupListProxyModel->layoutChanged();
this->mapAreaModel->setMap(this->editor->map->name);
this->areaListProxyModel->layoutChanged();
}
else {
// !TODO
qDebug() << "need to clear map list";
this->mapGroupModel->setMap(QString());
this->groupListProxyModel->layoutChanged();
this->ui->mapList->clearSelection();
this->mapAreaModel->setMap(QString());
this->areaListProxyModel->layoutChanged();
this->ui->areaList->clearSelection();
}
if (this->editor->layout) {
layoutTreeModel->setLayout(this->editor->layout->id);
layoutListProxyModel->layoutChanged();
this->layoutTreeModel->setLayout(this->editor->layout->id);
this->layoutListProxyModel->layoutChanged();
}
else {
qDebug() << "need to clear layout list";
this->layoutTreeModel->setLayout(QString());
this->layoutListProxyModel->layoutChanged();
this->ui->layoutList->clearSelection();
}
}

View file

@ -563,13 +563,12 @@ bool Project::readMapLayouts() {
return false;
}
mapLayouts.insert(layout->id, layout);
mapLayoutsMaster.insert(layout->id, layout->copy());
mapLayoutsTable.append(layout->id);
mapLayoutsTableMaster.append(layout->id);
layoutIdsToNames.insert(layout->id, layout->name);
}
// Deep copy
mapLayoutsTableMaster = mapLayoutsTable;
mapLayoutsTableMaster.detach();
return true;
}
@ -587,7 +586,7 @@ void Project::saveMapLayouts() {
bool useCustomBorderSize = projectConfig.getUseCustomBorderSize();
OrderedJson::array layoutsArr;
for (QString layoutId : mapLayoutsTableMaster) {
Layout *layout = mapLayouts.value(layoutId);
Layout *layout = mapLayoutsMaster.value(layoutId);
OrderedJson::object layoutObj;
layoutObj["id"] = layout->id;
layoutObj["name"] = layout->name;
@ -1191,14 +1190,14 @@ void Project::setNewMapBorder(Map *map) {
map->layout->lastCommitBlocks.borderDimensions = QSize(width, height);
}
void Project::saveLayoutBorder(Map *map) {
QString path = QString("%1/%2").arg(root).arg(map->layout->border_path);
writeBlockdata(path, map->layout->border);
void Project::saveLayoutBorder(Layout *layout) {
QString path = QString("%1/%2").arg(root).arg(layout->border_path);
writeBlockdata(path, layout->border);
}
void Project::saveLayoutBlockdata(Map* map) {
QString path = QString("%1/%2").arg(root).arg(map->layout->blockdata_path);
writeBlockdata(path, map->layout->blockdata);
void Project::saveLayoutBlockdata(Layout *layout) {
QString path = QString("%1/%2").arg(root).arg(layout->blockdata_path);
writeBlockdata(path, layout->blockdata);
}
void Project::writeBlockdata(QString path, const Blockdata &blockdata) {
@ -1353,21 +1352,28 @@ void Project::saveMap(Map *map) {
jsonDoc.dump(&mapFile);
mapFile.close();
saveLayoutBorder(map);
saveLayoutBlockdata(map);
saveLayout(map->layout);
saveHealLocations(map);
// Update global data structures with current map data.
updateMapLayout(map);
map->isPersistedToFile = true;
map->hasUnsavedDataChanges = false;
map->editHistory.setClean();
}
void Project::updateMapLayout(Map* map) {
if (!mapLayoutsTableMaster.contains(map->layoutId)) {
mapLayoutsTableMaster.append(map->layoutId);
void Project::saveLayout(Layout *layout) {
//
saveLayoutBorder(layout);
saveLayoutBlockdata(layout);
// Update global data structures with current map data.
updateLayout(layout);
layout->editHistory.setClean();
}
void Project::updateLayout(Layout *layout) {
if (!mapLayoutsTableMaster.contains(layout->id)) {
mapLayoutsTableMaster.append(layout->id);
}
// !TODO: why is[was] this a deep copy??
@ -1376,6 +1382,12 @@ void Project::updateMapLayout(Map* map) {
// Layout *newLayout = new Layout();
// *newLayout = *layout;
// mapLayoutsMaster.insert(map->layoutId, newLayout);
if (mapLayoutsMaster.contains(layout->id)) {
mapLayoutsMaster[layout->id]->copyFrom(layout);
}
else {
mapLayoutsMaster.insert(layout->id, layout->copy());
}
}
void Project::saveAllDataStructures() {