Skip to content

Commit 4ab3322

Browse files
Merge pull request #767 from GriffinRichards/map-list-sorting
Add sorting options to the Locations and Layouts list
2 parents fb45714 + e232ef7 commit 4ab3322

File tree

10 files changed

+65
-24
lines changed

10 files changed

+65
-24
lines changed

include/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class PorymapConfig: public KeyValueConfigBase
9898
int mapListTab;
9999
bool mapListEditGroupsEnabled;
100100
QMap<int, bool> mapListHideEmptyEnabled;
101+
bool mapListLayoutsSorted;
102+
bool mapListLocationsSorted;
101103
bool prettyCursors;
102104
bool mirrorConnectingMaps;
103105
bool showDiveEmergeMaps;

include/mainwindow.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,10 @@ private slots:
393393
void openDuplicateMapOrLayoutDialog();
394394
void openNewMapGroupDialog();
395395
void openNewLocationDialog();
396-
void scrollMapList(MapTree *list, const QString &itemName);
396+
void scrollMapList(MapTree *list, const QString &itemName, bool expandItem = true);
397397
void scrollMapListToCurrentMap(MapTree *list);
398398
void scrollMapListToCurrentLayout(MapTree *list);
399-
void scrollCurrentMapListToItem(const QString &itemName);
399+
void scrollCurrentMapListToItem(const QString &itemName, bool expandItem = true);
400400
void showFileWatcherWarning();
401401
bool openProject(QString dir, bool initial = false);
402402
bool closeProject();
@@ -410,6 +410,7 @@ private slots:
410410

411411
void rebuildMapList_Locations();
412412
void rebuildMapList_Layouts();
413+
void setMapListSorted(MapTree *list, bool sort);
413414
void updateMapList();
414415
void openMapListItem(const QModelIndex &index);
415416
void onMapListTabChanged(int index);

include/project.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ class Project : public QObject
5252
QStringList bgEventFacingDirections;
5353
QStringList trainerTypes;
5454
QStringList globalScriptLabels;
55-
QStringList mapSectionIdNamesSaveOrder;
56-
QStringList mapSectionIdNames;
5755
QMap<uint32_t, QString> encounterTypeToName;
5856
QMap<uint32_t, QString> terrainTypeToName;
5957
QMap<QString, QMap<QString, uint16_t>> metatileLabelsMap;
@@ -83,6 +81,7 @@ class Project : public QObject
8381
Map* loadMap(const QString &mapName);
8482

8583
const QStringList& layoutIds() const { return this->alphabeticalLayoutIds; }
84+
const QStringList& layoutIdsOrdered() const { return this->orderedLayoutIds; }
8685
bool isKnownLayout(const QString &layoutId) const { return this->mapLayouts.contains(layoutId); }
8786
bool isLoadedLayout(const QString &layoutId) const { return this->loadedLayoutIds.contains(layoutId); }
8887
bool isUnsavedLayout(const QString &layoutId) const;
@@ -93,6 +92,12 @@ class Project : public QObject
9392
Layout* getLayout(const QString &layoutId) const { return this->mapLayouts.value(layoutId); }
9493
Layout* loadLayout(const QString &layoutId);
9594

95+
const QStringList& locationNames() const { return this->mapSectionIdNames; }
96+
const QStringList& locationNamesOrdered() const { return this->mapSectionIdNamesSaveOrder; }
97+
98+
QString getLocationName(int locationValue) const { return this->mapSectionIdNamesSaveOrder.value(locationValue, getEmptyMapsecName()); }
99+
int getLocationValue(const QString &locationName) const { return this->mapSectionIdNamesSaveOrder.indexOf(locationName); }
100+
96101
void clearMaps();
97102
void clearTilesetCache();
98103
void clearMapLayouts();
@@ -284,6 +289,8 @@ class Project : public QObject
284289
QStringList orderedLayoutIdsMaster;
285290
QHash<QString, Layout*> mapLayouts;
286291
QHash<QString, Layout*> mapLayoutsMaster;
292+
QStringList mapSectionIdNamesSaveOrder;
293+
QStringList mapSectionIdNames;
287294

288295
// Fields for preserving top-level JSON data that Porymap isn't expecting.
289296
QJsonObject customLayoutsData;

src/config.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ void PorymapConfig::reset() {
327327
this->mapListTab = 0;
328328
this->mapListEditGroupsEnabled = false;
329329
this->mapListHideEmptyEnabled.clear();
330+
this->mapListLayoutsSorted = true;
331+
this->mapListLocationsSorted = true;
330332
this->prettyCursors = true;
331333
this->mirrorConnectingMaps = true;
332334
this->showDiveEmergeMaps = false;
@@ -407,6 +409,10 @@ void PorymapConfig::parseConfigKeyValue(QString key, QString value) {
407409
return;
408410
}
409411
this->mapListHideEmptyEnabled.insert(tab, getConfigBool(key, value));
412+
} else if (key == "map_list_layouts_sorted") {
413+
this->mapListLayoutsSorted = getConfigBool(key, value);
414+
} else if (key == "map_list_locations_sorted") {
415+
this->mapListLocationsSorted = getConfigBool(key, value);
410416
} else if (key == "main_window_geometry") {
411417
this->mainWindowGeometry = bytesFromString(value);
412418
} else if (key == "main_window_state") {
@@ -591,6 +597,8 @@ QMap<QString, QString> PorymapConfig::getKeyValueMap() {
591597
for (auto i = this->mapListHideEmptyEnabled.constBegin(); i != this->mapListHideEmptyEnabled.constEnd(); i++) {
592598
map.insert(QStringLiteral("map_list_hide_empty_enabled/") + QString::number(i.key()), i.value() ? "1" : "0");
593599
}
600+
map.insert("map_list_layouts_sorted", this->mapListLayoutsSorted ? "1" : "0");
601+
map.insert("map_list_locations_sorted", this->mapListLocationsSorted ? "1" : "0");
594602
map.insert("main_window_geometry", stringFromByteArray(this->mainWindowGeometry));
595603
map.insert("main_window_state", stringFromByteArray(this->mainWindowState));
596604
map.insert("map_splitter_state", stringFromByteArray(this->mapSplitterState));

src/core/regionmap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ bool RegionMap::loadLayout(poryjson::Json layoutJson) {
156156
for (int x = 0; x < this->layout_width; x++) {
157157
int bin_index = x + y * this->layout_width;
158158
uint8_t square_section_id = mapBinData.at(bin_index);
159-
QString square_section_name = project->mapSectionIdNames.value(square_section_id, this->default_map_section);
159+
QString square_section_name = project->getLocationName(square_section_id);
160160

161161
LayoutSquare square;
162162
square.map_section = square_section_name;
@@ -397,11 +397,11 @@ void RegionMap::saveLayout() {
397397
case LayoutFormat::Binary:
398398
{
399399
QByteArray data;
400-
int defaultValue = this->project->mapSectionIdNames.indexOf(this->default_map_section);
400+
int defaultValue = this->project->getLocationValue(this->default_map_section);
401401
for (int m = 0; m < this->layout_height; m++) {
402402
for (int n = 0; n < this->layout_width; n++) {
403403
int i = n + this->layout_width * m;
404-
int mapSectionValue = this->project->mapSectionIdNames.indexOf(this->layouts["main"][i].map_section);
404+
int mapSectionValue = this->project->getLocationValue(this->layouts["main"][i].map_section);
405405
if (mapSectionValue < 0){
406406
mapSectionValue = defaultValue;
407407
}

src/mainwindow.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,16 +1440,15 @@ bool MainWindow::setProjectUI() {
14401440
this->locationListProxyModel = new FilterChildrenProxyModel();
14411441
this->locationListProxyModel->setSourceModel(this->mapLocationModel);
14421442
this->locationListProxyModel->setHideEmpty(porymapConfig.mapListHideEmptyEnabled[MapListTab::Locations]);
1443-
14441443
ui->locationList->setModel(locationListProxyModel);
1445-
ui->locationList->sortByColumn(0, Qt::SortOrder::AscendingOrder);
1444+
setMapListSorted(ui->locationList, porymapConfig.mapListLocationsSorted);
14461445

14471446
this->layoutTreeModel = new LayoutTreeModel(editor->project);
14481447
this->layoutListProxyModel = new FilterChildrenProxyModel();
14491448
this->layoutListProxyModel->setSourceModel(this->layoutTreeModel);
14501449
this->layoutListProxyModel->setHideEmpty(porymapConfig.mapListHideEmptyEnabled[MapListTab::Layouts]);
14511450
ui->layoutList->setModel(layoutListProxyModel);
1452-
ui->layoutList->sortByColumn(0, Qt::SortOrder::AscendingOrder);
1451+
setMapListSorted(ui->layoutList, porymapConfig.mapListLayoutsSorted);
14531452

14541453
ui->mapCustomAttributesFrame->table()->setRestrictedKeys(project->getTopLevelMapFields());
14551454

@@ -1503,7 +1502,7 @@ void MainWindow::clearProjectUI() {
15031502
resetMapNavigation();
15041503
}
15051504

1506-
void MainWindow::scrollMapList(MapTree *list, const QString &itemName) {
1505+
void MainWindow::scrollMapList(MapTree *list, const QString &itemName, bool expandItem) {
15071506
if (!list || itemName.isEmpty())
15081507
return;
15091508
auto model = static_cast<FilterChildrenProxyModel*>(list->model());
@@ -1516,7 +1515,7 @@ void MainWindow::scrollMapList(MapTree *list, const QString &itemName) {
15161515
return;
15171516

15181517
list->setCurrentIndex(index);
1519-
list->setExpanded(index, true);
1518+
if (expandItem) list->setExpanded(index, true);
15201519
list->scrollTo(index, QAbstractItemView::PositionAtCenter);
15211520
}
15221521

@@ -1537,13 +1536,13 @@ void MainWindow::scrollMapListToCurrentLayout(MapTree *list) {
15371536
// - The map list was in the middle of a search
15381537
// - A map/layout is being opened by interacting with the list (in which case `lockMapListAutoScroll` is true)
15391538
// - The item is not in the list (e.g. a layout ID for the Groups list)
1540-
void MainWindow::scrollCurrentMapListToItem(const QString &itemName) {
1539+
void MainWindow::scrollCurrentMapListToItem(const QString &itemName, bool expandItem) {
15411540
if (this->lockMapListAutoScroll)
15421541
return;
15431542

15441543
auto toolbar = getCurrentMapListToolBar();
15451544
if (toolbar && toolbar->filterText().isEmpty()) {
1546-
scrollMapList(toolbar->list(), itemName);
1545+
scrollMapList(toolbar->list(), itemName, expandItem);
15471546
}
15481547
}
15491548

@@ -1565,6 +1564,7 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point) {
15651564
QAction* openItemAction = nullptr;
15661565
QAction* copyListNameAction = nullptr;
15671566
QAction* copyToolTipAction = nullptr;
1567+
QAction* sortFoldersAction = nullptr;
15681568

15691569
if (itemType == "map_name") {
15701570
// Right-clicking on a map.
@@ -1596,6 +1596,8 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point) {
15961596
deleteFolderAction = menu.addAction("Delete Location");
15971597
if (itemName == this->editor->project->getEmptyMapsecName())
15981598
deleteFolderAction->setEnabled(false); // Disallow deleting the default name
1599+
menu.addSeparator();
1600+
sortFoldersAction = menu.addAction(list->isSortingEnabled() ? "Sort List by Value" : "Sort List Alphabetically");
15991601
} else if (itemType == "map_layout") {
16001602
// Right-clicking on a map layout
16011603
openItemAction = menu.addAction("Open Layout");
@@ -1612,6 +1614,8 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point) {
16121614
addToFolderAction = menu.addAction("Add New Map with Layout");
16131615
//menu.addSeparator();
16141616
//deleteFolderAction = menu.addAction("Delete Layout"); // TODO: No support for deleting layouts
1617+
menu.addSeparator();
1618+
sortFoldersAction = menu.addAction(list->isSortingEnabled() ? "Sort List by Value" : "Sort List Alphabetically");
16151619
}
16161620

16171621
if (addToFolderAction) {
@@ -1646,6 +1650,12 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point) {
16461650
setClipboardData(selectedItem->toolTip());
16471651
});
16481652
}
1653+
if (sortFoldersAction) {
1654+
connect(sortFoldersAction, &QAction::triggered, [this, list, itemName] {
1655+
setMapListSorted(list, !list->isSortingEnabled());
1656+
scrollCurrentMapListToItem(itemName, false);
1657+
});
1658+
}
16491659

16501660
if (menu.actions().length() != 0)
16511661
menu.exec(QCursor::pos());
@@ -1895,6 +1905,19 @@ void MainWindow::rebuildMapList_Layouts() {
18951905
ui->mapListToolBar_Layouts->refreshFilter();
18961906
}
18971907

1908+
void MainWindow::setMapListSorted(MapTree *list, bool sort) {
1909+
if (sort == list->isSortingEnabled())
1910+
return;
1911+
list->setSortingEnabled(sort);
1912+
list->sortByColumn(sort ? 0 : -1, Qt::SortOrder::AscendingOrder);
1913+
1914+
if (list == ui->locationList) {
1915+
porymapConfig.mapListLocationsSorted = sort;
1916+
} else if (list == ui->layoutList) {
1917+
porymapConfig.mapListLayoutsSorted = sort;
1918+
}
1919+
}
1920+
18981921
QString MainWindow::getActiveItemName() {
18991922
if (this->editor->map) return this->editor->map->name();
19001923
if (this->editor->layout) return this->editor->layout->id;

src/scriptapi/apiutility.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ QList<QString> ScriptUtility::getSongNames() {
288288
QList<QString> ScriptUtility::getLocationNames() {
289289
if (!window || !window->editor || !window->editor->project)
290290
return QList<QString>();
291-
return window->editor->project->mapSectionIdNames;
291+
return window->editor->project->locationNames();
292292
}
293293

294294
QList<QString> ScriptUtility::getWeatherNames() {

src/ui/mapheaderform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void MapHeaderForm::setProject(Project * project, bool allowChanges) {
6767

6868
const QSignalBlocker b_Locations(ui->comboBox_Location);
6969
ui->comboBox_Location->clear();
70-
ui->comboBox_Location->addItems(m_project->mapSectionIdNames);
70+
ui->comboBox_Location->addItems(m_project->locationNames());
7171

7272
// Hide config-specific settings
7373

src/ui/maplistmodels.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ bool MapGroupModel::setData(const QModelIndex &index, const QVariant &value, int
442442
MapLocationModel::MapLocationModel(Project *project, QObject *parent) : MapListModel(project, parent) {
443443
this->folderTypeName = "map_section";
444444

445-
for (const auto &idName : this->project->mapSectionIdNames) {
445+
for (const auto &idName : this->project->locationNamesOrdered()) {
446446
insertMapFolderItem(idName);
447447
}
448448
for (const auto &mapName : this->project->mapNames()) {
@@ -466,7 +466,7 @@ QStandardItem *MapLocationModel::createMapFolderItem(const QString &folderName,
466466
LayoutTreeModel::LayoutTreeModel(Project *project, QObject *parent) : MapListModel(project, parent) {
467467
this->folderTypeName = "map_layout";
468468

469-
for (const auto &layoutId : this->project->layoutIds()) {
469+
for (const auto &layoutId : this->project->layoutIdsOrdered()) {
470470
insertMapFolderItem(layoutId);
471471
}
472472
for (const auto &mapName : this->project->mapNames()) {

src/ui/regionmapeditor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ void RegionMapEditor::displayRegionMapLayoutOptions() {
648648

649649
const QSignalBlocker b(ui->comboBox_RM_ConnectedMap);
650650
this->ui->comboBox_RM_ConnectedMap->clear();
651-
this->ui->comboBox_RM_ConnectedMap->addItems(this->project->mapSectionIdNames);
651+
this->ui->comboBox_RM_ConnectedMap->addItems(this->project->locationNames());
652652

653653
this->ui->frame_RM_Options->setEnabled(true);
654654

@@ -711,7 +711,7 @@ void RegionMapEditor::displayRegionMapEntryOptions() {
711711
if (!this->region_map->layoutEnabled()) return;
712712

713713
this->ui->comboBox_RM_Entry_MapSection->clear();
714-
this->ui->comboBox_RM_Entry_MapSection->addItems(this->project->mapSectionIdNames);
714+
this->ui->comboBox_RM_Entry_MapSection->addItems(this->project->locationNames());
715715
this->ui->spinBox_RM_Entry_x->setMaximum(128);
716716
this->ui->spinBox_RM_Entry_y->setMaximum(128);
717717
this->ui->spinBox_RM_Entry_width->setMinimum(1);
@@ -1103,10 +1103,10 @@ void RegionMapEditor::on_action_Swap_triggered() {
11031103
QFormLayout form(&popup);
11041104

11051105
QComboBox *oldSecBox = new QComboBox();
1106-
oldSecBox->addItems(this->project->mapSectionIdNames);
1106+
oldSecBox->addItems(this->project->locationNames());
11071107
form.addRow(new QLabel("Map Section 1:"), oldSecBox);
11081108
QComboBox *newSecBox = new QComboBox();
1109-
newSecBox->addItems(this->project->mapSectionIdNames);
1109+
newSecBox->addItems(this->project->locationNames());
11101110
form.addRow(new QLabel("Map Section 2:"), newSecBox);
11111111

11121112
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &popup);
@@ -1142,10 +1142,10 @@ void RegionMapEditor::on_action_Replace_triggered() {
11421142
QFormLayout form(&popup);
11431143

11441144
QComboBox *oldSecBox = new QComboBox();
1145-
oldSecBox->addItems(this->project->mapSectionIdNames);
1145+
oldSecBox->addItems(this->project->locationNames());
11461146
form.addRow(new QLabel("Old Map Section:"), oldSecBox);
11471147
QComboBox *newSecBox = new QComboBox();
1148-
newSecBox->addItems(this->project->mapSectionIdNames);
1148+
newSecBox->addItems(this->project->locationNames());
11491149
form.addRow(new QLabel("New Map Section:"), newSecBox);
11501150

11511151
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &popup);

0 commit comments

Comments
 (0)