#include "FITKAppFramework.h" #include "FITKGlobalData.h" #include "FITKComponents.h" #include "FITKAbstractMainWinGenerator.h" #include "FITKAbstractGlobalDataFactory.h" #include "FITKAbstractAutoSaver.h" #include "FITKComponentFactory.h" #include "FITKSignalTransfer.h" #include "FITKGlobalEventFilter.h" #include "FITKAppSettings.h" #include "FITKCommandLineHandler.h" #include "FITKAbstractPythonRegister.h" #include "FITKMessage.h" #include "FITKCopyrightDialog.h" #include "FITKProgramTaskManager.h" #include "FITKWorkBenchHandler.h" #include "FITKAbstractSysChecker.h" #include "FITK_Kernel/FITKCore/FITKThreadPool.h" #include "FITK_Kernel/FITKCore/FITKDataRepo.h" #include #include namespace AppFrame { FITKApplication::FITKApplication(int &argc, char **argv) :QApplication(argc, argv) { //创建golbalData _golbalData = new FITKGlobalData; _components = new FITKCmponents; _signalTransfer = new FITKSignalTransfer(this); _programTaskManager = new FITKProgramTaskManeger; Core::FITKThreadPool::getInstance()->init(_signalTransfer); //全局事件捕获添加 _globalEvent = new FITKGlobalEventFilter(); this->installEventFilter(_globalEvent); this->applicationDirPath(); } FITKApplication::~FITKApplication() { Core::FITKThreadPool::getInstance()->wait(); if (_programTaskManager) delete _programTaskManager; if (_signalTransfer) delete _signalTransfer; if (_golbalData) delete _golbalData; if (_components) delete _components; if (_gDataFactory) delete _gDataFactory; if (_componentsFactory) delete _componentsFactory; if (_mwGenerator) delete _mwGenerator; if (_globalEvent)delete _globalEvent; if (_commandLineHandler) delete _commandLineHandler; if (_workbenchHandler) delete _workbenchHandler; if (_settings) delete _settings; if (_pyReg) delete _pyReg; if (_autoSaver) delete _autoSaver; } void FITKApplication::checkSystem(FITKAbstractSysChecker* checker) { if (checker == nullptr) return; QStringList error = checker->check(); _passSystemCheck = error.isEmpty(); delete checker; //弹出对话框 if(!_passSystemCheck) QMessageBox::critical(nullptr, "Error", error.join("\n")); } int FITKApplication::exec() { if (!_passSystemCheck) return -2; //开始事件循环 if (!init()) return -1; int exitcode = 0; //无界面 if (_commandLineHandler && _commandLineHandler->isNoGUI()) { exitcode = -1; } //有界面 else { // this->showCopyrightDlg(); //启动自动保存 if (_autoSaver) _autoSaver->start(); if(exitcode >= 0) exitcode = QApplication::exec(); } //停止自动保存 if (_autoSaver) { _autoSaver->stop(); _autoSaver->finalize(); } //写出配置文件,读取在init函数实现 if (_settings) _settings->write(); //workbench保存文件 if (_workbenchHandler && _workbenchHandler->workingInWorkBench()) _workbenchHandler->execOutput(); //尝试释放主界面 if (_golbalData && _golbalData->getMainWindow()) { QWidget* mw = _golbalData->getMainWindow(); delete mw; } return exitcode; } void FITKApplication::regMainWindowGenerator(FITKAbstractMainwindowGenerator* generator) { //注册主界面生成器 _mwGenerator = generator; } void FITKApplication::regGlobalDataFactory(FITKAbstractGlobalDataFactory* fac) { _gDataFactory = fac; } void FITKApplication::regCommandLineHandler(FITKCommandLineHandler* handler) { _commandLineHandler = handler; } AppFrame::FITKCommandLineHandler* FITKApplication::getCommandLineHandler() { return _commandLineHandler; } void FITKApplication::regAppSettings(FITKAppSettings* settings) { _settings = settings; } FITKAppSettings* FITKApplication::getAppSettings() { return _settings; } FITKGlobalData* FITKApplication::getGlobalData() { return _golbalData; } void FITKApplication::regComponentsFactory(FITKComponentFactory* fac) { _componentsFactory = fac; } void FITKApplication::regPythonRegister(FITKAbstractPythonRegister* pyreg) { if (pyreg == nullptr) return; //已经存在则先删除 if (_pyReg != nullptr) delete _pyReg; _pyReg = pyreg; } FITKCmponents* FITKApplication::getComponents() { return _components; } FITKSignalTransfer* FITKApplication::getSignalTransfer() { return _signalTransfer; } FITKProgramTaskManeger* FITKApplication::getProgramTaskManager() { return _programTaskManager; } void FITKApplication::addGolbalSignalProcesser(QObject* obj) { if (_signalTransfer == nullptr) return; _signalTransfer->addSignalProcesser(obj); } void FITKApplication::addEventFilter(FITKAbstractEventFilter* filter) { if (_globalEvent) _globalEvent->addEventFilter(filter); } void FITKApplication::regAutoSaver(FITKAbstractAutoSaver* saver) { _autoSaver = saver; } AppFrame::FITKAbstractAutoSaver* FITKApplication::getAutoSaver() { return _autoSaver; } bool FITKApplication::init() { if (!_passSystemCheck) return false; static bool Inited = false; if (Inited) return true; //读取配置文件 if (_settings) _settings->read(); //错误判断 if (!_golbalData || !_gDataFactory ||!_componentsFactory) return false; //生成全局数据 _gDataFactory->createData(_golbalData); //加载qss if (_mwGenerator) { _mwGenerator->loadQssStyle(); _mwGenerator->loadTranslatorQmFiles(); } //加载全部组件 _componentsFactory->create(); //无界面 if (_commandLineHandler && _commandLineHandler->isNoGUI()) { this->execCommand(); return true; } //有界面显示 if (!_mwGenerator) return false; _mwGenerator->showWelcomePage(); if(!_mwGenerator->showPreSettingPage()) return false; //生成主界面 QWidget* w = _mwGenerator->genMainWindow(); if (!w) return false; //最大化窗口 if (_mwGenerator->showMaximize()) w->showMaximized(); else w->showNormal(); //存储主界面 _golbalData->setMainWindow(w); //注册Python接口 if (_pyReg) _pyReg->registWapper(); //执行命令行处理 this->execCommand(); Inited = true; return true; } AppFrame::FITKGlobalEventFilter* FITKApplication::getGlobalEventFilter() { return _globalEvent; } void FITKApplication::showCopyrightDlg() { FITKCopyRightDialog* dlg = new FITKCopyRightDialog(_golbalData->getMainWindow()); dlg->show(); } void FITKApplication::resetDataObjects(const QList& savedIDs) { if (_golbalData == nullptr) return; QList ids = _golbalData->getGlobalDataIDs(); ids.append(savedIDs); FITKDATAREPO->resetRepo(ids); } bool FITKApplication::workingInWorkBench() { return _workbenchHandler == nullptr? false: _workbenchHandler->workingInWorkBench(); } void FITKApplication::regWorkBenchHandler(FITKWorkBenchHandler* handler) { _workbenchHandler = handler; } AppFrame::FITKWorkBenchHandler* FITKApplication::getWorkBenchHandler() { return _workbenchHandler; } void FITKApplication::execCommand() { //输出提示 QString appname = QCoreApplication::applicationName(); AppFrame::FITKMessageNormal(appname + " initialized !"); //执行命令行命令 if(_commandLineHandler) _commandLineHandler->exec(); if (_workbenchHandler && _workbenchHandler->workingInWorkBench()) _workbenchHandler->execHandler(); } }