chore(clang-tidy): fix clang tidy warnings

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2026-05-07 10:22:37 +03:00
parent 4c9081a934
commit bac959bc6f
No known key found for this signature in database
GPG key ID: 55EF5DA53DB36318
23 changed files with 650 additions and 546 deletions

View file

@ -11,6 +11,7 @@
#include <QStyle>
#include <QThreadPool>
#include <QUrl>
#include <algorithm>
#include <utility>
#include "Application.h"
@ -27,10 +28,10 @@
#include "tasks/Task.h"
#include "ui/dialogs/CustomMessageBox.h"
ResourceFolderModel::ResourceFolderModel(const QDir& dir, BaseInstance* instance, bool is_indexed, bool create_dir, QObject* parent)
: QAbstractListModel(parent), m_dir(dir), m_instance(instance), m_watcher(this), m_is_indexed(is_indexed)
ResourceFolderModel::ResourceFolderModel(const QDir& dir, BaseInstance* instance, bool isIndexed, bool createDir, QObject* parent)
: QAbstractListModel(parent), m_dir(dir), m_instance(instance), m_watcher(this), m_isIndexed(isIndexed)
{
if (create_dir) {
if (createDir) {
FS::ensureFolderPathExists(m_dir.absolutePath());
}
@ -49,70 +50,75 @@ ResourceFolderModel::ResourceFolderModel(const QDir& dir, BaseInstance* instance
ResourceFolderModel::~ResourceFolderModel()
{
while (!QThreadPool::globalInstance()->waitForDone(100))
while (!QThreadPool::globalInstance()->waitForDone(100)) {
QCoreApplication::processEvents();
}
}
bool ResourceFolderModel::startWatching(const QStringList& paths)
{
// Remove orphaned metadata next time
m_first_folder_load = true;
m_firstFolderLoad = true;
if (m_is_watching)
if (m_isWatching) {
return false;
}
auto couldnt_be_watched = m_watcher.addPaths(paths);
for (auto path : paths) {
if (couldnt_be_watched.contains(path))
auto couldntBeWatched = m_watcher.addPaths(paths);
for (const auto& path : paths) {
if (couldntBeWatched.contains(path)) {
qDebug() << "Failed to start watching" << path;
else
} else {
qDebug() << "Started watching" << path;
}
}
update();
m_is_watching = !m_is_watching;
return m_is_watching;
m_isWatching = !m_isWatching;
return m_isWatching;
}
bool ResourceFolderModel::stopWatching(const QStringList& paths)
{
if (!m_is_watching)
if (!m_isWatching) {
return false;
auto couldnt_be_stopped = m_watcher.removePaths(paths);
for (auto path : paths) {
if (couldnt_be_stopped.contains(path))
qDebug() << "Failed to stop watching" << path;
else
qDebug() << "Stopped watching" << path;
}
m_is_watching = !m_is_watching;
return !m_is_watching;
auto couldntBeStopped = m_watcher.removePaths(paths);
for (const auto& path : paths) {
if (couldntBeStopped.contains(path)) {
qDebug() << "Failed to stop watching" << path;
} else {
qDebug() << "Stopped watching" << path;
}
}
m_isWatching = !m_isWatching;
return !m_isWatching;
}
bool ResourceFolderModel::installResource(QString original_path)
bool ResourceFolderModel::installResource(QString originalPath)
{
// NOTE: fix for GH-1178: remove trailing slash to avoid issues with using the empty result of QFileInfo::fileName
original_path = FS::NormalizePath(original_path);
QFileInfo file_info(original_path);
originalPath = FS::NormalizePath(originalPath);
QFileInfo fileInfo(originalPath);
if (!file_info.exists() || !file_info.isReadable()) {
qWarning() << "Caught attempt to install non-existing file or file-like object:" << original_path;
if (!fileInfo.exists() || !fileInfo.isReadable()) {
qWarning() << "Caught attempt to install non-existing file or file-like object:" << originalPath;
return false;
}
qDebug() << "Installing:" << file_info.absoluteFilePath();
qDebug() << "Installing:" << fileInfo.absoluteFilePath();
Resource resource(file_info);
Resource resource(fileInfo);
if (!resource.valid()) {
qWarning() << original_path << "is not a valid resource. Ignoring it.";
qWarning() << originalPath << "is not a valid resource. Ignoring it.";
return false;
}
auto new_path = FS::NormalizePath(m_dir.filePath(file_info.fileName()));
if (original_path == new_path) {
qWarning() << "Overwriting the mod (" << original_path << ") with itself makes no sense...";
auto newPath = FS::NormalizePath(m_dir.filePath(fileInfo.fileName()));
if (originalPath == newPath) {
qWarning() << "Overwriting the mod (" << originalPath << ") with itself makes no sense...";
return false;
}
@ -120,45 +126,47 @@ bool ResourceFolderModel::installResource(QString original_path)
case ResourceType::SINGLEFILE:
case ResourceType::ZIPFILE:
case ResourceType::LITEMOD: {
if (QFile::exists(new_path) || QFile::exists(new_path + QString(".disabled"))) {
if (!FS::deletePath(new_path)) {
qCritical() << "Cleaning up new location (" << new_path << ") was unsuccessful!";
if (QFile::exists(newPath) || QFile::exists(newPath + QString(".disabled"))) {
if (!FS::deletePath(newPath)) {
qCritical() << "Cleaning up new location (" << newPath << ") was unsuccessful!";
return false;
}
qDebug() << new_path << "has been deleted.";
qDebug() << newPath << "has been deleted.";
}
if (!QFile::copy(original_path, new_path)) {
qCritical() << "Copy from" << original_path << "to" << new_path << "has failed.";
if (!QFile::copy(originalPath, newPath)) {
qCritical() << "Copy from" << originalPath << "to" << newPath << "has failed.";
return false;
}
FS::updateTimestamp(new_path);
FS::updateTimestamp(newPath);
QFileInfo new_path_file_info(new_path);
resource.setFile(new_path_file_info);
QFileInfo newPathFileInfo(newPath);
resource.setFile(newPathFileInfo);
if (!m_is_watching)
if (!m_isWatching) {
return update();
}
return true;
}
case ResourceType::FOLDER: {
if (QFile::exists(new_path)) {
qDebug() << "Ignoring folder '" << original_path << "', it would merge with" << new_path;
if (QFile::exists(newPath)) {
qDebug() << "Ignoring folder '" << originalPath << "', it would merge with" << newPath;
return false;
}
if (!FS::copy(original_path, new_path)()) {
qWarning() << "Copy of folder from" << original_path << "to" << new_path << "has (potentially partially) failed.";
if (!FS::copy(originalPath, newPath)()) {
qWarning() << "Copy of folder from" << originalPath << "to" << newPath << "has (potentially partially) failed.";
return false;
}
QFileInfo newpathInfo(new_path);
QFileInfo newpathInfo(newPath);
resource.setFile(newpathInfo);
if (!m_is_watching)
if (!m_isWatching) {
return update();
}
return true;
}
@ -168,24 +176,24 @@ bool ResourceFolderModel::installResource(QString original_path)
return false;
}
void ResourceFolderModel::installResourceWithFlameMetadata(QString path, ModPlatform::IndexedVersion& vers)
void ResourceFolderModel::installResourceWithFlameMetadata(const QString& path, ModPlatform::IndexedVersion& vers)
{
auto install = [this, path] { installResource(std::move(path)); };
auto install = [this, path] { installResource(path); };
if (vers.addonId.isValid()) {
ModPlatform::IndexedPack pack{
vers.addonId,
ModPlatform::ResourceProvider::FLAME,
.addonId = vers.addonId,
.provider = ModPlatform::ResourceProvider::FLAME,
};
auto [job, response] = FlameAPI().getProject(vers.addonId.toString());
connect(job.get(), &Task::failed, this, install);
connect(job.get(), &Task::aborted, this, install);
connect(job.get(), &Task::succeeded, [response, this, &vers, install, &pack] {
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response for mod info at" << parse_error.offset
<< "reason:" << parse_error.errorString();
QJsonParseError parseError{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parseError);
if (parseError.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response for mod info at" << parseError.offset
<< "reason:" << parseError.errorString();
qDebug() << *response;
return;
}
@ -196,9 +204,9 @@ void ResourceFolderModel::installResourceWithFlameMetadata(QString path, ModPlat
qDebug() << doc;
qWarning() << "Error while reading mod info:" << e.cause();
}
LocalResourceUpdateTask update_metadata(indexDir(), pack, vers);
connect(&update_metadata, &Task::finished, this, install);
update_metadata.start();
LocalResourceUpdateTask updateMetadata(indexDir(), pack, vers);
connect(&updateMetadata, &Task::finished, this, install);
updateMetadata.start();
});
job->start();
@ -207,7 +215,7 @@ void ResourceFolderModel::installResourceWithFlameMetadata(QString path, ModPlat
}
}
bool ResourceFolderModel::uninstallResource(const QString& file_name, bool preserve_metadata)
bool ResourceFolderModel::uninstallResource(const QString& fileName, bool preserveMetadata)
{
for (auto& resource : m_resources) {
auto resourceFileInfo = resource->fileinfo();
@ -216,8 +224,8 @@ bool ResourceFolderModel::uninstallResource(const QString& file_name, bool prese
resourceFileName.chop(9);
}
if (resourceFileName == file_name) {
auto res = resource->destroy(indexDir(), preserve_metadata, false);
if (resourceFileName == fileName) {
auto res = resource->destroy(indexDir(), preserveMetadata, false);
update();
@ -229,14 +237,16 @@ bool ResourceFolderModel::uninstallResource(const QString& file_name, bool prese
bool ResourceFolderModel::deleteResources(const QModelIndexList& indexes)
{
if (indexes.isEmpty())
if (indexes.isEmpty()) {
return true;
}
for (auto i : indexes) {
if (i.column() != 0)
if (i.column() != 0) {
continue;
}
auto& resource = m_resources.at(i.row());
const auto& resource = m_resources.at(i.row());
resource->destroy(indexDir());
}
@ -247,14 +257,16 @@ bool ResourceFolderModel::deleteResources(const QModelIndexList& indexes)
void ResourceFolderModel::deleteMetadata(const QModelIndexList& indexes)
{
if (indexes.isEmpty())
if (indexes.isEmpty()) {
return;
}
for (auto i : indexes) {
if (i.column() != 0)
if (i.column() != 0) {
continue;
}
auto& resource = m_resources.at(i.row());
const auto& resource = m_resources.at(i.row());
resource->destroyMetadata(indexDir());
}
@ -271,33 +283,36 @@ bool ResourceFolderModel::setResourceEnabled(const QModelIndexList& indexes, Ena
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
->exec();
if (response != QMessageBox::Yes)
if (response != QMessageBox::Yes) {
return false;
}
}
if (indexes.isEmpty())
if (indexes.isEmpty()) {
return true;
}
bool succeeded = true;
for (auto const& idx : indexes) {
if (!validateIndex(idx) || idx.column() != 0)
for (const auto& idx : indexes) {
if (!validateIndex(idx) || idx.column() != 0) {
continue;
}
int row = idx.row();
auto& resource = m_resources[row];
// Preserve the row, but change its ID
auto old_id = resource->internal_id();
auto oldId = resource->internalId();
if (!resource->enable(action)) {
succeeded = false;
continue;
}
auto new_id = resource->internal_id();
auto newId = resource->internalId();
m_resources_index.remove(old_id);
m_resources_index[new_id] = row;
m_resourcesIndex.remove(oldId);
m_resourcesIndex[newId] = row;
emit dataChanged(index(row, 0), index(row, columnCount(QModelIndex()) - 1));
}
@ -312,24 +327,25 @@ bool ResourceFolderModel::update()
QMutexLocker lock(&s_update_task_mutex);
// Already updating, so we schedule a future update and return.
if (m_current_update_task) {
m_scheduled_update = true;
if (m_currentUpdateTask) {
m_scheduledUpdate = true;
return false;
}
m_current_update_task.reset(createUpdateTask());
if (!m_current_update_task)
m_currentUpdateTask.reset(createUpdateTask());
if (!m_currentUpdateTask) {
return false;
}
connect(m_current_update_task.get(), &Task::succeeded, this, &ResourceFolderModel::onUpdateSucceeded,
connect(m_currentUpdateTask.get(), &Task::succeeded, this, &ResourceFolderModel::onUpdateSucceeded,
Qt::ConnectionType::QueuedConnection);
connect(m_current_update_task.get(), &Task::failed, this, &ResourceFolderModel::onUpdateFailed, Qt::ConnectionType::QueuedConnection);
connect(m_currentUpdateTask.get(), &Task::failed, this, &ResourceFolderModel::onUpdateFailed, Qt::ConnectionType::QueuedConnection);
connect(
m_current_update_task.get(), &Task::finished, this,
m_currentUpdateTask.get(), &Task::finished, this,
[this] {
m_current_update_task.reset();
if (m_scheduled_update) {
m_scheduled_update = false;
m_currentUpdateTask.reset();
if (m_scheduledUpdate) {
m_scheduledUpdate = false;
update();
} else {
emit updateFinished();
@ -340,16 +356,16 @@ bool ResourceFolderModel::update()
Task::Ptr preUpdate{ createPreUpdateTask() };
if (preUpdate != nullptr) {
auto task = new SequentialTask("ResourceFolderModel::update");
auto* task = new SequentialTask("ResourceFolderModel::update");
task->addTask(preUpdate);
task->addTask(m_current_update_task);
task->addTask(m_currentUpdateTask);
connect(task, &Task::finished, [task] { task->deleteLater(); });
QThreadPool::globalInstance()->start(task);
} else {
QThreadPool::globalInstance()->start(m_current_update_task.get());
QThreadPool::globalInstance()->start(m_currentUpdateTask.get());
}
return true;
@ -362,24 +378,25 @@ void ResourceFolderModel::resolveResource(Resource::Ptr res)
}
Task::Ptr task{ createParseTask(*res) };
if (!task)
if (!task) {
return;
}
int ticket = m_next_resolution_ticket.fetch_add(1);
int ticket = m_nextResolutionTicket.fetch_add(1);
res->setResolving(true, ticket);
m_active_parse_tasks.insert(ticket, task);
m_activeParseTasks.insert(ticket, task);
connect(
task.get(), &Task::succeeded, this, [this, ticket, res] { onParseSucceeded(ticket, res->internal_id()); },
task.get(), &Task::succeeded, this, [this, ticket, res] { onParseSucceeded(ticket, res->internalId()); },
Qt::ConnectionType::QueuedConnection);
connect(
task.get(), &Task::failed, this, [this, ticket, res] { onParseFailed(ticket, res->internal_id()); },
task.get(), &Task::failed, this, [this, ticket, res] { onParseFailed(ticket, res->internalId()); },
Qt::ConnectionType::QueuedConnection);
connect(
task.get(), &Task::finished, this,
[this, ticket] {
m_active_parse_tasks.remove(ticket);
m_activeParseTasks.remove(ticket);
emit parseFinished();
},
Qt::ConnectionType::QueuedConnection);
@ -394,44 +411,45 @@ void ResourceFolderModel::resolveResource(Resource::Ptr res)
void ResourceFolderModel::onUpdateSucceeded()
{
auto update_results = static_cast<ResourceFolderLoadTask*>(m_current_update_task.get())->result();
auto updateResults = static_cast<ResourceFolderLoadTask*>(m_currentUpdateTask.get())->result();
auto& new_resources = update_results->resources;
auto& newResources = updateResults->resources;
auto current_list = m_resources_index.keys();
QSet<QString> current_set(current_list.begin(), current_list.end());
auto currentList = m_resourcesIndex.keys();
QSet<QString> currentSet(currentList.begin(), currentList.end());
auto new_list = new_resources.keys();
QSet<QString> new_set(new_list.begin(), new_list.end());
auto newList = newResources.keys();
QSet<QString> newSet(newList.begin(), newList.end());
applyUpdates(current_set, new_set, new_resources);
applyUpdates(currentSet, newSet, newResources);
}
void ResourceFolderModel::onParseSucceeded(int ticket, QString resource_id)
void ResourceFolderModel::onParseSucceeded(int ticket, const QString& resourceId)
{
auto iter = m_active_parse_tasks.constFind(ticket);
if (iter == m_active_parse_tasks.constEnd() || !m_resources_index.contains(resource_id))
auto iter = m_activeParseTasks.constFind(ticket);
if (iter == m_activeParseTasks.constEnd() || !m_resourcesIndex.contains(resourceId)) {
return;
}
int row = m_resources_index[resource_id];
int row = m_resourcesIndex[resourceId];
emit dataChanged(index(row), index(row, columnCount(QModelIndex()) - 1));
}
Task* ResourceFolderModel::createUpdateTask()
{
auto index_dir = indexDir();
auto task = new ResourceFolderLoadTask(dir(), index_dir, m_is_indexed, m_first_folder_load,
[this](const QFileInfo& file) { return createResource(file); });
m_first_folder_load = false;
auto indexDir2 = indexDir();
auto* task = new ResourceFolderLoadTask(dir(), indexDir2, m_isIndexed, m_firstFolderLoad,
[this](const QFileInfo& file) { return createResource(file); });
m_firstFolderLoad = false;
return task;
}
bool ResourceFolderModel::hasPendingParseTasks() const
{
return !m_active_parse_tasks.isEmpty();
return !m_activeParseTasks.isEmpty();
}
void ResourceFolderModel::directoryChanged(QString path)
void ResourceFolderModel::directoryChanged(const QString& /*path*/)
{
update();
}
@ -446,8 +464,9 @@ Qt::ItemFlags ResourceFolderModel::flags(const QModelIndex& index) const
{
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
auto flags = defaultFlags | Qt::ItemIsDropEnabled;
if (index.isValid())
if (index.isValid()) {
flags |= Qt::ItemIsUserCheckable;
}
return flags;
}
@ -458,21 +477,25 @@ QStringList ResourceFolderModel::mimeTypes() const
return types;
}
bool ResourceFolderModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int, int, const QModelIndex&)
bool ResourceFolderModel::dropMimeData(const QMimeData* data,
Qt::DropAction action,
int /*row*/,
int /*column*/,
const QModelIndex& /*parent*/)
{
if (action == Qt::IgnoreAction) {
return true;
}
// check if the action is supported
if (!data || !(action & supportedDropActions())) {
if ((data == nullptr) || !(action & supportedDropActions())) {
return false;
}
// files dropped from outside?
if (data->hasUrls()) {
auto urls = data->urls();
for (auto url : urls) {
for (const auto& url : urls) {
// only local files may be dropped...
if (!url.isLocalFile()) {
continue;
@ -488,14 +511,12 @@ bool ResourceFolderModel::dropMimeData(const QMimeData* data, Qt::DropAction act
bool ResourceFolderModel::validateIndex(const QModelIndex& index) const
{
if (!index.isValid())
if (!index.isValid()) {
return false;
}
int row = index.row();
if (row < 0 || row >= m_resources.size())
return false;
return true;
return row >= 0 && row < m_resources.size();
}
// HACK: all subclasses need to call this to have the whole row painted
@ -504,15 +525,15 @@ QBrush ResourceFolderModel::rowBackground(int row) const
{
if (APPLICATION->settings()->get("ShowModIncompat").toBool() && m_resources[row]->hasIssues()) {
return { QColor(255, 0, 0, 40) };
} else {
return {};
}
return {};
}
QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
{
if (!validateIndex(index))
if (!validateIndex(index)) {
return {};
}
int row = index.row();
int column = index.column();
@ -536,7 +557,7 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
return {};
}
case Qt::ToolTipRole: {
QString tooltip = m_resources[row]->internal_id();
QString tooltip = m_resources[row]->internalId();
if (column == NameColumn) {
if (APPLICATION->settings()->get("ShowModIncompat").toBool()) {
@ -547,7 +568,7 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
if (at(row).isSymLinkUnder(instDirPath())) {
tooltip +=
m_resources[row]->internal_id() +
m_resources[row]->internalId() +
tr("\nWarning: This resource is symbolically linked from elsewhere. Editing it will also change the original."
"\nCanonical Path: %1")
.arg(at(row).fileinfo().canonicalFilePath());
@ -564,7 +585,8 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
if (column == NameColumn) {
if (APPLICATION->settings()->get("ShowModIncompat").toBool() && at(row).hasIssues()) {
return QIcon::fromTheme("status-bad");
} else if (at(row).isSymLinkUnder(instDirPath()) || at(row).isMoreThanOneHardLink()) {
}
if (at(row).isSymLinkUnder(instDirPath()) || at(row).isMoreThanOneHardLink()) {
return QIcon::fromTheme("status-yellow");
}
}
@ -572,8 +594,9 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
return {};
}
case Qt::CheckStateRole:
if (column == ActiveColumn)
if (column == ActiveColumn) {
return m_resources[row]->enabled() ? Qt::Checked : Qt::Unchecked;
}
return {};
default:
return {};
@ -583,8 +606,9 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const
bool ResourceFolderModel::setData(const QModelIndex& index, [[maybe_unused]] const QVariant& value, int role)
{
int row = index.row();
if (row < 0 || row >= rowCount(index.parent()) || !index.isValid())
if (row < 0 || row >= rowCount(index.parent()) || !index.isValid()) {
return false;
}
if (role == Qt::CheckStateRole) {
return setResourceEnabled({ index }, EnableAction::TOGGLE);
@ -643,22 +667,22 @@ void ResourceFolderModel::setupHeaderAction(QAction* act, int column)
void ResourceFolderModel::saveColumns(QTreeView* tree)
{
auto const stateSettingName = QString("UI/%1_Page/Columns").arg(id());
auto const overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id());
auto const visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id());
const auto stateSettingName = QString("UI/%1_Page/Columns").arg(id());
const auto overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id());
const auto visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id());
auto stateSetting = m_instance->settings()->getSetting(stateSettingName);
stateSetting->set(QString::fromUtf8(tree->header()->saveState().toBase64()));
// neither passthrough nor override settings works for this usecase as I need to only set the global when the gate is false
auto settings = m_instance->settings();
auto* settings = m_instance->settings();
if (!settings->get(overrideSettingName).toBool()) {
settings = APPLICATION->settings();
}
auto visibility = Json::toMap(settings->get(visibilitySettingName).toString());
for (auto i = 0; i < m_column_names.size(); ++i) {
for (auto i = 0; i < m_columnNames.size(); ++i) {
if (m_columnsHideable[i]) {
auto name = m_column_names[i];
auto name = m_columnNames[i];
visibility[name] = !tree->isColumnHidden(i);
}
}
@ -667,24 +691,24 @@ void ResourceFolderModel::saveColumns(QTreeView* tree)
void ResourceFolderModel::loadColumns(QTreeView* tree)
{
auto const stateSettingName = QString("UI/%1_Page/Columns").arg(id());
auto const overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id());
auto const visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id());
const auto stateSettingName = QString("UI/%1_Page/Columns").arg(id());
const auto overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id());
const auto visibilitySettingName = QString("UI/%1_Page/ColumnsVisibility").arg(id());
auto stateSetting = m_instance->settings()->getOrRegisterSetting(stateSettingName, "");
tree->header()->restoreState(QByteArray::fromBase64(stateSetting->get().toString().toUtf8()));
auto setVisible = [this, tree](QVariant value) {
auto setVisible = [this, tree](const QVariant& value) {
auto visibility = Json::toMap(value.toString());
for (auto i = 0; i < m_column_names.size(); ++i) {
for (auto i = 0; i < m_columnNames.size(); ++i) {
if (m_columnsHideable[i]) {
auto name = m_column_names[i];
auto name = m_columnNames[i];
tree->setColumnHidden(i, !visibility.value(name, false).toBool());
}
}
};
auto const defaultValue = Json::fromMap({
const auto defaultValue = Json::fromMap({
{ "Image", true },
{ "Version", true },
{ "Last Modified", true },
@ -692,7 +716,7 @@ void ResourceFolderModel::loadColumns(QTreeView* tree)
{ "Pack Format", true },
});
// neither passthrough nor override settings works for this usecase as I need to only set the global when the gate is false
auto settings = m_instance->settings();
auto* settings = m_instance->settings();
if (!settings->getOrRegisterSetting(overrideSettingName, false)->get().toBool()) {
settings = APPLICATION->settings();
}
@ -701,7 +725,7 @@ void ResourceFolderModel::loadColumns(QTreeView* tree)
// allways connect the signal in case the setting is toggled on and off
auto gSetting = APPLICATION->settings()->getOrRegisterSetting(visibilitySettingName, defaultValue);
connect(gSetting.get(), &Setting::SettingChanged, tree, [this, setVisible, overrideSettingName](const Setting&, QVariant value) {
connect(gSetting.get(), &Setting::SettingChanged, tree, [this, setVisible, overrideSettingName](const Setting&, const QVariant& value) {
if (!m_instance->settings()->get(overrideSettingName).toBool()) {
setVisible(value);
}
@ -710,11 +734,11 @@ void ResourceFolderModel::loadColumns(QTreeView* tree)
QMenu* ResourceFolderModel::createHeaderContextMenu(QTreeView* tree)
{
auto menu = new QMenu(tree);
auto* menu = new QMenu(tree);
{ // action to decide if the visibility is per instance or not
auto act = new QAction(tr("Override Columns Visibility"), menu);
auto const overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id());
auto* act = new QAction(tr("Override Columns Visibility"), menu);
const auto overrideSettingName = QString("UI/%1_Page/ColumnsOverride").arg(id());
act->setCheckable(true);
act->setChecked(m_instance->settings()->getOrRegisterSetting(overrideSettingName, false)->get().toBool());
@ -730,9 +754,10 @@ QMenu* ResourceFolderModel::createHeaderContextMenu(QTreeView* tree)
for (int col = 0; col < columnCount(); ++col) {
// Skip creating actions for columns that should not be hidden
if (!m_columnsHideable.at(col))
if (!m_columnsHideable.at(col)) {
continue;
auto act = new QAction(menu);
}
auto* act = new QAction(menu);
setupHeaderAction(act, col);
act->setCheckable(true);
@ -741,8 +766,9 @@ QMenu* ResourceFolderModel::createHeaderContextMenu(QTreeView* tree)
connect(act, &QAction::toggled, tree, [this, col, tree](bool toggled) {
tree->setColumnHidden(col, !toggled);
for (int c = 0; c < columnCount(); ++c) {
if (m_column_resize_modes.at(c) == QHeaderView::ResizeToContents)
if (m_columnResizeModes.at(c) == QHeaderView::ResizeToContents) {
tree->resizeColumnToContents(c);
}
}
saveColumns(tree);
});
@ -760,41 +786,43 @@ QSortFilterProxyModel* ResourceFolderModel::createFilterProxyModel(QObject* pare
SortType ResourceFolderModel::columnToSortKey(size_t column) const
{
Q_ASSERT(m_column_sort_keys.size() == columnCount());
return m_column_sort_keys.at(column);
Q_ASSERT(m_columnSortKeys.size() == columnCount());
return m_columnSortKeys.at(column);
}
/* Standard Proxy Model for createFilterProxyModel */
bool ResourceFolderModel::ProxyModel::filterAcceptsRow(int source_row, [[maybe_unused]] const QModelIndex& source_parent) const
bool ResourceFolderModel::ProxyModel::filterAcceptsRow(int sourceRow, [[maybe_unused]] const QModelIndex& sourceParent) const
{
auto* model = qobject_cast<ResourceFolderModel*>(sourceModel());
if (!model)
if (!model) {
return true;
}
const auto& resource = model->at(source_row);
const auto& resource = model->at(sourceRow);
return resource.applyFilter(filterRegularExpression());
}
bool ResourceFolderModel::ProxyModel::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const
bool ResourceFolderModel::ProxyModel::lessThan(const QModelIndex& sourceLeft, const QModelIndex& sourceRight) const
{
auto* model = qobject_cast<ResourceFolderModel*>(sourceModel());
if (!model || !source_left.isValid() || !source_right.isValid() || source_left.column() != source_right.column()) {
return QSortFilterProxyModel::lessThan(source_left, source_right);
if (!model || !sourceLeft.isValid() || !sourceRight.isValid() || sourceLeft.column() != sourceRight.column()) {
return QSortFilterProxyModel::lessThan(sourceLeft, sourceRight);
}
// we are now guaranteed to have two valid indexes in the same column... we love the provided invariants unconditionally and
// proceed.
auto column_sort_key = model->columnToSortKey(source_left.column());
auto const& resource_left = model->at(source_left.row());
auto const& resource_right = model->at(source_right.row());
auto columnSortKey = model->columnToSortKey(sourceLeft.column());
const auto& resourceLeft = model->at(sourceLeft.row());
const auto& resourceRight = model->at(sourceRight.row());
auto compare_result = resource_left.compare(resource_right, column_sort_key);
if (compare_result == 0)
return QSortFilterProxyModel::lessThan(source_left, source_right);
auto compareResult = resourceLeft.compare(resourceRight, columnSortKey);
if (compareResult == 0) {
return QSortFilterProxyModel::lessThan(sourceLeft, sourceRight);
}
return compare_result < 0;
return compareResult < 0;
}
QString ResourceFolderModel::instDirPath() const
@ -802,50 +830,51 @@ QString ResourceFolderModel::instDirPath() const
return QFileInfo(m_instance->instanceRoot()).absoluteFilePath();
}
void ResourceFolderModel::onParseFailed(int ticket, QString resource_id)
void ResourceFolderModel::onParseFailed(int ticket, const QString& resourceId)
{
auto iter = m_active_parse_tasks.constFind(ticket);
if (iter == m_active_parse_tasks.constEnd() || !m_resources_index.contains(resource_id))
auto iter = m_activeParseTasks.constFind(ticket);
if (iter == m_activeParseTasks.constEnd() || !m_resourcesIndex.contains(resourceId)) {
return;
}
auto removed_index = m_resources_index[resource_id];
auto removed_it = m_resources.begin() + removed_index;
Q_ASSERT(removed_it != m_resources.end());
auto removedIndex = m_resourcesIndex[resourceId];
auto removedIt = m_resources.begin() + removedIndex;
Q_ASSERT(removedIt != m_resources.end());
beginRemoveRows(QModelIndex(), removed_index, removed_index);
m_resources.erase(removed_it);
beginRemoveRows(QModelIndex(), removedIndex, removedIndex);
m_resources.erase(removedIt);
// update index
m_resources_index.clear();
m_resourcesIndex.clear();
int idx = 0;
for (auto const& mod : qAsConst(m_resources)) {
m_resources_index[mod->internal_id()] = idx;
for (const auto& mod : qAsConst(m_resources)) {
m_resourcesIndex[mod->internalId()] = idx;
idx++;
}
endRemoveRows();
}
void ResourceFolderModel::applyUpdates(QSet<QString>& current_set, QSet<QString>& new_set, QMap<QString, Resource::Ptr>& new_resources)
void ResourceFolderModel::applyUpdates(QSet<QString>& currentSet, QSet<QString>& newSet, QMap<QString, Resource::Ptr>& newResources)
{
// see if the kept resources changed in some way
{
QSet<QString> kept_set = current_set;
kept_set.intersect(new_set);
QSet<QString> keptSet = currentSet;
keptSet.intersect(newSet);
for (auto const& kept : kept_set) {
auto row_it = m_resources_index.constFind(kept);
Q_ASSERT(row_it != m_resources_index.constEnd());
auto row = row_it.value();
for (const auto& kept : keptSet) {
auto rowIt = m_resourcesIndex.constFind(kept);
Q_ASSERT(rowIt != m_resourcesIndex.constEnd());
auto row = rowIt.value();
auto& new_resource = new_resources[kept];
auto const& current_resource = m_resources.at(row);
auto& newResource = newResources[kept];
const auto& currentResource = m_resources.at(row);
if (new_resource->dateTimeChanged() == current_resource->dateTimeChanged()) {
if (newResource->dateTimeChanged() == currentResource->dateTimeChanged()) {
// no significant change
bool hadIssues = !current_resource->hasIssues();
current_resource->updateIssues(m_instance);
bool hadIssues = !currentResource->hasIssues();
currentResource->updateIssues(m_instance);
if (hadIssues != current_resource->hasIssues()) {
if (hadIssues != currentResource->hasIssues()) {
emit dataChanged(index(row, 0), index(row, columnCount({}) - 1));
}
continue;
@ -853,16 +882,16 @@ void ResourceFolderModel::applyUpdates(QSet<QString>& current_set, QSet<QString>
// If the resource is resolving, but something about it changed, we don't want to
// continue the resolving.
if (current_resource->isResolving()) {
auto ticket = current_resource->resolutionTicket();
if (m_active_parse_tasks.contains(ticket)) {
auto task = (*m_active_parse_tasks.find(ticket)).get();
if (currentResource->isResolving()) {
auto ticket = currentResource->resolutionTicket();
if (m_activeParseTasks.contains(ticket)) {
auto* task = (*m_activeParseTasks.find(ticket)).get();
task->abort();
}
}
m_resources[row].reset(new_resource);
new_resource->updateIssues(m_instance);
m_resources[row].reset(newResource);
newResource->updateIssues(m_instance);
resolveResource(m_resources.at(row));
emit dataChanged(index(row, 0), index(row, columnCount(QModelIndex()) - 1));
@ -871,46 +900,47 @@ void ResourceFolderModel::applyUpdates(QSet<QString>& current_set, QSet<QString>
// remove resources no longer present
{
QSet<QString> removed_set = current_set;
removed_set.subtract(new_set);
QSet<QString> removedSet = currentSet;
removedSet.subtract(newSet);
QList<int> removed_rows;
for (auto& removed : removed_set)
removed_rows.append(m_resources_index[removed]);
QList<int> removedRows;
for (const auto& removed : removedSet) {
removedRows.append(m_resourcesIndex[removed]);
}
std::sort(removed_rows.begin(), removed_rows.end(), std::greater<int>());
std::ranges::sort(removedRows, std::greater());
for (auto& removed_index : removed_rows) {
auto removed_it = m_resources.begin() + removed_index;
for (auto& removedIndex : removedRows) {
auto removedIt = m_resources.begin() + removedIndex;
Q_ASSERT(removed_it != m_resources.end());
Q_ASSERT(removedIt != m_resources.end());
if ((*removed_it)->isResolving()) {
auto ticket = (*removed_it)->resolutionTicket();
if (m_active_parse_tasks.contains(ticket)) {
auto task = (*m_active_parse_tasks.find(ticket)).get();
if ((*removedIt)->isResolving()) {
auto ticket = (*removedIt)->resolutionTicket();
if (m_activeParseTasks.contains(ticket)) {
auto* task = (*m_activeParseTasks.find(ticket)).get();
task->abort();
}
}
beginRemoveRows(QModelIndex(), removed_index, removed_index);
m_resources.erase(removed_it);
beginRemoveRows(QModelIndex(), removedIndex, removedIndex);
m_resources.erase(removedIt);
endRemoveRows();
}
}
// add new resources to the end
{
QSet<QString> added_set = new_set;
added_set.subtract(current_set);
QSet<QString> addedSet = newSet;
addedSet.subtract(currentSet);
// When you have a Qt build with assertions turned on, proceeding here will abort the application
if (added_set.size() > 0) {
if (addedSet.size() > 0) {
beginInsertRows(QModelIndex(), static_cast<int>(m_resources.size()),
static_cast<int>(m_resources.size() + added_set.size() - 1));
static_cast<int>(m_resources.size() + addedSet.size() - 1));
for (auto& added : added_set) {
auto res = new_resources[added];
for (const auto& added : addedSet) {
auto res = newResources[added];
res->updateIssues(m_instance);
m_resources.append(res);
resolveResource(m_resources.last());
@ -922,10 +952,10 @@ void ResourceFolderModel::applyUpdates(QSet<QString>& current_set, QSet<QString>
// update index
{
m_resources_index.clear();
m_resourcesIndex.clear();
int idx = 0;
for (auto const& mod : qAsConst(m_resources)) {
m_resources_index[mod->internal_id()] = idx;
for (const auto& mod : qAsConst(m_resources)) {
m_resourcesIndex[mod->internalId()] = idx;
idx++;
}
}
@ -933,17 +963,19 @@ void ResourceFolderModel::applyUpdates(QSet<QString>& current_set, QSet<QString>
Resource::Ptr ResourceFolderModel::find(QString id)
{
auto iter =
std::find_if(m_resources.constBegin(), m_resources.constEnd(), [&](Resource::Ptr const& r) { return r->internal_id() == id; });
if (iter == m_resources.constEnd())
std::find_if(m_resources.constBegin(), m_resources.constEnd(), [&](const Resource::Ptr& r) { return r->internalId() == id; });
if (iter == m_resources.constEnd()) {
return nullptr;
}
return *iter;
}
QList<Resource*> ResourceFolderModel::allResources()
{
QList<Resource*> result;
result.reserve(m_resources.size());
for (const Resource ::Ptr& resource : m_resources)
for (const Resource ::Ptr& resource : m_resources) {
result.append((resource.get()));
}
return result;
}
@ -951,8 +983,9 @@ QList<Resource*> ResourceFolderModel::selectedResources(const QModelIndexList& i
{
QList<Resource*> result;
for (const QModelIndex& index : indexes) {
if (index.column() != 0)
if (index.column() != 0) {
continue;
}
result.append(&at(index.row()));
}
return result;