From 3bd5ddbf2f1c227356f8eed9fbac64ee499b8f8d Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 28 Oct 2024 16:02:17 -0400 Subject: [PATCH] Simplify saving the map list tab --- include/config.h | 10 ++----- include/mainwindow.h | 5 +--- include/ui/newmappopup.h | 2 +- src/config.cpp | 18 ++--------- src/mainwindow.cpp | 65 ++++++++++++---------------------------- src/ui/newmappopup.cpp | 10 +++---- 6 files changed, 31 insertions(+), 79 deletions(-) diff --git a/include/config.h b/include/config.h index 01a7b09c..31f7fa7f 100644 --- a/include/config.h +++ b/include/config.h @@ -22,12 +22,6 @@ static const QVersionNumber porymapVersion = QVersionNumber::fromString(PORYMAP_ #define CONFIG_BACKWARDS_COMPATABILITY -enum MapSortOrder { - SortByGroup = 0, - SortByArea = 1, - SortByLayout = 2, -}; - class KeyValueConfigBase { public: @@ -56,7 +50,7 @@ public: this->recentProjects.clear(); this->projectManuallyClosed = false; this->reopenOnLaunch = true; - this->mapSortOrder = MapSortOrder::SortByGroup; + this->mapListTab = 0; this->prettyCursors = true; this->mirrorConnectingMaps = true; this->showDiveEmergeMaps = false; @@ -107,7 +101,7 @@ public: bool reopenOnLaunch; bool projectManuallyClosed; - MapSortOrder mapSortOrder; + int mapListTab; bool prettyCursors; bool mirrorConnectingMaps; bool showDiveEmergeMaps; diff --git a/include/mainwindow.h b/include/mainwindow.h index e0bf7177..2abc151d 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -242,11 +242,7 @@ private slots: void on_toolButton_Move_clicked(); void on_toolButton_Shift_clicked(); - void on_mapListContainer_currentChanged(int index); void onOpenMapListContextMenu(const QPoint &point); - void onAddNewMapToGroupClick(QAction* triggeredAction); - void onAddNewMapToAreaClick(QAction* triggeredAction); - void onAddNewMapToLayoutClick(QAction* triggeredAction); void currentMetatilesSelectionChanged(); void on_action_Export_Map_Image_triggered(); @@ -393,6 +389,7 @@ private: void mapListRemoveArea(); void mapListRemoveLayout(); void openMapListItem(const QModelIndex &index); + void saveMapListTab(int index); void displayMapProperties(); void checkToolButtons(); diff --git a/include/ui/newmappopup.h b/include/ui/newmappopup.h index 3d24715d..66a15a91 100644 --- a/include/ui/newmappopup.h +++ b/include/ui/newmappopup.h @@ -24,7 +24,7 @@ public: QString layoutId; void init(); void initUi(); - void init(MapSortOrder type, QVariant data); + void init(int tabIndex, QVariant data); void init(Layout *); static void setDefaultSettings(Project *project); diff --git a/src/config.cpp b/src/config.cpp index 0d72542d..ca6473b1 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -278,12 +278,6 @@ uint32_t KeyValueConfigBase::getConfigUint32(QString key, QString value, uint32_ return qMin(max, qMax(min, result)); } -const QMap mapSortOrderMap = { - {"group", MapSortOrder::SortByGroup}, - {"layout", MapSortOrder::SortByLayout}, - {"area", MapSortOrder::SortByArea}, -}; - PorymapConfig porymapConfig; QString PorymapConfig::getConfigFilepath() { @@ -308,14 +302,8 @@ void PorymapConfig::parseConfigKeyValue(QString key, QString value) { this->reopenOnLaunch = getConfigBool(key, value); } else if (key == "pretty_cursors") { this->prettyCursors = getConfigBool(key, value); - } else if (key == "map_sort_order") { - QString sortOrder = value.toLower(); - if (mapSortOrderMap.contains(sortOrder)) { - this->mapSortOrder = mapSortOrderMap.value(sortOrder); - } else { - this->mapSortOrder = MapSortOrder::SortByGroup; - logWarn(QString("Invalid config value for map_sort_order: '%1'. Must be 'group', 'area', or 'layout'.").arg(value)); - } + } else if (key == "map_list_tab") { + this->mapListTab = getConfigInteger(key, value, 0, 2, 0); } else if (key == "main_window_geometry") { this->mainWindowGeometry = bytesFromString(value); } else if (key == "main_window_state") { @@ -432,7 +420,7 @@ QMap PorymapConfig::getKeyValueMap() { map.insert("project_manually_closed", this->projectManuallyClosed ? "1" : "0"); map.insert("reopen_on_launch", this->reopenOnLaunch ? "1" : "0"); map.insert("pretty_cursors", this->prettyCursors ? "1" : "0"); - map.insert("map_sort_order", mapSortOrderMap.key(this->mapSortOrder)); + map.insert("map_list_tab", QString::number(this->mapListTab)); map.insert("main_window_geometry", stringFromByteArray(this->mainWindowGeometry)); map.insert("main_window_state", stringFromByteArray(this->mainWindowState)); map.insert("map_splitter_state", stringFromByteArray(this->mapSplitterState)); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 8ea0d5df..db7cf6b1 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -371,7 +371,7 @@ void MainWindow::initMiscHeapObjects() { } void MainWindow::initMapList() { - ui->mapListContainer->setCurrentIndex(static_cast(porymapConfig.mapSortOrder)); + ui->mapListContainer->setCurrentIndex(porymapConfig.mapListTab); WheelFilter *wheelFilter = new WheelFilter(this); ui->mainTabBar->installEventFilter(wheelFilter); @@ -430,6 +430,8 @@ void MainWindow::initMapList() { connect(ui->mapListToolBar_Groups, &MapListToolBar::addFolderClicked, this, &MainWindow::mapListAddGroup); connect(ui->mapListToolBar_Areas, &MapListToolBar::addFolderClicked, this, &MainWindow::mapListAddArea); connect(ui->mapListToolBar_Layouts, &MapListToolBar::addFolderClicked, this, &MainWindow::mapListAddLayout); + + connect(ui->mapListContainer, &QTabWidget::currentChanged, this, &MainWindow::saveMapListTab); } void MainWindow::updateWindowTitle() { @@ -1209,7 +1211,6 @@ bool MainWindow::setProjectUI() { ui->spinBox_SelectedCollision->setMaximum(Block::getMaxCollision()); // map models - // !TODO: delete these on close this->mapGroupModel = new MapGroupModel(editor->project); this->groupListProxyModel = new FilterChildrenProxyModel(); groupListProxyModel->setSourceModel(this->mapGroupModel); @@ -1308,32 +1309,30 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point) { int dataRole; FilterChildrenProxyModel *proxy; QTreeView *list; - void (MainWindow::*addFunction)(QAction *); QString actionText; - switch (porymapConfig.mapSortOrder) { - case MapSortOrder::SortByGroup: + int currentTab = ui->mapListContainer->currentIndex(); + + switch (currentTab) { + case MapListTab::Groups: model = this->mapGroupModel; dataRole = MapListUserRoles::GroupRole; proxy = this->groupListProxyModel; list = this->ui->mapList; - addFunction = &MainWindow::onAddNewMapToGroupClick; actionText = "Add New Map to Group"; break; - case MapSortOrder::SortByArea: + case MapListTab::Areas: model = this->mapAreaModel; dataRole = Qt::UserRole; proxy = this->areaListProxyModel; list = this->ui->areaList; - addFunction = &MainWindow::onAddNewMapToAreaClick; actionText = "Add New Map to Area"; break; - case MapSortOrder::SortByLayout: + case MapListTab::Layouts: model = this->layoutTreeModel; dataRole = Qt::UserRole; proxy = this->layoutListProxyModel; list = this->ui->layoutList; - addFunction = &MainWindow::onAddNewMapToLayoutClick; actionText = "Add New Map with Layout"; break; } @@ -1358,7 +1357,14 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point) { QMenu menu(this); QActionGroup actions(&menu); actions.addAction(menu.addAction(actionText))->setData(itemData); - (this->*addFunction)(menu.exec(QCursor::pos())); + + auto triggeredAction = menu.exec(QCursor::pos()); + if (!triggeredAction) + return; + + // At the moment all the actions do the same thing (add new map/layout). + openNewMapPopupWindow(); + this->newMapPrompt->init(currentTab, triggeredAction->data()); } void MainWindow::mapListAddGroup() { @@ -1598,28 +1604,6 @@ void MainWindow::mapListRemoveLayout() { // TODO: consider this in the future } - -void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction) { - if (!triggeredAction) return; - - openNewMapPopupWindow(); - this->newMapPrompt->init(MapSortOrder::SortByGroup, triggeredAction->data()); -} - -void MainWindow::onAddNewMapToAreaClick(QAction* triggeredAction) { - if (!triggeredAction) return; - - openNewMapPopupWindow(); - this->newMapPrompt->init(MapSortOrder::SortByArea, triggeredAction->data()); -} - -void MainWindow::onAddNewMapToLayoutClick(QAction* triggeredAction) { - if (!triggeredAction) return; - - openNewMapPopupWindow(); - this->newMapPrompt->init(MapSortOrder::SortByLayout, triggeredAction->data()); -} - void MainWindow::onNewMapCreated() { QString newMapName = this->newMapPrompt->map->name; int newMapGroup = this->newMapPrompt->group; @@ -1862,19 +1846,8 @@ void MainWindow::currentMetatilesSelectionChanged() { scrollMetatileSelectorToSelection(); } -// TODO: Redundant. Remove -void MainWindow::on_mapListContainer_currentChanged(int index) { - switch (index) { - case MapListTab::Groups: - porymapConfig.mapSortOrder = MapSortOrder::SortByGroup; - break; - case MapListTab::Areas: - porymapConfig.mapSortOrder = MapSortOrder::SortByArea; - break; - case MapListTab::Layouts: - porymapConfig.mapSortOrder = MapSortOrder::SortByLayout; - break; - } +void MainWindow::saveMapListTab(int index) { + porymapConfig.mapListTab = index; } void MainWindow::openMapListItem(const QModelIndex &index) { diff --git a/src/ui/newmappopup.cpp b/src/ui/newmappopup.cpp index 17c1ed29..a9d661da 100644 --- a/src/ui/newmappopup.cpp +++ b/src/ui/newmappopup.cpp @@ -102,17 +102,17 @@ void NewMapPopup::init() { } // Creating new map by right-clicking in the map list -void NewMapPopup::init(MapSortOrder type, QVariant data) { +void NewMapPopup::init(int tabIndex, QVariant data) { initUi(); - switch (type) + switch (tabIndex) { - case MapSortOrder::SortByGroup: + case MapListTab::Groups: settings.group = project->groupNames.at(data.toInt()); break; - case MapSortOrder::SortByArea: + case MapListTab::Areas: settings.location = data.toString(); break; - case MapSortOrder::SortByLayout: + case MapListTab::Layouts: this->ui->checkBox_UseExistingLayout->setCheckState(Qt::Checked); useLayout(data.toString()); break;