refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
#include "ExternalResourcesPage.h"
|
|
|
|
#include "ui_ExternalResourcesPage.h"
|
|
|
|
|
|
|
|
#include "DesktopServices.h"
|
|
|
|
#include "Version.h"
|
2022-08-10 23:18:34 +05:30
|
|
|
#include "minecraft/mod/ResourceFolderModel.h"
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
#include "ui/GuiUtil.h"
|
|
|
|
|
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QMenu>
|
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
ExternalResourcesPage::ExternalResourcesPage(BaseInstance* instance, std::shared_ptr<ResourceFolderModel> model, QWidget* parent)
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
: QMainWindow(parent), m_instance(instance), ui(new Ui::ExternalResourcesPage), m_model(model)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
ui->actionsToolbar->insertSpacer(ui->actionViewConfigs);
|
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
m_filterModel = model->createFilterProxyModel(this);
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
m_filterModel->setDynamicSortFilter(true);
|
|
|
|
m_filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
m_filterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
m_filterModel->setSourceModel(m_model.get());
|
|
|
|
m_filterModel->setFilterKeyColumn(-1);
|
|
|
|
ui->treeView->setModel(m_filterModel);
|
|
|
|
|
|
|
|
ui->treeView->installEventFilter(this);
|
|
|
|
ui->treeView->sortByColumn(1, Qt::AscendingOrder);
|
|
|
|
ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
|
|
|
// The default function names by Qt are pretty ugly, so let's just connect the actions manually,
|
|
|
|
// to make it easier to read :)
|
|
|
|
connect(ui->actionAddItem, &QAction::triggered, this, &ExternalResourcesPage::addItem);
|
|
|
|
connect(ui->actionRemoveItem, &QAction::triggered, this, &ExternalResourcesPage::removeItem);
|
|
|
|
connect(ui->actionEnableItem, &QAction::triggered, this, &ExternalResourcesPage::enableItem);
|
|
|
|
connect(ui->actionDisableItem, &QAction::triggered, this, &ExternalResourcesPage::disableItem);
|
|
|
|
connect(ui->actionViewConfigs, &QAction::triggered, this, &ExternalResourcesPage::viewConfigs);
|
|
|
|
connect(ui->actionViewFolder, &QAction::triggered, this, &ExternalResourcesPage::viewFolder);
|
|
|
|
|
|
|
|
connect(ui->treeView, &ModListView::customContextMenuRequested, this, &ExternalResourcesPage::ShowContextMenu);
|
2022-08-13 20:28:39 +05:30
|
|
|
connect(ui->treeView, &ModListView::activated, this, &ExternalResourcesPage::itemActivated);
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
|
|
|
|
auto selection_model = ui->treeView->selectionModel();
|
|
|
|
connect(selection_model, &QItemSelectionModel::currentChanged, this, &ExternalResourcesPage::current);
|
|
|
|
connect(ui->filterEdit, &QLineEdit::textChanged, this, &ExternalResourcesPage::filterTextChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExternalResourcesPage::~ExternalResourcesPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMenu* ExternalResourcesPage::createPopupMenu()
|
|
|
|
{
|
|
|
|
QMenu* filteredMenu = QMainWindow::createPopupMenu();
|
|
|
|
filteredMenu->removeAction(ui->actionsToolbar->toggleViewAction());
|
|
|
|
return filteredMenu;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalResourcesPage::ShowContextMenu(const QPoint& pos)
|
|
|
|
{
|
|
|
|
auto menu = ui->actionsToolbar->createContextMenu(this, tr("Context menu"));
|
|
|
|
menu->exec(ui->treeView->mapToGlobal(pos));
|
|
|
|
delete menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalResourcesPage::openedImpl()
|
|
|
|
{
|
|
|
|
m_model->startWatching();
|
2022-11-20 01:42:31 +05:30
|
|
|
|
|
|
|
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
|
|
|
|
if (!APPLICATION->settings()->contains(setting_name))
|
|
|
|
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
|
|
|
|
else
|
|
|
|
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
|
|
|
|
|
|
|
|
ui->actionsToolbar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalResourcesPage::closedImpl()
|
|
|
|
{
|
|
|
|
m_model->stopWatching();
|
2022-11-20 01:42:31 +05:30
|
|
|
|
|
|
|
m_wide_bar_setting->set(ui->actionsToolbar->getVisibilityState());
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalResourcesPage::retranslate()
|
|
|
|
{
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
|
|
|
|
2022-08-13 20:28:39 +05:30
|
|
|
void ExternalResourcesPage::itemActivated(const QModelIndex&)
|
|
|
|
{
|
|
|
|
if (!m_controlsEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto selection = m_filterModel->mapSelectionToSource(ui->treeView->selectionModel()->selection());
|
|
|
|
m_model->setResourceEnabled(selection.indexes(), EnableAction::TOGGLE);
|
|
|
|
}
|
|
|
|
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
void ExternalResourcesPage::filterTextChanged(const QString& newContents)
|
|
|
|
{
|
|
|
|
m_viewFilter = newContents;
|
2022-07-24 08:20:56 +05:30
|
|
|
m_filterModel->setFilterRegularExpression(m_viewFilter);
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool ExternalResourcesPage::shouldDisplay() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExternalResourcesPage::listFilter(QKeyEvent* keyEvent)
|
|
|
|
{
|
|
|
|
switch (keyEvent->key()) {
|
|
|
|
case Qt::Key_Delete:
|
|
|
|
removeItem();
|
|
|
|
return true;
|
|
|
|
case Qt::Key_Plus:
|
|
|
|
addItem();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QWidget::eventFilter(ui->treeView, keyEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExternalResourcesPage::eventFilter(QObject* obj, QEvent* ev)
|
|
|
|
{
|
|
|
|
if (ev->type() != QEvent::KeyPress)
|
|
|
|
return QWidget::eventFilter(obj, ev);
|
|
|
|
|
|
|
|
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(ev);
|
|
|
|
if (obj == ui->treeView)
|
|
|
|
return listFilter(keyEvent);
|
|
|
|
|
|
|
|
return QWidget::eventFilter(obj, ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalResourcesPage::addItem()
|
|
|
|
{
|
|
|
|
if (!m_controlsEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
auto list = GuiUtil::BrowseForFiles(
|
|
|
|
helpPage(), tr("Select %1", "Select whatever type of files the page contains. Example: 'Loader Mods'").arg(displayName()),
|
|
|
|
m_fileSelectionFilter.arg(displayName()), APPLICATION->settings()->get("CentralModsDir").toString(), this->parentWidget());
|
|
|
|
|
|
|
|
if (!list.isEmpty()) {
|
|
|
|
for (auto filename : list) {
|
2022-08-10 23:18:34 +05:30
|
|
|
m_model->installResource(filename);
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalResourcesPage::removeItem()
|
|
|
|
{
|
|
|
|
if (!m_controlsEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto selection = m_filterModel->mapSelectionToSource(ui->treeView->selectionModel()->selection());
|
2022-08-10 23:18:34 +05:30
|
|
|
m_model->deleteResources(selection.indexes());
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
}
|
|
|
|
|
2022-08-13 20:28:39 +05:30
|
|
|
void ExternalResourcesPage::enableItem()
|
|
|
|
{
|
|
|
|
if (!m_controlsEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto selection = m_filterModel->mapSelectionToSource(ui->treeView->selectionModel()->selection());
|
|
|
|
m_model->setResourceEnabled(selection.indexes(), EnableAction::ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalResourcesPage::disableItem()
|
|
|
|
{
|
|
|
|
if (!m_controlsEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto selection = m_filterModel->mapSelectionToSource(ui->treeView->selectionModel()->selection());
|
|
|
|
m_model->setResourceEnabled(selection.indexes(), EnableAction::DISABLE);
|
|
|
|
}
|
|
|
|
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
void ExternalResourcesPage::viewConfigs()
|
|
|
|
{
|
|
|
|
DesktopServices::openDirectory(m_instance->instanceConfigFolder(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalResourcesPage::viewFolder()
|
|
|
|
{
|
|
|
|
DesktopServices::openDirectory(m_model->dir().absolutePath(), true);
|
|
|
|
}
|
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
bool ExternalResourcesPage::current(const QModelIndex& current, const QModelIndex& previous)
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
{
|
|
|
|
if (!current.isValid()) {
|
|
|
|
ui->frame->clear();
|
2022-08-10 23:18:34 +05:30
|
|
|
return false;
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
}
|
|
|
|
|
2022-08-10 23:18:34 +05:30
|
|
|
return onSelectionChanged(current, previous);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExternalResourcesPage::onSelectionChanged(const QModelIndex& current, const QModelIndex& previous)
|
|
|
|
{
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
auto sourceCurrent = m_filterModel->mapToSource(current);
|
|
|
|
int row = sourceCurrent.row();
|
2022-08-10 23:18:34 +05:30
|
|
|
Resource const& resource = m_model->at(row);
|
|
|
|
ui->frame->updateWithResource(resource);
|
|
|
|
|
|
|
|
return true;
|
refactor: Create a more clear hierarchy for some instance pages
Previously, the Shaders, Texture packs and Resource packs tabs had as
parent the ModFolderPage, making it so that making changes only to the
Mods page would require checking the id of the page for the correct one.
This was hackish and error-prone.
Now, those pages all inherit from a single class, ExternalResourcesPage,
that handles the basic behaviour of all of them, while allowing for
individual modification in code.
This is still not a clear separation, since internally, all those
resources are derived from Mods, so for now there's still some awkward
common code :/
2022-03-12 02:33:21 +05:30
|
|
|
}
|
2022-08-10 23:18:34 +05:30
|
|
|
|