From 3815684039525a1e41d2dbd3ed862c6421b59776 Mon Sep 17 00:00:00 2001 From: Krad Date: Thu, 16 Dec 2021 17:21:45 +0800 Subject: [PATCH] Error handle --- src/errorhandle/GUIErrorBase.h | 2 +- src/errorhandle/GUIErrorFactory.cpp | 28 +++++++++++++++++++++++++ src/errorhandle/GUIErrorFactory.h | 15 ++++++++++++++ src/errorhandle/GUIErrorHandle.cpp | 25 ++++++++++++++++++++++ src/errorhandle/GUIErrorHandle.h | 20 ++++++++++++++++++ src/errorhandle/GUIErrorLE.cpp | 21 +++++++++++++++++++ src/errorhandle/GUIErrorLE.h | 32 +++++++++++++++++++++++++++++ 7 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 src/errorhandle/GUIErrorFactory.cpp create mode 100644 src/errorhandle/GUIErrorFactory.h create mode 100644 src/errorhandle/GUIErrorHandle.cpp create mode 100644 src/errorhandle/GUIErrorHandle.h create mode 100644 src/errorhandle/GUIErrorLE.cpp create mode 100644 src/errorhandle/GUIErrorLE.h diff --git a/src/errorhandle/GUIErrorBase.h b/src/errorhandle/GUIErrorBase.h index 0175971..d77a704 100644 --- a/src/errorhandle/GUIErrorBase.h +++ b/src/errorhandle/GUIErrorBase.h @@ -25,7 +25,7 @@ public: * @param parent The parent widget of error handling */ void setErrorHandleParent(QWidget* parent){ - this->parent = parent; + if (parent)this->parent = parent; }; /** diff --git a/src/errorhandle/GUIErrorFactory.cpp b/src/errorhandle/GUIErrorFactory.cpp new file mode 100644 index 0000000..650f405 --- /dev/null +++ b/src/errorhandle/GUIErrorFactory.cpp @@ -0,0 +1,28 @@ +// +// Created by Krad on 2021/12/16. +// + +#include "GUIErrorFactory.h" +#include "GUIErrorLE.h" + +GUIErrorBase *GUIErrorFactory::getError(const QString &errorMsg) { + + if (errorMsg.isEmpty()) return new GUIErrorBase; + //新模式,分级 + if (errorMsg[1] == ' ') { + char c = errorMsg[0].toLower().toLatin1(); + switch (c) { + //WARN 级别 + case 'w': { + return nullptr; + } + //ERROR 级别 + case 'e': + default: + auto error = new GUIErrorLE; + error->parse(errorMsg); + return error; + } + } + return new GUIErrorLE; +} diff --git a/src/errorhandle/GUIErrorFactory.h b/src/errorhandle/GUIErrorFactory.h new file mode 100644 index 0000000..b475450 --- /dev/null +++ b/src/errorhandle/GUIErrorFactory.h @@ -0,0 +1,15 @@ +// +// Created by Krad on 2021/12/16. +// + +#ifndef GUI_GUIERRORFACTORY_H +#define GUI_GUIERRORFACTORY_H + +#include "GUIErrorBase.h" +class GUIErrorFactory { +public: + static GUIErrorBase* getError(const QString& errorMsg); +}; + + +#endif //GUI_GUIERRORFACTORY_H diff --git a/src/errorhandle/GUIErrorHandle.cpp b/src/errorhandle/GUIErrorHandle.cpp new file mode 100644 index 0000000..de49604 --- /dev/null +++ b/src/errorhandle/GUIErrorHandle.cpp @@ -0,0 +1,25 @@ +// +// Created by Krad on 2021/12/16. +// + +#include "GUIErrorHandle.h" +#include "GUIErrorFactory.h" +#include "event/EventCenter.h" +#include +void GUIErrorHandle::init() { + connect(EventCenter::Default(),&EventCenter::GUIErrorRaise,[=](QObject*, QObject* msg){ + QString str; + if (msg) { + str = *(QString *) msg; + } else { + str = "Something went error!"; + } + auto error = GUIErrorFactory::getError(str); +// if (qApp->activeModalWidget()){ +// error->setErrorHandleParent(qApp->activeModalWidget()); +// } else{ +// error->setErrorHandleParent(qApp->activeWindow()); +// } + error->handle(); + }); +} diff --git a/src/errorhandle/GUIErrorHandle.h b/src/errorhandle/GUIErrorHandle.h new file mode 100644 index 0000000..bd931f3 --- /dev/null +++ b/src/errorhandle/GUIErrorHandle.h @@ -0,0 +1,20 @@ +// +// Created by Krad on 2021/12/16. +// + +#ifndef GUI_GUIERRORHANDLE_H +#define GUI_GUIERRORHANDLE_H + +#include +class GUIErrorHandle: public QObject { + Q_OBJECT +public: + static GUIErrorHandle* Default(){ + static GUIErrorHandle handle; + return &handle; + } + void init(); +}; + + +#endif //GUI_GUIERRORHANDLE_H diff --git a/src/errorhandle/GUIErrorLE.cpp b/src/errorhandle/GUIErrorLE.cpp new file mode 100644 index 0000000..76b775a --- /dev/null +++ b/src/errorhandle/GUIErrorLE.cpp @@ -0,0 +1,21 @@ +// +// Created by Krad on 2021/12/16. +// + +#include "GUIErrorLE.h" +#include "event/EventCenter.h" +#include +#include + +void GUIErrorLE::parse(const QString &errorMsg) { + if (errorMsg.isEmpty()) return; + if (errorMsg[0] == 'e' && errorMsg[1] == ' ') + { + errorMessage = errorMsg.right(errorMsg.length()-2); + } +} + +void GUIErrorLE::handle() { + EventCenter::Default()->triggerEvent(DeviceErrorRaise, nullptr,(QObject*)&errorMessage); +} + diff --git a/src/errorhandle/GUIErrorLE.h b/src/errorhandle/GUIErrorLE.h new file mode 100644 index 0000000..58afd97 --- /dev/null +++ b/src/errorhandle/GUIErrorLE.h @@ -0,0 +1,32 @@ +// +// Created by Krad on 2021/12/16. +// + +#ifndef GUI_GUIERRORLE_H +#define GUI_GUIERRORLE_H + +#include "GUIErrorBase.h" +/** + * GUIError of Level Error + */ +class GUIErrorLE: public GUIErrorBase { +public: + GUIErrorLE()= default; + + /** + * parse error object from error msg + * @param errorMsg + */ + void parse(const QString& errorMsg) override ; + + /** + * handle error, should be implement in sub class + */ + void handle() override; + +private: + QString errorMessage = "Unknown error!"; +}; + + +#endif //GUI_GUIERRORLE_H