diff --git a/include/core/maplayout.h b/include/core/maplayout.h index 968b8960..c17248ce 100644 --- a/include/core/maplayout.h +++ b/include/core/maplayout.h @@ -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(); diff --git a/include/project.h b/include/project.h index 24f14611..34a6a49d 100644 --- a/include/project.h +++ b/include/project.h @@ -58,7 +58,7 @@ public: QString layoutsLabel; QMap layoutIdsToNames; QMap mapLayouts; -// QMap mapLayoutsMaster; + QMap mapLayoutsMaster; QMap mapSecToMapHoverName; QMap mapSectionNameToValue; QMap 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); diff --git a/src/core/maplayout.cpp b/src/core/maplayout.cpp index 4fd05b4f..3dd9b9f2 100644 --- a/src/core/maplayout.cpp +++ b/src/core/maplayout.cpp @@ -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) { diff --git a/src/editor.cpp b/src/editor.cpp index 7d6d2780..fc77b2b6 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -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); } } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index d5c985b3..db766c0c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -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 &){ - // 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(); } } diff --git a/src/project.cpp b/src/project.cpp index ba273f17..245f3fb8 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -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() {