Simplify saving the map list tab

This commit is contained in:
GriffinR 2024-10-28 16:02:17 -04:00
parent a18b2c960b
commit 3bd5ddbf2f
6 changed files with 31 additions and 79 deletions

View file

@ -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;

View file

@ -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();

View file

@ -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);

View file

@ -278,12 +278,6 @@ uint32_t KeyValueConfigBase::getConfigUint32(QString key, QString value, uint32_
return qMin(max, qMax(min, result));
}
const QMap<QString, MapSortOrder> 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<QString, QString> 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));

View file

@ -371,7 +371,7 @@ void MainWindow::initMiscHeapObjects() {
}
void MainWindow::initMapList() {
ui->mapListContainer->setCurrentIndex(static_cast<int>(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) {

View file

@ -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;