Add create shortcut button for worlds

Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
Yihe Li 2025-05-11 19:01:37 +08:00
parent 69469b4484
commit 37213ecc34
No known key found for this signature in database
6 changed files with 152 additions and 5 deletions

View File

@ -46,6 +46,9 @@
#include <QUuid> #include <QUuid>
#include <Qt> #include <Qt>
#include <DesktopServices.h>
#include "minecraft/ShortcutUtils.h"
WorldList::WorldList(const QString& dir, BaseInstance* instance) : QAbstractListModel(), m_instance(instance), m_dir(dir) WorldList::WorldList(const QString& dir, BaseInstance* instance) : QAbstractListModel(), m_instance(instance), m_dir(dir)
{ {
FS::ensureFolderPathExists(m_dir.absolutePath()); FS::ensureFolderPathExists(m_dir.absolutePath());
@ -454,4 +457,39 @@ void WorldList::loadWorldsAsync()
} }
} }
void WorldList::createWorldShortcut(const QModelIndex& index, QWidget* parent) const
{
if (!m_instance)
return;
if (DesktopServices::isFlatpak())
createWorldShortcutInOther(index, parent);
else
createWorldShortcutOnDesktop(index, parent);
}
void WorldList::createWorldShortcutOnDesktop(const QModelIndex& index, QWidget* parent) const
{
auto world = static_cast<World*>(data(index, ObjectRole).value<void*>());
QString name = QString(tr("%1 - %2")).arg(m_instance->name(), world->name());
QStringList extraArgs{ "--world", world->name() };
ShortcutUtils::createInstanceShortcutOnDesktop(m_instance, name, tr("world"), parent, extraArgs);
}
void WorldList::createWorldShortcutInApplications(const QModelIndex& index, QWidget* parent) const
{
auto world = static_cast<World*>(data(index, ObjectRole).value<void*>());
QString name = QString(tr("%1 - %2")).arg(m_instance->name(), world->name());
QStringList extraArgs{ "--world", world->name() };
ShortcutUtils::createInstanceShortcutInApplications(m_instance, name, tr("world"), parent, extraArgs);
}
void WorldList::createWorldShortcutInOther(const QModelIndex& index, QWidget* parent) const
{
auto world = static_cast<World*>(data(index, ObjectRole).value<void*>());
QString name = QString(tr("%1 - %2")).arg(m_instance->name(), world->name());
QStringList extraArgs{ "--world", world->name() };
ShortcutUtils::createInstanceShortcutInOther(m_instance, name, tr("world"), parent, extraArgs);
}
#include "WorldList.moc" #include "WorldList.moc"

View File

@ -84,6 +84,11 @@ class WorldList : public QAbstractListModel {
const QList<World>& allWorlds() const { return m_worlds; } const QList<World>& allWorlds() const { return m_worlds; }
void createWorldShortcut(const QModelIndex& index, QWidget* parent = nullptr) const;
void createWorldShortcutOnDesktop(const QModelIndex& index, QWidget* parent = nullptr) const;
void createWorldShortcutInApplications(const QModelIndex& index, QWidget* parent = nullptr) const;
void createWorldShortcutInOther(const QModelIndex& index, QWidget* parent = nullptr) const;
private slots: private slots:
void directoryChanged(QString path); void directoryChanged(QString path);
void loadWorldsAsync(); void loadWorldsAsync();

View File

@ -214,17 +214,17 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi
QString desktopDir = FS::getDesktopDir(); QString desktopDir = FS::getDesktopDir();
QString applicationDir = FS::getApplicationsDir(); QString applicationDir = FS::getApplicationsDir();
if(!applicationDir.isEmpty()) if (!applicationDir.isEmpty())
shortcutActions.push_front(ui->actionCreateInstanceShortcutApplications); shortcutActions.push_front(ui->actionCreateInstanceShortcutApplications);
if(!desktopDir.isEmpty()) if (!desktopDir.isEmpty())
shortcutActions.push_front(ui->actionCreateInstanceShortcutDesktop); shortcutActions.push_front(ui->actionCreateInstanceShortcutDesktop);
} }
if(shortcutActions.length() > 1) { if (shortcutActions.length() > 1) {
auto shortcutInstanceMenu = new QMenu(this); auto shortcutInstanceMenu = new QMenu(this);
for(auto action : shortcutActions) for (auto action : shortcutActions)
shortcutInstanceMenu->addAction(action); shortcutInstanceMenu->addAction(action);
ui->actionCreateInstanceShortcut->setMenu(shortcutInstanceMenu); ui->actionCreateInstanceShortcut->setMenu(shortcutInstanceMenu);
} }

View File

@ -345,6 +345,28 @@ void WorldListPage::worldChanged([[maybe_unused]] const QModelIndex& current, [[
if (!supportsJoin) { if (!supportsJoin) {
ui->toolBar->removeAction(ui->actionJoin); ui->toolBar->removeAction(ui->actionJoin);
ui->toolBar->removeAction(ui->actionCreateWorldShortcut);
} else {
QList<QAction*> shortcutActions = { ui->actionCreateWorldShortcutOther };
if (!DesktopServices::isFlatpak()) {
QString desktopDir = FS::getDesktopDir();
QString applicationDir = FS::getApplicationsDir();
if (!applicationDir.isEmpty())
shortcutActions.push_front(ui->actionCreateWorldShortcutApplications);
if (!desktopDir.isEmpty())
shortcutActions.push_front(ui->actionCreateWorldShortcutDesktop);
}
if (shortcutActions.length() > 1) {
auto shortcutInstanceMenu = new QMenu(this);
for (auto action : shortcutActions)
shortcutInstanceMenu->addAction(action);
ui->actionCreateWorldShortcut->setMenu(shortcutInstanceMenu);
}
ui->actionCreateWorldShortcut->setEnabled(enable);
} }
} }
@ -420,6 +442,42 @@ void WorldListPage::on_actionRename_triggered()
} }
} }
void WorldListPage::on_actionCreateWorldShortcut_triggered()
{
QModelIndex index = getSelectedWorld();
if (!index.isValid()) {
return;
}
m_worlds->createWorldShortcut(index, this);
}
void WorldListPage::on_actionCreateWorldShortcutDesktop_triggered()
{
QModelIndex index = getSelectedWorld();
if (!index.isValid()) {
return;
}
m_worlds->createWorldShortcutOnDesktop(index, this);
}
void WorldListPage::on_actionCreateWorldShortcutApplications_triggered()
{
QModelIndex index = getSelectedWorld();
if (!index.isValid()) {
return;
}
m_worlds->createWorldShortcutInApplications(index, this);
}
void WorldListPage::on_actionCreateWorldShortcutOther_triggered()
{
QModelIndex index = getSelectedWorld();
if (!index.isValid()) {
return;
}
m_worlds->createWorldShortcutInOther(index, this);
}
void WorldListPage::on_actionRefresh_triggered() void WorldListPage::on_actionRefresh_triggered()
{ {
m_worlds->update(); m_worlds->update();

View File

@ -95,6 +95,10 @@ class WorldListPage : public QMainWindow, public BasePage {
void on_actionAdd_triggered(); void on_actionAdd_triggered();
void on_actionCopy_triggered(); void on_actionCopy_triggered();
void on_actionRename_triggered(); void on_actionRename_triggered();
void on_actionCreateWorldShortcut_triggered();
void on_actionCreateWorldShortcutDesktop_triggered();
void on_actionCreateWorldShortcutApplications_triggered();
void on_actionCreateWorldShortcutOther_triggered();
void on_actionRefresh_triggered(); void on_actionRefresh_triggered();
void on_actionView_Folder_triggered(); void on_actionView_Folder_triggered();
void on_actionDatapacks_triggered(); void on_actionDatapacks_triggered();

View File

@ -85,10 +85,11 @@
<addaction name="actionRename"/> <addaction name="actionRename"/>
<addaction name="actionCopy"/> <addaction name="actionCopy"/>
<addaction name="actionRemove"/> <addaction name="actionRemove"/>
<addaction name="actionCreateWorldShortcut"/>
<addaction name="separator"/>
<addaction name="actionMCEdit"/> <addaction name="actionMCEdit"/>
<addaction name="actionDatapacks"/> <addaction name="actionDatapacks"/>
<addaction name="actionReset_Icon"/> <addaction name="actionReset_Icon"/>
<addaction name="separator"/>
<addaction name="actionCopy_Seed"/> <addaction name="actionCopy_Seed"/>
<addaction name="actionRefresh"/> <addaction name="actionRefresh"/>
<addaction name="actionView_Folder"/> <addaction name="actionView_Folder"/>
@ -118,6 +119,14 @@
<string>Delete</string> <string>Delete</string>
</property> </property>
</action> </action>
<action name="actionCreateWorldShortcut">
<property name="text">
<string>Create Shortcut</string>
</property>
<property name="toolTip">
<string>Creates a shortcut on a selected folder to join the selected world.</string>
</property>
</action>
<action name="actionMCEdit"> <action name="actionMCEdit">
<property name="text"> <property name="text">
<string>MCEdit</string> <string>MCEdit</string>
@ -154,6 +163,39 @@
<string>Manage datapacks inside the world.</string> <string>Manage datapacks inside the world.</string>
</property> </property>
</action> </action>
<action name="actionCreateWorldShortcutDesktop">
<property name="text">
<string>Desktop</string>
</property>
<property name="toolTip">
<string>Creates a shortcut to this world on your desktop</string>
</property>
<property name="menuRole">
<enum>QAction::TextHeuristicRole</enum>
</property>
</action>
<action name="actionCreateWorldShortcutApplications">
<property name="text">
<string>Applications</string>
</property>
<property name="toolTip">
<string>Create a shortcut of this world on your start menu</string>
</property>
<property name="menuRole">
<enum>QAction::TextHeuristicRole</enum>
</property>
</action>
<action name="actionCreateWorldShortcutOther">
<property name="text">
<string>Other...</string>
</property>
<property name="toolTip">
<string>Creates a shortcut in a folder selected by you</string>
</property>
<property name="menuRole">
<enum>QAction::TextHeuristicRole</enum>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>