Error handle
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
28
src/errorhandle/GUIErrorFactory.cpp
Normal file
28
src/errorhandle/GUIErrorFactory.cpp
Normal file
@@ -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;
|
||||
}
|
||||
15
src/errorhandle/GUIErrorFactory.h
Normal file
15
src/errorhandle/GUIErrorFactory.h
Normal file
@@ -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
|
||||
25
src/errorhandle/GUIErrorHandle.cpp
Normal file
25
src/errorhandle/GUIErrorHandle.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by Krad on 2021/12/16.
|
||||
//
|
||||
|
||||
#include "GUIErrorHandle.h"
|
||||
#include "GUIErrorFactory.h"
|
||||
#include "event/EventCenter.h"
|
||||
#include <QApplication>
|
||||
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();
|
||||
});
|
||||
}
|
||||
20
src/errorhandle/GUIErrorHandle.h
Normal file
20
src/errorhandle/GUIErrorHandle.h
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by Krad on 2021/12/16.
|
||||
//
|
||||
|
||||
#ifndef GUI_GUIERRORHANDLE_H
|
||||
#define GUI_GUIERRORHANDLE_H
|
||||
|
||||
#include <QObject>
|
||||
class GUIErrorHandle: public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
static GUIErrorHandle* Default(){
|
||||
static GUIErrorHandle handle;
|
||||
return &handle;
|
||||
}
|
||||
void init();
|
||||
};
|
||||
|
||||
|
||||
#endif //GUI_GUIERRORHANDLE_H
|
||||
21
src/errorhandle/GUIErrorLE.cpp
Normal file
21
src/errorhandle/GUIErrorLE.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by Krad on 2021/12/16.
|
||||
//
|
||||
|
||||
#include "GUIErrorLE.h"
|
||||
#include "event/EventCenter.h"
|
||||
#include <qDebug>
|
||||
#include <QApplication>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
32
src/errorhandle/GUIErrorLE.h
Normal file
32
src/errorhandle/GUIErrorLE.h
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user