Error handle

This commit is contained in:
Krad
2021-12-16 17:21:45 +08:00
parent 70e18d814b
commit 3815684039
7 changed files with 142 additions and 1 deletions

View File

@@ -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;
};
/**

View 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;
}

View 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

View 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();
});
}

View 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

View 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);
}

View 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