#include "srcedit_defaulttext.h" #include "mainwindow.h" #include "srcedit_storyboard.h" #include "srcedit_storychain.h" #include "srcedit_storyunit.h" #include "srcedit_storyvolume.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 MakeTools; using namespace Parse::Result; using namespace Components; using namespace Core; using namespace Tools; using namespace CommandList; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), app_core(new AppCore(this, this)), disp_core(new Schedule::CommandsDispatcher(QDir::home(), true)), sync_kernel(new StatusSyncCore(this)), horizontal_split(new QSplitter(Qt::Horizontal, this)), vertical_split(new QSplitter(Qt::Vertical, this)), left_funcs(new QTabWidget(this)), right_funcs(new QTabWidget(this)), center_funcs(new QTabWidget(this)), bottom_funcs(new QTabWidget(this)), project_manager(new XMLProjectManager(this)), welcome_list(new QListView(this)), project_present(new ProjectPresent(app_core, this)), docs_container(new DocumentsManager(app_core, project_manager, this)), chains_view(new StorychainsPresent(disp_core, this)), units_view(new StoryunitsPresent(disp_core, this)), errors_present(new MessagePresent(app_core->getMakeCore(), this)), boards_view(new StoryboardsPresent(app_core, this)), concept_view(new StoryconceptsPresent(disp_core, this)), fragments_order(new FragmentsOrderView(disp_core, this)) { QApplication::instance()->installEventFilter(this); this->app_core->setCurrentProject(project_manager); disp_core->registerMember(docs_container); disp_core->registerMember(project_present); disp_core->registerMember(this->app_core); setMinimumSize(1000, 600); setWindowTitle("提线木偶"); chains_view->setVisible(false); units_view->setVisible(false); errors_present->setVisible(false); boards_view->setVisible(false); concept_view->setVisible(false); fragments_order->setVisible(false); left_funcs->setVisible(false); right_funcs->setVisible(false); bottom_funcs->setVisible(false); auto xtool = new QToolBar(this); xtool->setVisible(false); addToolBar(Qt::ToolBarArea::TopToolBarArea, xtool); auto xstatus = statusBar(); xstatus->setVisible(false); statusBar()->addWidget(new QLabel("文本消息", this)); auto mbar = menuBar(); initial_menubar(mbar); setCentralWidget(this->vertical_split); this->vertical_split->addWidget(this->horizontal_split); this->vertical_split->addWidget(bottom_funcs); this->horizontal_split->addWidget(left_funcs); this->horizontal_split->addWidget(center_funcs); this->horizontal_split->addWidget(right_funcs); left_funcs->setTabsClosable(true); connect(left_funcs, &QTabWidget::tabCloseRequested, [this](int index){ toggle_widget_visible(false, left_funcs, left_funcs->widget(index)); }); bottom_funcs->setTabsClosable(true); connect(bottom_funcs, &QTabWidget::tabCloseRequested, [this](int index){ toggle_widget_visible(false, bottom_funcs, bottom_funcs->widget(index)); }); right_funcs->setTabsClosable(true); connect(right_funcs, &QTabWidget::tabCloseRequested, [this](int index){ toggle_widget_visible(false, right_funcs, right_funcs->widget(index)); }); center_funcs->setTabsClosable(true); connect(center_funcs, &QTabWidget::tabCloseRequested, [this](int index){ auto view = center_funcs->widget(index); toggle_widget_visible(false, center_funcs, view); auto comp = app_core->getDocsManager()->queryTextComponent(view); if(comp) app_core->getDocsManager()->closeTextComponent(QFileInfo(comp->absoluteTargetPath())); }); this->app_core->getDocsManager()->addProcTrigger([this](){ this->refresh_views(); }); center_funcs->addTab(welcome_list, "欢迎界面"); uilayout_load(); connect(horizontal_split, &QSplitter::splitterMoved, [this](){ splitter_layout_save({"uilayout","split-policy-h","lens"}, horizontal_split->sizes()); }); connect(vertical_split, &QSplitter::splitterMoved, [this](){ splitter_layout_save({"uilayout", "split-policy-v", "lens"}, vertical_split->sizes()); }); } MainWindow::~MainWindow() { } void MainWindow::initial_menubar(QMenuBar *mbar) { // 项目菜单树 auto project = mbar->addMenu("项目"); auto opnp = project->addAction("打开项目", [this](){ auto file = QFileDialog::getOpenFileName(this, "打开项目", QString(), "小说项目(*.nsf)"); if(file == "") return; disp_core->postCommand(OpenProject(file)); build_internal(true);}); sync_kernel->actionSync(opnp, [this]()->bool{return !project_manager->isOpen();}); auto newp = project->addAction("新建项目", [this]() { auto name = QInputDialog::getText(this, "输入项目名称", "项目名称"); if(name == "") return; auto dir_path = QFileDialog::getExistingDirectory(this, "指定项目存储路径"); if(dir_path == "") return; disp_core->postCommand(NewProject(QDir(dir_path), name)); }); sync_kernel->actionSync(newp, [this]()->bool{return !project_manager->isOpen();}); auto clsp = project->addAction("关闭项目", [this]() { disp_core->postCommand(CloseProject()); this->refresh_views(); }); sync_kernel->actionSync(clsp, [this]()->bool{return project_manager->isOpen();}); project->addSeparator(); auto pnew = project->addAction("新建路径", [this]() { auto packages = QInputDialog::getText(this, "输入包路径名称/PackA/PackB/PackC", "包路径:"); if(packages != "") disp_core->postCommand(NewPackage(packages)); }); sync_kernel->actionSync(pnew, [this]()->bool{return project_manager->isOpen();}); auto _xnew = project->addMenu("新建文件"); auto types = docs_container->fileTypes(); for(auto &t : types){ _xnew->addAction(t, [this, &t](){ auto name = QInputDialog::getText(this, "输入名称", "名称:"); if(name == "") return; auto idx = project_view->currentIndex(); if(!idx.isValid()) idx = project_manager->model()->item(0)->index(); auto group_path = docs_container->converter(idx); this->app_core->disp_core->postCommand(NewFile(group_path, name, t)); }); } sync_kernel->widgetSync(_xnew, [this]()->bool{return project_manager->isOpen();}); project->addSeparator(); auto sav = project->addAction( "保存全部", [this]() { disp_core->postCommand(SaveAll()); }); sav->setShortcut(QKeySequence::StandardKey::Save); sync_kernel->actionSync(sav, [this]()->bool{return project_manager->isOpen();}); project->addSeparator(); project->addAction("退出", [this]() { disp_core->postCommand(SaveAll()); QApplication::exit(0); }); // 编辑菜单 auto edit = mbar->addMenu("编辑"); edit->addAction("撤销一步"); edit->addAction("重做一步"); edit->addSeparator(); edit->addAction("查找关键词"); edit->addAction("替换关键词"); // 视图菜单 auto view = mbar->addMenu("视图"); auto area = view->addMenu("区域管理"); auto act_tool = area->addAction("工具栏"); act_tool->setCheckable(true); connect(act_tool, &QAction::triggered, [xtool](bool v){ xtool->setVisible(v); }); sync_kernel->registerAutoRun([xtool]()->bool{return xtool->isVisible();}, [act_tool](bool v){ if(v != act_tool->isChecked()) act_tool->setChecked(v); }); auto act_status = area->addAction("状态栏"); act_status->setCheckable(true); connect(act_status,&QAction::triggered, [xstatus](bool v){xstatus->setVisible(v);}); sync_kernel->registerAutoRun([xstatus]()->bool{return xstatus->isVisible();}, [act_status](bool v){ if(v != act_status->isChecked()) act_status->setChecked(v); }); auto act_left = area->addAction("左侧功能区"); act_left->setCheckable(true); connect(act_left, &QAction::triggered, [this](bool v){this->left_funcs->setVisible(v);}); sync_kernel->registerAutoRun([this]()->bool{return this->left_funcs->isVisible();}, [act_left](bool v){ if(v!= act_left->isChecked()) act_left->setChecked(v); }); auto act_right = area->addAction("右侧功能区"); act_right->setCheckable(true); connect(act_right, &QAction::triggered, [this](bool v){this->right_funcs->setVisible(v);}); sync_kernel->registerAutoRun([this]()->bool{return this->right_funcs->isVisible();}, [act_right](bool v){if(v != act_right->isChecked()) act_right->setChecked(v);}); auto act_bottom = area->addAction("底部功能区"); act_bottom->setCheckable(true); connect(act_bottom, &QAction::triggered, [this](bool v){this->bottom_funcs->setVisible(v);}); sync_kernel->registerAutoRun([this]()->bool{return this->bottom_funcs->isVisible();}, [act_bottom](bool v){ if(v!= act_bottom->isChecked()) act_bottom->setChecked(v);}); view->addSeparator(); auto change = view->addMenu("内容切换"); change->addAction("序列视图"); change->addAction("编辑视图"); view->addSeparator(); auto func = view->addMenu("功能视图"); // // 项目管理 // { // auto project_v = func->addAction("项目管理", [this](bool v){ // toggle_widget_visible(v, left_funcs, project_structure, "项目管理"); // }); // project_v->setCheckable(true); // sync_kernel->registerAutoRun([this]()->bool{return this->project_structure->isVisible();}, // [project_v](bool v){ if(v != project_v->isChecked()) project_v->setChecked(v); }); // } func->addAction("引用统计"); func->addAction("脉络分蘖"); // 编译信息 { auto msgs = func->addAction("编译输出", [this](bool v){ toggle_widget_visible(v, bottom_funcs, errors_present, "编译输出"); }); msgs->setCheckable(true); sync_kernel->registerAutoRun([this]()->bool{return bottom_funcs->indexOf(errors_present) != -1;}, [msgs](bool v){if(v!=msgs->isChecked()) msgs->setChecked(v);}); } // 脉络统计 { auto chain_v = func->addAction("脉络统计", [this](bool v){ toggle_widget_visible(v, right_funcs, chains_view, "脉络统计"); }); chain_v->setCheckable(true); sync_kernel->registerAutoRun([this]()->bool{return right_funcs->indexOf(chains_view) != -1;}, [chain_v](bool v){ if(v != chain_v->isChecked()) chain_v->setChecked(v);}); } // 单元统计 { auto unit_v = func->addAction("单元统计", [this](bool v){ toggle_widget_visible(v, right_funcs, units_view, "单元统计"); }); unit_v->setCheckable(true); sync_kernel->registerAutoRun([this]()->bool{return right_funcs->indexOf(units_view)!=-1;}, [unit_v](bool v){if(v != unit_v->isChecked()) unit_v->setChecked(v);}); } // 故事统计 { auto board_v = func->addAction("故事统计", [this](bool v){ toggle_widget_visible(v, right_funcs, boards_view, "故事统计"); }); board_v->setCheckable(true); sync_kernel->registerAutoRun([this]()->bool{return right_funcs->indexOf(boards_view)!=-1;}, [board_v](bool v){if(v!=board_v->isChecked()) board_v->setChecked(v);}); } // 概念统计 { auto concept_v = func->addAction("概念统计", [this](bool v){ toggle_widget_visible(v, right_funcs, concept_view, "概念统计"); }); concept_v->setCheckable(true); sync_kernel->registerAutoRun([this]()->bool{return right_funcs->indexOf(concept_view)!=-1;}, [concept_v](bool v){if(v != concept_v->isChecked()) concept_v->setChecked(v);}); } // 情节序列 { auto fragments_v = func->addAction("情节序列", [this](bool v){ toggle_widget_visible(v, right_funcs, fragments_order, "情节序列"); }); fragments_v->setCheckable(true); sync_kernel->registerAutoRun([this]()->bool{return right_funcs->indexOf(fragments_order)!=-1;}, [fragments_v](bool v){if(v != fragments_v->isChecked()) fragments_v->setChecked(v);}); } // 工具菜单 auto tool = mbar->addMenu("工具"); 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内容"); auto build = tool->addAction("编译", [this](){ this->build_internal(); }); sync_kernel->actionSync(build, [this]()->bool{ return project_manager->isOpen(); }); // 窗口菜单 auto window = mbar->addMenu("窗口"); window->addAction("新建窗口"); window->addAction("关闭窗口"); window->addSeparator(); window->addAction("布局管理"); window->addMenu("布局切换"); window->addMenu("窗口切换"); // 系统 auto sys = mbar->addMenu("系统"); sys->addAction("软件激活"); sys->addAction("系统信息"); sys->addSeparator(); sys->addAction("关于……"); } void MainWindow::contentViewAppend(QWidget *widget, const QString &name) { for(auto idx=0; idxcount(); ++idx){ if(center_funcs->widget(idx) == widget){ center_funcs->setCurrentWidget(widget); return; } } center_funcs->addTab(widget, name); center_funcs->setCurrentWidget(widget); } void MainWindow::contentViewClose(QWidget *widget) { for(auto idx=0; idxcount(); ++idx){ if(center_funcs->widget(idx) == widget){ } } } void MainWindow::refresh_views() { errors_present->refresh(); chains_view->refresh(); units_view->refresh(); boards_view->refresh(); concept_view->refresh(); fragments_order->refresh(); } void MainWindow::build_internal(bool all_from_disk) { disp_core->postCommand(Build(all_from_disk)); refresh_views(); } void MainWindow::toggle_widget_visible(bool visible, QTabWidget *con, QWidget *target, const QString &title) { if(visible && con->indexOf(target) == -1){ target->setVisible(true); con->addTab(target, title); }else if(!visible && con->indexOf(target) != -1){ target->setVisible(false); con->removeTab(con->indexOf(target)); } } void MainWindow::splitter_layout_save(const QList &path_type, const QList &size) { auto convert = [](QList sizes)->QStringList{ QStringList size_strs; for(auto i : sizes) size_strs << QString("%1").arg(i); return size_strs; }; app_core->globalConfig()->setList(path_type, convert(size)); } void MainWindow::uilayout_save() { auto global = app_core->globalConfig(); auto xvisible_save = [global](QWidget *ins, const QString &name){ global->setConfig({"uilayout", "visible", name}, QString("%1").arg(ins->isVisible())); }; xvisible_save(left_funcs, "area-left"); xvisible_save(right_funcs, "area-right"); xvisible_save(bottom_funcs, "area-bottom"); // xvisible_save(project_structure, "project_tree"); // xvisible_save(errors_present, "message-view"); // xvisible_save(chains_view, "chains-sum"); // xvisible_save(units_view, "units-sum"); } void MainWindow::uilayout_load() { auto global = app_core->globalConfig(); auto xvisible_load = [global](QWidget *ins, const QString &name){ auto visible = Config::ConfigHelper::getConfigAsDefault(global, {"uilayout","visible",name}, QString("%1").arg(false)); ins->setVisible(visible.toInt()); }; xvisible_load(left_funcs, "area-left"); xvisible_load(right_funcs, "area-right"); xvisible_load(bottom_funcs, "area-bottom"); // xvisible_load(project_structure, "project_tree"); // xvisible_load(errors_present, "message-view"); // xvisible_load(chains_view, "chains-sum"); // xvisible_load(units_view, "units-sum"); auto convert = [](QStringList in)->QList{ QList size; for(auto &s : in) size << s.toInt(); return size; }; auto strings = global->getList({"uilayout", "split-policy-h", "lens"}); horizontal_split->setSizes(convert(strings)); strings = global->getList({"uilayout", "split-policy-v", "lens"}); vertical_split->setSizes(convert(strings)); } void MainWindow::append(MakeTools::ContentPresent *ins) { contentViewAppend(ins->widget(), ins->name()); } void MainWindow::active(MakeTools::ContentPresent *ins) { contentViewAppend(ins->widget(), ins->name()); } void MainWindow::remove(MakeTools::ContentPresent *ins) { contentViewClose(ins->widget()); } void MainWindow::closeEvent(QCloseEvent *event) { // 关闭事件 if (project_manager->isOpen()) { disp_core->postCommand(CloseProject()); } uilayout_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); }