refactor: adapt rest of the codebase to the new resource model
In order to access the ModFolderModel from the ModFolderPage, i created a new m_model for the correct type, shadowing the m_model of type ResourceFolderModel. This creates two shared_ptr references to the same object, but since they will have the same lifetime, it doesn't generate a memory leak. Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
@ -3,100 +3,13 @@
|
||||
|
||||
#include "DesktopServices.h"
|
||||
#include "Version.h"
|
||||
#include "minecraft/mod/ModFolderModel.h"
|
||||
#include "minecraft/mod/ResourceFolderModel.h"
|
||||
#include "ui/GuiUtil.h"
|
||||
|
||||
#include <QKeyEvent>
|
||||
#include <QMenu>
|
||||
|
||||
namespace {
|
||||
// FIXME: wasteful
|
||||
void RemoveThePrefix(QString& string)
|
||||
{
|
||||
QRegularExpression regex(QStringLiteral("^(?:the|teh) +"), QRegularExpression::CaseInsensitiveOption);
|
||||
string.remove(regex);
|
||||
string = string.trimmed();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
class SortProxy : public QSortFilterProxyModel {
|
||||
public:
|
||||
explicit SortProxy(QObject* parent = nullptr) : QSortFilterProxyModel(parent) {}
|
||||
|
||||
protected:
|
||||
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override
|
||||
{
|
||||
ModFolderModel* model = qobject_cast<ModFolderModel*>(sourceModel());
|
||||
if (!model)
|
||||
return false;
|
||||
|
||||
const auto& mod = model->at(source_row);
|
||||
|
||||
if (filterRegularExpression().match(mod->name()).hasMatch())
|
||||
return true;
|
||||
if (filterRegularExpression().match(mod->description()).hasMatch())
|
||||
return true;
|
||||
|
||||
for (auto& author : mod->authors()) {
|
||||
if (filterRegularExpression().match(author).hasMatch()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override
|
||||
{
|
||||
ModFolderModel* model = qobject_cast<ModFolderModel*>(sourceModel());
|
||||
if (!model || !source_left.isValid() || !source_right.isValid() || source_left.column() != source_right.column()) {
|
||||
return QSortFilterProxyModel::lessThan(source_left, source_right);
|
||||
}
|
||||
|
||||
// we are now guaranteed to have two valid indexes in the same column... we love the provided invariants unconditionally and
|
||||
// proceed.
|
||||
|
||||
auto column = (ModFolderModel::Columns) source_left.column();
|
||||
bool invert = false;
|
||||
switch (column) {
|
||||
// GH-2550 - sort by enabled/disabled
|
||||
case ModFolderModel::ActiveColumn: {
|
||||
auto dataL = source_left.data(Qt::CheckStateRole).toBool();
|
||||
auto dataR = source_right.data(Qt::CheckStateRole).toBool();
|
||||
if (dataL != dataR)
|
||||
return dataL > dataR;
|
||||
|
||||
// fallthrough
|
||||
invert = sortOrder() == Qt::DescendingOrder;
|
||||
}
|
||||
// GH-2722 - sort mod names in a way that discards "The" prefixes
|
||||
case ModFolderModel::NameColumn: {
|
||||
auto dataL = model->data(model->index(source_left.row(), ModFolderModel::NameColumn)).toString();
|
||||
RemoveThePrefix(dataL);
|
||||
auto dataR = model->data(model->index(source_right.row(), ModFolderModel::NameColumn)).toString();
|
||||
RemoveThePrefix(dataR);
|
||||
|
||||
auto less = dataL.compare(dataR, sortCaseSensitivity());
|
||||
if (less != 0)
|
||||
return invert ? (less > 0) : (less < 0);
|
||||
|
||||
// fallthrough
|
||||
invert = sortOrder() == Qt::DescendingOrder;
|
||||
}
|
||||
// GH-2762 - sort versions by parsing them as versions
|
||||
case ModFolderModel::VersionColumn: {
|
||||
auto dataL = Version(model->data(model->index(source_left.row(), ModFolderModel::VersionColumn)).toString());
|
||||
auto dataR = Version(model->data(model->index(source_right.row(), ModFolderModel::VersionColumn)).toString());
|
||||
return invert ? (dataL > dataR) : (dataL < dataR);
|
||||
}
|
||||
default: {
|
||||
return QSortFilterProxyModel::lessThan(source_left, source_right);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ExternalResourcesPage::ExternalResourcesPage(BaseInstance* instance, std::shared_ptr<ModFolderModel> model, QWidget* parent)
|
||||
ExternalResourcesPage::ExternalResourcesPage(BaseInstance* instance, std::shared_ptr<ResourceFolderModel> model, QWidget* parent)
|
||||
: QMainWindow(parent), m_instance(instance), ui(new Ui::ExternalResourcesPage), m_model(model)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
@ -105,7 +18,7 @@ ExternalResourcesPage::ExternalResourcesPage(BaseInstance* instance, std::shared
|
||||
|
||||
ui->actionsToolbar->insertSpacer(ui->actionViewConfigs);
|
||||
|
||||
m_filterModel = new SortProxy(this);
|
||||
m_filterModel = model->createFilterProxyModel(this);
|
||||
m_filterModel->setDynamicSortFilter(true);
|
||||
m_filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
|
||||
m_filterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
|
||||
@ -127,7 +40,6 @@ ExternalResourcesPage::ExternalResourcesPage(BaseInstance* instance, std::shared
|
||||
connect(ui->actionViewFolder, &QAction::triggered, this, &ExternalResourcesPage::viewFolder);
|
||||
|
||||
connect(ui->treeView, &ModListView::customContextMenuRequested, this, &ExternalResourcesPage::ShowContextMenu);
|
||||
connect(ui->treeView, &ModListView::activated, this, &ExternalResourcesPage::itemActivated);
|
||||
|
||||
auto selection_model = ui->treeView->selectionModel();
|
||||
connect(selection_model, &QItemSelectionModel::currentChanged, this, &ExternalResourcesPage::current);
|
||||
@ -137,19 +49,9 @@ ExternalResourcesPage::ExternalResourcesPage(BaseInstance* instance, std::shared
|
||||
|
||||
ExternalResourcesPage::~ExternalResourcesPage()
|
||||
{
|
||||
m_model->stopWatching();
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void ExternalResourcesPage::itemActivated(const QModelIndex&)
|
||||
{
|
||||
if (!m_controlsEnabled)
|
||||
return;
|
||||
|
||||
auto selection = m_filterModel->mapSelectionToSource(ui->treeView->selectionModel()->selection());
|
||||
m_model->setModStatus(selection.indexes(), ModFolderModel::Toggle);
|
||||
}
|
||||
|
||||
QMenu* ExternalResourcesPage::createPopupMenu()
|
||||
{
|
||||
QMenu* filteredMenu = QMainWindow::createPopupMenu();
|
||||
@ -241,7 +143,7 @@ void ExternalResourcesPage::addItem()
|
||||
|
||||
if (!list.isEmpty()) {
|
||||
for (auto filename : list) {
|
||||
m_model->installMod(filename);
|
||||
m_model->installResource(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -252,25 +154,7 @@ void ExternalResourcesPage::removeItem()
|
||||
return;
|
||||
|
||||
auto selection = m_filterModel->mapSelectionToSource(ui->treeView->selectionModel()->selection());
|
||||
m_model->deleteMods(selection.indexes());
|
||||
}
|
||||
|
||||
void ExternalResourcesPage::enableItem()
|
||||
{
|
||||
if (!m_controlsEnabled)
|
||||
return;
|
||||
|
||||
auto selection = m_filterModel->mapSelectionToSource(ui->treeView->selectionModel()->selection());
|
||||
m_model->setModStatus(selection.indexes(), ModFolderModel::Enable);
|
||||
}
|
||||
|
||||
void ExternalResourcesPage::disableItem()
|
||||
{
|
||||
if (!m_controlsEnabled)
|
||||
return;
|
||||
|
||||
auto selection = m_filterModel->mapSelectionToSource(ui->treeView->selectionModel()->selection());
|
||||
m_model->setModStatus(selection.indexes(), ModFolderModel::Disable);
|
||||
m_model->deleteResources(selection.indexes());
|
||||
}
|
||||
|
||||
void ExternalResourcesPage::viewConfigs()
|
||||
@ -283,15 +167,23 @@ void ExternalResourcesPage::viewFolder()
|
||||
DesktopServices::openDirectory(m_model->dir().absolutePath(), true);
|
||||
}
|
||||
|
||||
void ExternalResourcesPage::current(const QModelIndex& current, const QModelIndex& previous)
|
||||
bool ExternalResourcesPage::current(const QModelIndex& current, const QModelIndex& previous)
|
||||
{
|
||||
if (!current.isValid()) {
|
||||
ui->frame->clear();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
return onSelectionChanged(current, previous);
|
||||
}
|
||||
|
||||
bool ExternalResourcesPage::onSelectionChanged(const QModelIndex& current, const QModelIndex& previous)
|
||||
{
|
||||
auto sourceCurrent = m_filterModel->mapToSource(current);
|
||||
int row = sourceCurrent.row();
|
||||
Mod& m = *m_model->operator[](row);
|
||||
ui->frame->updateWithMod(m);
|
||||
Resource const& resource = m_model->at(row);
|
||||
ui->frame->updateWithResource(resource);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user