Make actions more consistent

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad 2024-04-26 01:40:18 +01:00
parent 27780cc7ae
commit 803e26a401
No known key found for this signature in database
GPG Key ID: 5E39D70B4C93C38E
9 changed files with 59 additions and 43 deletions

View File

@ -81,15 +81,28 @@ ExternalResourcesPage::ExternalResourcesPage(BaseInstance* instance, std::shared
connect(ui->treeView, &ModListView::activated, this, &ExternalResourcesPage::itemActivated); connect(ui->treeView, &ModListView::activated, this, &ExternalResourcesPage::itemActivated);
auto selection_model = ui->treeView->selectionModel(); auto selection_model = ui->treeView->selectionModel();
connect(selection_model, &QItemSelectionModel::currentChanged, this, &ExternalResourcesPage::current);
connect(selection_model, &QItemSelectionModel::currentChanged, this, [this](const QModelIndex& current, const QModelIndex& previous) {
if (!current.isValid()) {
ui->frame->clear();
return;
}
updateFrame(current, previous);
});
auto updateExtra = [this]() { auto updateExtra = [this]() {
if (updateExtraInfo) if (updateExtraInfo)
updateExtraInfo(id(), extraHeaderInfoString()); updateExtraInfo(id(), extraHeaderInfoString());
}; };
connect(selection_model, &QItemSelectionModel::selectionChanged, this, updateExtra); connect(selection_model, &QItemSelectionModel::selectionChanged, this, updateExtra);
connect(model.get(), &ResourceFolderModel::updateFinished, this, updateExtra); connect(model.get(), &ResourceFolderModel::updateFinished, this, updateExtra);
connect(ui->filterEdit, &QLineEdit::textChanged, this, &ExternalResourcesPage::filterTextChanged); connect(selection_model, &QItemSelectionModel::selectionChanged, this, [this] { updateActions(); });
connect(m_model.get(), &ResourceFolderModel::rowsInserted, this, [this] { updateActions(); });
connect(m_model.get(), &ResourceFolderModel::rowsRemoved, this, [this] { updateActions(); });
connect(m_model.get(), &ResourceFolderModel::updateFinished, this, [this] { updateActions(); });
auto viewHeader = ui->treeView->header(); auto viewHeader = ui->treeView->header();
viewHeader->setContextMenuPolicy(Qt::CustomContextMenu); viewHeader->setContextMenuPolicy(Qt::CustomContextMenu);
@ -298,23 +311,22 @@ void ExternalResourcesPage::viewFolder()
DesktopServices::openPath(m_model->dir().absolutePath(), true); DesktopServices::openPath(m_model->dir().absolutePath(), true);
} }
bool ExternalResourcesPage::current(const QModelIndex& current, const QModelIndex& previous) void ExternalResourcesPage::updateActions()
{ {
if (!current.isValid()) { const bool hasSelection = ui->treeView->selectionModel()->hasSelection();
ui->frame->clear(); ui->actionUpdateItem->setEnabled(!m_model->empty());
return false; ui->actionResetItemMetadata->setEnabled(hasSelection);
} ui->actionRemoveItem->setEnabled(hasSelection);
ui->actionEnableItem->setEnabled(hasSelection);
return onSelectionChanged(current, previous); ui->actionDisableItem->setEnabled(hasSelection);
} }
bool ExternalResourcesPage::onSelectionChanged(const QModelIndex& current, [[maybe_unused]] const QModelIndex& previous) void ExternalResourcesPage::updateFrame(const QModelIndex& current, [[maybe_unused]] const QModelIndex& previous)
{ {
auto sourceCurrent = m_filterModel->mapToSource(current); auto sourceCurrent = m_filterModel->mapToSource(current);
int row = sourceCurrent.row(); int row = sourceCurrent.row();
Resource const& resource = m_model->at(row); Resource const& resource = m_model->at(row);
ui->frame->updateWithResource(resource); ui->frame->updateWithResource(resource);
return true;
} }
QString ExternalResourcesPage::extraHeaderInfoString() QString ExternalResourcesPage::extraHeaderInfoString()

View File

@ -42,9 +42,8 @@ class ExternalResourcesPage : public QMainWindow, public BasePage {
QMenu* createPopupMenu() override; QMenu* createPopupMenu() override;
public slots: public slots:
bool current(const QModelIndex& current, const QModelIndex& previous); virtual void updateActions();
virtual void updateFrame(const QModelIndex& current, const QModelIndex& previous);
virtual bool onSelectionChanged(const QModelIndex& current, const QModelIndex& previous);
protected slots: protected slots:
void itemActivated(const QModelIndex& index); void itemActivated(const QModelIndex& index);

View File

@ -102,6 +102,9 @@
</property> </property>
</action> </action>
<action name="actionRemoveItem"> <action name="actionRemoveItem">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>&amp;Remove</string> <string>&amp;Remove</string>
</property> </property>
@ -110,6 +113,9 @@
</property> </property>
</action> </action>
<action name="actionEnableItem"> <action name="actionEnableItem">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>&amp;Enable</string> <string>&amp;Enable</string>
</property> </property>
@ -118,6 +124,9 @@
</property> </property>
</action> </action>
<action name="actionDisableItem"> <action name="actionDisableItem">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>&amp;Disable</string> <string>&amp;Disable</string>
</property> </property>
@ -154,7 +163,7 @@
</action> </action>
<action name="actionUpdateItem"> <action name="actionUpdateItem">
<property name="enabled"> <property name="enabled">
<bool>true</bool> <bool>false</bool>
</property> </property>
<property name="text"> <property name="text">
<string>Check for &amp;Updates</string> <string>Check for &amp;Updates</string>

View File

@ -106,14 +106,12 @@ bool ModFolderPage::shouldDisplay() const
return true; return true;
} }
bool ModFolderPage::onSelectionChanged(const QModelIndex& current, [[maybe_unused]] const QModelIndex& previous) void ModFolderPage::updateFrame(const QModelIndex& current, [[maybe_unused]] const QModelIndex& previous)
{ {
auto sourceCurrent = m_filterModel->mapToSource(current); auto sourceCurrent = m_filterModel->mapToSource(current);
int row = sourceCurrent.row(); int row = sourceCurrent.row();
const Mod& mod = m_model->at(row); const Mod& mod = m_model->at(row);
ui->frame->updateWithMod(mod); ui->frame->updateWithMod(mod);
return true;
} }
void ModFolderPage::removeItems(const QItemSelection& selection) void ModFolderPage::removeItems(const QItemSelection& selection)

View File

@ -57,7 +57,7 @@ class ModFolderPage : public ExternalResourcesPage {
virtual bool shouldDisplay() const override; virtual bool shouldDisplay() const override;
public slots: public slots:
bool onSelectionChanged(const QModelIndex& current, const QModelIndex& previous) override; void updateFrame(const QModelIndex& current, const QModelIndex& previous) override;
private slots: private slots:
void removeItems(const QItemSelection& selection) override; void removeItems(const QItemSelection& selection) override;

View File

@ -69,14 +69,12 @@ ResourcePackPage::ResourcePackPage(MinecraftInstance* instance, std::shared_ptr<
ui->actionUpdateItem->setMenu(updateMenu); ui->actionUpdateItem->setMenu(updateMenu);
} }
bool ResourcePackPage::onSelectionChanged(const QModelIndex& current, [[maybe_unused]] const QModelIndex& previous) void ResourcePackPage::updateFrame(const QModelIndex& current, [[maybe_unused]] const QModelIndex& previous)
{ {
auto sourceCurrent = m_filterModel->mapToSource(current); auto sourceCurrent = m_filterModel->mapToSource(current);
int row = sourceCurrent.row(); int row = sourceCurrent.row();
auto& rp = static_cast<ResourcePack&>(m_model->at(row)); auto& rp = static_cast<ResourcePack&>(m_model->at(row));
ui->frame->updateWithResourcePack(rp); ui->frame->updateWithResourcePack(rp);
return true;
} }
void ResourcePackPage::downloadResourcePacks() void ResourcePackPage::downloadResourcePacks()
@ -127,13 +125,13 @@ void ResourcePackPage::updateResourcePacks()
return; return;
} }
if (m_instance != nullptr && m_instance->isRunning()) { if (m_instance != nullptr && m_instance->isRunning()) {
auto response = auto response = CustomMessageBox::selectable(
CustomMessageBox::selectable(this, tr("Confirm Update"), this, tr("Confirm Update"),
tr("Updating resource packs while the game is running may cause pack duplication and game crashes.\n" tr("Updating resource packs while the game is running may cause pack duplication and game crashes.\n"
"The old files may not be deleted as they are in use.\n" "The old files may not be deleted as they are in use.\n"
"Are you sure you want to do this?"), "Are you sure you want to do this?"),
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No) QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
->exec(); ->exec();
if (response != QMessageBox::Yes) if (response != QMessageBox::Yes)
return; return;
@ -166,7 +164,8 @@ void ResourcePackPage::updateResourcePacks()
} }
if (update_dialog.exec()) { if (update_dialog.exec()) {
auto tasks = new ConcurrentTask(this, "Download Resource Packs", APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt()); auto tasks =
new ConcurrentTask(this, "Download Resource Packs", APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt());
connect(tasks, &Task::failed, [this, tasks](QString reason) { connect(tasks, &Task::failed, [this, tasks](QString reason) {
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
tasks->deleteLater(); tasks->deleteLater();

View File

@ -58,7 +58,7 @@ class ResourcePackPage : public ExternalResourcesPage {
} }
public slots: public slots:
bool onSelectionChanged(const QModelIndex& current, const QModelIndex& previous) override; void updateFrame(const QModelIndex& current, const QModelIndex& previous) override;
private slots: private slots:
void downloadResourcePacks(); void downloadResourcePacks();

View File

@ -71,14 +71,12 @@ TexturePackPage::TexturePackPage(MinecraftInstance* instance, std::shared_ptr<Te
ui->actionUpdateItem->setMenu(updateMenu); ui->actionUpdateItem->setMenu(updateMenu);
} }
bool TexturePackPage::onSelectionChanged(const QModelIndex& current, [[maybe_unused]] const QModelIndex& previous) void TexturePackPage::updateFrame(const QModelIndex& current, [[maybe_unused]] const QModelIndex& previous)
{ {
auto sourceCurrent = m_filterModel->mapToSource(current); auto sourceCurrent = m_filterModel->mapToSource(current);
int row = sourceCurrent.row(); int row = sourceCurrent.row();
auto& rp = static_cast<TexturePack&>(m_model->at(row)); auto& rp = static_cast<TexturePack&>(m_model->at(row));
ui->frame->updateWithTexturePack(rp); ui->frame->updateWithTexturePack(rp);
return true;
} }
void TexturePackPage::downloadTexturePacks() void TexturePackPage::downloadTexturePacks()
@ -129,13 +127,13 @@ void TexturePackPage::updateTexturePacks()
return; return;
} }
if (m_instance != nullptr && m_instance->isRunning()) { if (m_instance != nullptr && m_instance->isRunning()) {
auto response = auto response = CustomMessageBox::selectable(
CustomMessageBox::selectable(this, tr("Confirm Update"), this, tr("Confirm Update"),
tr("Updating texture packs while the game is running may cause pack duplication and game crashes.\n" tr("Updating texture packs while the game is running may cause pack duplication and game crashes.\n"
"The old files may not be deleted as they are in use.\n" "The old files may not be deleted as they are in use.\n"
"Are you sure you want to do this?"), "Are you sure you want to do this?"),
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No) QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
->exec(); ->exec();
if (response != QMessageBox::Yes) if (response != QMessageBox::Yes)
return; return;
@ -168,7 +166,8 @@ void TexturePackPage::updateTexturePacks()
} }
if (update_dialog.exec()) { if (update_dialog.exec()) {
auto tasks = new ConcurrentTask(this, "Download Texture Packs", APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt()); auto tasks =
new ConcurrentTask(this, "Download Texture Packs", APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt());
connect(tasks, &Task::failed, [this, tasks](QString reason) { connect(tasks, &Task::failed, [this, tasks](QString reason) {
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
tasks->deleteLater(); tasks->deleteLater();

View File

@ -55,7 +55,7 @@ class TexturePackPage : public ExternalResourcesPage {
virtual bool shouldDisplay() const override { return m_instance->traits().contains("texturepacks"); } virtual bool shouldDisplay() const override { return m_instance->traits().contains("texturepacks"); }
public slots: public slots:
bool onSelectionChanged(const QModelIndex& current, const QModelIndex& previous) override; void updateFrame(const QModelIndex& current, const QModelIndex& previous) override;
void downloadTexturePacks(); void downloadTexturePacks();
void updateTexturePacks(); void updateTexturePacks();
void deleteTexturePackMetadata(); void deleteTexturePackMetadata();