From c3defd6887b384046b625a913a14801c161cc3bc Mon Sep 17 00:00:00 2001 From: garakmon Date: Fri, 31 Jul 2020 17:12:08 -0400 Subject: [PATCH] small fixes - connect DEL key to event deletion action - update checks for unsaved changes to map - add an asterisk next to the map name in the window title when a map has unsaved changes --- include/core/editcommands.h | 26 ++++++++++++++++---------- include/core/maplayout.h | 1 - include/mainwindow.h | 2 ++ src/core/map.cpp | 3 ++- src/mainwindow.cpp | 27 +++++++++++++++++++++------ src/project.cpp | 12 ++++++------ 6 files changed, 47 insertions(+), 24 deletions(-) diff --git a/include/core/editcommands.h b/include/core/editcommands.h index c57afa47..ab53817d 100644 --- a/include/core/editcommands.h +++ b/include/core/editcommands.h @@ -28,7 +28,8 @@ enum CommandId { -/// +/// Implements a command to commit metatile paint actions +/// onto the map using the pencil tool. class PaintMetatile : public QUndoCommand { public: PaintMetatile(Map *map, @@ -53,7 +54,7 @@ private: -/// +/// Implements a command to commit paint actions on the map border. class PaintBorder : public QUndoCommand { public: PaintBorder(Map *map, @@ -78,7 +79,8 @@ private: -/// +/// Implements a command to commit flood fill metatile actions +/// with the bucket tool onto the map. class BucketFillMetatile : public PaintMetatile { public: BucketFillMetatile(Map *map, @@ -92,7 +94,8 @@ public: -/// +/// Implements a command to commit magic fill metatile actions +/// with the bucket or paint tool onto the map. class MagicFillMetatile : public PaintMetatile { public: MagicFillMetatile(Map *map, @@ -107,7 +110,7 @@ public: -/// +/// Implements a command to commit metatile shift actions. class ShiftMetatiles : public QUndoCommand { public: ShiftMetatiles(Map *map, @@ -133,7 +136,7 @@ private: -/// +/// Implements a command to commit a map or border resize action. class ResizeMap : public QUndoCommand { public: ResizeMap(Map *map, QSize oldMapDimensions, QSize newMapDimensions, @@ -171,7 +174,8 @@ private: -/// +/// Implements a command to commit a single- or multi-Event move action. +/// Actions are merged into one until the mouse is released. class EventMove : public QUndoCommand { public: EventMove(QList events, @@ -195,7 +199,7 @@ private: -/// +/// Implements a command to commit Event shift actions. class EventShift : public EventMove { public: EventShift(QList events, @@ -208,7 +212,8 @@ public: -/// +/// Implements a command to commit Event create actions. +/// Works for a single Event only. class EventCreate : public QUndoCommand { public: EventCreate(Editor *editor, Map *map, Event *event, @@ -229,7 +234,8 @@ private: -/// +/// Implements a command to commit Event deletions. +/// Applies to every currently selected Event. class EventDelete : public QUndoCommand { public: EventDelete(Editor *editor, Map *map, QList selectedEvents, diff --git a/include/core/maplayout.h b/include/core/maplayout.h index c0c603ee..2e4d5222 100644 --- a/include/core/maplayout.h +++ b/include/core/maplayout.h @@ -30,7 +30,6 @@ public: Blockdata *cached_blockdata = nullptr; Blockdata *cached_collision = nullptr; Blockdata *cached_border = nullptr; - bool has_unsaved_changes = false; }; #endif // MAPLAYOUT_H diff --git a/include/mainwindow.h b/include/mainwindow.h index c4624efe..f6b382c2 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -290,6 +290,8 @@ private: void checkToolButtons(); void clickToolButtonFromEditMode(QString editMode); + void showWindowTitle(); + void initWindow(); void initCustomUI(); void initExtraShortcuts(); diff --git a/src/core/map.cpp b/src/core/map.cpp index 3b61a874..ed20b835 100644 --- a/src/core/map.cpp +++ b/src/core/map.cpp @@ -14,6 +14,7 @@ Map::Map(QObject *parent) : QObject(parent) { + editHistory.setClean(); } void Map::setName(QString mapName) { @@ -466,5 +467,5 @@ void Map::addEvent(Event *event) { } bool Map::hasUnsavedChanges() { - return !metatileHistory.isSaved() || !isPersistedToFile || layout->has_unsaved_changes; + return !editHistory.isClean() || !isPersistedToFile; } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 05846e2a..a69c3ee9 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -107,6 +107,7 @@ void MainWindow::initExtraShortcuts() { new QShortcut(QKeySequence("Ctrl+0"), this, SLOT(resetMapViewScale())); new QShortcut(QKeySequence("Ctrl+G"), ui->checkBox_ToggleGrid, SLOT(toggle())); new QShortcut(QKeySequence("Ctrl+D"), this, SLOT(duplicate())); + new QShortcut(QKeySequence::Delete, this, SLOT(on_toolButton_deleteObject_clicked())); ui->actionZoom_In->setShortcuts({QKeySequence("Ctrl++"), QKeySequence("Ctrl+=")}); } @@ -168,6 +169,9 @@ void MainWindow::initEditor() { //ui->comboBox_History->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength); //ui->comboBox_History->setModel(undoView->model()); //ui->comboBox_History->setView(undoView); + + // Toggle an asterisk in the window title when the undo state is changed + connect(&editor->editGroup, &QUndoGroup::cleanChanged, this, &MainWindow::showWindowTitle); } void MainWindow::initMiscHeapObjects() { @@ -211,6 +215,16 @@ void MainWindow::initMapSortOrder() { sortOrder->setChecked(true); } +void MainWindow::showWindowTitle() { + if (editor->map) { + setWindowTitle(QString("%1%2 - %3") + .arg(editor->map->hasUnsavedChanges() ? "* " : "") + .arg(editor->map->name) + .arg(editor->project->getProjectTitle()) + ); + } +} + void MainWindow::setProjectSpecificUIVisibility() { ui->actionUse_Encounter_Json->setChecked(projectConfig.getEncounterJsonActive()); @@ -399,7 +413,7 @@ bool MainWindow::openProject(QString dir) { } if (success) { - setWindowTitle(editor->project->getProjectTitle()); + showWindowTitle(); this->statusBar()->showMessage(QString("Opened project %1").arg(nativeDir)); } else { this->statusBar()->showMessage(QString("Failed to open project %1").arg(nativeDir)); @@ -515,7 +529,7 @@ bool MainWindow::setMap(QString map_name, bool scrollTreeView) { ui->mapList->setExpanded(mapListProxyModel->mapFromSource(mapListIndexes.value(map_name)), true); - setWindowTitle(map_name + " - " + editor->project->getProjectTitle()); + showWindowTitle(); connect(editor->map, SIGNAL(mapChanged(Map*)), this, SLOT(onMapChanged(Map *))); connect(editor->map, SIGNAL(mapNeedsRedrawing()), this, SLOT(onMapNeedsRedrawing())); @@ -2108,9 +2122,11 @@ void MainWindow::on_horizontalSlider_CollisionTransparency_valueChanged(int valu this->editor->collision_item->draw(true); } -// TODO: connect this to the DEL key when undo/redo history is extended to events -void MainWindow::on_toolButton_deleteObject_clicked() -{ +void MainWindow::on_toolButton_deleteObject_clicked() { + if (ui->mainTabBar->currentIndex() != 1) { + // do not delete an event when not on event tab + return; + } if (editor && editor->selected_events) { if (editor->selected_events->length()) { DraggablePixmapItem *next_selected_event = nullptr; @@ -2311,7 +2327,6 @@ void MainWindow::onLoadMapRequested(QString mapName, QString fromMapName) { } void MainWindow::onMapChanged(Map *map) { - map->layout->has_unsaved_changes = true; updateMapList(); } diff --git a/src/project.cpp b/src/project.cpp index 981e4304..ba4f6063 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1120,7 +1120,7 @@ void Project::saveTilesetPalettes(Tileset *tileset) { } bool Project::loadMapTilesets(Map* map) { - if (map->layout->has_unsaved_changes) { + if (map->hasUnsavedChanges()) { return true; } @@ -1179,8 +1179,8 @@ Tileset* Project::loadTileset(QString label, Tileset *tileset) { return tileset; } -bool Project::loadBlockdata(Map* map) { - if (!map->isPersistedToFile || map->layout->has_unsaved_changes) { +bool Project::loadBlockdata(Map *map) { + if (map->hasUnsavedChanges()) { return true; } @@ -1198,7 +1198,7 @@ bool Project::loadBlockdata(Map* map) { return true; } -void Project::setNewMapBlockdata(Map* map) { +void Project::setNewMapBlockdata(Map *map) { Blockdata *blockdata = new Blockdata; for (int i = 0; i < map->getWidth() * map->getHeight(); i++) { blockdata->addBlock(qint16(0x3001)); @@ -1207,7 +1207,7 @@ void Project::setNewMapBlockdata(Map* map) { } bool Project::loadMapBorder(Map *map) { - if (!map->isPersistedToFile || map->layout->has_unsaved_changes) { + if (map->hasUnsavedChanges()) { return true; } @@ -1461,7 +1461,7 @@ void Project::saveMap(Map *map) { updateMapLayout(map); map->isPersistedToFile = true; - map->layout->has_unsaved_changes = false; + map->editHistory.setClean(); } void Project::updateMapLayout(Map* map) {