From 18b59428098846a1609aae0dce951ef4fce461ad Mon Sep 17 00:00:00 2001 From: DanielSvoboda Date: Wed, 28 Aug 2024 23:47:45 -0300 Subject: [PATCH] Remove the item from the patchesListView if no patches were added (the game has patches, but not for the current version) --- src/qt_gui/cheats_patches.cpp | 42 +++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/qt_gui/cheats_patches.cpp b/src/qt_gui/cheats_patches.cpp index 7f491775..662d52cc 100644 --- a/src/qt_gui/cheats_patches.cpp +++ b/src/qt_gui/cheats_patches.cpp @@ -203,28 +203,20 @@ void CheatsPatches::setupUI() { patchesScrollArea->setMinimumHeight(490); patchesLayout->addWidget(patchesScrollArea); - // Lista de arquivos em patchesListView + // List of files in patchesListView patchesListView = new QListView(); patchesListView->setSelectionMode(QAbstractItemView::SingleSelection); patchesListView->setEditTriggers(QAbstractItemView::NoEditTriggers); - // Adicionar o novo rĂ³tulo "Select Patch File:" acima da QListView + // Add new label "Select Patch File:" above the QListView QVBoxLayout* patchFileListLayout = new QVBoxLayout(); patchFileListLayout->addWidget(new QLabel(tr("Select Patch File:"))); patchFileListLayout->addWidget(patchesListView); patchesLayout->addLayout(patchFileListLayout, 2); - // Lista de arquivos em patchesListView QStringListModel* patchesModel = new QStringListModel(); patchesListView->setModel(patchesModel); - // Conectar o clique duplo na lista com o carregamento de patches - connect(patchesListView, &QListView::doubleClicked, this, [=](const QModelIndex& index) { - QString selectedText = patchesModel->data(index).toString(); - QString patchName = selectedText.section(" | ", 0, 0); // Pega o texto antes de " | " - addPatchesToLayout(patchName); - }); - QHBoxLayout* patchesControlLayout = new QHBoxLayout(); QLabel* patchesRepositoryLabel = new QLabel(tr("Repository:")); @@ -271,7 +263,7 @@ void CheatsPatches::setupUI() { } void CheatsPatches::onSaveButtonClicked() { - // Obter o nome da pasta selecionada na patchesListView + // Get the name of the selected folder in the patchesListView QString selectedPatchName; QModelIndexList selectedIndexes = patchesListView->selectionModel()->selectedIndexes(); if (selectedIndexes.isEmpty()) { @@ -661,8 +653,7 @@ void CheatsPatches::populateFileListPatches() { QModelIndexList selectedIndexes = patchesListView->selectionModel()->selectedIndexes(); if (!selectedIndexes.isEmpty()) { QString selectedText = selectedIndexes.first().data().toString(); - QString filePath = selectedText.section(" | ", 1, 1); - addPatchesToLayout(filePath); + addPatchesToLayout(selectedText); } }); @@ -981,7 +972,13 @@ void CheatsPatches::populateFileListCheats() { } } -void CheatsPatches::addPatchesToLayout(const QString& folderPath) { +void CheatsPatches::addPatchesToLayout(const QString& filePath) { + if (filePath == "") { + return; + } + QString folderPath = filePath.section(" | ", 1, 1); + + // Clear existing layout items QLayoutItem* item; while ((item = patchesGroupBoxLayout->takeAt(0)) != nullptr) { delete item->widget(); @@ -1013,6 +1010,8 @@ void CheatsPatches::addPatchesToLayout(const QString& folderPath) { QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData); QJsonObject jsonObject = jsonDoc.object(); + bool patchAdded = false; + // Iterate over each entry in the JSON file for (auto it = jsonObject.constBegin(); it != jsonObject.constEnd(); ++it) { QString xmlFileName = it.key(); @@ -1099,11 +1098,26 @@ void CheatsPatches::addPatchesToLayout(const QString& folderPath) { patchAuthor.clear(); patchNote.clear(); patchLines = QJsonArray(); + patchAdded = true; } } xmlFile.close(); } } + + // Remove the item from the list view if no patches were added (the game has patches, but not + // for the current version) + if (!patchAdded) { + QStringListModel* model = qobject_cast(patchesListView->model()); + if (model) { + QStringList items = model->stringList(); + int index = items.indexOf(filePath); + if (index != -1) { + items.removeAt(index); + model->setStringList(items); + } + } + } } void CheatsPatches::updateNoteTextEdit(const QString& patchName) {