diff --git a/include/mainwindow.h b/include/mainwindow.h index 0c3bd4ee..73d16a91 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -394,6 +394,9 @@ private: void mapListAddGroup(); void mapListAddLayout(); void mapListAddArea(); + void mapListRemoveGroup(); + void mapListRemoveArea(); + void mapListRemoveLayout(); void displayMapProperties(); void checkToolButtons(); diff --git a/include/ui/maplistmodels.h b/include/ui/maplistmodels.h index 5787a622..7d56da17 100644 --- a/include/ui/maplistmodels.h +++ b/include/ui/maplistmodels.h @@ -77,6 +77,7 @@ public: QStandardItem *insertGroupItem(QString groupName); QStandardItem *insertMapItem(QString mapName, QString groupName); + void removeGroup(int groupIndex); QStandardItem *getItem(const QModelIndex &index) const; QModelIndex indexOfMap(QString mapName); @@ -119,6 +120,7 @@ public: QStandardItem *insertAreaItem(QString areaName); QStandardItem *insertMapItem(QString mapName, QString areaName, int groupIndex); + void removeArea(int groupIndex); QStandardItem *getItem(const QModelIndex &index) const; QModelIndex indexOfMap(QString mapName); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index dc0ac54f..b96003a9 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1459,6 +1459,8 @@ void MainWindow::mapListAddArea() { } void MainWindow::mapListAddItem() { + if (!this->editor || !this->editor->project) return; + switch (this->ui->mapListContainer->currentIndex()) { case 0: this->mapListAddGroup(); @@ -1472,8 +1474,61 @@ void MainWindow::mapListAddItem() { } } +void MainWindow::mapListRemoveGroup() { + QItemSelectionModel *selectionModel = this->ui->mapList->selectionModel(); + if (selectionModel->hasSelection()) { + QModelIndexList selectedIndexes = selectionModel->selectedRows(); + for (QModelIndex proxyIndex : selectedIndexes) { + QModelIndex index = this->groupListProxyModel->mapToSource(proxyIndex); + QStandardItem *item = this->mapGroupModel->getItem(index)->child(index.row(), index.column()); + if (!item) continue; + QString type = item->data(MapListRoles::TypeRole).toString(); + if (type == "map_group" && !item->hasChildren()) { + QString groupName = item->data(Qt::UserRole).toString(); + // delete empty group + this->mapGroupModel->removeGroup(index.row()); + } + } + } +} + +void MainWindow::mapListRemoveArea() { + QItemSelectionModel *selectionModel = this->ui->areaList->selectionModel(); + if (selectionModel->hasSelection()) { + QModelIndexList selectedIndexes = selectionModel->selectedRows(); + for (QModelIndex proxyIndex : selectedIndexes) { + QModelIndex index = this->areaListProxyModel->mapToSource(proxyIndex); + QStandardItem *item = this->mapAreaModel->getItem(index)->child(index.row(), index.column()); + if (!item) continue; + QString type = item->data(MapListRoles::TypeRole).toString(); + if (type == "map_section" && !item->hasChildren()) { + QString groupName = item->data(Qt::UserRole).toString(); + // delete empty section + this->mapAreaModel->removeArea(index.row()); + } + } + } +} + +void MainWindow::mapListRemoveLayout() { + // TODO: consider this + // do nothing, for now at least +} + void MainWindow::mapListRemoveItem() { - // !TODO + if (!this->editor || !this->editor->project) return; + + switch (this->ui->mapListContainer->currentIndex()) { + case 0: + this->mapListRemoveGroup(); + break; + case 1: + this->mapListRemoveArea(); + break; + case 2: + this->mapListRemoveLayout(); + break; + } } void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction) { diff --git a/src/project.cpp b/src/project.cpp index 6ee18551..0f641c91 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -712,6 +712,8 @@ void Project::saveMapSections() { longestLength = label.size(); } + longestLength += 1; + // mapSectionValueToName for (int value : this->mapSectionValueToName.keys()) { QString line = QString("#define %1 0x%2\n") diff --git a/src/ui/maplistmodels.cpp b/src/ui/maplistmodels.cpp index 8dbc24b8..880a98ca 100644 --- a/src/ui/maplistmodels.cpp +++ b/src/ui/maplistmodels.cpp @@ -244,6 +244,11 @@ QStandardItem *MapGroupModel::insertGroupItem(QString groupName) { return group; } +void MapGroupModel::removeGroup(int groupIndex) { + this->removeRow(groupIndex); + this->updateProject(); +} + QStandardItem *MapGroupModel::insertMapItem(QString mapName, QString groupName) { QStandardItem *group = this->groupItems[groupName]; if (!group) { @@ -412,6 +417,11 @@ QStandardItem *MapAreaModel::insertMapItem(QString mapName, QString areaName, in return map; } +void MapAreaModel::removeArea(int areaIndex) { + this->removeRow(areaIndex); + this->project->mapSectionNameToValue.remove(this->project->mapSectionValueToName.take(areaIndex)); +} + void MapAreaModel::initialize() { this->areaItems.clear(); this->mapItems.clear(); @@ -454,6 +464,8 @@ QModelIndex MapAreaModel::indexOfMap(QString mapName) { } QVariant MapAreaModel::data(const QModelIndex &index, int role) const { + if (!index.isValid()) return QVariant(); + int row = index.row(); int col = index.column(); @@ -600,6 +612,8 @@ QModelIndex LayoutTreeModel::indexOfLayout(QString layoutName) { } QVariant LayoutTreeModel::data(const QModelIndex &index, int role) const { + if (!index.isValid()) return QVariant(); + int row = index.row(); int col = index.column();