#include "mainwindow.h" #include "srcedit_defaulttext.h" #include "srcedit_storyboard.h" #include "xapp.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "command_list.h" using namespace Project; using namespace Components; using namespace Core; using namespace Tools; using namespace CommandList; using namespace bridge; MainWindow::MainWindow(XApp *core, const QString &layout, QWidget *parent) : QMainWindow(parent), layout_name_store(layout), core_bind(core), sync_kernel(new StatusSyncCore(this)), present_host(new SplitFrame::ViewPresent(this)), session_service(new ViewSession(XApp::gconfig, this)), actions_stack(new QToolBar(this)), views_bar(new ViewStackedBar(present_host, this)), center_frame(new PresentContainerView(present_host)), project_present(new ProjectView(present_host, XApp::disp_core, core->docsManager())) { setMinimumSize(1000, 600); setWindowTitle(tr("提线木偶")); QApplication::instance()->installEventFilter(this); auto mbar = menuBar(); initial_menubar(mbar); setCentralWidget(present_host->bind()); addToolBar(Qt::LeftToolBarArea, views_bar); present_host->addListener(views_bar); session_service->initPresentView(center_frame, project_present, core->parseService()->errorsPresentModel()); project_present->setVisible(true); core->docsManager()->appendPresent(center_frame); session_service->viewStatesRestore(this, present_host); } MainWindow::~MainWindow() { core_bind->docsManager()->removePresent(center_frame); } SplitFrame::ViewPresent *MainWindow::bindPresent() const { return present_host; } QString MainWindow::layoutName() const { return layout_name_store; } void MainWindow::resetLayoutName(const QString &name) { layout_name_store = name; } void MainWindow::initial_menubar(QMenuBar *mbar) { // 项目菜单树 auto project = mbar->addMenu("项目"); this->build_project_menu(project); // 编辑菜单 auto edit = mbar->addMenu("编辑"); this->build_edit_menu(edit); // 视图菜单 auto view = mbar->addMenu("视图"); build_view_menu(view); // 版本管理 // 工具菜单 auto tool = mbar->addMenu("工具"); build_tools_menu(tool); // 窗口菜单 auto window = mbar->addMenu("窗口"); build_window_menu(window); // 系统 auto sys = mbar->addMenu("系统"); sys->addAction("软件激活"); sys->addAction("系统信息"); sys->addSeparator(); sys->addAction("关于……"); } void MainWindow::build_project_menu(QMenu *project) { auto opnp = project->addAction("打开项目", [this]() { auto file = QFileDialog::getOpenFileName(this, "打开项目", QString(), "小说项目(*.nsf)"); if (file == "") return; XApp::disp_core->postCommand(OpenProject(file)); }); sync_kernel->actionEnableSync(opnp, [this]() -> bool { return !core_bind->pjtManager()->isOpenning(); }); auto newp = project->addAction("新建项目", [this]() { auto name = QInputDialog::getText(this, "输入项目名称", "项目名称"); if (name == "") return; auto dir_path = QFileDialog::getExistingDirectory(this, "指定项目存储路径"); if (dir_path == "") return; XApp::disp_core->postCommand(NewProject(QDir(dir_path), name)); }); sync_kernel->actionEnableSync(newp, [this]() -> bool { return !core_bind->pjtManager()->isOpenning(); }); auto clsp = project->addAction("关闭项目", [this]() { XApp::disp_core->postCommand(CloseProject()); }); sync_kernel->actionEnableSync(clsp, [this]() -> bool { return core_bind->pjtManager()->isOpenning(); }); project->addSeparator(); auto pnew = project->addAction("新建路径", [this]() { auto packages = QInputDialog::getText(this, "输入包路径名称/PackA/PackB/PackC", "包路径:"); if (packages != "") XApp::disp_core->postCommand(NewPackage(packages)); }); sync_kernel->actionEnableSync(pnew, [this]() -> bool { return core_bind->pjtManager()->isOpenning(); }); auto _xnew = project->addMenu("新建文件"); _xnew->setEnabled(core_bind->pjtManager()->isOpenning()); auto types = core_bind->docsManager()->fileTypes(); for (auto &t : types) { _xnew->addAction(t, [this, &t]() { auto name = QInputDialog::getText(this, "输入名称", "名称:"); if (name == "") return; auto idx = project_present->currentIndex(); if (!idx.isValid()) idx = core_bind->pjtManager()->model()->item(0)->index(); auto group_path = core_bind->docsManager()->convertPath(idx); XApp::disp_core->postCommand(NewFile(group_path, name, t)); }); } sync_kernel->widgetEnableSync(_xnew, [this]() -> bool { return core_bind->pjtManager()->isOpenning(); }); project->addSeparator(); auto sav = project->addAction("保存全部", []() { XApp::disp_core->postCommand(SaveAll()); }); sav->setShortcut(QKeySequence::StandardKey::Save); sync_kernel->actionEnableSync(sav, [this]() -> bool { return core_bind->pjtManager()->isOpenning(); }); project->addSeparator(); project->addAction("退出", []() { XApp::disp_core->postCommand(SaveAll()); QApplication::exit(0); }); } void MainWindow::build_edit_menu(QMenu *edit) { edit->addAction("撤销一步"); edit->addAction("重做一步"); edit->addSeparator(); edit->addAction("查找关键词"); edit->addAction("替换关键词"); } void MainWindow::build_view_menu(QMenu *view) { auto area = view->addMenu("区域管理"); actions_stack->setMovable(false); addToolBar(Qt::ToolBarArea::TopToolBarArea, actions_stack); auto act_tool = area->addAction("工具栏"); sync_kernel->actionCheckSync(act_tool, [this]() -> bool { return actions_stack->isVisible(); }); connect(act_tool, &QAction::triggered, [this](bool v) { actions_stack->setVisible(v); }); auto xstatus = statusBar(); xstatus->setVisible(false); xstatus->addWidget(new QLabel("文本消息", this)); auto act_status = area->addAction("状态栏"); sync_kernel->actionCheckSync(act_status, [xstatus]() -> bool { return xstatus->isVisible(); }); connect(act_status, &QAction::triggered, [xstatus](bool v) { xstatus->setVisible(v); }); view->addSeparator(); auto change = view->addMenu("内容切换"); change->addAction("序列视图"); change->addAction("编辑视图"); view->addSeparator(); auto func = view->addMenu("功能视图"); sync_kernel->widgetEnableSync(func, [this]() { return this->core_bind->pjtManager()->isOpenning(); }); } void MainWindow::build_tools_menu(QMenu *tool) { auto word = tool->addMenu("敏感词检测"); word->addAction("工具配置"); word->addAction("敏感词编辑"); auto vcs = tool->addMenu("版本管理"); vcs->addAction("工具配置"); vcs->addAction("启用"); auto exp = tool->addMenu("导入导出"); exp->addAction("导入WsOutlines"); exp->addSeparator(); exp->addAction("导出TXT大纲"); exp->addAction("导出Web大纲"); exp->addAction("导出WsOutlines"); exp->addSeparator(); exp->addAction("导出TXT内容"); } QList layout_peak_path = {"sys-configs", "foreground", "view-layouts"}; void MainWindow::build_window_menu(QMenu *window) { auto new_window = window->addMenu("新建窗口"); connect(new_window, &QMenu::aboutToShow, [new_window]() { auto names = XApp::gconfig->getList(layout_peak_path); if (!names.size()) { names << "default"; XApp::gconfig->setList(layout_peak_path, names); } new_window->clear(); for (auto &n : names) new_window->addAction(n); }); connect(new_window, &QMenu::triggered, [this](QAction *s) { auto layout = s->text(); auto newone = new MainWindow(this->core_bind, layout); newone->show(); }); window->addAction("关闭窗口", [this]() { XApp::disp_core->postCommand(SaveAll()); this->close(); }); window->addSeparator(); window->addAction("保存当前布局", [this]() { auto new_name = QInputDialog::getText(this, "输入新布局名称", "名称:"); if (new_name.isEmpty()) return; auto names = XApp::gconfig->getList(layout_peak_path); if (names.contains(new_name)) { QMessageBox::critical(this, "数据校验", "输入错误,输入了重复的布局名称!"); return; } names << new_name; XApp::gconfig->setList(layout_peak_path, names); this->resetLayoutName(new_name); session_service->viewStatesSave(this, this->present_host); }); window->addMenu("布局切换"); window->addMenu("布局删除"); } void MainWindow::closeEvent(QCloseEvent *event) { // 关闭事件 if (core_bind->pjtManager()->isOpenning()) { XApp::disp_core->postCommand(CloseProject()); } session_service->viewStatesSave(this, present_host); XApp::gconfig->save(); QMainWindow::closeEvent(event); } bool MainWindow::eventFilter(QObject *watched, QEvent *event) { auto ev = event->type(); switch (ev) { case QEvent::Type::MouseButtonPress: case QEvent::Type::MouseButtonRelease: case QEvent::Type::Wheel: case QEvent::Type::MouseMove: case QEvent::Type::KeyPress: case QEvent::Type::KeyRelease: sync_kernel->sync(); break; default: break; } return QMainWindow::eventFilter(watched, event); }