Skip to content

Commit

Permalink
Sort the Open World sub-menu by world name. (mrkite#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
madmaxoft authored Nov 10, 2023
1 parent a3b327a commit cd4301f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
44 changes: 29 additions & 15 deletions minutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,30 +722,44 @@ void Minutor::createStatusBar() {

void Minutor::getWorldList() {
QDir mc(dialogSettings->mcpath);
if (mc.exists("saves"))
if (mc.exists("saves")) {
mc.cd("saves");
}

// Create an action for each world found:
WorldInfo & wi(WorldInfo::Instance());
QDirIterator it(mc);
int key = 1;
while (it.hasNext()) {
it.next();
if (it.fileInfo().isDir()) {
if (wi.parseFolder(it.filePath())) {
QAction *w = new QAction(this);
w->setText(wi.getLevelName());
w->setData(it.filePath());
if (key < 10) {
w->setShortcut("Ctrl+"+QString::number(key));
key++;
}
connect(w, SIGNAL(triggered()),
this, SLOT(openWorld()));
worldActions.append(w);
}
if (!it.fileInfo().isDir() || !wi.parseFolder(it.filePath())) {
continue;
}
auto * w = new QAction(this);
w->setText(wi.getLevelName());
w->setData(it.filePath());
connect(w, SIGNAL(triggered()),
this, SLOT(openWorld()));
worldActions.append(w);
}
wi.clear();

// Sort the actions by the world name:
std::sort(worldActions.begin(), worldActions.end(),
[](auto * act1, auto * act2) {
return (act1->text() < act2->text());
}
);

// Assign Ctrl+number shortcuts to the first 10 worlds:
int key = 1;
for (auto & act: worldActions)
{
act->setShortcut("Ctrl+" + QString::number(key));
key++;
if (key >= 10) {
break;
}
}
}

MapView *Minutor::getMapview() const
Expand Down
3 changes: 3 additions & 0 deletions minutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ private slots:
void insertToggleAllAction(QMenu* menu);
void updateToggleAllState(QMenu* menu);

/** Populates worldActions with one action for each world encountered in the default Minecraft saves directory.
Each action opens the linked world upon triggering.
The actions are sorted by world's folder name. */
void getWorldList();

MapView *mapview;
Expand Down

0 comments on commit cd4301f

Please sign in to comment.