From b2471bcf0deb300f0c1c0bb174d756ddc047a29c Mon Sep 17 00:00:00 2001 From: zhuzichu Date: Wed, 13 Sep 2023 15:11:22 +0800 Subject: [PATCH] update --- example/qml-Qt6/page/T_Captcha.qml | 11 +++++-- example/qml/page/T_Captcha.qml | 11 +++++-- src/FluApp.cpp | 17 ++--------- src/FluApp.h | 11 +++---- src/FluCaptcha.cpp | 11 ++++--- src/FluCaptcha.h | 4 ++- src/FluColorSet.cpp | 5 +-- src/FluColors.cpp | 14 +-------- src/FluColors.h | 9 ++---- src/FluEventBus.cpp | 27 +++------------- src/FluEventBus.h | 11 +++---- src/FluHttp.cpp | 12 ++------ src/FluHttpInterceptor.cpp | 5 +-- src/FluHttpInterceptor.h | 3 -- src/FluRegister.cpp | 4 +-- src/FluTextStyle.cpp | 15 +-------- src/FluTextStyle.h | 9 ++---- src/FluTheme.cpp | 27 ++++------------ src/FluTheme.h | 13 +++----- src/FluTools.cpp | 29 ++++-------------- src/FluTools.h | 37 ++-------------------- src/FluViewModel.cpp | 49 ++++++++---------------------- src/FluViewModel.h | 8 ++--- src/FluWatermark.cpp | 6 ++-- src/MainThread.cpp | 19 ++++-------- src/MainThread.h | 5 ++- src/QRCode.cpp | 6 ++-- src/QRCode.h | 1 - src/Screenshot.cpp | 14 +++------ src/Screenshot.h | 2 -- src/WindowHelper.cpp | 5 +-- src/WindowHelper.h | 13 -------- src/singleton.h | 47 ++++++++++++++++++++++++++++ 33 files changed, 155 insertions(+), 305 deletions(-) create mode 100644 src/singleton.h diff --git a/example/qml-Qt6/page/T_Captcha.qml b/example/qml-Qt6/page/T_Captcha.qml index 8ac59e75..f8068e71 100644 --- a/example/qml-Qt6/page/T_Captcha.qml +++ b/example/qml-Qt6/page/T_Captcha.qml @@ -9,10 +9,10 @@ FluScrollablePage{ title:"Captcha" - FluCaptcha{ id:captcha Layout.topMargin: 20 + ignoreCase:switch_case.checked MouseArea{ anchors.fill: parent cursorShape: Qt.PointingHandCursor @@ -30,6 +30,13 @@ FluScrollablePage{ } } + FluToggleSwitch{ + id:switch_case + text:"Ignore Case" + checked: true + Layout.topMargin: 10 + } + RowLayout{ spacing: 10 Layout.topMargin: 10 @@ -49,6 +56,4 @@ FluScrollablePage{ } } } - - } diff --git a/example/qml/page/T_Captcha.qml b/example/qml/page/T_Captcha.qml index 45797105..6b48a1ac 100644 --- a/example/qml/page/T_Captcha.qml +++ b/example/qml/page/T_Captcha.qml @@ -10,10 +10,10 @@ FluScrollablePage{ title:"Captcha" - FluCaptcha{ id:captcha Layout.topMargin: 20 + ignoreCase:switch_case.checked MouseArea{ anchors.fill: parent cursorShape: Qt.PointingHandCursor @@ -31,6 +31,13 @@ FluScrollablePage{ } } + FluToggleSwitch{ + id:switch_case + text:"Ignore Case" + checked: true + Layout.topMargin: 10 + } + RowLayout{ spacing: 10 Layout.topMargin: 10 @@ -50,6 +57,4 @@ FluScrollablePage{ } } } - - } diff --git a/src/FluApp.cpp b/src/FluApp.cpp index 8dece17a..b95b78ab 100644 --- a/src/FluApp.cpp +++ b/src/FluApp.cpp @@ -10,19 +10,7 @@ #include #include "Def.h" -FluApp* FluApp::m_instance = nullptr; - -FluApp *FluApp::getInstance() -{ - if(FluApp::m_instance == nullptr){ - FluApp::m_instance = new FluApp; - } - return FluApp::m_instance; -} - -FluApp::FluApp(QObject *parent) - : QObject{parent} -{ +FluApp::FluApp(QObject *parent):QObject{parent}{ httpInterceptor(nullptr); } @@ -83,8 +71,7 @@ void FluApp::navigate(const QString& route,const QJsonObject& argument,FluRegist view->setColor(QColor(Qt::transparent)); } -QJsonArray FluApp::awesomelist(const QString& keyword) -{ +QJsonArray FluApp::awesomelist(const QString& keyword){ QJsonArray arr; QMetaEnum enumType = Fluent_Awesome::staticMetaObject.enumerator(Fluent_Awesome::staticMetaObject.indexOfEnumerator("Fluent_AwesomeType")); for(int i=0; i < enumType.keyCount(); ++i){ diff --git a/src/FluApp.h b/src/FluApp.h index cfb5cb54..a5122bcd 100644 --- a/src/FluApp.h +++ b/src/FluApp.h @@ -11,6 +11,7 @@ #include "FluRegister.h" #include "FluHttpInterceptor.h" #include "stdafx.h" +#include "singleton.h" /** * @brief The FluApp class @@ -25,13 +26,10 @@ class FluApp : public QObject QML_SINGLETON private: explicit FluApp(QObject *parent = nullptr); -public: ~FluApp(); - static FluApp *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) - { - return getInstance(); - } - static FluApp *getInstance(); +public: + SINGLETONG(FluApp) + static FluApp *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} Q_INVOKABLE void run(); Q_INVOKABLE void navigate(const QString& route,const QJsonObject& argument = {},FluRegister* fluRegister = nullptr); Q_INVOKABLE void init(QQuickWindow *window); @@ -41,7 +39,6 @@ public: public: QMap wnds; private: - static FluApp* m_instance; QWindow *appWindow; }; diff --git a/src/FluCaptcha.cpp b/src/FluCaptcha.cpp index 9910802a..7969010b 100644 --- a/src/FluCaptcha.cpp +++ b/src/FluCaptcha.cpp @@ -5,9 +5,8 @@ #include #include -FluCaptcha::FluCaptcha(QQuickItem *parent) - : QQuickPaintedItem(parent) -{ +FluCaptcha::FluCaptcha(QQuickItem *parent):QQuickPaintedItem(parent){ + ignoreCase(true); QFont fontStype; fontStype.setPixelSize(28); fontStype.setBold(true); @@ -17,8 +16,7 @@ FluCaptcha::FluCaptcha(QQuickItem *parent) refresh(); } -void FluCaptcha::paint(QPainter* painter) -{ +void FluCaptcha::paint(QPainter* painter){ painter->save(); painter->fillRect(boundingRect().toRect(),QColor(255,255,255,255)); QPen pen; @@ -72,5 +70,8 @@ void FluCaptcha::refresh(){ } bool FluCaptcha::verify(const QString& code){ + if(_ignoreCase){ + return this->_code.toUpper() == code.toUpper(); + } return this->_code == code; } diff --git a/src/FluCaptcha.h b/src/FluCaptcha.h index a309d79d..932b0a60 100644 --- a/src/FluCaptcha.h +++ b/src/FluCaptcha.h @@ -10,15 +10,17 @@ class FluCaptcha : public QQuickPaintedItem { Q_OBJECT Q_PROPERTY_AUTO(QFont,font); + Q_PROPERTY_AUTO(bool,ignoreCase); QML_NAMED_ELEMENT(FluCaptcha) private: int _generaNumber(int number); - QString _code; public: explicit FluCaptcha(QQuickItem *parent = nullptr); void paint(QPainter* painter) override; Q_INVOKABLE void refresh(); Q_INVOKABLE bool verify(const QString& code); +private: + QString _code; }; #endif // FLUCAPTCHA_H diff --git a/src/FluColorSet.cpp b/src/FluColorSet.cpp index ebe9771c..be41fa50 100644 --- a/src/FluColorSet.cpp +++ b/src/FluColorSet.cpp @@ -1,7 +1,4 @@ #include "FluColorSet.h" -FluColorSet::FluColorSet(QObject *parent) - : QObject{parent} -{ - +FluColorSet::FluColorSet(QObject *parent):QObject{parent}{ } diff --git a/src/FluColors.cpp b/src/FluColors.cpp index 41fb9399..32e62253 100644 --- a/src/FluColors.cpp +++ b/src/FluColors.cpp @@ -1,18 +1,6 @@ #include "FluColors.h" -FluColors* FluColors::m_instance = nullptr; - -FluColors *FluColors::getInstance() -{ - if(FluColors::m_instance == nullptr){ - FluColors::m_instance = new FluColors; - } - return FluColors::m_instance; -} - -FluColors::FluColors(QObject *parent) - : QObject{parent} -{ +FluColors::FluColors(QObject *parent):QObject{parent}{ Transparent("#00000000"); Black("#000000"); White("#ffffff"); diff --git a/src/FluColors.h b/src/FluColors.h index d70f7f31..052cabea 100644 --- a/src/FluColors.h +++ b/src/FluColors.h @@ -5,6 +5,7 @@ #include #include "FluColorSet.h" #include "stdafx.h" +#include "singleton.h" /** * @brief The FluColors class @@ -49,13 +50,9 @@ class FluColors : public QObject QML_SINGLETON private: explicit FluColors(QObject *parent = nullptr); - static FluColors* m_instance; public: - static FluColors *getInstance(); - static FluColors *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) - { - return getInstance(); - } + SINGLETONG(FluColors) + static FluColors *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} }; #endif // FLUCOLORS_H diff --git a/src/FluEventBus.cpp b/src/FluEventBus.cpp index 2bede5f9..e9472110 100644 --- a/src/FluEventBus.cpp +++ b/src/FluEventBus.cpp @@ -1,38 +1,21 @@ #include "FluEventBus.h" -FluEventBus* FluEventBus::m_instance = nullptr; - -FluEvent::FluEvent(QObject *parent) - : QObject{parent} -{ - +FluEvent::FluEvent(QObject *parent):QObject{parent}{ } -FluEventBus *FluEventBus::getInstance() -{ - if(FluEventBus::m_instance == nullptr){ - FluEventBus::m_instance = new FluEventBus; - } - return FluEventBus::m_instance; -} - -FluEventBus::FluEventBus(QObject *parent) - : QObject{parent} -{ - +FluEventBus::FluEventBus(QObject *parent):QObject{parent}{ } void FluEventBus::registerEvent(FluEvent* event){ - eventData.append(event); + _eventData.append(event); } - void FluEventBus::unRegisterEvent(FluEvent* event){ - eventData.removeOne(event); + _eventData.removeOne(event); } void FluEventBus::post(const QString& name,const QMap& data){ - foreach (auto event, eventData) { + foreach (auto event, _eventData) { if(event->name()==name){ Q_EMIT event->triggered(data); } diff --git a/src/FluEventBus.h b/src/FluEventBus.h index 2ea6038e..8d98799c 100644 --- a/src/FluEventBus.h +++ b/src/FluEventBus.h @@ -4,6 +4,7 @@ #include #include #include "stdafx.h" +#include "singleton.h" class FluEvent : public QObject{ Q_OBJECT @@ -20,19 +21,15 @@ class FluEventBus : public QObject QML_NAMED_ELEMENT(FluEventBus) QML_SINGLETON private: - static FluEventBus* m_instance; explicit FluEventBus(QObject *parent = nullptr); public: - static FluEventBus *getInstance(); - static FluEventBus *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) - { - return getInstance(); - } + SINGLETONG(FluEventBus) + static FluEventBus *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} Q_INVOKABLE void registerEvent(FluEvent* event); Q_INVOKABLE void unRegisterEvent(FluEvent* event); Q_INVOKABLE void post(const QString& name,const QMap& params = {}); private: - QList eventData; + QList _eventData; }; #endif // FLUEVENTBUS_H diff --git a/src/FluHttp.cpp b/src/FluHttp.cpp index 22ab5545..1103c3bb 100644 --- a/src/FluHttp.cpp +++ b/src/FluHttp.cpp @@ -14,9 +14,7 @@ #include "FluApp.h" #include "FluTools.h" -HttpRequest::HttpRequest(QObject *parent) - : QObject{parent} -{ +HttpRequest::HttpRequest(QObject *parent):QObject{parent}{ } QMap HttpRequest::toMap(){ @@ -55,14 +53,10 @@ QString HttpRequest::httpId(){ return FluTools::getInstance()->sha256(QJsonDocument::fromVariant(QVariant(toMap())).toJson(QJsonDocument::Compact)); } -HttpCallable::HttpCallable(QObject *parent) - : QObject{parent} -{ +HttpCallable::HttpCallable(QObject *parent):QObject{parent}{ } -FluHttp::FluHttp(QObject *parent) - : QObject{parent} -{ +FluHttp::FluHttp(QObject *parent):QObject{parent}{ retry(3); timeout(15000); cacheMode(FluHttpType::CacheMode::NoCache); diff --git a/src/FluHttpInterceptor.cpp b/src/FluHttpInterceptor.cpp index 38b8a7a4..3f6cb79b 100644 --- a/src/FluHttpInterceptor.cpp +++ b/src/FluHttpInterceptor.cpp @@ -1,7 +1,4 @@ #include "FluHttpInterceptor.h" -FluHttpInterceptor::FluHttpInterceptor(QObject *parent) - : QObject{parent} -{ - +FluHttpInterceptor::FluHttpInterceptor(QObject *parent):QObject{parent}{ } diff --git a/src/FluHttpInterceptor.h b/src/FluHttpInterceptor.h index dcef4b60..67e50bd7 100644 --- a/src/FluHttpInterceptor.h +++ b/src/FluHttpInterceptor.h @@ -10,9 +10,6 @@ class FluHttpInterceptor : public QObject QML_NAMED_ELEMENT(FluHttpInterceptor) public: explicit FluHttpInterceptor(QObject *parent = nullptr); - -signals: - }; #endif // FLUHTTPINTERCEPTOR_H diff --git a/src/FluRegister.cpp b/src/FluRegister.cpp index 888cc9a4..bedf4c0f 100644 --- a/src/FluRegister.cpp +++ b/src/FluRegister.cpp @@ -3,9 +3,7 @@ #include "FluApp.h" #include -FluRegister::FluRegister(QObject *parent) - : QObject{parent} -{ +FluRegister::FluRegister(QObject *parent):QObject{parent}{ from(nullptr); to(nullptr); path(""); diff --git a/src/FluTextStyle.cpp b/src/FluTextStyle.cpp index fa35cc84..46dadf94 100644 --- a/src/FluTextStyle.cpp +++ b/src/FluTextStyle.cpp @@ -1,19 +1,6 @@ #include "FluTextStyle.h" -FluTextStyle* FluTextStyle::m_instance = nullptr; - -FluTextStyle *FluTextStyle::getInstance() -{ - if(FluTextStyle::m_instance == nullptr){ - FluTextStyle::m_instance = new FluTextStyle; - } - return FluTextStyle::m_instance; -} - - -FluTextStyle::FluTextStyle(QObject *parent) - : QObject{parent} -{ +FluTextStyle::FluTextStyle(QObject *parent):QObject{parent}{ QFont caption; caption.setPixelSize(12); Caption(caption); diff --git a/src/FluTextStyle.h b/src/FluTextStyle.h index 7a8524e0..c266a786 100644 --- a/src/FluTextStyle.h +++ b/src/FluTextStyle.h @@ -5,6 +5,7 @@ #include #include #include "stdafx.h" +#include "singleton.h" class FluTextStyle : public QObject { @@ -21,13 +22,9 @@ public: QML_SINGLETON private: explicit FluTextStyle(QObject *parent = nullptr); - static FluTextStyle* m_instance; public: - static FluTextStyle *getInstance(); - static FluTextStyle *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) - { - return getInstance(); - } + SINGLETONG(FluTextStyle) + static FluTextStyle *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} }; #endif // FLUTEXTSTYLE_H diff --git a/src/FluTheme.cpp b/src/FluTheme.cpp index 343f6662..2c3ec38c 100644 --- a/src/FluTheme.cpp +++ b/src/FluTheme.cpp @@ -1,7 +1,6 @@ #include "FluTheme.h" -#include "Def.h" -#include "FluColors.h" +#include #if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) #include #elif ((QT_VERSION >= QT_VERSION_CHECK(6, 2, 1))) @@ -10,22 +9,10 @@ #else #include #endif +#include "Def.h" +#include "FluColors.h" -#include - -FluTheme* FluTheme::m_instance = nullptr; - -FluTheme *FluTheme::getInstance() -{ - if(FluTheme::m_instance == nullptr){ - FluTheme::m_instance = new FluTheme; - } - return FluTheme::m_instance; -} - -FluTheme::FluTheme(QObject *parent) - : QObject{parent} -{ +FluTheme::FluTheme(QObject *parent):QObject{parent}{ connect(this,&FluTheme::darkModeChanged,this,[=]{ Q_EMIT darkChanged(); }); @@ -37,8 +24,7 @@ FluTheme::FluTheme(QObject *parent) qApp->installEventFilter(this); } -bool FluTheme::eventFilter(QObject *obj, QEvent *event) -{ +bool FluTheme::eventFilter(QObject *obj, QEvent *event){ Q_UNUSED(obj); if (event->type() == QEvent::ApplicationPaletteChange || event->type() == QEvent::ThemeChange) { @@ -50,8 +36,7 @@ bool FluTheme::eventFilter(QObject *obj, QEvent *event) return false; } -bool FluTheme::systemDark() -{ +bool FluTheme::systemDark(){ #if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) return (QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark); #elif ((QT_VERSION >= QT_VERSION_CHECK(6, 2, 1))) diff --git a/src/FluTheme.h b/src/FluTheme.h index a7a40df0..cef3dfa2 100644 --- a/src/FluTheme.h +++ b/src/FluTheme.h @@ -5,6 +5,7 @@ #include #include "FluColorSet.h" #include "stdafx.h" +#include "singleton.h" /** * @brief The FluTheme class @@ -20,21 +21,17 @@ class FluTheme : public QObject QML_NAMED_ELEMENT(FluTheme) QML_SINGLETON private: - static FluTheme* m_instance; explicit FluTheme(QObject *parent = nullptr); + bool eventFilter(QObject *obj, QEvent *event); + bool systemDark(); public: - static FluTheme *getInstance(); - static FluTheme *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) - { - return getInstance(); - } + SINGLETONG(FluTheme) + static FluTheme *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} bool dark(); Q_SIGNAL void darkChanged(); private: bool _dark; bool _systemDark; - bool eventFilter(QObject *obj, QEvent *event); - bool systemDark(); }; #endif // FLUTHEME_H diff --git a/src/FluTools.cpp b/src/FluTools.cpp index 15f0f4d6..4f742f03 100644 --- a/src/FluTools.cpp +++ b/src/FluTools.cpp @@ -1,4 +1,5 @@ #include "FluTools.h" + #include #include #include @@ -11,21 +12,7 @@ #include #include -FluTools* FluTools::m_instance = nullptr; - -FluTools *FluTools::getInstance() -{ - if(FluTools::m_instance == nullptr){ - FluTools::m_instance = new FluTools; - } - return FluTools::m_instance; -} - - -FluTools::FluTools(QObject *parent) - : QObject{parent} -{ - +FluTools::FluTools(QObject *parent):QObject{parent}{ } void FluTools::clipText(const QString& text){ @@ -36,8 +23,7 @@ QString FluTools::uuid(){ return QUuid::createUuid().toString(); } -QString FluTools::readFile(const QString &fileName) -{ +QString FluTools::readFile(const QString &fileName){ QString content; QFile file(fileName); if (file.open(QIODevice::ReadOnly)) { @@ -132,18 +118,15 @@ QColor FluTools::colorAlpha(const QColor& color,qreal alpha){ return QColor(color.red(),color.green(),color.blue(),255*alpha); } -QString FluTools::md5(QString text) -{ +QString FluTools::md5(QString text){ return QCryptographicHash::hash(text.toUtf8(), QCryptographicHash::Md5).toHex(); } -QString FluTools::toBase64(QString text) -{ +QString FluTools::toBase64(QString text){ return text.toUtf8().toBase64(); } -QString FluTools::fromBase64(QString text) -{ +QString FluTools::fromBase64(QString text){ return QByteArray::fromBase64(text.toUtf8()); } diff --git a/src/FluTools.h b/src/FluTools.h index e5e18d46..ad370f29 100644 --- a/src/FluTools.h +++ b/src/FluTools.h @@ -5,6 +5,7 @@ #include #include #include +#include "singleton.h" /** * @brief The FluTools class @@ -12,71 +13,39 @@ class FluTools : public QObject { Q_OBJECT - QML_NAMED_ELEMENT(FluTools) QML_SINGLETON private: explicit FluTools(QObject *parent = nullptr); - static FluTools* m_instance; public: - static FluTools *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) - { - return getInstance(); - } - static FluTools *getInstance(); - + SINGLETONG(FluTools) + static FluTools *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} Q_INVOKABLE int qtMajor(); - Q_INVOKABLE int qtMinor(); - Q_INVOKABLE bool isMacos(); - Q_INVOKABLE bool isLinux(); - Q_INVOKABLE bool isWin(); - Q_INVOKABLE void clipText(const QString& text); - Q_INVOKABLE QString uuid(); - Q_INVOKABLE QString readFile(const QString& fileName); - Q_INVOKABLE void setQuitOnLastWindowClosed(bool val); - Q_INVOKABLE void setOverrideCursor(Qt::CursorShape shape); - Q_INVOKABLE void restoreOverrideCursor(); - Q_INVOKABLE QString html2PlantText(const QString& html); - Q_INVOKABLE QString toLocalPath(const QUrl& url); - Q_INVOKABLE void deleteItem(QObject *p); - Q_INVOKABLE QString getFileNameByUrl(const QUrl& url); - Q_INVOKABLE QRect getVirtualGeometry(); - Q_INVOKABLE QString getApplicationDirPath(); - Q_INVOKABLE QUrl getUrlByFilePath(const QString& path); - Q_INVOKABLE QColor colorAlpha(const QColor&,qreal alpha); - Q_INVOKABLE QString md5(QString text); - Q_INVOKABLE QString sha256(QString text); - Q_INVOKABLE QString toBase64(QString text); - Q_INVOKABLE QString fromBase64(QString text); - Q_INVOKABLE bool removeDir(QString dirPath); - Q_INVOKABLE bool removeFile(QString filePath); - Q_INVOKABLE void showFileInFolder(QString path); - }; #endif // FLUTOOLS_H diff --git a/src/FluViewModel.cpp b/src/FluViewModel.cpp index 11bf3292..d578992d 100644 --- a/src/FluViewModel.cpp +++ b/src/FluViewModel.cpp @@ -3,63 +3,44 @@ #include #include "Def.h" -ViewModelManager* ViewModelManager::m_instance = nullptr; - -ViewModelManager *ViewModelManager::getInstance() -{ - if(ViewModelManager::m_instance == nullptr){ - ViewModelManager::m_instance = new ViewModelManager; - } - return ViewModelManager::m_instance; +Model::Model(QObject *parent):QObject{parent}{ } -Model::Model(QObject *parent) - : QObject{parent} -{ - +Model::~Model(){ } -Model::~Model() -{ -} - -ViewModelManager::ViewModelManager(QObject *parent) - : QObject{parent} -{ - +ViewModelManager::ViewModelManager(QObject *parent): QObject{parent}{ } void ViewModelManager::insertViewModel(FluViewModel* value){ - m_viewmodel.append(value); + _viewmodel.append(value); } void ViewModelManager::deleteViewModel(FluViewModel* value){ - m_viewmodel.removeOne(value); + _viewmodel.removeOne(value); } QObject* ViewModelManager::getModel(const QString& key){ - return m_data.value(key); + return _data.value(key); } void ViewModelManager::insert(const QString& key,QObject* value){ - m_data.insert(key,value); + _data.insert(key,value); } bool ViewModelManager::exist(const QString& key){ - return m_data.contains(key); + return _data.contains(key); } void ViewModelManager::refreshViewModel(FluViewModel* viewModel,QString key,QVariant value){ - foreach (auto item, m_viewmodel) { + foreach (auto item, _viewmodel) { if(item->getKey() == viewModel->getKey()){ item->setProperty(key.toStdString().c_str(),value); } } } -PropertyObserver::PropertyObserver(QString name,QObject* model,QObject *parent) - : QObject{parent} -{ +PropertyObserver::PropertyObserver(QString name,QObject* model,QObject *parent):QObject{parent}{ _name = name; _model = model; _property = QQmlProperty(parent,_name); @@ -75,9 +56,7 @@ void PropertyObserver::_propertyChange(){ ViewModelManager::getInstance()->refreshViewModel((FluViewModel*)parent(),_name,value); } -FluViewModel::FluViewModel(QObject *parent) - : QObject{parent} -{ +FluViewModel::FluViewModel(QObject *parent):QObject{parent}{ ViewModelManager::getInstance()->insertViewModel(this); scope(FluViewModelType::Scope::Window); } @@ -86,12 +65,10 @@ FluViewModel::~FluViewModel(){ ViewModelManager::getInstance()->deleteViewModel(this); } -void FluViewModel::classBegin() -{ +void FluViewModel::classBegin(){ } -void FluViewModel::componentComplete() -{ +void FluViewModel::componentComplete(){ auto o = parent(); while (nullptr != o) { _window = o; diff --git a/src/FluViewModel.h b/src/FluViewModel.h index 2e6b02ea..e440d031 100644 --- a/src/FluViewModel.h +++ b/src/FluViewModel.h @@ -6,6 +6,7 @@ #include #include #include "stdafx.h" +#include "singleton.h" class Model : public QObject{ Q_OBJECT @@ -52,7 +53,7 @@ class ViewModelManager:public QObject{ private: explicit ViewModelManager(QObject *parent = nullptr); public: - static ViewModelManager *getInstance(); + SINGLETONG(ViewModelManager) bool exist(const QString& key); void insert(const QString& key,QObject* value); QObject* getModel(const QString& key); @@ -60,9 +61,8 @@ public: void deleteViewModel(FluViewModel* value); void refreshViewModel(FluViewModel* viewModel,QString key,QVariant value); private: - static ViewModelManager* m_instance; - QMap m_data; - QList m_viewmodel; + QMap _data; + QList _viewmodel; }; #endif // FLUVIEWMODEL_H diff --git a/src/FluWatermark.cpp b/src/FluWatermark.cpp index 6a50443f..363de1f5 100644 --- a/src/FluWatermark.cpp +++ b/src/FluWatermark.cpp @@ -1,7 +1,6 @@ #include "FluWatermark.h" -FluWatermark::FluWatermark(QQuickItem* parent) : QQuickPaintedItem(parent) -{ +FluWatermark::FluWatermark(QQuickItem* parent) : QQuickPaintedItem(parent){ gap(QPoint(100,100)); offset(QPoint(_gap.x()/2,_gap.y()/2)); rotate(22); @@ -16,8 +15,7 @@ FluWatermark::FluWatermark(QQuickItem* parent) : QQuickPaintedItem(parent) connect(this,&FluWatermark::textSizeChanged,this,[=]{update();}); } -void FluWatermark::paint(QPainter* painter) -{ +void FluWatermark::paint(QPainter* painter){ QFont font; font.setPixelSize(_textSize); painter->setFont(font); diff --git a/src/MainThread.cpp b/src/MainThread.cpp index cb5ae18e..33936ffe 100644 --- a/src/MainThread.cpp +++ b/src/MainThread.cpp @@ -2,17 +2,13 @@ #include #include -std::shared_ptr MainThread::createShared(QObject* bindObject) -{ +std::shared_ptr MainThread::createShared(QObject* bindObject){ return std::shared_ptr(new MainThread(bindObject), [=](QObject* mainThread) { mainThread->deleteLater(); }); } -MainThread::MainThread(QObject* bindObject) - : mBindObject(bindObject) - , mIgnoreNullObject(bindObject == nullptr) -{ +MainThread::MainThread(QObject* bindObject): _bindObject(bindObject), _ignoreNullObject(bindObject == nullptr){ qRegisterMetaType>("std::function"); auto mainUIThread = qApp->thread(); if (this->thread() != mainUIThread) @@ -21,18 +17,15 @@ MainThread::MainThread(QObject* bindObject) } } -MainThread::~MainThread() -{ +MainThread::~MainThread(){ } -void MainThread::post(std::function func) -{ +void MainThread::post(std::function func){ QMetaObject::invokeMethod(createShared().get(), "mainThreadSlot", Q_ARG(std::function, func)); } -void MainThread::mainThreadSlot(std::function func) -{ - if ((mIgnoreNullObject || mBindObject) && func) +void MainThread::mainThreadSlot(std::function func){ + if ((_ignoreNullObject || _bindObject) && func) { func(); } diff --git a/src/MainThread.h b/src/MainThread.h index e108c026..6e77eaa5 100644 --- a/src/MainThread.h +++ b/src/MainThread.h @@ -9,7 +9,6 @@ class MainThread : public QObject { Q_OBJECT public: - static void post(std::function func); ~MainThread(); private: @@ -18,7 +17,7 @@ private slots: void mainThreadSlot(std::function func); private: MainThread(QObject* bindObject = nullptr); - QPointer mBindObject; - bool mIgnoreNullObject{ false }; + QPointer _bindObject; + bool _ignoreNullObject{ false }; }; #endif // MAINTHREAD_H diff --git a/src/QRCode.cpp b/src/QRCode.cpp index 8790e3d1..e038908e 100644 --- a/src/QRCode.cpp +++ b/src/QRCode.cpp @@ -6,8 +6,7 @@ using namespace ZXing; -QRCode::QRCode(QQuickItem* parent) : QQuickPaintedItem(parent) -{ +QRCode::QRCode(QQuickItem* parent):QQuickPaintedItem(parent){ color(QColor(0,0,0,255)); bgColor(QColor(255,255,255,255)); size(100); @@ -24,8 +23,7 @@ QRCode::QRCode(QQuickItem* parent) : QQuickPaintedItem(parent) } -void QRCode::paint(QPainter* painter) -{ +void QRCode::paint(QPainter* painter){ if(_text.isEmpty()){ return; } diff --git a/src/QRCode.h b/src/QRCode.h index ea9789da..88b08bee 100644 --- a/src/QRCode.h +++ b/src/QRCode.h @@ -17,7 +17,6 @@ class QRCode : public QQuickPaintedItem public: explicit QRCode(QQuickItem *parent = nullptr); void paint(QPainter* painter) override; - }; #endif // QRCODE_H diff --git a/src/Screenshot.cpp b/src/Screenshot.cpp index 892c18dc..1343e7da 100644 --- a/src/Screenshot.cpp +++ b/src/Screenshot.cpp @@ -6,12 +6,9 @@ #include #include #include - #include "Def.h" - -Screenshot::Screenshot(QQuickItem* parent) : QQuickPaintedItem(parent) -{ +Screenshot::Screenshot(QQuickItem* parent):QQuickPaintedItem(parent){ _desktopGeometry = qApp->primaryScreen()->virtualGeometry(); maskColor(QColor(0,0,0,150)); start(QPoint(0,0)); @@ -20,8 +17,7 @@ Screenshot::Screenshot(QQuickItem* parent) : QQuickPaintedItem(parent) connect(this,&Screenshot::endChanged,this,[=]{update();}); } -void Screenshot::paint(QPainter* painter) -{ +void Screenshot::paint(QPainter* painter){ painter->save(); painter->fillRect(_desktopGeometry,_maskColor); painter->setCompositionMode(QPainter::CompositionMode_Clear); @@ -29,8 +25,7 @@ void Screenshot::paint(QPainter* painter) painter->restore(); } -ScreenshotBackground::ScreenshotBackground(QQuickItem* parent) : QQuickPaintedItem(parent) -{ +ScreenshotBackground::ScreenshotBackground(QQuickItem* parent) : QQuickPaintedItem(parent){ _devicePixelRatio = qApp->primaryScreen()->devicePixelRatio(); _desktopGeometry = qApp->primaryScreen()->virtualGeometry(); _desktopPixmap = qApp->primaryScreen()->grabWindow(0, _desktopGeometry.x(), _desktopGeometry.y(), _desktopGeometry.width(), _desktopGeometry.height()); @@ -45,8 +40,7 @@ ScreenshotBackground::ScreenshotBackground(QQuickItem* parent) : QQuickPaintedIt setHeight(h); } -void ScreenshotBackground::paint(QPainter* painter) -{ +void ScreenshotBackground::paint(QPainter* painter){ painter->save(); _sourcePixmap = _desktopPixmap.copy(); painter->drawPixmap(_desktopGeometry,_sourcePixmap); diff --git a/src/Screenshot.h b/src/Screenshot.h index 96ec7b64..f9cfb77e 100644 --- a/src/Screenshot.h +++ b/src/Screenshot.h @@ -29,7 +29,6 @@ private: QRect _captureRect; }; - class Screenshot : public QQuickPaintedItem { Q_OBJECT @@ -40,7 +39,6 @@ class Screenshot : public QQuickPaintedItem public: Screenshot(QQuickItem* parent = nullptr); void paint(QPainter* painter) override; - private: QRect _desktopGeometry; }; diff --git a/src/WindowHelper.cpp b/src/WindowHelper.cpp index baf84e72..f4b2f6c6 100644 --- a/src/WindowHelper.cpp +++ b/src/WindowHelper.cpp @@ -2,10 +2,7 @@ #include "FluRegister.h" -WindowHelper::WindowHelper(QObject *parent) - : QObject{parent} -{ - +WindowHelper::WindowHelper(QObject *parent):QObject{parent}{ } void WindowHelper::initWindow(QQuickWindow* window){ diff --git a/src/WindowHelper.h b/src/WindowHelper.h index d2596498..e169d09d 100644 --- a/src/WindowHelper.h +++ b/src/WindowHelper.h @@ -17,21 +17,8 @@ class WindowHelper : public QObject QML_NAMED_ELEMENT(WindowHelper) public: explicit WindowHelper(QObject *parent = nullptr); - - /** - * @brief initWindow FluWindow中初始化调用 - * @param window - */ Q_INVOKABLE void initWindow(QQuickWindow* window); - - /** - * @brief createRegister 创建一个FluRegsiter对象,在FluWindow中registerForWindowResult方法调用 - * @param window - * @param path - * @return - */ Q_INVOKABLE QVariant createRegister(QQuickWindow* window,const QString& path); - private: QQuickWindow* window; }; diff --git a/src/singleton.h b/src/singleton.h new file mode 100644 index 00000000..ef685f96 --- /dev/null +++ b/src/singleton.h @@ -0,0 +1,47 @@ +#ifndef SINGLETON_H +#define SINGLETON_H + +#include +#include +#include +#include + +template +class Singleton { +public: + static T* getInstance(); + + Singleton(const Singleton& other) = delete; + Singleton& operator=(const Singleton& other) = delete; + +private: + static std::mutex mutex; + static T* instance; +}; + +template +std::mutex Singleton::mutex; +template +T* Singleton::instance; +template +T* Singleton::getInstance() { + if (instance == nullptr) { + std::lock_guard locker(mutex); + if (instance == nullptr) { + instance = new T(); + } + } + return instance; +} + +#define SINGLETONG(Class) \ +private: \ + friend class Singleton; \ + friend struct QScopedPointerDeleter; \ + \ + public: \ + static Class* getInstance() { \ + return Singleton::getInstance(); \ +} + +#endif // SINGLETON_H