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: public:
Layout() {} Layout() {}
void copyAttributesFrom(Layout *other);
static QString layoutConstantFromName(QString mapName); static QString layoutConstantFromName(QString mapName);
bool loaded = false; bool loaded = false;
@ -72,6 +70,9 @@ public:
QUndoStack editHistory; QUndoStack editHistory;
public: public:
Layout *copy();
void copyFrom(Layout *other);
int getWidth(); int getWidth();
int getHeight(); int getHeight();
int getBorderWidth(); int getBorderWidth();

View file

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

View file

@ -50,8 +50,27 @@
// BorderMetatilesPixmapItem *borderItem = nullptr; // BorderMetatilesPixmapItem *borderItem = nullptr;
// QUndoStack editHistory; // 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) { QString Layout::layoutConstantFromName(QString mapName) {

View file

@ -62,10 +62,13 @@ void Editor::saveProject() {
} }
void Editor::save() { void Editor::save() {
if (project && map) { if (this->project && this->map) {
saveUiFields(); saveUiFields();
project->saveMap(map); this->project->saveMap(this->map);
project->saveAllDataStructures(); 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() { void MainWindow::unsetMap() {
//
logInfo("Disabling map-related edits");
//
this->editor->unsetMap(); this->editor->unsetMap();
// disable other tabs // disable other tabs
@ -696,7 +692,6 @@ void MainWindow::unsetMap() {
this->ui->mainTabBar->setTabEnabled(3, false); this->ui->mainTabBar->setTabEnabled(3, false);
this->ui->mainTabBar->setTabEnabled(4, false); this->ui->mainTabBar->setTabEnabled(4, false);
//
this->ui->comboBox_LayoutSelector->setEnabled(false); this->ui->comboBox_LayoutSelector->setEnabled(false);
} }
@ -752,8 +747,12 @@ bool MainWindow::setMap(QString map_name, bool scroll) {
} }
bool MainWindow::setLayout(QString layoutId) { bool MainWindow::setLayout(QString layoutId) {
// if this->editor->setLayout(layoutName); if (this->editor->map)
// this->editor->layout = layout; 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)) { if (!this->editor->setLayout(layoutId)) {
return false; return false;
@ -762,17 +761,7 @@ bool MainWindow::setLayout(QString layoutId) {
layoutTreeModel->setLayout(layoutId); layoutTreeModel->setLayout(layoutId);
refreshMapScene(); 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(); showWindowTitle();
updateMapList(); updateMapList();
// !TODO: make sure these connections are not duplicated / cleared later // !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->layout, &Layout::needsRedrawing, this, &MainWindow::onLayoutNeedsRedrawing);
// connect(editor->map, &Map::modified, [this](){ this->markMapEdited(); }); // 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(); updateTilesetEditor();
return true; return true;
@ -1116,15 +1096,6 @@ bool MainWindow::populateMapList() {
ui->mapList->setModel(groupListProxyModel); ui->mapList->setModel(groupListProxyModel);
this->ui->mapList->setItemDelegateForColumn(0, new GroupNameDelegate(this->editor->project, this)); 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); connect(this->mapGroupModel, &MapGroupModel::dragMoveCompleted, this->ui->mapList, &MapTree::removeSelected);
this->mapAreaModel = new MapAreaModel(editor->project); this->mapAreaModel = new MapAreaModel(editor->project);
@ -1497,12 +1468,6 @@ void MainWindow::on_layoutList_activated(const QModelIndex &index) {
QVariant data = index.data(Qt::UserRole); QVariant data = index.data(Qt::UserRole);
if (index.data(MapListRoles::TypeRole) == "map_layout" && !data.isNull()) { if (index.data(MapListRoles::TypeRole) == "map_layout" && !data.isNull()) {
QString layoutId = data.toString(); 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)) { if (!setLayout(layoutId)) {
QMessageBox msgBox(this); QMessageBox msgBox(this);
@ -1517,22 +1482,28 @@ void MainWindow::on_layoutList_activated(const QModelIndex &index) {
void MainWindow::updateMapList() { void MainWindow::updateMapList() {
if (this->editor->map) { if (this->editor->map) {
mapGroupModel->setMap(this->editor->map->name); this->mapGroupModel->setMap(this->editor->map->name);
groupListProxyModel->layoutChanged(); this->groupListProxyModel->layoutChanged();
mapAreaModel->setMap(this->editor->map->name); this->mapAreaModel->setMap(this->editor->map->name);
areaListProxyModel->layoutChanged(); this->areaListProxyModel->layoutChanged();
} }
else { else {
// !TODO this->mapGroupModel->setMap(QString());
qDebug() << "need to clear map list"; this->groupListProxyModel->layoutChanged();
this->ui->mapList->clearSelection();
this->mapAreaModel->setMap(QString());
this->areaListProxyModel->layoutChanged();
this->ui->areaList->clearSelection();
} }
if (this->editor->layout) { if (this->editor->layout) {
layoutTreeModel->setLayout(this->editor->layout->id); this->layoutTreeModel->setLayout(this->editor->layout->id);
layoutListProxyModel->layoutChanged(); this->layoutListProxyModel->layoutChanged();
} }
else { 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; return false;
} }
mapLayouts.insert(layout->id, layout); mapLayouts.insert(layout->id, layout);
mapLayoutsMaster.insert(layout->id, layout->copy());
mapLayoutsTable.append(layout->id); mapLayoutsTable.append(layout->id);
mapLayoutsTableMaster.append(layout->id);
layoutIdsToNames.insert(layout->id, layout->name); layoutIdsToNames.insert(layout->id, layout->name);
} }
// Deep copy
mapLayoutsTableMaster = mapLayoutsTable;
mapLayoutsTableMaster.detach();
return true; return true;
} }
@ -587,7 +586,7 @@ void Project::saveMapLayouts() {
bool useCustomBorderSize = projectConfig.getUseCustomBorderSize(); bool useCustomBorderSize = projectConfig.getUseCustomBorderSize();
OrderedJson::array layoutsArr; OrderedJson::array layoutsArr;
for (QString layoutId : mapLayoutsTableMaster) { for (QString layoutId : mapLayoutsTableMaster) {
Layout *layout = mapLayouts.value(layoutId); Layout *layout = mapLayoutsMaster.value(layoutId);
OrderedJson::object layoutObj; OrderedJson::object layoutObj;
layoutObj["id"] = layout->id; layoutObj["id"] = layout->id;
layoutObj["name"] = layout->name; layoutObj["name"] = layout->name;
@ -1191,14 +1190,14 @@ void Project::setNewMapBorder(Map *map) {
map->layout->lastCommitBlocks.borderDimensions = QSize(width, height); map->layout->lastCommitBlocks.borderDimensions = QSize(width, height);
} }
void Project::saveLayoutBorder(Map *map) { void Project::saveLayoutBorder(Layout *layout) {
QString path = QString("%1/%2").arg(root).arg(map->layout->border_path); QString path = QString("%1/%2").arg(root).arg(layout->border_path);
writeBlockdata(path, map->layout->border); writeBlockdata(path, layout->border);
} }
void Project::saveLayoutBlockdata(Map* map) { void Project::saveLayoutBlockdata(Layout *layout) {
QString path = QString("%1/%2").arg(root).arg(map->layout->blockdata_path); QString path = QString("%1/%2").arg(root).arg(layout->blockdata_path);
writeBlockdata(path, map->layout->blockdata); writeBlockdata(path, layout->blockdata);
} }
void Project::writeBlockdata(QString path, const Blockdata &blockdata) { void Project::writeBlockdata(QString path, const Blockdata &blockdata) {
@ -1353,21 +1352,28 @@ void Project::saveMap(Map *map) {
jsonDoc.dump(&mapFile); jsonDoc.dump(&mapFile);
mapFile.close(); mapFile.close();
saveLayoutBorder(map); saveLayout(map->layout);
saveLayoutBlockdata(map);
saveHealLocations(map); saveHealLocations(map);
// Update global data structures with current map data.
updateMapLayout(map);
map->isPersistedToFile = true; map->isPersistedToFile = true;
map->hasUnsavedDataChanges = false; map->hasUnsavedDataChanges = false;
map->editHistory.setClean(); map->editHistory.setClean();
} }
void Project::updateMapLayout(Map* map) { void Project::saveLayout(Layout *layout) {
if (!mapLayoutsTableMaster.contains(map->layoutId)) { //
mapLayoutsTableMaster.append(map->layoutId); 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?? // !TODO: why is[was] this a deep copy??
@ -1376,6 +1382,12 @@ void Project::updateMapLayout(Map* map) {
// Layout *newLayout = new Layout(); // Layout *newLayout = new Layout();
// *newLayout = *layout; // *newLayout = *layout;
// mapLayoutsMaster.insert(map->layoutId, newLayout); // 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() { void Project::saveAllDataStructures() {