Replaced QSet with QHash

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
(cherry picked from commit 73d33f93b30f658f9671358ac52bf4e03afeaefd)
This commit is contained in:
Trial97 2024-10-22 09:41:00 +03:00 committed by github-actions[bot]
parent 51a71d0471
commit 85422427b9
3 changed files with 24 additions and 19 deletions

View File

@ -1887,20 +1887,25 @@ const QString Application::javaPath()
void Application::addQSavePath(QString path) void Application::addQSavePath(QString path)
{ {
QMutexLocker locker(&m_qsaveResourcesMutex); QMutexLocker locker(&m_qsaveResourcesMutex);
m_qsaveResources.insert(path); m_qsaveResources[path] = m_qsaveResources.value(path, 0) + 1;
} }
void Application::removeQSavePath(QString path) void Application::removeQSavePath(QString path)
{ {
QMutexLocker locker(&m_qsaveResourcesMutex); QMutexLocker locker(&m_qsaveResourcesMutex);
m_qsaveResources.remove(path); auto count = m_qsaveResources.value(path, 0) - 1;
if (count <= 0) {
m_qsaveResources.remove(path);
} else {
m_qsaveResources[path] = count;
}
} }
bool Application::checkQSavePath(QString path) bool Application::checkQSavePath(QString path)
{ {
QMutexLocker locker(&m_qsaveResourcesMutex); QMutexLocker locker(&m_qsaveResourcesMutex);
for (auto r : m_qsaveResources) { for (auto partialPath : m_qsaveResources.keys()) {
if (path.contains(r)) { if (path.startsWith(partialPath) && m_qsaveResources.value(partialPath, 0) > 0) {
return true; return true;
} }
} }

View File

@ -43,7 +43,6 @@
#include <QFlag> #include <QFlag>
#include <QIcon> #include <QIcon>
#include <QMutex> #include <QMutex>
#include <QSet>
#include <QUrl> #include <QUrl>
#include <memory> #include <memory>
@ -312,6 +311,6 @@ class Application : public QApplication {
bool checkQSavePath(QString); bool checkQSavePath(QString);
private: private:
QSet<QString> m_qsaveResources; QHash<QString, int> m_qsaveResources;
mutable QMutex m_qsaveResourcesMutex; mutable QMutex m_qsaveResourcesMutex;
}; };

View File

@ -17,6 +17,7 @@
*/ */
#pragma once #pragma once
#include <QFileInfo>
#include <QSaveFile> #include <QSaveFile>
#include "Application.h" #include "Application.h"
@ -46,24 +47,24 @@
*/ */
class PSaveFile : public QSaveFile { class PSaveFile : public QSaveFile {
public: public:
PSaveFile(const QString& name) : QSaveFile(name) PSaveFile(const QString& name) : QSaveFile(name) { addPath(name); }
{ PSaveFile(const QString& name, QObject* parent) : QSaveFile(name, parent) { addPath(name); }
if (auto app = APPLICATION_DYN) {
app->addQSavePath(name + ".");
}
}
PSaveFile(const QString& name, QObject* parent) : QSaveFile(name, parent)
{
if (auto app = APPLICATION_DYN) {
app->addQSavePath(name + ".");
}
}
virtual ~PSaveFile() virtual ~PSaveFile()
{ {
if (auto app = APPLICATION_DYN) { if (auto app = APPLICATION_DYN) {
app->removeQSavePath(fileName() + "."); app->removeQSavePath(m_absoluteFilePath);
} }
} }
private:
void addPath(const QString& path)
{
m_absoluteFilePath = QFileInfo(path).absoluteFilePath() + "."; // add dot for tmp files only
if (auto app = APPLICATION_DYN) {
app->addQSavePath(m_absoluteFilePath);
}
}
QString m_absoluteFilePath;
}; };
#else #else
#define PSaveFile QSaveFile #define PSaveFile QSaveFile