From b871b6729e4286c39bcd0f60a793a85fe20e63fd Mon Sep 17 00:00:00 2001 From: jared Date: Sun, 7 May 2023 01:41:10 +0800 Subject: [PATCH] format --- 3rdparty/TaoCommon/.clang-format | 32 +- 3rdparty/TaoCommon/.gitignore | 3 +- 3rdparty/TaoCommon/TaoCommon.pro | 2 +- 3rdparty/TaoCommon/clang-foramt-All.ps1 | 8 + .../TaoCommon/demo/CommonWithModule/main.cpp | 29 +- .../TaoCommon/demo/CommonWithSource/main.cpp | 29 +- 3rdparty/TaoCommon/demo/ExampleLog/main.cpp | 87 +-- .../src/TaoCommon/Common/FileReadWrite.h | 122 ++--- .../src/TaoCommon/Common/JsonSerialize.h | 154 +++--- .../TaoCommon/src/TaoCommon/Common/MathHelp.h | 32 +- .../src/TaoCommon/Common/ObjectMap.h | 260 ++++----- .../TaoCommon/src/TaoCommon/Common/Package.h | 46 +- .../src/TaoCommon/Common/PropertyHelper.h | 190 +++---- .../TaoCommon/src/TaoCommon/Common/Subject.h | 182 +++---- .../TaoCommon/Frameless/TaoFrameLessView.h | 56 +- .../Frameless/TaoFrameLessView_unix.cpp | 109 ++-- .../Frameless/TaoFrameLessView_win.cpp | 498 +++++++++--------- .../TaoCommon/src/TaoCommon/Logger/Logger.cpp | 196 +++---- .../TaoCommon/src/TaoCommon/Logger/Logger.h | 12 +- .../QuickModel/QuickListItemBase.cpp | 2 +- .../TaoCommon/QuickModel/QuickListItemBase.h | 50 +- .../TaoCommon/QuickModel/QuickListModel.cpp | 466 ++++++++-------- .../src/TaoCommon/QuickModel/QuickListModel.h | 196 +++---- .../TaoCommon/QuickModel/QuickModelBase.hpp | 348 ++++++------ .../src/TaoCommon/QuickTool/QuickTool.cpp | 102 ++-- .../src/TaoCommon/QuickTool/QuickTool.h | 48 +- .../TaoCommon/QuickTree/Model/QuickTreeItem.h | 206 ++++---- .../QuickTree/Model/QuickTreeModel.cpp | 100 ++-- .../QuickTree/Model/QuickTreeModel.h | 144 ++--- .../src/TaoCommon/Thread/ThreadCommon.h | 2 +- .../src/TaoCommon/Thread/ThreadPool.cpp | 14 +- .../src/TaoCommon/Thread/ThreadPool.h | 32 +- .../TaoCommon/Thread/ThreadWorkerController.h | 212 ++++---- .../TaoCommon/src/TaoCommon/Trans/Trans.cpp | 180 +++---- .../TaoCommon/src/TaoCommon/Trans/Trans.h | 56 +- 35 files changed, 2128 insertions(+), 2077 deletions(-) create mode 100644 3rdparty/TaoCommon/clang-foramt-All.ps1 diff --git a/3rdparty/TaoCommon/.clang-format b/3rdparty/TaoCommon/.clang-format index 5b37407d..005ff205 100644 --- a/3rdparty/TaoCommon/.clang-format +++ b/3rdparty/TaoCommon/.clang-format @@ -17,20 +17,31 @@ ColumnLimit: 160 TabWidth: 4 # 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, Always -UseTab: Never +UseTab: Always # 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行) AlignAfterOpenBracket: AlwaysBreak -# 左对齐逃脱换行(使用反斜杠换行)的反斜杠 -AlignEscapedNewlines: Right +#对齐逃脱换行(使用反斜杠换行)的反斜杠 +AlignEscapedNewlines: Right +# 连续赋值时,对齐所有等号 +AlignConsecutiveAssignments: true + +# 连续声明时,对齐所有声明的变量名 +AlignConsecutiveDeclarations: true + +# 对齐连续的宏定义 +AlignConsecutiveMacros: true + +# 对齐尾注释 +AlignTrailingComments: true # 在大括号前换行: Attach(始终将大括号附加到周围的上下文), Linux(除函数、命名空间和类定义,与Attach类似), # Mozilla(除枚举、函数、记录定义,与Attach类似), Stroustrup(除函数定义、catch、else,与Attach类似), # Allman(总是在大括号前换行), GNU(总是在大括号前换行,并对于控制语句的大括号增加额外的缩进), WebKit(在函数前换行), Custom # 注:这里认为语句块也属于函数 -BreakBeforeBraces: Custom +BreakBeforeBraces: Allman # 大括号换行,只有当BreakBeforeBraces设置为Custom时才有效 BraceWrapping: # class定义后面 @@ -58,7 +69,10 @@ BraceWrapping: # 缩进大括号 IndentBraces: false - +# 在二元运算符前换行: None(在操作符后换行), NonAssignment(在非赋值的操作符前换行), All(在操作符前换行) +BreakBeforeBinaryOperators: None + + # 继承列表的逗号前换行 BreakBeforeInheritanceComma: true @@ -86,12 +100,19 @@ AlignTrailingComments: true # 允许短的函数放在同一行: None, InlineOnly(定义在类中), Empty(空函数), Inline(定义在类中,空函数), All AllowShortFunctionsOnASingleLine: Empty +# false表示函数实参要么都在同一行,要么都各自一行 +BinPackArguments: false + +# false表示所有形参要么都在同一行,要么都各自一行 +BinPackParameters: false + # 需要被解读为foreach循环而不是函数调用的宏 ForEachMacros: - foreach - Q_FOREACH - BOOST_FOREACH +IndentCaseLabels: true IncludeBlocks: Preserve # 对#include进行排序,匹配了某正则表达式的#include拥有对应的优先级,匹配不到的则默认优先级为INT_MAX(优先级越小排序越靠前), @@ -105,5 +126,4 @@ IncludeCategories: Priority: 1 - Regex: '.*' Priority: 1 - ... diff --git a/3rdparty/TaoCommon/.gitignore b/3rdparty/TaoCommon/.gitignore index 9b8015da..759cea4f 100644 --- a/3rdparty/TaoCommon/.gitignore +++ b/3rdparty/TaoCommon/.gitignore @@ -1,4 +1,5 @@ Makefile .qmake.cache .qmake.super -*.pro.user* +*.user +*.bak \ No newline at end of file diff --git a/3rdparty/TaoCommon/TaoCommon.pro b/3rdparty/TaoCommon/TaoCommon.pro index 9e0e183a..27628fe0 100644 --- a/3rdparty/TaoCommon/TaoCommon.pro +++ b/3rdparty/TaoCommon/TaoCommon.pro @@ -3,7 +3,7 @@ CONFIG += ordered #SUBDIRS += demo OTHER_FILES += \ .* \ - _clang-format \ + .clang-format \ LICENSE \ *.md \ *.profile \ diff --git a/3rdparty/TaoCommon/clang-foramt-All.ps1 b/3rdparty/TaoCommon/clang-foramt-All.ps1 new file mode 100644 index 00000000..3138c8ca --- /dev/null +++ b/3rdparty/TaoCommon/clang-foramt-All.ps1 @@ -0,0 +1,8 @@ +$scriptDir=$PSScriptRoot +$currentDir=Get-Location +Write-Host "currentDir" $currentDir +Write-Host "scriptDir" $scriptDir +$names="*.h","*.cpp","*.c","*.hpp" +Get-ChildItem -Include $names -Recurse $currentDir | ForEach-Object { + clang-format -i $_.FullName +} \ No newline at end of file diff --git a/3rdparty/TaoCommon/demo/CommonWithModule/main.cpp b/3rdparty/TaoCommon/demo/CommonWithModule/main.cpp index 71340045..edac70f7 100644 --- a/3rdparty/TaoCommon/demo/CommonWithModule/main.cpp +++ b/3rdparty/TaoCommon/demo/CommonWithModule/main.cpp @@ -3,18 +3,21 @@ #include int main(int argc, char* argv[]) { - QGuiApplication app(argc, argv); - Logger::initLog(); - QQmlApplicationEngine engine; - const QUrl url(QStringLiteral("qrc:/main.qml")); - QObject::connect( - &engine, &QQmlApplicationEngine::objectCreated, &app, - [url](QObject* obj, const QUrl& objUrl) { - if (!obj && url == objUrl) - QCoreApplication::exit(-1); - }, - Qt::QueuedConnection); - engine.load(url); + QGuiApplication app(argc, argv); + Logger::initLog(); + QQmlApplicationEngine engine; + const QUrl url(QStringLiteral("qrc:/main.qml")); + QObject::connect( + &engine, + &QQmlApplicationEngine::objectCreated, + &app, + [url](QObject* obj, const QUrl& objUrl) + { + if (!obj && url == objUrl) + QCoreApplication::exit(-1); + }, + Qt::QueuedConnection); + engine.load(url); - return app.exec(); + return app.exec(); } diff --git a/3rdparty/TaoCommon/demo/CommonWithSource/main.cpp b/3rdparty/TaoCommon/demo/CommonWithSource/main.cpp index 3e5eac50..bc33b38a 100644 --- a/3rdparty/TaoCommon/demo/CommonWithSource/main.cpp +++ b/3rdparty/TaoCommon/demo/CommonWithSource/main.cpp @@ -4,18 +4,21 @@ int main(int argc, char* argv[]) { - QGuiApplication app(argc, argv); - Logger::initLog(); - QQmlApplicationEngine engine; - const QUrl url(QStringLiteral("qrc:/main.qml")); - QObject::connect( - &engine, &QQmlApplicationEngine::objectCreated, &app, - [url](QObject* obj, const QUrl& objUrl) { - if (!obj && url == objUrl) - QCoreApplication::exit(-1); - }, - Qt::QueuedConnection); - engine.load(url); + QGuiApplication app(argc, argv); + Logger::initLog(); + QQmlApplicationEngine engine; + const QUrl url(QStringLiteral("qrc:/main.qml")); + QObject::connect( + &engine, + &QQmlApplicationEngine::objectCreated, + &app, + [url](QObject* obj, const QUrl& objUrl) + { + if (!obj && url == objUrl) + QCoreApplication::exit(-1); + }, + Qt::QueuedConnection); + engine.load(url); - return app.exec(); + return app.exec(); } diff --git a/3rdparty/TaoCommon/demo/ExampleLog/main.cpp b/3rdparty/TaoCommon/demo/ExampleLog/main.cpp index ad18c2c4..9284e600 100644 --- a/3rdparty/TaoCommon/demo/ExampleLog/main.cpp +++ b/3rdparty/TaoCommon/demo/ExampleLog/main.cpp @@ -14,62 +14,65 @@ const int N = 17; // 原子计数器。不要用普通的int,多线程情况下不准确。 static atomic_int gCount(0); -static QString currentThreadId() +static QString currentThreadId() { - stringstream ss; - ss << this_thread::get_id(); - return QString::fromStdString(ss.str()); + stringstream ss; + ss << this_thread::get_id(); + return QString::fromStdString(ss.str()); } // 输出log的函数,随便打印一点信息 static void showSomeLogger() { - // 在文件本身的编码是utf-8的前提下,以下三种方式都可以直接输出中文。 - // u8是c++11标准支持的字符串字面量写法,可以参考https://zh.cppreference.com/w/cpp/language/string_literal - // QStringLiteral是Qt特有的宏,用来在编译期生成字符串字面量 - // QString::fromLocal8Bit可以在运行过程中,动态处理中文字符串。 - for (int i = 0; i < 1000; ++i) - { - gCount++; - int count = gCount; - LOG_DEBUG << currentThreadId() << u8"山有木兮木有枝,心悦君兮君不知。"; - LOG_DEBUG << currentThreadId() << QStringLiteral("黄河远上白云间,一片孤城万仞山。"); - LOG_DEBUG << currentThreadId() << QStringLiteral("人生若只如初见,何事秋风悲画扇。"); - LOG_INFO << currentThreadId() << u8"玲珑骰子安红豆,入骨相思知不知。"; - LOG_WARN << currentThreadId() << u8"此情可待成追忆,只是当时已惘然。"; - LOG_CRIT << currentThreadId() << u8"严重的事情发生了,股票跌了!" << count; - qApp->processEvents(); - } + // 在文件本身的编码是utf-8的前提下,以下三种方式都可以直接输出中文。 + // u8是c++11标准支持的字符串字面量写法,可以参考https://zh.cppreference.com/w/cpp/language/string_literal + // QStringLiteral是Qt特有的宏,用来在编译期生成字符串字面量 + // QString::fromLocal8Bit可以在运行过程中,动态处理中文字符串。 + for (int i = 0; i < 1000; ++i) + { + gCount++; + int count = gCount; + LOG_DEBUG << currentThreadId() << u8"山有木兮木有枝,心悦君兮君不知。"; + LOG_DEBUG << currentThreadId() << QStringLiteral("黄河远上白云间,一片孤城万仞山。"); + LOG_DEBUG << currentThreadId() << QStringLiteral("人生若只如初见,何事秋风悲画扇。"); + LOG_INFO << currentThreadId() << u8"玲珑骰子安红豆,入骨相思知不知。"; + LOG_WARN << currentThreadId() << u8"此情可待成追忆,只是当时已惘然。"; + LOG_CRIT << currentThreadId() << u8"严重的事情发生了,股票跌了!" << count; + qApp->processEvents(); + } } static void logThread() { - vector threads; + vector threads; - // 创建N个线程 - for (unsigned long long i = 0; i < N; ++i) - { - threads.emplace_back(thread(showSomeLogger)); - } + // 创建N个线程 + for (unsigned long long i = 0; i < N; ++i) + { + threads.emplace_back(thread(showSomeLogger)); + } - // join,等线程结束。 - for (unsigned long long i = 0; i < N; ++i) - { - threads[i].join(); - } - LOG_INFO << currentThreadId() << u8"故事到这就结束了,总共输出log" << gCount; - qApp->exit(); + // join,等线程结束。 + for (unsigned long long i = 0; i < N; ++i) + { + threads[i].join(); + } + LOG_INFO << currentThreadId() << u8"故事到这就结束了,总共输出log" << gCount; + qApp->exit(); } int main(int argc, char* argv[]) { - QCoreApplication app(argc, argv); + QCoreApplication app(argc, argv); - Logger::initLog(); + Logger::initLog(); - LOG_INFO << currentThreadId() << u8"故事就是从这里开始的"; + LOG_INFO << currentThreadId() << u8"故事就是从这里开始的"; - // 延迟创建 logger - QTimer::singleShot(2000, [&]() { - std::thread logger(logThread); - logger.detach(); - }); - return app.exec(); + // 延迟创建 logger + QTimer::singleShot( + 2000, + [&]() + { + std::thread logger(logThread); + logger.detach(); + }); + return app.exec(); } diff --git a/3rdparty/TaoCommon/src/TaoCommon/Common/FileReadWrite.h b/3rdparty/TaoCommon/src/TaoCommon/Common/FileReadWrite.h index 18955ad1..67ec3b78 100644 --- a/3rdparty/TaoCommon/src/TaoCommon/Common/FileReadWrite.h +++ b/3rdparty/TaoCommon/src/TaoCommon/Common/FileReadWrite.h @@ -12,107 +12,107 @@ namespace TaoCommon { UNUSED static bool readFile(const QString& filePath, QByteArray& content) { - QFile file(filePath); - if (!file.open(QFile::ReadOnly)) - { - LOG_WARN << "open file " << filePath << "failed:" << file.errorString(); - return false; - } - content = file.readAll(); - file.close(); - return true; + QFile file(filePath); + if (!file.open(QFile::ReadOnly)) + { + LOG_WARN << "open file " << filePath << "failed:" << file.errorString(); + return false; + } + content = file.readAll(); + file.close(); + return true; } UNUSED static bool readJson(const QByteArray& data, QJsonDocument& doc) { - QJsonParseError err; - doc = QJsonDocument::fromJson(data, &err); - if (doc.isNull()) - { - LOG_WARN << "parse json failed:" << err.errorString(); - return false; - } - return true; + QJsonParseError err; + doc = QJsonDocument::fromJson(data, &err); + if (doc.isNull()) + { + LOG_WARN << "parse json failed:" << err.errorString(); + return false; + } + return true; } UNUSED static bool readJson(const QByteArray& data, QJsonArray& array) { - QJsonDocument doc; - bool ok = readJson(data, doc); - if (ok) - { - array = doc.array(); - } - return ok; + QJsonDocument doc; + bool ok = readJson(data, doc); + if (ok) + { + array = doc.array(); + } + return ok; } UNUSED static bool readJson(const QByteArray& data, QJsonObject& object) { - QJsonDocument doc; - bool ok = readJson(data, doc); - if (ok) - { - object = doc.object(); - } - return ok; + QJsonDocument doc; + bool ok = readJson(data, doc); + if (ok) + { + object = doc.object(); + } + return ok; } UNUSED static bool readJsonFile(const QString& filePath, QJsonDocument& jsonDoc) { - QByteArray data; - if (!readFile(filePath, data)) - { - return false; - } - return readJson(data, jsonDoc); + QByteArray data; + if (!readFile(filePath, data)) + { + return false; + } + return readJson(data, jsonDoc); } static bool readJsonFile(const QString& filePath, QJsonObject& jsonObj) { - QByteArray data; - if (!readFile(filePath, data)) - { - return false; - } - return readJson(data, jsonObj); + QByteArray data; + if (!readFile(filePath, data)) + { + return false; + } + return readJson(data, jsonObj); } UNUSED static bool readJsonFile(const QString& filePath, QJsonArray& jsonArray) { - QByteArray data; - if (!readFile(filePath, data)) - { - return false; - } - return readJson(data, jsonArray); + QByteArray data; + if (!readFile(filePath, data)) + { + return false; + } + return readJson(data, jsonArray); } UNUSED static bool writeFile(const QString& filePath, const QByteArray& content) { - QFile file(filePath); - if (!file.open(QFile::WriteOnly)) - { - LOG_WARN << "open file " << filePath << "failed:" << file.errorString(); - return false; - } - file.write(content); - file.close(); - return true; + QFile file(filePath); + if (!file.open(QFile::WriteOnly)) + { + LOG_WARN << "open file " << filePath << "failed:" << file.errorString(); + return false; + } + file.write(content); + file.close(); + return true; } UNUSED static bool writeJsonFile(const QString& filePath, const QJsonDocument& doc) { - return writeFile(filePath, doc.toJson()); + return writeFile(filePath, doc.toJson()); } UNUSED static bool writeJsonFile(const QString& filePath, const QJsonArray& jsonArray) { - return writeJsonFile(filePath, QJsonDocument(jsonArray)); + return writeJsonFile(filePath, QJsonDocument(jsonArray)); } UNUSED static bool writeJsonFile(const QString& filePath, const QJsonObject& jsonObj) { - return writeJsonFile(filePath, QJsonDocument(jsonObj)); + return writeJsonFile(filePath, QJsonDocument(jsonObj)); } } // namespace TaoCommon diff --git a/3rdparty/TaoCommon/src/TaoCommon/Common/JsonSerialize.h b/3rdparty/TaoCommon/src/TaoCommon/Common/JsonSerialize.h index c32006ea..c04704a6 100644 --- a/3rdparty/TaoCommon/src/TaoCommon/Common/JsonSerialize.h +++ b/3rdparty/TaoCommon/src/TaoCommon/Common/JsonSerialize.h @@ -12,72 +12,72 @@ ///@brief 序列化. object to Json, object to Variant or VariantMap #define JsonSerialize_Begin() \ public: \ - operator QVariant() const \ - { \ - return QVariant::fromValue(this->operator QVariantMap()); \ - } \ - operator QJsonObject() const \ - { \ - return QJsonObject::fromVariantMap(this->operator QVariantMap()); \ - } \ - operator QVariantMap() const \ - { \ - QVariantMap vmap; + operator QVariant() const \ + { \ + return QVariant::fromValue(this->operator QVariantMap()); \ + } \ + operator QJsonObject() const \ + { \ + return QJsonObject::fromVariantMap(this->operator QVariantMap()); \ + } \ + operator QVariantMap() const \ + { \ + QVariantMap vmap; #define JsonSerialize_End() \ - return vmap; \ - } + return vmap; \ + } ///@brief 序列化属性映射 #define JsonPureProperty(NAME) vmap.unite((QVariantMap)m_##NAME); #define JsonProperty(NAME) \ - vmap[#NAME] = QVariant::fromValue(m_##NAME); \ - if (vmap.value(#NAME).isNull()) \ - vmap.remove(#NAME); + vmap[#NAME] = QVariant::fromValue(m_##NAME); \ + if (vmap.value(#NAME).isNull()) \ + vmap.remove(#NAME); #define JsonContainerProperty(NAME) \ - { \ - QVariantList lst; \ - lst.reserve(m_##NAME.size()); \ - for (const auto& t : m_##NAME) \ - { \ - lst << QVariant::fromValue(t); \ - } \ - vmap[#NAME] = lst; \ - } + { \ + QVariantList lst; \ + lst.reserve(m_##NAME.size()); \ + for (const auto& t : m_##NAME) \ + { \ + lst << QVariant::fromValue(t); \ + } \ + vmap[#NAME] = lst; \ + } ///@brief 反序列化 object from json #define JsonDeserialize_Begin(class_name) \ public: \ - class_name(const QJsonObject& other) \ - { \ - QVariantMap vmap = other.toVariantMap(); + class_name(const QJsonObject& other) \ + { \ + QVariantMap vmap = other.toVariantMap(); #define JsonDeserialize_End() } ///@brief 部分反序列化 #define JsonPartialDeserialize_Begin(class_name) \ public: \ - class_name& operator=(const QJsonObject& other) \ - { \ - QVariantMap vmap = other.toVariantMap(); + class_name& operator=(const QJsonObject& other) \ + { \ + QVariantMap vmap = other.toVariantMap(); #define JsonPartialDeserialize_End() \ - return *this; \ - } + return *this; \ + } #define JsonDeserializeContainerProperty(NAME) \ - if (vmap.value(#NAME).canConvert()) \ - { \ - const auto& list = vmap.value(#NAME).value(); \ - m_##NAME.clear(); \ - m_##NAME.reserve(list.size()); \ - for (const auto& v : list) \ - { \ - m_##NAME.push_back(v.value()); \ - } \ - } + if (vmap.value(#NAME).canConvert()) \ + { \ + const auto& list = vmap.value(#NAME).value(); \ + m_##NAME.clear(); \ + m_##NAME.reserve(list.size()); \ + for (const auto& v : list) \ + { \ + m_##NAME.push_back(v.value()); \ + } \ + } #define JsonDeserializeProperty(NAME) m_##NAME = vmap.value(#NAME).value(); @@ -86,42 +86,42 @@ public: * class AppInfo : public QObject { - Q_OBJECT - - AUTO_PROPERTY(QString, appName, "") - AUTO_PROPERTY(bool, splashShow, false) - AUTO_PROPERTY(float, scale, 1.0f) - AUTO_PROPERTY(double, ratio, 14.0 / 9.0) - AUTO_PROPERTY(QStringList, customs, {}) - - JsonSerialize_Begin() - JsonProperty(appName) - JsonProperty(splashShow) - JsonProperty(scale) - JsonProperty(ratio) - JsonContainerProperty(customs) - JsonSerialize_End() - - JsonDeserialize_Begin(AppInfo) - JsonDeserializeProperty(appName) - JsonDeserializeProperty(splashShow) - JsonDeserializeProperty(scale) - JsonDeserializeProperty(ratio) - JsonDeserializeContainerProperty(customs) - JsonDeserialize_End() - - - JsonPartialDeserialize_Begin(AppInfo) - JsonDeserializeProperty(appName) - JsonDeserializeProperty(splashShow) - JsonDeserializeProperty(scale) - JsonDeserializeProperty(ratio) - JsonDeserializeContainerProperty(customs) - JsonPartialDeserialize_End() + Q_OBJECT + + AUTO_PROPERTY(QString, appName, "") + AUTO_PROPERTY(bool, splashShow, false) + AUTO_PROPERTY(float, scale, 1.0f) + AUTO_PROPERTY(double, ratio, 14.0 / 9.0) + AUTO_PROPERTY(QStringList, customs, {}) + + JsonSerialize_Begin() + JsonProperty(appName) + JsonProperty(splashShow) + JsonProperty(scale) + JsonProperty(ratio) + JsonContainerProperty(customs) + JsonSerialize_End() + + JsonDeserialize_Begin(AppInfo) + JsonDeserializeProperty(appName) + JsonDeserializeProperty(splashShow) + JsonDeserializeProperty(scale) + JsonDeserializeProperty(ratio) + JsonDeserializeContainerProperty(customs) + JsonDeserialize_End() + + + JsonPartialDeserialize_Begin(AppInfo) + JsonDeserializeProperty(appName) + JsonDeserializeProperty(splashShow) + JsonDeserializeProperty(scale) + JsonDeserializeProperty(ratio) + JsonDeserializeContainerProperty(customs) + JsonPartialDeserialize_End() public: - explicit AppInfo(QObject *parent = nullptr); - virtual ~AppInfo() override; + explicit AppInfo(QObject *parent = nullptr); + virtual ~AppInfo() override; }; these MACOR will generate these function: diff --git a/3rdparty/TaoCommon/src/TaoCommon/Common/MathHelp.h b/3rdparty/TaoCommon/src/TaoCommon/Common/MathHelp.h index a45cbdc8..6becf87a 100644 --- a/3rdparty/TaoCommon/src/TaoCommon/Common/MathHelp.h +++ b/3rdparty/TaoCommon/src/TaoCommon/Common/MathHelp.h @@ -8,35 +8,35 @@ template static bool inRange(const T& value, const T& min, const T& max) { - if (min <= value && value <= max) - { - return true; - } - return false; + if (min <= value && value <= max) + { + return true; + } + return false; } // inRange 模版偏特化,遇到double时,使用std的 "浮点数比较" 代替 "常规比较", 规避精度误差 template <> static bool inRange(const double& value, const double& min, const double& max) { - if (std::isgreaterequal(value, min) && std::islessequal(value, max)) - { - return true; - } - return false; + if (std::isgreaterequal(value, min) && std::islessequal(value, max)) + { + return true; + } + return false; } // inRange 模版偏特化,遇到float时,使用std的"浮点数比较" 代替 "常规比较", 规避精度误差 template <> static bool inRange(const float& value, const float& min, const float& max) { - if (std::isgreaterequal(value, min) && std::islessequal(value, max)) - { - return true; - } - return false; + if (std::isgreaterequal(value, min) && std::islessequal(value, max)) + { + return true; + } + return false; } // clamp, 限制 value在 [min - Max]区间. template static T clamp(const T& value, const T& min, const T& max) { - return std::max(min, std::min(value, max)); + return std::max(min, std::min(value, max)); } diff --git a/3rdparty/TaoCommon/src/TaoCommon/Common/ObjectMap.h b/3rdparty/TaoCommon/src/TaoCommon/Common/ObjectMap.h index 6c140604..6ace75a4 100644 --- a/3rdparty/TaoCommon/src/TaoCommon/Common/ObjectMap.h +++ b/3rdparty/TaoCommon/src/TaoCommon/Common/ObjectMap.h @@ -14,162 +14,162 @@ template class ObjectMap { public: - virtual ~ObjectMap() - { - clear(); - } - void addObj(const Key& key, const Value& obj) - { - m_objMap[key] = obj; - } - bool removeObj(const Key& key) - { - return (0 != m_objMap.erase(key)); - } - Value getObj(const Key& key) const - { - auto itor = m_objMap.find(key); - if (itor == m_objMap.end()) - { - return nullptr; - } - else - { - return itor->second; - } - } - template - void forEach(const CallbackType& callback) const - { - for (const auto& pair : m_objMap) - { - callback(pair.second.get()); - } - } - void clear() - { - m_objMap.clear(); - } + virtual ~ObjectMap() + { + clear(); + } + void addObj(const Key& key, const Value& obj) + { + m_objMap[key] = obj; + } + bool removeObj(const Key& key) + { + return (0 != m_objMap.erase(key)); + } + Value getObj(const Key& key) const + { + auto itor = m_objMap.find(key); + if (itor == m_objMap.end()) + { + return nullptr; + } + else + { + return itor->second; + } + } + template + void forEach(const CallbackType& callback) const + { + for (const auto& pair : m_objMap) + { + callback(pair.second.get()); + } + } + void clear() + { + m_objMap.clear(); + } protected: - std::unordered_map m_objMap; + std::unordered_map m_objMap; }; // 智能对象存储器。自动生成key,自动管理对象。 template class CObjectMap { public: - virtual ~CObjectMap() - { - clear(); - } - template - DeriveObjectType* getObject() const - { - static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); + virtual ~CObjectMap() + { + clear(); + } + template + DeriveObjectType* getObject() const + { + static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); - auto objPtr = m_objMap.getObj(std::type_index(typeid(std::shared_ptr))); - return std::static_pointer_cast(objPtr).get(); - } - template - void createObject(Args&... args) - { - static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); + auto objPtr = m_objMap.getObj(std::type_index(typeid(std::shared_ptr))); + return std::static_pointer_cast(objPtr).get(); + } + template + void createObject(Args&... args) + { + static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); - auto obj = std::make_shared(args...); - m_objMap.addObj(std::type_index(typeid(obj)), std::static_pointer_cast(obj)); - } - template - bool destroyObject() - { - static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); + auto obj = std::make_shared(args...); + m_objMap.addObj(std::type_index(typeid(obj)), std::static_pointer_cast(obj)); + } + template + bool destroyObject() + { + static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); - return m_objMap.removeObj(std::type_index(typeid(std::shared_ptr))); - } - void forEach(const std::function& callback) const - { - m_objMap.forEach(callback); - } - void clear() - { - m_objMap.clear(); - } + return m_objMap.removeObj(std::type_index(typeid(std::shared_ptr))); + } + void forEach(const std::function& callback) const + { + m_objMap.forEach(callback); + } + void clear() + { + m_objMap.clear(); + } protected: - ObjectMap> m_objMap; + ObjectMap> m_objMap; }; // 优先级对象存储器。自动生成key,自动管理对象。支持按优先级处理 template class CLevelObjectMap { public: - virtual ~CLevelObjectMap() - { - clear(); - } - template - DeriveObjectType* getObject() const - { - static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); + virtual ~CLevelObjectMap() + { + clear(); + } + template + DeriveObjectType* getObject() const + { + static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); - auto index = std::type_index(typeid(std::shared_ptr)); + auto index = std::type_index(typeid(std::shared_ptr)); - for (const auto& mainPair : m_map) - { - const std::unordered_map>& subMap = mainPair.second; + for (const auto& mainPair : m_map) + { + const std::unordered_map>& subMap = mainPair.second; - auto itor = subMap.find(index); - if (itor != subMap.end()) - { - return std::static_pointer_cast(itor->second).get(); - } - } - return nullptr; - } - template - void createObject(uint32_t level, Args&&... args) - { - static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); + auto itor = subMap.find(index); + if (itor != subMap.end()) + { + return std::static_pointer_cast(itor->second).get(); + } + } + return nullptr; + } + template + void createObject(uint32_t level, Args&&... args) + { + static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); - auto obj = std::make_shared(args...); - m_map[level][std::type_index(typeid(obj))] = std::static_pointer_cast(obj); - } - template - bool destroyObject() - { - static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); + auto obj = std::make_shared(args...); + m_map[level][std::type_index(typeid(obj))] = std::static_pointer_cast(obj); + } + template + bool destroyObject() + { + static_assert(std::is_base_of::value, "DeriveObjectType must be derive from ObjectType"); - auto index = std::type_index(typeid(std::shared_ptr)); - for (auto& mainPair : m_map) - { - std::unordered_map>& subMap = mainPair.second; + auto index = std::type_index(typeid(std::shared_ptr)); + for (auto& mainPair : m_map) + { + std::unordered_map>& subMap = mainPair.second; - auto itor = subMap.find(index); - if (itor != subMap.end()) - { - auto retItor = subMap.erase(itor); - return retItor != subMap.end(); - } - } - return false; - } - void forEach(const std::function& callback) const - { - for (const auto& mainPair : m_map) - { - const std::unordered_map>& subMap = mainPair.second; - for (const auto& subPair : subMap) - { - callback(subPair.second.get()); - } - } - } - void clear() - { - m_map.clear(); - } + auto itor = subMap.find(index); + if (itor != subMap.end()) + { + auto retItor = subMap.erase(itor); + return retItor != subMap.end(); + } + } + return false; + } + void forEach(const std::function& callback) const + { + for (const auto& mainPair : m_map) + { + const std::unordered_map>& subMap = mainPair.second; + for (const auto& subPair : subMap) + { + callback(subPair.second.get()); + } + } + } + void clear() + { + m_map.clear(); + } private: - std::map>> m_map; + std::map>> m_map; }; } // namespace TaoCommon diff --git a/3rdparty/TaoCommon/src/TaoCommon/Common/Package.h b/3rdparty/TaoCommon/src/TaoCommon/Common/Package.h index 6d45f915..0b15f49a 100644 --- a/3rdparty/TaoCommon/src/TaoCommon/Common/Package.h +++ b/3rdparty/TaoCommon/src/TaoCommon/Common/Package.h @@ -13,31 +13,31 @@ const int static headerLength = sizeof(quint32); // 封包。 入参为数据,返回值是 数据前面加一个头. 这就是一个数据包了 static QByteArray pack(const QByteArray& data) { - QByteArray header(headerLength, 0); - QDataStream os(&header, QIODevice::WriteOnly); - os << static_cast(data.length()); - return header + data; + QByteArray header(headerLength, 0); + QDataStream os(&header, QIODevice::WriteOnly); + os << static_cast(data.length()); + return header + data; } // 拆包。入参为连续的数据,返回值是拆出来的所有包列表 static QByteArrayList unpack(const QByteArray& data) { - QByteArrayList list; - QDataStream inStream(data); - quint32 sum = data.size(); - quint32 pos = 0; - while (pos + headerLength < sum) - { - quint32 packageLen = 0; - packageLen = 0; - inStream >> packageLen; - if (packageLen + headerLength > sum - pos) - { - break; - } - QByteArray subPackage = data.mid(pos + headerLength, packageLen); - inStream.skipRawData(packageLen); - list.append(subPackage); - pos += headerLength + packageLen; - } - return list; + QByteArrayList list; + QDataStream inStream(data); + quint32 sum = data.size(); + quint32 pos = 0; + while (pos + headerLength < sum) + { + quint32 packageLen = 0; + packageLen = 0; + inStream >> packageLen; + if (packageLen + headerLength > sum - pos) + { + break; + } + QByteArray subPackage = data.mid(pos + headerLength, packageLen); + inStream.skipRawData(packageLen); + list.append(subPackage); + pos += headerLength + packageLen; + } + return list; } diff --git a/3rdparty/TaoCommon/src/TaoCommon/Common/PropertyHelper.h b/3rdparty/TaoCommon/src/TaoCommon/Common/PropertyHelper.h index 5dfadcb6..a726d06a 100644 --- a/3rdparty/TaoCommon/src/TaoCommon/Common/PropertyHelper.h +++ b/3rdparty/TaoCommon/src/TaoCommon/Common/PropertyHelper.h @@ -10,28 +10,28 @@ struct Compare; template struct Compare { - static bool isEqual(const T& t1, const T& t2) - { - return t1 == t2; - } + static bool isEqual(const T& t1, const T& t2) + { + return t1 == t2; + } }; // float 比较器。 template <> struct Compare { - static bool isEqual(float f1, float f2) - { - return qFuzzyCompare(f1, f2); - } + static bool isEqual(float f1, float f2) + { + return qFuzzyCompare(f1, f2); + } }; // double 比较器。 template <> struct Compare { - static bool isEqual(double d1, double d2) - { - return qFuzzyCompare(d1, d2); - } + static bool isEqual(double d1, double d2) + { + return qFuzzyCompare(d1, d2); + } }; //**********************************普通成员及函数********************************** // 成员变量 @@ -39,25 +39,25 @@ struct Compare // 普通 get 函数 #define MEM_GET(T, NAME) \ - const T& NAME() const \ - { \ - return m_##NAME; \ - } + const T& NAME() const \ + { \ + return m_##NAME; \ + } // 普通 set 函数 #define MEM_SET(T, NAME) \ - void set_##NAME(const T& value) \ - { \ - m_##NAME = value; \ - } + void set_##NAME(const T& value) \ + { \ + m_##NAME = value; \ + } // 普通 可读可写 成员及函数 #define AUTO_MEM(T, NAME, InitValue) \ public: \ - MEM_GET(T, NAME) \ - MEM_SET(T, NAME) \ + MEM_GET(T, NAME) \ + MEM_SET(T, NAME) \ private: \ - PROP_MEM(T, NAME, InitValue) + PROP_MEM(T, NAME, InitValue) //**********************************QObject 属性********************************** @@ -69,63 +69,63 @@ private: // 属性 set 函数 (比普通set 多 changeCheck,change 时发信号. check 时处理 float 和 double 数据的精度) #define PROP_SET(T, NAME) \ - void set_##NAME(const T& value) \ - { \ - if (Compare::isEqual(m_##NAME, value)) \ - return; \ - m_##NAME = value; \ - emit NAME##Changed(value); \ - } + void set_##NAME(const T& value) \ + { \ + if (Compare::isEqual(m_##NAME, value)) \ + return; \ + m_##NAME = value; \ + emit NAME##Changed(value); \ + } // 只读属性,外部不能改 #define READONLY_PROPERTY(T, NAME, InitValue) \ private: \ - Q_PROPERTY(T NAME READ NAME CONSTANT) \ + Q_PROPERTY(T NAME READ NAME CONSTANT) \ public: \ - PROP_GET(T, NAME) \ + PROP_GET(T, NAME) \ private: \ - PROP_MEM(T, NAME, InitValue) + PROP_MEM(T, NAME, InitValue) // 可读属性,比只读属性 多一个 属性set函数。 #define READ_PROPERTY(T, NAME, InitValue) \ private: \ - Q_PROPERTY(T NAME READ NAME NOTIFY NAME##Changed) \ + Q_PROPERTY(T NAME READ NAME NOTIFY NAME##Changed) \ public: \ - PROP_GET(T, NAME) \ - PROP_SET(T, NAME) \ - PROP_CHANGE(T, NAME) \ + PROP_GET(T, NAME) \ + PROP_SET(T, NAME) \ + PROP_CHANGE(T, NAME) \ private: \ - PROP_MEM(T, NAME, InitValue) + PROP_MEM(T, NAME, InitValue) // 可读可写 属性。属性set函数 提升为 slot ,可被 invok / qml 调用 #define AUTO_PROPERTY(T, NAME, InitValue) \ private: \ - Q_PROPERTY(T NAME READ NAME WRITE set_##NAME NOTIFY NAME##Changed) \ + Q_PROPERTY(T NAME READ NAME WRITE set_##NAME NOTIFY NAME##Changed) \ public: \ - PROP_GET(T, NAME); \ - Q_SLOT PROP_SET(T, NAME); \ - PROP_CHANGE(T, NAME); \ + PROP_GET(T, NAME); \ + Q_SLOT PROP_SET(T, NAME); \ + PROP_CHANGE(T, NAME); \ \ private: \ - PROP_MEM(T, NAME, InitValue) + PROP_MEM(T, NAME, InitValue) /** * Example: * class AppInfo : public QObject { - Q_OBJECT - - AUTO_PROPERTY(QString, appName, "") - AUTO_PROPERTY(QString, descript, "") - AUTO_PROPERTY(QString, compilerVendor, "") - AUTO_PROPERTY(bool, splashShow, false) - AUTO_PROPERTY(float, scale, 1.0f) - AUTO_PROPERTY(double, ratio, 14.0 / 9.0) - AUTO_PROPERTY(QStringList, customs, {}) + Q_OBJECT + + AUTO_PROPERTY(QString, appName, "") + AUTO_PROPERTY(QString, descript, "") + AUTO_PROPERTY(QString, compilerVendor, "") + AUTO_PROPERTY(bool, splashShow, false) + AUTO_PROPERTY(float, scale, 1.0f) + AUTO_PROPERTY(double, ratio, 14.0 / 9.0) + AUTO_PROPERTY(QStringList, customs, {}) public: - explicit AppInfo(QObject *parent = nullptr); - virtual ~AppInfo() override; + explicit AppInfo(QObject *parent = nullptr); + virtual ~AppInfo() override; public: }; @@ -150,8 +150,8 @@ class AppInfo : public QObject // d 指针的声明 #define D_DECL \ private: \ - struct D_TYPE; \ - D_TYPE* D_NAME = nullptr; + struct D_TYPE; \ + D_TYPE* D_NAME = nullptr; // d 指针的定义 #define D_IMPL(ClassName) struct ClassName::D_TYPE @@ -161,35 +161,35 @@ private: // 属性 get 函数定义 #define PROP_GET_IMPL(ClasName, T, NAME) \ - const T& ClasName::NAME() const \ - { \ - return D_NAME->m_##NAME; \ - } + const T& ClasName::NAME() const \ + { \ + return D_NAME->m_##NAME; \ + } // 属性 set 函数定义 #define PROP_SET_IMPL(ClasName, T, NAME) \ - void ClasName::set_##NAME(const T& value) \ - { \ - if (Compare::isEqual(D_NAME->m_##NAME, value)) \ - return; \ - D_NAME->m_##NAME = value; \ - emit NAME##Changed(value); \ - } + void ClasName::set_##NAME(const T& value) \ + { \ + if (Compare::isEqual(D_NAME->m_##NAME, value)) \ + return; \ + D_NAME->m_##NAME = value; \ + emit NAME##Changed(value); \ + } // 可读可写 属性声明。 #define AUTO_PROPERTY_DECL(T, NAME) \ private: \ - Q_PROPERTY(T NAME READ NAME WRITE set_##NAME NOTIFY NAME##Changed) \ + Q_PROPERTY(T NAME READ NAME WRITE set_##NAME NOTIFY NAME##Changed) \ public: \ - PROP_GET_DECL(T, NAME); \ + PROP_GET_DECL(T, NAME); \ public slots: \ - PROP_SET_DECL(T, NAME); \ + PROP_SET_DECL(T, NAME); \ signals: \ - PROP_CHANGE(T, NAME) + PROP_CHANGE(T, NAME) // 可读可写 属性定义。 #define AUTO_PROPERTY_IMPL(ClassName, T, NAME) \ - PROP_GET_IMPL(ClassName, T, NAME) \ - PROP_SET_IMPL(ClassName, T, NAME) + PROP_GET_IMPL(ClassName, T, NAME) \ + PROP_SET_IMPL(ClassName, T, NAME) /** * Example: @@ -198,20 +198,20 @@ signals: //AppInfo.h class AppInfo : public QObject { - Q_OBJECT - - AUTO_PROPERTY_DECL(QString, appName, "") - AUTO_PROPERTY_DECL(QString, descript, "") - AUTO_PROPERTY_DECL(QString, compilerVendor, "") - AUTO_PROPERTY_DECL(bool, splashShow, false) - AUTO_PROPERTY_DECL(float, scale, 1.0f) - AUTO_PROPERTY_DECL(double, ratio, 14.0 / 9.0) - AUTO_PROPERTY_DECL(QStringList, customs, {}) + Q_OBJECT + + AUTO_PROPERTY_DECL(QString, appName, "") + AUTO_PROPERTY_DECL(QString, descript, "") + AUTO_PROPERTY_DECL(QString, compilerVendor, "") + AUTO_PROPERTY_DECL(bool, splashShow, false) + AUTO_PROPERTY_DECL(float, scale, 1.0f) + AUTO_PROPERTY_DECL(double, ratio, 14.0 / 9.0) + AUTO_PROPERTY_DECL(QStringList, customs, {}) public: - explicit AppInfo(QObject *parent = nullptr); - virtual ~AppInfo() override; + explicit AppInfo(QObject *parent = nullptr); + virtual ~AppInfo() override; private: - D_DECL; + D_DECL; }; @@ -219,13 +219,13 @@ class AppInfo : public QObject D_IMPL(AppInfo) { - D_MEM(QString, appName, "") - D_MEM(QString, descript, "") - D_MEM(QString, compilerVendor, "") - D_MEM(bool, splashShow, false) - D_MEM(float, scale, 1.0f) - D_MEM(double, ratio, 14.0 / 9.0) - D_MEM(QStringList, customs, {}) + D_MEM(QString, appName, "") + D_MEM(QString, descript, "") + D_MEM(QString, compilerVendor, "") + D_MEM(bool, splashShow, false) + D_MEM(float, scale, 1.0f) + D_MEM(double, ratio, 14.0 / 9.0) + D_MEM(QStringList, customs, {}) }; AUTO_PROPERTY_IMPL(AppInfo, QString, appName) @@ -237,12 +237,12 @@ AUTO_PROPERTY_IMPL(AppInfo, double, ratio) AUTO_PROPERTY_IMPL(AppInfo, QStringList, customs) AppInfo::AppInfo(QObject *parent) - : QObject(parent) - , D_NAME(new D_TYPE) + : QObject(parent) + , D_NAME(new D_TYPE) {} AppInfo::~AppInfo() { - delete D_NAME; + delete D_NAME; } */ diff --git a/3rdparty/TaoCommon/src/TaoCommon/Common/Subject.h b/3rdparty/TaoCommon/src/TaoCommon/Common/Subject.h index ef5d9220..99c1ba08 100644 --- a/3rdparty/TaoCommon/src/TaoCommon/Common/Subject.h +++ b/3rdparty/TaoCommon/src/TaoCommon/Common/Subject.h @@ -10,50 +10,50 @@ template class Subject { public: - virtual ~Subject() - { - m_obsList.clear(); - } - // 订阅 - void subscibe(ObserverType* obs) - { - auto itor = std::find(m_obsList.begin(), m_obsList.end(), obs); - if (m_obsList.end() == itor) - { - m_obsList.push_back(obs); - } - } - // 取消订阅 - void unSubscibe(ObserverType* obs) - { - m_obsList.erase(std::remove(m_obsList.begin(), m_obsList.end(), obs)); - } - // 发布。这里的模板参数为函数类型。 - template - void publish(FuncType func) - { - for (auto obs : m_obsList) - { - // 调用回调函数,将obs作为第一个参数传递 - func(obs); - } - } - // 发布。支持过滤观察者。通常用在 观察者触发消息发布时,过滤观察者自己。 - template - void publish(FuncType func, ObserverType* exceptObs) - { - for (auto obs : m_obsList) - { - // 调用回调函数,将obs作为第一个参数传递 - if (obs != exceptObs) - { - func(obs); - } - } - } + virtual ~Subject() + { + m_obsList.clear(); + } + // 订阅 + void subscibe(ObserverType* obs) + { + auto itor = std::find(m_obsList.begin(), m_obsList.end(), obs); + if (m_obsList.end() == itor) + { + m_obsList.push_back(obs); + } + } + // 取消订阅 + void unSubscibe(ObserverType* obs) + { + m_obsList.erase(std::remove(m_obsList.begin(), m_obsList.end(), obs)); + } + // 发布。这里的模板参数为函数类型。 + template + void publish(FuncType func) + { + for (auto obs : m_obsList) + { + // 调用回调函数,将obs作为第一个参数传递 + func(obs); + } + } + // 发布。支持过滤观察者。通常用在 观察者触发消息发布时,过滤观察者自己。 + template + void publish(FuncType func, ObserverType* exceptObs) + { + for (auto obs : m_obsList) + { + // 调用回调函数,将obs作为第一个参数传递 + if (obs != exceptObs) + { + func(obs); + } + } + } private: - std::vector m_obsList; + std::vector m_obsList; }; // 优先级观察者模式,Subject-Observer。 @@ -61,56 +61,56 @@ template class LevelSubject { public: - virtual ~LevelSubject() - { - m_obsMap.clear(); - } - // 订阅 - void subscibe(ObserverType* obs, uint32_t level) - { - auto& vec = m_obsMap[level]; - auto itor = std::find(vec.begin(), vec.end(), obs); - if (vec.end() == itor) - { - vec.push_back(obs); - } - } - // 取消订阅 - void unSubscibe(ObserverType* obs) - { - for (auto& obsPair : m_obsMap) - { - obsPair.second.erase(std::remove(obsPair.second.begin(), obsPair.second.end(), obs)); - } - } - // 发布。这里的模板参数为函数类型。 - template - void publish(FuncType func) - { - for (const auto& obsPair : m_obsMap) - { - for (const auto& obs : obsPair.second) - { - func(obs); - } - } - } - template - void publish(FuncType func, ObserverType* exceptObs) - { - for (const auto& obsPair : m_obsMap) - { - for (const auto& obs : obsPair.second) - { - if (obs != exceptObs) - { - func(obs); - } - } - } - } + virtual ~LevelSubject() + { + m_obsMap.clear(); + } + // 订阅 + void subscibe(ObserverType* obs, uint32_t level) + { + auto& vec = m_obsMap[level]; + auto itor = std::find(vec.begin(), vec.end(), obs); + if (vec.end() == itor) + { + vec.push_back(obs); + } + } + // 取消订阅 + void unSubscibe(ObserverType* obs) + { + for (auto& obsPair : m_obsMap) + { + obsPair.second.erase(std::remove(obsPair.second.begin(), obsPair.second.end(), obs)); + } + } + // 发布。这里的模板参数为函数类型。 + template + void publish(FuncType func) + { + for (const auto& obsPair : m_obsMap) + { + for (const auto& obs : obsPair.second) + { + func(obs); + } + } + } + template + void publish(FuncType func, ObserverType* exceptObs) + { + for (const auto& obsPair : m_obsMap) + { + for (const auto& obs : obsPair.second) + { + if (obs != exceptObs) + { + func(obs); + } + } + } + } private: - std::map> m_obsMap; + std::map> m_obsMap; }; } // namespace TaoCommon diff --git a/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView.h b/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView.h index 4c3dff39..cbc42c4f 100644 --- a/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView.h +++ b/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView.h @@ -10,47 +10,47 @@ class TaoFrameLessViewPrivate; class TAO_API TaoFrameLessView : public QQuickView { - Q_OBJECT - using Super = QQuickView; - Q_PROPERTY(bool isMax READ isMax NOTIFY isMaxChanged) - Q_PROPERTY(bool isFull READ isFull NOTIFY isFullChanged) + Q_OBJECT + using Super = QQuickView; + Q_PROPERTY(bool isMax READ isMax NOTIFY isMaxChanged) + Q_PROPERTY(bool isFull READ isFull NOTIFY isFullChanged) public: - explicit TaoFrameLessView(QWindow* parent = nullptr); - ~TaoFrameLessView(); - void moveToScreenCenter(); - bool isMax() const; - bool isFull() const; - QQuickItem* titleItem() const; + explicit TaoFrameLessView(QWindow* parent = nullptr); + ~TaoFrameLessView(); + void moveToScreenCenter(); + bool isMax() const; + bool isFull() const; + QQuickItem* titleItem() const; - static QRect calcCenterGeo(const QRect& screenGeo, const QSize& normalSize); + static QRect calcCenterGeo(const QRect& screenGeo, const QSize& normalSize); public slots: - void setIsMax(bool isMax); - void setIsFull(bool isFull); - void setTitleItem(QQuickItem* item); + void setIsMax(bool isMax); + void setIsFull(bool isFull); + void setTitleItem(QQuickItem* item); signals: - void isMaxChanged(bool isMax); - void isFullChanged(bool isFull); - void mousePressed(int xPos, int yPos, int button); + void isMaxChanged(bool isMax); + void isFullChanged(bool isFull); + void mousePressed(int xPos, int yPos, int button); protected: - void showEvent(QShowEvent* e) override; - void resizeEvent(QResizeEvent* e) override; + void showEvent(QShowEvent* e) override; + void resizeEvent(QResizeEvent* e) override; #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) - bool nativeEvent(const QByteArray& eventType, void* message, qintptr* result) override; + bool nativeEvent(const QByteArray& eventType, void* message, qintptr* result) override; #else - bool nativeEvent(const QByteArray& eventType, void* message, long* result) override; + bool nativeEvent(const QByteArray& eventType, void* message, long* result) override; #endif - void mousePressEvent(QMouseEvent* event) override - { + void mousePressEvent(QMouseEvent* event) override + { #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) - emit mousePressed(event->position().x(), event->position().y(), event->button()); + emit mousePressed(event->position().x(), event->position().y(), event->button()); #else - emit mousePressed(event->x(), event->y(), event->button()); + emit mousePressed(event->x(), event->y(), event->button()); #endif - Super::mousePressEvent(event); - } + Super::mousePressEvent(event); + } private: - TaoFrameLessViewPrivate* d; + TaoFrameLessViewPrivate* d; }; diff --git a/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView_unix.cpp b/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView_unix.cpp index 76e3c7ec..716bbfe5 100644 --- a/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView_unix.cpp +++ b/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView_unix.cpp @@ -7,93 +7,98 @@ class TaoFrameLessViewPrivate { public: - bool m_isMax = false; - bool m_isFull = false; - QQuickItem* m_titleItem = nullptr; + bool m_isMax = false; + bool m_isFull = false; + QQuickItem* m_titleItem = nullptr; }; TaoFrameLessView::TaoFrameLessView(QWindow* parent) - : Super(parent) - , d(new TaoFrameLessViewPrivate) + : Super(parent) + , d(new TaoFrameLessViewPrivate) { - setFlags(Qt::CustomizeWindowHint | Qt::Window | Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint); - setResizeMode(SizeRootObjectToView); + setFlags(Qt::CustomizeWindowHint | Qt::Window | Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint); + setResizeMode(SizeRootObjectToView); - setIsMax(windowState() == Qt::WindowMaximized); - setIsFull(windowState() == Qt::WindowFullScreen); - connect(this, &QWindow::windowStateChanged, this, [&](Qt::WindowState state) { - (void)state; - setIsMax(windowState() == Qt::WindowMaximized); - setIsFull(windowState() == Qt::WindowFullScreen); - }); + setIsMax(windowState() == Qt::WindowMaximized); + setIsFull(windowState() == Qt::WindowFullScreen); + connect( + this, + &QWindow::windowStateChanged, + this, + [&](Qt::WindowState state) + { + (void)state; + setIsMax(windowState() == Qt::WindowMaximized); + setIsFull(windowState() == Qt::WindowFullScreen); + }); } TaoFrameLessView::~TaoFrameLessView() { - delete d; + delete d; } void TaoFrameLessView::showEvent(QShowEvent* e) { - Super::showEvent(e); + Super::showEvent(e); } QRect TaoFrameLessView::calcCenterGeo(const QRect& screenGeo, const QSize& normalSize) { - int w = normalSize.width(); - int h = normalSize.height(); - int x = screenGeo.x() + (screenGeo.width() - w) / 2; - int y = screenGeo.y() + (screenGeo.height() - h) / 2; - if (screenGeo.width() < w) - { - x = screenGeo.x(); - w = screenGeo.width(); - } - if (screenGeo.height() < h) - { - y = screenGeo.y(); - h = screenGeo.height(); - } + int w = normalSize.width(); + int h = normalSize.height(); + int x = screenGeo.x() + (screenGeo.width() - w) / 2; + int y = screenGeo.y() + (screenGeo.height() - h) / 2; + if (screenGeo.width() < w) + { + x = screenGeo.x(); + w = screenGeo.width(); + } + if (screenGeo.height() < h) + { + y = screenGeo.y(); + h = screenGeo.height(); + } - return { x, y, w, h }; + return { x, y, w, h }; } void TaoFrameLessView::moveToScreenCenter() { - auto geo = calcCenterGeo(screen()->availableGeometry(), size()); - if (minimumWidth() > geo.width() || minimumHeight() > geo.height()) - { - setMinimumSize(geo.size()); - } - setGeometry(geo); - update(); + auto geo = calcCenterGeo(screen()->availableGeometry(), size()); + if (minimumWidth() > geo.width() || minimumHeight() > geo.height()) + { + setMinimumSize(geo.size()); + } + setGeometry(geo); + update(); } bool TaoFrameLessView::isMax() const { - return d->m_isMax; + return d->m_isMax; } bool TaoFrameLessView::isFull() const { - return d->m_isFull; + return d->m_isFull; } QQuickItem* TaoFrameLessView::titleItem() const { - return d->m_titleItem; + return d->m_titleItem; } void TaoFrameLessView::setIsMax(bool isMax) { - if (d->m_isMax == isMax) - return; + if (d->m_isMax == isMax) + return; - d->m_isMax = isMax; - emit isMaxChanged(d->m_isMax); + d->m_isMax = isMax; + emit isMaxChanged(d->m_isMax); } void TaoFrameLessView::setIsFull(bool isFull) { - if (d->m_isFull == isFull) - return; + if (d->m_isFull == isFull) + return; - d->m_isFull = isFull; - emit isFullChanged(d->m_isFull); + d->m_isFull = isFull; + emit isFullChanged(d->m_isFull); } void TaoFrameLessView::setTitleItem(QQuickItem* item) { - d->m_titleItem = item; + d->m_titleItem = item; } #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) bool TaoFrameLessView::nativeEvent(const QByteArray& eventType, void* message, qintptr* result) @@ -102,10 +107,10 @@ bool TaoFrameLessView::nativeEvent(const QByteArray& eventType, void* message, l #endif { - return Super::nativeEvent(eventType, message, result); + return Super::nativeEvent(eventType, message, result); } void TaoFrameLessView::resizeEvent(QResizeEvent* e) { - Super::resizeEvent(e); + Super::resizeEvent(e); } diff --git a/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView_win.cpp b/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView_win.cpp index bb0e2626..55dbd9f3 100644 --- a/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView_win.cpp +++ b/3rdparty/TaoCommon/src/TaoCommon/Frameless/TaoFrameLessView_win.cpp @@ -24,234 +24,239 @@ // WS_MAXIMIZEBOX, WS_MINIMIZEBOX: enable minimize/maximize enum class Style : DWORD { - windowed = WS_OVERLAPPEDWINDOW | WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, - aero_borderless = WS_POPUP | WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, - basic_borderless = WS_POPUP | WS_THICKFRAME | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX + windowed = WS_OVERLAPPEDWINDOW | WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, + aero_borderless = WS_POPUP | WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, + basic_borderless = WS_POPUP | WS_THICKFRAME | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX }; static bool isCompositionEnabled() { - BOOL composition_enabled = FALSE; - bool success = ::DwmIsCompositionEnabled(&composition_enabled) == S_OK; - return composition_enabled && success; + BOOL composition_enabled = FALSE; + bool success = ::DwmIsCompositionEnabled(&composition_enabled) == S_OK; + return composition_enabled && success; } static Style selectBorderLessStyle() { - return isCompositionEnabled() ? Style::aero_borderless : Style::basic_borderless; + return isCompositionEnabled() ? Style::aero_borderless : Style::basic_borderless; } static void setShadow(HWND handle, bool enabled) { - if (isCompositionEnabled()) - { - static const MARGINS shadow_state[2] { { 0, 0, 0, 0 }, { 1, 1, 1, 1 } }; - ::DwmExtendFrameIntoClientArea(handle, &shadow_state[enabled]); - } + if (isCompositionEnabled()) + { + static const MARGINS shadow_state[2] { { 0, 0, 0, 0 }, { 1, 1, 1, 1 } }; + ::DwmExtendFrameIntoClientArea(handle, &shadow_state[enabled]); + } } static long hitTest(RECT winrect, long x, long y, int borderWidth) { - // 鼠标区域位于窗体边框,进行缩放 - if ((x >= winrect.left) && (x < winrect.left + borderWidth) && (y >= winrect.top) && (y < winrect.top + borderWidth)) - { - return HTTOPLEFT; - } - else if (x < winrect.right && x >= winrect.right - borderWidth && y >= winrect.top && y < winrect.top + borderWidth) - { - return HTTOPRIGHT; - } - else if (x >= winrect.left && x < winrect.left + borderWidth && y < winrect.bottom && y >= winrect.bottom - borderWidth) - { - return HTBOTTOMLEFT; - } - else if (x < winrect.right && x >= winrect.right - borderWidth && y < winrect.bottom && y >= winrect.bottom - borderWidth) - { - return HTBOTTOMRIGHT; - } - else if (x >= winrect.left && x < winrect.left + borderWidth) - { - return HTLEFT; - } - else if (x < winrect.right && x >= winrect.right - borderWidth) - { - return HTRIGHT; - } - else if (y >= winrect.top && y < winrect.top + borderWidth) - { - return HTTOP; - } - else if (y < winrect.bottom && y >= winrect.bottom - borderWidth) - { - return HTBOTTOM; - } - else - { - return 0; - } + // 鼠标区域位于窗体边框,进行缩放 + if ((x >= winrect.left) && (x < winrect.left + borderWidth) && (y >= winrect.top) && (y < winrect.top + borderWidth)) + { + return HTTOPLEFT; + } + else if (x < winrect.right && x >= winrect.right - borderWidth && y >= winrect.top && y < winrect.top + borderWidth) + { + return HTTOPRIGHT; + } + else if (x >= winrect.left && x < winrect.left + borderWidth && y < winrect.bottom && y >= winrect.bottom - borderWidth) + { + return HTBOTTOMLEFT; + } + else if (x < winrect.right && x >= winrect.right - borderWidth && y < winrect.bottom && y >= winrect.bottom - borderWidth) + { + return HTBOTTOMRIGHT; + } + else if (x >= winrect.left && x < winrect.left + borderWidth) + { + return HTLEFT; + } + else if (x < winrect.right && x >= winrect.right - borderWidth) + { + return HTRIGHT; + } + else if (y >= winrect.top && y < winrect.top + borderWidth) + { + return HTTOP; + } + else if (y < winrect.bottom && y >= winrect.bottom - borderWidth) + { + return HTBOTTOM; + } + else + { + return 0; + } } static bool isMaxWin(QWindow* win) { - return win->windowState() == Qt::WindowMaximized; + return win->windowState() == Qt::WindowMaximized; } static bool isFullWin(QQuickView* win) { - return win->windowState() == Qt::WindowFullScreen; + return win->windowState() == Qt::WindowFullScreen; } class TaoFrameLessViewPrivate { public: - bool m_firstRun = true; - bool m_isMax = false; - bool m_isFull = false; - QQuickItem* m_titleItem = nullptr; - HMENU mMenuHandler = NULL; - bool borderless = true; // is the window currently borderless - bool borderless_resize = true; // should the window allow resizing by dragging the borders while borderless - bool borderless_drag = true; // should the window allow moving my dragging the client area - bool borderless_shadow = true; // should the window display a native aero shadow while borderless - void setBorderLess(HWND handle, bool enabled) - { - auto newStyle = enabled ? selectBorderLessStyle() : Style::windowed; - auto oldStyle = static_cast