Skip to content

Commit

Permalink
Finish support for deleting MAPSEC values
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Nov 8, 2024
1 parent d448765 commit 06ece16
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 24 deletions.
1 change: 1 addition & 0 deletions include/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ private slots:
void scrollMetatileSelectorToSelection();
MapListToolBar* getCurrentMapListToolBar();
MapTree* getCurrentMapList();
void refreshLocationsComboBox();

QObjectList shortcutableObjects() const;
void addCustomHeaderValue(QString key, QJsonValue value, bool isNew = false);
Expand Down
4 changes: 3 additions & 1 deletion include/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ class Project : public QObject
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;
Expand Down Expand Up @@ -266,6 +267,7 @@ class Project : public QObject

signals:
void fileChanged(QString filepath);
void mapSectionIdNamesChanged();
void mapLoaded(Map *map);
};

Expand Down
18 changes: 13 additions & 5 deletions include/ui/maplistmodels.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ class MapListModel : public QStandardItemModel {
~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 {
Expand All @@ -94,13 +96,15 @@ class MapGroupModel : public MapListModel {

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();
Expand Down Expand Up @@ -137,13 +141,15 @@ class MapAreaModel : public MapListModel {

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;
Expand Down Expand Up @@ -176,13 +182,15 @@ class LayoutTreeModel : public MapListModel {

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;
Expand Down
38 changes: 31 additions & 7 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
Expand Down
12 changes: 11 additions & 1 deletion src/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2319,14 +2319,24 @@ 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);
} else {
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
Expand Down
19 changes: 9 additions & 10 deletions src/ui/maplistmodels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions src/ui/newmappopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) :
Expand Down

0 comments on commit 06ece16

Please sign in to comment.