From 09c784532a436bd64b06328a01d83668d558a404 Mon Sep 17 00:00:00 2001 From: jared Date: Fri, 13 Nov 2020 13:35:42 +0800 Subject: [PATCH] update PropertyHeader --- 3rdparty/{ => TaoCommon}/CMakeLists.txt | 2 +- 3rdparty/TaoCommon/Common/JsonSerialize.h | 78 +++++++++++++ 3rdparty/TaoCommon/Common/PropertyHelper.h | 105 +++++++++++------- 3rdparty/TaoCommon/TaoCommon.pri | 31 ++---- examples/TaoQuickShow/CMakeLists.txt | 2 +- examples/TaoQuickShow/Src/AppInfo.cpp | 80 ------------- examples/TaoQuickShow/Src/AppInfo.h | 93 ++-------------- .../Src/DeviceAddTable/DeviceAddItem.cpp | 12 +- .../Src/DeviceAddTable/DeviceAddItem.h | 80 +------------ .../Src/DeviceAddTable/DeviceAddModel.cpp | 10 +- 10 files changed, 177 insertions(+), 316 deletions(-) rename 3rdparty/{ => TaoCommon}/CMakeLists.txt (65%) create mode 100644 3rdparty/TaoCommon/Common/JsonSerialize.h diff --git a/3rdparty/CMakeLists.txt b/3rdparty/TaoCommon/CMakeLists.txt similarity index 65% rename from 3rdparty/CMakeLists.txt rename to 3rdparty/TaoCommon/CMakeLists.txt index 2e98dd1..4dcde63 100644 --- a/3rdparty/CMakeLists.txt +++ b/3rdparty/TaoCommon/CMakeLists.txt @@ -1,4 +1,4 @@ -set(TaoCommonPath ${CMAKE_CURRENT_SOURCE_DIR}/TaoCommon PARENT_SCOPE) +set(TaoCommonPath ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE) file(GLOB_RECURSE files *.h *.hpp *.cpp *.c) set(TaoCommonFiles ${files} PARENT_SCOPE) message("TaoCommonFiles ${TaoCommonFiles}") diff --git a/3rdparty/TaoCommon/Common/JsonSerialize.h b/3rdparty/TaoCommon/Common/JsonSerialize.h new file mode 100644 index 0000000..ffa2167 --- /dev/null +++ b/3rdparty/TaoCommon/Common/JsonSerialize.h @@ -0,0 +1,78 @@ +#pragma once +#include "CIMCommDef.h" + +//json ÐòÁл¯ +#include +#include +#include +#include +#include +#include + +///@brief ÐòÁл¯ +#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; + +#define JsonSerialize_End() \ + return vmap; \ + } + +///@brief ·´ÐòÁл¯ +#define JsonDeserialize_Begin(class_name) \ +public:\ + 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(); + +#define JsonPartialDeserialize_End()\ + return *this;\ + }\ + +///@brief ÐòÁл¯ÊôÐÔÓ³Éä +#define JsonPureProperty(val) \ + vmap.unite((QVariantMap)val); + +#define JsonProperty(name, val, atom_type) \ + vmap[name] = QVariant::fromValue(val);\ + if(vmap[name].isNull()) vmap.remove(name); + +#define JsonContainerProperty(name, val, atom_type)\ + {\ + QVariantList lst;\ + for(atom_type t : val)\ + {\ + lst << QVariant::fromValue(t);\ + }\ + vmap[name] = lst;\ + }\ + +#define JsonDeserializeContainerProperty(name, val, atom_type)\ + if(vmap[name].canConvert())\ + {\ + QSequentialIterable iterable = vmap[name].value();\ + Q_FOREACH(const QVariant &v, iterable) \ + {\ + val.push_back(v.value());\ + }\ + } + +#define JsonDeserializeProperty(name, val, type)\ + val = vmap[name].value(); \ No newline at end of file diff --git a/3rdparty/TaoCommon/Common/PropertyHelper.h b/3rdparty/TaoCommon/Common/PropertyHelper.h index a339ca4..5eedc28 100644 --- a/3rdparty/TaoCommon/Common/PropertyHelper.h +++ b/3rdparty/TaoCommon/Common/PropertyHelper.h @@ -1,47 +1,68 @@ #pragma once #include +#include -#define READ_PROPERTY(TYPE, NAME) \ -private: \ - Q_PROPERTY(TYPE NAME READ NAME NOTIFY NAME##Changed) \ -public: \ - const TYPE &NAME() const \ - { \ - return m_##NAME; \ - } \ - Q_SIGNAL void NAME##Changed(const TYPE &value); \ - \ -private: \ - TYPE m_##NAME; -#define READONLY_PROPERTY(TYPE, NAME) \ -private: \ - Q_PROPERTY(TYPE NAME READ NAME CONSTANT) \ -public: \ - const TYPE &NAME() const \ - { \ - return m_##NAME; \ - } \ - \ -private: \ - TYPE m_##NAME; +template +struct Compare; -#define AUTO_PROPERTY(TYPE, NAME) \ -private: \ - Q_PROPERTY(TYPE NAME READ NAME WRITE NAME NOTIFY NAME##Changed) \ -public: \ - const TYPE &NAME() const \ - { \ - return m_##NAME; \ - } \ - void NAME(const TYPE &value) \ - { \ - if (m_##NAME == value) \ - return; \ - m_##NAME = value; \ - emit NAME##Changed(value); \ - } \ - Q_SIGNAL void NAME##Changed(const TYPE &value); \ - \ -private: \ - TYPE m_##NAME; +template +struct Compare +{ + static bool isEqual(const T &t1, const T &t2) + { + return t1 == t2; + } +}; +template<> +struct Compare +{ + static bool isEqual(float f1, float f2) + { + return qFuzzyCompare(f1, f2); + } +}; +template<> +struct Compare +{ + static bool isEqual(double d1, double d2) + { + return qFuzzyCompare(d1, d2); + } +}; + +#define READ_PROPERTY(T, NAME, InitValue) \ +private: \ + Q_PROPERTY(T NAME READ NAME NOTIFY NAME##Changed) \ +public: \ + const T &NAME() const { return m_##NAME; } \ + Q_SIGNAL void NAME##Changed(const T &value); \ + \ +private: \ + T m_##NAME = InitValue; + +#define READONLY_PROPERTY(T, NAME, InitValue) \ +private: \ + Q_PROPERTY(T NAME READ NAME CONSTANT) \ +public: \ + const T &NAME() const { return m_##NAME; } \ + \ +private: \ + T m_##NAME = InitValue; + +#define AUTO_PROPERTY(T, NAME, InitValue) \ +private: \ + Q_PROPERTY(T NAME READ NAME WRITE set_##NAME NOTIFY NAME##Changed) \ +public: \ + const T &NAME() const { return m_##NAME; } \ + Q_SLOT void set_##NAME(const T &value) \ + { \ + if (Compare::isEqual(m_##NAME, value)) \ + return; \ + m_##NAME = value; \ + emit NAME##Changed(value); \ + } \ + Q_SIGNAL void NAME##Changed(const T &value); \ + \ +private: \ + T m_##NAME = InitValue; diff --git a/3rdparty/TaoCommon/TaoCommon.pri b/3rdparty/TaoCommon/TaoCommon.pri index 538258d..c1a93ad 100644 --- a/3rdparty/TaoCommon/TaoCommon.pri +++ b/3rdparty/TaoCommon/TaoCommon.pri @@ -4,31 +4,14 @@ INCLUDEPATH += $$PWD +hFile = $$files($$PWD/*.h, true) +hppFile = $$files($$PWD/*.hpp, true) +cppFile = $$files($$PWD/*.cpp, true) HEADERS += \ - $$PWD/Common/FileReadWrite.h \ - $$PWD/Common/PropertyHelper.h \ - $$PWD/Common/ObjectMap.h \ - $$PWD/QuickTool/QuickTool.h \ - $$PWD/Common/Subject.h \ - $$PWD/Common/Package.h \ - $$PWD/Frameless/TaoFrameLessView.h \ - $$PWD/Logger/LoggerTemplate.h \ - $$PWD/Logger/Logger.h \ - $$PWD/TaoModel/TaoListItemBase.h \ - $$PWD/TaoModel/TaoListModel.h \ - $$PWD/TaoModel/TaoListModelBase.hpp \ - $$PWD/Thread/ThreadCommon.h \ - $$PWD/Thread/ThreadPool.h \ - $$PWD/Thread/ThreadWorkerController.h \ - $$PWD/Trans/Trans.h \ - $$PWD/TaoCommonGlobal.h + $$hFile \ + $$hppFile + SOURCES += \ - $$PWD/Frameless/TaoFrameLessView.cpp \ - $$PWD/Logger/Logger.cpp \ - $$PWD/QuickTool/QuickTool.cpp \ - $$PWD/TaoModel/TaoListItemBase.cpp \ - $$PWD/TaoModel/TaoListModel.cpp \ - $$PWD/Thread/ThreadPool.cpp \ - $$PWD/Trans/Trans.cpp + $$cppFile diff --git a/examples/TaoQuickShow/CMakeLists.txt b/examples/TaoQuickShow/CMakeLists.txt index 2d82444..78a7689 100644 --- a/examples/TaoQuickShow/CMakeLists.txt +++ b/examples/TaoQuickShow/CMakeLists.txt @@ -13,7 +13,7 @@ aux_source_directory(Src/DeviceAddTable SRC2) include(taoVersion) add_compile_definitions(TaoCommon_NO_LIB) -add_subdirectory(${CMAKE_SOURCE_DIR}/3rdparty ${CMAKE_SOURCE_DIR}/build/3rdparty) +add_subdirectory(${CMAKE_SOURCE_DIR}/3rdparty/TaoCommon ${CMAKE_SOURCE_DIR}/build/3rdparty/TaoCommon) #message("TaoCommonPath ${TaoCommonPath}") #message("TaoCommonFiles ${TaoCommonFiles}") diff --git a/examples/TaoQuickShow/Src/AppInfo.cpp b/examples/TaoQuickShow/Src/AppInfo.cpp index a7a521f..c5a405a 100644 --- a/examples/TaoQuickShow/Src/AppInfo.cpp +++ b/examples/TaoQuickShow/Src/AppInfo.cpp @@ -21,83 +21,3 @@ void AppInfo::beforeUiReady(QQmlContext *ctx) void AppInfo::afterUiReady() {} -void AppInfo::setAppName(const QString &appName) -{ - if (m_appName == appName) - return; - - m_appName = appName; - emit appNameChanged(m_appName); -} - -void AppInfo::setAppVersion(const QString &appVersion) -{ - if (m_appVersion == appVersion) - return; - - m_appVersion = appVersion; - emit appVersionChanged(m_appVersion); -} - -void AppInfo::setLatestVersion(const QString &latestVersion) -{ - if (m_latestVersion == latestVersion) - return; - - m_latestVersion = latestVersion; - emit latestVersionChanged(m_latestVersion); -} - -void AppInfo::setBuildDateTime(const QString &buildDateTime) -{ - if (m_buildDateTime == buildDateTime) - return; - - m_buildDateTime = buildDateTime; - emit buildDateTimeChanged(m_buildDateTime); -} - -void AppInfo::setBuildRevision(const QString &buildRevision) -{ - if (m_buildRevision == buildRevision) - return; - - m_buildRevision = buildRevision; - emit buildRevisionChanged(m_buildRevision); -} - -void AppInfo::setCopyRight(const QString ©Right) -{ - if (m_copyRight == copyRight) - return; - - m_copyRight = copyRight; - emit copyRightChanged(m_copyRight); -} - -void AppInfo::setDescript(const QString &descript) -{ - if (m_descript == descript) - return; - - m_descript = descript; - emit descriptChanged(m_descript); -} - -void AppInfo::setCompilerVendor(const QString &compilerVendor) -{ - if (m_compilerVendor == compilerVendor) - return; - - m_compilerVendor = compilerVendor; - emit compilerVendorChanged(m_compilerVendor); -} - -void AppInfo::setSplashShow(bool splashShow) -{ - if (m_splashShow == splashShow) - return; - - m_splashShow = splashShow; - emit splashShowChanged(m_splashShow); -} diff --git a/examples/TaoQuickShow/Src/AppInfo.h b/examples/TaoQuickShow/Src/AppInfo.h index df3e0f4..ed5be0f 100644 --- a/examples/TaoQuickShow/Src/AppInfo.h +++ b/examples/TaoQuickShow/Src/AppInfo.h @@ -3,23 +3,21 @@ #include #include #include +#include "Common/PropertyHelper.h" class AppInfo : public QObject { Q_OBJECT - Q_PROPERTY(QString appName READ appName WRITE setAppName NOTIFY appNameChanged) - Q_PROPERTY(QString appVersion READ appVersion WRITE setAppVersion NOTIFY appVersionChanged) - Q_PROPERTY(QString latestVersion READ latestVersion WRITE setLatestVersion NOTIFY - latestVersionChanged) - Q_PROPERTY(QString buildDateTime READ buildDateTime WRITE setBuildDateTime NOTIFY - buildDateTimeChanged) - Q_PROPERTY(QString buildRevision READ buildRevision WRITE setBuildRevision NOTIFY - buildRevisionChanged) - Q_PROPERTY(QString copyRight READ copyRight WRITE setCopyRight NOTIFY copyRightChanged) - Q_PROPERTY(QString descript READ descript WRITE setDescript NOTIFY descriptChanged) - Q_PROPERTY(QString compilerVendor READ compilerVendor WRITE setCompilerVendor NOTIFY - compilerVendorChanged) - Q_PROPERTY(bool splashShow READ splashShow WRITE setSplashShow NOTIFY splashShowChanged) + AUTO_PROPERTY(QString, appName, "") + AUTO_PROPERTY(QString, appVersion, "") + AUTO_PROPERTY(QString, latestVersion, "") + AUTO_PROPERTY(QString, buildDateTime, "") + AUTO_PROPERTY(QString, buildRevision, "") + AUTO_PROPERTY(QString, copyRight, "") + AUTO_PROPERTY(QString, descript, "") + AUTO_PROPERTY(QString, compilerVendor, "") + AUTO_PROPERTY(bool, splashShow, false) + public: explicit AppInfo(QObject *parent = nullptr); @@ -28,73 +26,4 @@ public: void afterUiReady(); - const QString &appName() const { return m_appName; } - - const QString &appVersion() const { return m_appVersion; } - - const QString &latestVersion() const { return m_latestVersion; } - - const QString &buildDateTime() const { return m_buildDateTime; } - - const QString &buildRevision() const { return m_buildRevision; } - - const QString ©Right() const { return m_copyRight; } - - const QString &descript() const { return m_descript; } - - const QString &compilerVendor() const { return m_compilerVendor; } - - bool splashShow() const { return m_splashShow; } - -public slots: - - void setAppName(const QString &appName); - - void setAppVersion(const QString &appVersion); - - void setLatestVersion(const QString &latestVersion); - - void setBuildDateTime(const QString &buildDateTime); - - void setBuildRevision(const QString &buildRevision); - - void setCopyRight(const QString ©Right); - - void setDescript(const QString &descript); - - void setCompilerVendor(const QString &compilerVendor); - - void setSplashShow(bool splashShow); - -signals: - - void appNameChanged(const QString &appName); - - void appVersionChanged(const QString &appVersion); - - void latestVersionChanged(const QString &latestVersion); - - void buildDateTimeChanged(const QString &buildDateTime); - - void buildRevisionChanged(const QString &buildRevision); - - void copyRightChanged(const QString ©Right); - - void descriptChanged(const QString &descript); - - void compilerVendorChanged(const QString &compilerVendor); - - void splashShowChanged(bool splashShow); - -protected: -private: - QString m_appName; - QString m_appVersion; - QString m_latestVersion; - QString m_buildDateTime; - QString m_buildRevision; - QString m_copyRight; - QString m_descript; - QString m_compilerVendor; - bool m_splashShow = false; }; diff --git a/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddItem.cpp b/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddItem.cpp index 023a3b4..a6a872f 100644 --- a/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddItem.cpp +++ b/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddItem.cpp @@ -1,20 +1,18 @@ #include "DeviceAddItem.h" -DeviceAddItem::DeviceAddItem(QObject *parent) - : TaoListItemBase(parent) -{ -} +DeviceAddItem::DeviceAddItem(QObject *parent) : TaoListItemBase(parent) { } + +DeviceAddItem::~DeviceAddItem() { } bool DeviceAddItem::match(const QString &key) { - if (key.isEmpty()) - { + if (key.isEmpty()) { return true; } if (m_name.contains(key, Qt::CaseInsensitive)) { return true; } else if (m_address.contains(key, Qt::CaseInsensitive)) { return true; - } else if (m_modelString.contains(key, Qt::CaseInsensitive)){ + } else if (m_modelString.contains(key, Qt::CaseInsensitive)) { return true; } return false; diff --git a/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddItem.h b/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddItem.h index 8a99ad0..aa856b0 100644 --- a/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddItem.h +++ b/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddItem.h @@ -1,89 +1,21 @@ #pragma once #include "TaoModel/TaoListItemBase.h" +#include"Common/PropertyHelper.h" #include class DeviceAddItem : public TaoListItemBase { Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) - Q_PROPERTY(QString address READ address WRITE setAddress NOTIFY addressChanged) - Q_PROPERTY(QString modelString READ modelString WRITE setModelString NOTIFY modelStringChanged) - Q_PROPERTY(bool online READ online WRITE setOnline NOTIFY onlineChanged) + AUTO_PROPERTY(QString, name, "") + AUTO_PROPERTY(QString, address, "") + AUTO_PROPERTY(QString, modelString, "") + AUTO_PROPERTY(bool, online, false) public: explicit DeviceAddItem(QObject *parent = nullptr); - - const QString &name() const - { - return m_name; - } - - const QString &address() const - { - return m_address; - } - - const QString &modelString() const - { - return m_modelString; - } - - bool online() const - { - return m_online; - } + virtual ~DeviceAddItem() override; bool match(const QString &key) override; -public slots: - void setName(const QString &name) - { - if (m_name == name) - return; - - m_name = name; - emit nameChanged(m_name); - } - - void setAddress(const QString &address) - { - if (m_address == address) - return; - - m_address = address; - emit addressChanged(m_address); - } - - void setModelString(const QString &modelString) - { - if (m_modelString == modelString) - return; - - m_modelString = modelString; - emit modelStringChanged(m_modelString); - } - - void setOnline(bool online) - { - if (m_online == online) - return; - - m_online = online; - emit onlineChanged(m_online); - } - -signals: - - void nameChanged(const QString &name); - void addressChanged(const QString &address); - void modelStringChanged(const QString &modelString); - void onlineChanged(bool online); - private: - QString m_name; - QString m_address; - - QString m_modelString; - - bool m_online = false; }; diff --git a/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddModel.cpp b/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddModel.cpp index 81c1996..69691e6 100644 --- a/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddModel.cpp +++ b/examples/TaoQuickShow/Src/DeviceAddTable/DeviceAddModel.cpp @@ -154,15 +154,15 @@ void DeviceAddModel::removeRow(int row) DeviceAddItem *DeviceAddModel::genOne(uint32_t value) { auto item = new DeviceAddItem; - item->setOnline(value % 2 == 0); - item->setName(nameTemplate.arg(value)); + item->set_online(value % 2 == 0); + item->set_name(nameTemplate.arg(value)); int ip4 = value % 256; int ip3 = value / 256 % 256; int ip2 = value / 256 / 256 % 256; int ip1 = value / 256 / 256 / 256 % 256; - item->setAddress(ipTemplate.arg(ip1).arg(ip2).arg(ip3).arg(ip4)); - item->setModelString(modelTemplate.arg(value % 2 == 0 ? value : 0xffffffff - value)); + item->set_address(ipTemplate.arg(ip1).arg(ip2).arg(ip3).arg(ip4)); + item->set_modelString(modelTemplate.arg(value % 2 == 0 ? value : 0xffffffff - value)); return item; } void DeviceAddModel::doUpdateName(int row, const QString &name) @@ -174,5 +174,5 @@ void DeviceAddModel::doUpdateName(int row, const QString &name) if (n.isEmpty()) { return; } - static_cast(mDatas.at(row))->setName(name); + static_cast(mDatas.at(row))->set_name(name); }