Finish support for deleting MAPSEC values

This commit is contained in:
GriffinR 2024-11-08 13:55:50 -05:00
parent d448765d63
commit 06ece16b93
7 changed files with 70 additions and 24 deletions

View file

@ -417,6 +417,7 @@ private:
void scrollMetatileSelectorToSelection();
MapListToolBar* getCurrentMapListToolBar();
MapTree* getCurrentMapList();
void refreshLocationsComboBox();
QObjectList shortcutableObjects() const;
void addCustomHeaderValue(QString key, QJsonValue value, bool isNew = false);

View file

@ -139,7 +139,8 @@ public:
bool readSpeciesIconPaths();
QMap<QString, QString> speciesToIconPath;
void addNewMapsec(QString name);
void addNewMapsec(const QString &name);
void removeMapsec(const QString &name);
bool hasUnsavedChanges();
bool hasUnsavedDataChanges = false;
@ -266,6 +267,7 @@ private:
signals:
void fileChanged(QString filepath);
void mapSectionIdNamesChanged();
void mapLoaded(Map *map);
};

View file

@ -65,9 +65,11 @@ public:
~MapListModel() { }
virtual QModelIndex indexOf(QString id) const = 0;
virtual void removeFolder(int index) = 0;
virtual void removeItem(const QModelIndex &index);
virtual void removeItemAt(const QModelIndex &index);
virtual QStandardItem *getItem(const QModelIndex &index) const = 0;
protected:
virtual void removeItem(QStandardItem *item) = 0;
};
class MapGroupModel : public MapListModel {
@ -94,13 +96,15 @@ public:
QStandardItem *insertGroupItem(QString groupName);
QStandardItem *insertMapItem(QString mapName, QString groupName);
virtual void removeFolder(int index) override;
virtual QStandardItem *getItem(const QModelIndex &index) const override;
virtual QModelIndex indexOf(QString mapName) const override;
void initialize();
protected:
virtual void removeItem(QStandardItem *item) override;
private:
friend class MapTree;
void updateProject();
@ -137,13 +141,15 @@ public:
QStandardItem *insertAreaItem(QString areaName);
QStandardItem *insertMapItem(QString mapName, QString areaName, int groupIndex);
virtual void removeFolder(int index) override;
virtual QStandardItem *getItem(const QModelIndex &index) const override;
virtual QModelIndex indexOf(QString mapName) const override;
void initialize();
protected:
virtual void removeItem(QStandardItem *item) override;
private:
Project *project;
QStandardItem *root = nullptr;
@ -176,13 +182,15 @@ public:
QStandardItem *insertLayoutItem(QString layoutId);
QStandardItem *insertMapItem(QString mapName, QString layoutId);
virtual void removeFolder(int index) override;
virtual QStandardItem *getItem(const QModelIndex &index) const override;
virtual QModelIndex indexOf(QString layoutName) const override;
void initialize();
protected:
virtual void removeItem(QStandardItem *item) override;
private:
Project *project;
QStandardItem *root = nullptr;

View file

@ -598,8 +598,9 @@ bool MainWindow::openProject(QString dir, bool initial) {
// Create the project
auto project = new Project(editor);
project->set_root(dir);
QObject::connect(project, &Project::fileChanged, this, &MainWindow::showFileWatcherWarning);
QObject::connect(project, &Project::mapLoaded, this, &MainWindow::onMapLoaded);
connect(project, &Project::fileChanged, this, &MainWindow::showFileWatcherWarning);
connect(project, &Project::mapLoaded, this, &MainWindow::onMapLoaded);
connect(project, &Project::mapSectionIdNamesChanged, this, &MainWindow::refreshLocationsComboBox);
this->editor->setProject(project);
// Make sure project looks reasonable before attempting to load it
@ -1163,7 +1164,6 @@ bool MainWindow::setProjectUI() {
// Block signals to the comboboxes while they are being modified
const QSignalBlocker blocker1(ui->comboBox_Song);
const QSignalBlocker blocker2(ui->comboBox_Location);
const QSignalBlocker blocker3(ui->comboBox_PrimaryTileset);
const QSignalBlocker blocker4(ui->comboBox_SecondaryTileset);
const QSignalBlocker blocker5(ui->comboBox_Weather);
@ -1176,8 +1176,6 @@ bool MainWindow::setProjectUI() {
// Set up project comboboxes
ui->comboBox_Song->clear();
ui->comboBox_Song->addItems(project->songNames);
ui->comboBox_Location->clear();
ui->comboBox_Location->addItems(project->mapSectionIdNames);
ui->comboBox_PrimaryTileset->clear();
ui->comboBox_PrimaryTileset->addItems(project->primaryTilesetLabels);
ui->comboBox_SecondaryTileset->clear();
@ -1198,6 +1196,7 @@ bool MainWindow::setProjectUI() {
ui->comboBox_EmergeMap->addItems(project->mapNames);
ui->comboBox_EmergeMap->setClearButtonEnabled(true);
ui->comboBox_EmergeMap->setFocusedScrollingEnabled(false);
refreshLocationsComboBox();
// Show/hide parts of the UI that are dependent on the user's project settings
@ -1247,6 +1246,17 @@ bool MainWindow::setProjectUI() {
return true;
}
void MainWindow::refreshLocationsComboBox() {
QStringList locations = this->editor->project->mapSectionIdNames;
locations.sort();
const QSignalBlocker b(ui->comboBox_Location);
ui->comboBox_Location->clear();
ui->comboBox_Location->addItems(locations);
if (this->editor->map)
ui->comboBox_Location->setCurrentText(this->editor->map->location);
}
void MainWindow::clearProjectUI() {
// Block signals to the comboboxes while they are being modified
const QSignalBlocker blocker1(ui->comboBox_Song);
@ -1328,19 +1338,30 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point) {
QMenu menu(this);
QAction* addToFolderAction = nullptr;
QAction* deleteFolderAction = nullptr;
QAction* openItemAction = nullptr;
if (itemType == "map_name") {
// Right-clicking on a map.
// TODO: Add action to delete map once deleting maps is supported
openItemAction = menu.addAction("Open Map");
//menu.addSeparator();
//connect(menu.addAction("Delete Map"), &QAction::triggered, [this, index] { deleteMapListItem(index); }); // TODO: No support for deleting maps
} else if (itemType == "map_group") {
// Right-clicking on a map group folder
addToFolderAction = menu.addAction("Add New Map to Group");
menu.addSeparator();
deleteFolderAction = menu.addAction("Delete Map Group");
} else if (itemType == "map_section") {
// Right-clicking on an MAPSEC folder
addToFolderAction = menu.addAction("Add New Map to Area");
menu.addSeparator();
deleteFolderAction = menu.addAction("Delete Area");
if (itemName == this->editor->project->getEmptyMapsecName())
deleteFolderAction->setEnabled(false); // Disallow deleting the default name
} else if (itemType == "map_layout") {
// Right-clicking on a map layout
openItemAction = menu.addAction("Open Layout");
addToFolderAction = menu.addAction("Add New Map with Layout");
//menu.addSeparator();
//deleteFolderAction = menu.addAction("Delete Layout"); // TODO: No support for deleting layouts
}
if (addToFolderAction) {
@ -1351,13 +1372,16 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point) {
}
if (deleteFolderAction) {
connect(deleteFolderAction, &QAction::triggered, [sourceModel, index] {
sourceModel->removeFolder(index.row());
sourceModel->removeItemAt(index);
});
if (selectedItem->hasChildren()){
// TODO: No support for deleting maps, so you may only delete folders if they don't contain any maps.
deleteFolderAction->setEnabled(false);
}
}
if (openItemAction) {
connect(openItemAction, &QAction::triggered, [this, index] { openMapListItem(index); });
}
if (menu.actions().length() != 0)
menu.exec(QCursor::pos());

View file

@ -2319,7 +2319,7 @@ QString Project::getEmptyMapsecName() {
}
// This function assumes a valid and unique name
void Project::addNewMapsec(QString name) {
void Project::addNewMapsec(const QString &name) {
if (!this->mapSectionIdNames.isEmpty() && this->mapSectionIdNames.last() == getEmptyMapsecName()) {
// If the default map section name (MAPSEC_NONE) is last in the list we'll keep it last in the list.
this->mapSectionIdNames.insert(this->mapSectionIdNames.length() - 1, name);
@ -2327,6 +2327,16 @@ void Project::addNewMapsec(QString name) {
this->mapSectionIdNames.append(name);
}
this->hasUnsavedDataChanges = true;
emit mapSectionIdNamesChanged();
}
void Project::removeMapsec(const QString &name) {
if (!this->mapSectionIdNames.contains(name) || name == getEmptyMapsecName())
return;
this->mapSectionIdNames.removeOne(name);
this->hasUnsavedDataChanges = true;
emit mapSectionIdNamesChanged();
}
// Read the constants to preserve any "unused" heal locations when writing the file later

View file

@ -31,14 +31,14 @@ void MapTree::keyPressEvent(QKeyEvent *event) {
persistentIndexes.append(model->mapToSource(index));
}
for (const auto &index : persistentIndexes) {
sourceModel->removeItem(index);
sourceModel->removeItemAt(index);
}
} else {
QWidget::keyPressEvent(event);
}
}
void MapListModel::removeItem(const QModelIndex &index) {
void MapListModel::removeItemAt(const QModelIndex &index) {
QStandardItem *item = this->getItem(index)->child(index.row(), index.column());
if (!item)
return;
@ -49,7 +49,7 @@ void MapListModel::removeItem(const QModelIndex &index) {
} else {
// TODO: Because there's no support for deleting maps we can only delete empty folders
if (!item->hasChildren()) {
this->removeFolder(index.row());
this->removeItem(item);
}
}
}
@ -282,8 +282,8 @@ QStandardItem *MapGroupModel::insertGroupItem(QString groupName) {
return group;
}
void MapGroupModel::removeFolder(int index) {
this->removeRow(index);
void MapGroupModel::removeItem(QStandardItem *item) {
this->removeRow(item->row());
this->updateProject();
}
@ -454,10 +454,9 @@ QStandardItem *MapAreaModel::insertMapItem(QString mapName, QString areaName, in
return map;
}
// Note: Not actually supported in the interface at the moment.
void MapAreaModel::removeFolder(int index) {
this->removeRow(index);
this->project->mapSectionIdNames.removeAt(index);
void MapAreaModel::removeItem(QStandardItem *item) {
this->project->removeMapsec(item->data(Qt::UserRole).toString());
this->removeRow(item->row());
}
void MapAreaModel::initialize() {
@ -612,7 +611,7 @@ QStandardItem *LayoutTreeModel::insertMapItem(QString mapName, QString layoutId)
return map;
}
void LayoutTreeModel::removeFolder(int) {
void LayoutTreeModel::removeItem(QStandardItem *) {
// TODO: Deleting layouts not supported
}

View file

@ -8,6 +8,8 @@
#include <QSet>
#include <QStringList>
// TODO: Convert to modal dialog (among other things, this means we wouldn't need to worry about changes to the map list while this is open)
struct NewMapPopup::Settings NewMapPopup::settings = {};
NewMapPopup::NewMapPopup(QWidget *parent, Project *project) :