| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "optioncore.h" |
| 6 | #include "mainframe/optiondefaultkeeper.h" |
| 7 | #include "mainframe/optiongeneralgenerator.h" |
| 8 | |
| 9 | #include "common/common.h" |
| 10 | #include "base/abstractwidget.h" |
| 11 | #include "base/abstractaction.h" |
| 12 | #include "base/abstractcentral.h" |
| 13 | #include "services/window/windowservice.h" |
| 14 | #include "services/project/projectservice.h" |
| 15 | #include "services/option/optionservice.h" |
| 16 | |
| 17 | #include "framework/listener/listener.h" |
| 18 | |
| 19 | using namespace dpfservice; |
| 20 | void OptionCore::initialize() |
| 21 | { |
| 22 | auto &ctx = dpfInstance.serviceContext(); |
| 23 | QString errStr; |
| 24 | if (!ctx.load(OptionService::name(), &errStr)) { |
| 25 | qCritical() << errStr; |
| 26 | abort(); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | bool OptionCore::start() |
| 31 | { |
| 32 | toolchains::generatGlobalFile(); |
| 33 | |
| 34 | auto optionDialog = OptionDefaultKeeper::getOptionDialog(); |
| 35 | if (!optionDialog) { |
| 36 | qCritical() << "Failed, can't init option dialog!" ; |
| 37 | abort(); |
| 38 | } |
| 39 | |
| 40 | optionDialog->setModal(true); |
| 41 | |
| 42 | auto &ctx = dpfInstance.serviceContext(); |
| 43 | WindowService *windowService = ctx.service<WindowService>(WindowService::name()); |
| 44 | OptionService *optionService = ctx.service<OptionService>(OptionService::name()); |
| 45 | if (optionService) { |
| 46 | optionService->implGenerator<OptionGeneralGenerator>(OptionGeneralGenerator::kitName()); |
| 47 | auto generator = optionService->createGenerator<OptionGeneralGenerator>(OptionGeneralGenerator::kitName()); |
| 48 | if (generator) { |
| 49 | auto pageWidget = dynamic_cast<PageWidget*>(generator->optionWidget()); |
| 50 | if (pageWidget) { |
| 51 | optionDialog->insertOptionPanel(OptionGeneralGenerator::kitName(), pageWidget); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if (windowService && windowService->addAction && windowService->addToolBarActionItem) { |
| 57 | auto actionOptions = new QAction(MWMTA_OPTIONS); |
| 58 | ActionManager::getInstance()->registerAction(actionOptions, |
| 59 | "Tools.Options" , |
| 60 | MWMTA_OPTIONS, |
| 61 | QKeySequence(Qt::Modifier::CTRL | |
| 62 | Qt::Modifier::SHIFT | |
| 63 | Qt::Key::Key_H), |
| 64 | ":/optioncore/images/setting.png" ); |
| 65 | windowService->addAction(MWM_TOOLS, new AbstractAction(actionOptions)); |
| 66 | windowService->addToolBarActionItem("Options" , actionOptions, "Option.End" ); |
| 67 | |
| 68 | QObject::connect(actionOptions, &QAction::triggered, |
| 69 | optionDialog, &QDialog::show); |
| 70 | } |
| 71 | |
| 72 | DPF_USE_NAMESPACE |
| 73 | QObject::connect(&Listener::instance(), &Listener::pluginsStarted, [=](){ |
| 74 | if (optionDialog) { |
| 75 | auto list = optionService->supportGeneratorName<OptionGenerator>(); |
| 76 | for (auto name : list) { |
| 77 | if (0 == name.compare(OptionGeneralGenerator::kitName())) |
| 78 | continue; |
| 79 | auto generator = optionService->createGenerator<OptionGenerator>(name); |
| 80 | if (generator) { |
| 81 | PageWidget *optionWidget = dynamic_cast<PageWidget*>(generator->optionWidget()); |
| 82 | if (optionWidget) { |
| 83 | optionDialog->insertOptionPanel(name, optionWidget); |
| 84 | optionWidget->readConfig(); |
| 85 | optionWidget->saveConfig(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | dpf::Plugin::ShutdownFlag OptionCore::stop() |
| 96 | { |
| 97 | delete OptionDefaultKeeper::getOptionDialog(); |
| 98 | return Sync; |
| 99 | } |
| 100 | |