QtNovelUI/WordsIDE/mainwindow.cpp

512 lines
18 KiB
C++
Raw Normal View History

#include "srcedit_defaulttext.h"
2022-11-06 00:37:50 +00:00
#include "mainwindow.h"
#include "srcedit_storyboard.h"
#include "srcedit_storychain.h"
#include "srcedit_storyunit.h"
#include "srcedit_storyvolume.h"
2022-11-06 00:37:50 +00:00
#include <QLabel>
#include <QMenuBar>
#include <QStatusBar>
#include <QDebug>
#include <QDateTime>
#include <QTextEdit>
2022-11-15 08:07:02 +00:00
#include <QToolBar>
2022-11-18 23:47:32 +00:00
#include <xmlconfig.h>
2022-11-15 08:07:02 +00:00
#include <xmlprojectmanager.h>
#include <QFileDialog>
#include <QInputDialog>
#include <QApplication>
#include <QMessageBox>
#include <QList>
#include <QVBoxLayout>
2022-11-15 08:07:02 +00:00
2023-03-10 13:01:19 +00:00
#include "command_list.h"
2022-11-15 08:07:02 +00:00
using namespace Project;
using namespace MakeTools;
2022-11-15 12:58:12 +00:00
using namespace Parse::Result;
using namespace Components;
using namespace Core;
2022-12-01 13:54:04 +00:00
using namespace Tools;
2023-03-10 13:01:19 +00:00
using namespace CommandList;
2022-11-06 00:37:50 +00:00
MainWindow::MainWindow(QWidget *parent)
2023-03-11 07:01:28 +00:00
: QMainWindow(parent), app_core(new AppCore(this, this)),
disp_core(new Schedule::CommandsDispatcher(QDir::home(), true)),
2022-11-18 23:47:32 +00:00
sync_kernel(new StatusSyncCore(this)),
2022-11-15 08:07:02 +00:00
horizontal_split(new QSplitter(Qt::Horizontal, this)),
vertical_split(new QSplitter(Qt::Vertical, this)),
2023-03-11 04:51:30 +00:00
left_funcs(new QTabWidget(this)), right_funcs(new QTabWidget(this)),
center_funcs(new QTabWidget(this)), bottom_funcs(new QTabWidget(this)),
2022-11-15 12:58:12 +00:00
project_manager(new XMLProjectManager(this)),
2023-03-10 13:01:19 +00:00
welcome_list(new QListView(this)),
project_present(new ProjectPresent(app_core, this)),
docs_container(new DocumentsManager(app_core, project_manager, this)),
2023-03-11 07:01:28 +00:00
chains_view(new StorychainsPresent(disp_core, this)),
units_view(new StoryunitsPresent(disp_core, this)),
2022-12-31 13:05:58 +00:00
errors_present(new MessagePresent(app_core->getMakeCore(), this)),
2023-03-11 07:01:28 +00:00
boards_view(new StoryboardsPresent(app_core, this)),
concept_view(new StoryconceptsPresent(disp_core, this)),
fragments_order(new FragmentsOrderView(disp_core, this)) {
2022-11-18 23:47:32 +00:00
QApplication::instance()->installEventFilter(this);
2023-03-11 07:01:28 +00:00
this->app_core->setCurrentProject(project_manager);
2023-03-11 07:01:28 +00:00
disp_core->registerMember(docs_container);
disp_core->registerMember(project_present);
disp_core->registerMember(this->app_core);
2022-11-18 23:47:32 +00:00
setMinimumSize(1000, 600);
2023-01-01 05:10:31 +00:00
setWindowTitle("提线木偶");
2022-11-18 23:47:32 +00:00
chains_view->setVisible(false);
units_view->setVisible(false);
errors_present->setVisible(false);
2022-11-25 01:18:54 +00:00
boards_view->setVisible(false);
concept_view->setVisible(false);
2022-12-08 19:43:42 +00:00
fragments_order->setVisible(false);
2022-11-18 23:47:32 +00:00
left_funcs->setVisible(false);
right_funcs->setVisible(false);
bottom_funcs->setVisible(false);
2022-11-15 08:07:02 +00:00
auto xtool = new QToolBar(this);
2022-11-18 23:47:32 +00:00
xtool->setVisible(false);
2022-11-15 08:07:02 +00:00
addToolBar(Qt::ToolBarArea::TopToolBarArea, xtool);
auto xstatus = statusBar();
2022-11-18 23:47:32 +00:00
xstatus->setVisible(false);
2022-11-15 08:07:02 +00:00
statusBar()->addWidget(new QLabel("文本消息", this));
auto mbar = menuBar();
2023-03-10 13:01:19 +00:00
initial_menubar(mbar);
2023-03-10 13:01:19 +00:00
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()));
});
2023-03-10 13:01:19 +00:00
this->app_core->getDocsManager()->addProcTrigger([this](){
this->refresh_views();
});
2023-03-10 13:01:19 +00:00
center_funcs->addTab(welcome_list, "欢迎界面");
uilayout_load();
connect(horizontal_split, &QSplitter::splitterMoved, [this](){
splitter_layout_save({"uilayout","split-policy-h","lens"}, horizontal_split->sizes());
2022-11-15 08:07:02 +00:00
});
2023-03-10 13:01:19 +00:00
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](){
2022-11-15 08:07:02 +00:00
auto file = QFileDialog::getOpenFileName(this, "打开项目", QString(), "小说项目(*.nsf)");
if(file == "")
return;
2023-03-11 07:01:28 +00:00
disp_core->postCommand(OpenProject(file));
2023-03-10 13:01:19 +00:00
build_internal(true);});
sync_kernel->actionSync(opnp, [this]()->bool{return !project_manager->isOpen();});
2023-03-11 07:01:28 +00:00
auto newp = project->addAction("新建项目", [this]() {
2022-11-15 08:07:02 +00:00
auto name = QInputDialog::getText(this, "输入项目名称", "项目名称");
if(name == "")
return;
auto dir_path = QFileDialog::getExistingDirectory(this, "指定项目存储路径");
if(dir_path == "")
return;
2023-03-11 07:01:28 +00:00
disp_core->postCommand(NewProject(QDir(dir_path), name));
});
2023-03-10 13:01:19 +00:00
sync_kernel->actionSync(newp, [this]()->bool{return !project_manager->isOpen();});
2023-03-11 07:01:28 +00:00
auto clsp = project->addAction("关闭项目", [this]() {
disp_core->postCommand(CloseProject());
this->refresh_views();
});
2023-03-10 13:01:19 +00:00
sync_kernel->actionSync(clsp, [this]()->bool{return project_manager->isOpen();});
project->addSeparator();
2023-03-10 13:01:19 +00:00
2023-03-11 07:01:28 +00:00
auto pnew = project->addAction("新建路径", [this]() {
2023-03-10 13:01:19 +00:00
auto packages = QInputDialog::getText(this, "输入包路径名称/PackA/PackB/PackC", "包路径:");
if(packages != "")
2023-03-11 07:01:28 +00:00
disp_core->postCommand(NewPackage(packages));
});
2023-03-10 13:01:19 +00:00
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();
2023-03-10 13:01:19 +00:00
2023-03-11 07:01:28 +00:00
auto sav = project->addAction(
"保存全部", [this]() { disp_core->postCommand(SaveAll()); });
2023-03-10 13:01:19 +00:00
sav->setShortcut(QKeySequence::StandardKey::Save);
sync_kernel->actionSync(sav, [this]()->bool{return project_manager->isOpen();});
project->addSeparator();
2023-03-11 07:01:28 +00:00
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("视图");
2022-11-15 08:07:02 +00:00
auto area = view->addMenu("区域管理");
2022-11-18 23:47:32 +00:00
2022-11-15 08:07:02 +00:00
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); });
2022-11-15 08:07:02 +00:00
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); });
2022-11-15 08:07:02 +00:00
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); });
2022-11-15 08:07:02 +00:00
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);});
2022-11-15 08:07:02 +00:00
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();
2022-11-15 08:07:02 +00:00
auto change = view->addMenu("内容切换");
change->addAction("序列视图");
change->addAction("编辑视图");
2022-11-15 08:07:02 +00:00
view->addSeparator();
auto func = view->addMenu("功能视图");
2023-03-10 13:01:19 +00:00
// // 项目管理
// {
// 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("脉络分蘖");
2022-11-18 23:47:32 +00:00
// 编译信息
{
auto msgs = func->addAction("编译输出", [this](bool v){
toggle_widget_visible(v, bottom_funcs, errors_present, "编译输出");
2022-11-18 23:47:32 +00:00
});
msgs->setCheckable(true);
sync_kernel->registerAutoRun([this]()->bool{return bottom_funcs->indexOf(errors_present) != -1;},
2022-11-18 23:47:32 +00:00
[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;},
2022-11-18 23:47:32 +00:00
[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;},
2022-11-18 23:47:32 +00:00
[unit_v](bool v){if(v != unit_v->isChecked()) unit_v->setChecked(v);});
}
2022-11-25 01:18:54 +00:00
// 故事统计
{
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);});
}
2022-12-08 19:43:42 +00:00
// 情节序列
{
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("导入导出");
2022-11-18 23:47:32 +00:00
exp->addAction("导入WsOutlines");
exp->addSeparator();
2022-11-15 08:07:02 +00:00
exp->addAction("导出TXT大纲");
2022-11-18 23:47:32 +00:00
exp->addAction("导出Web大纲");
exp->addAction("导出WsOutlines");
exp->addSeparator();
exp->addAction("导出TXT内容");
auto build = tool->addAction("编译", [this](){
2022-11-15 12:58:12 +00:00
this->build_internal();
});
2023-03-10 13:01:19 +00:00
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("关于……");
2022-11-06 00:37:50 +00:00
}
2022-11-15 12:58:12 +00:00
void MainWindow::contentViewAppend(QWidget *widget, const QString &name)
{
for(auto idx=0; idx<center_funcs->count(); ++idx){
if(center_funcs->widget(idx) == widget){
center_funcs->setCurrentWidget(widget);
2022-11-29 03:47:12 +00:00
return;
}
}
2022-11-29 03:47:12 +00:00
center_funcs->addTab(widget, name);
center_funcs->setCurrentWidget(widget);
}
2023-01-01 05:10:31 +00:00
void MainWindow::contentViewClose(QWidget *widget)
{
for(auto idx=0; idx<center_funcs->count(); ++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)
2022-11-15 12:58:12 +00:00
{
2023-03-11 07:01:28 +00:00
disp_core->postCommand(Build(all_from_disk));
2023-01-01 05:10:31 +00:00
refresh_views();
2022-11-18 23:47:32 +00:00
}
void MainWindow::toggle_widget_visible(bool visible, QTabWidget *con, QWidget *target, const QString &title)
{
if(visible && con->indexOf(target) == -1){
target->setVisible(true);
2022-11-29 03:47:12 +00:00
con->addTab(target, title);
2022-11-18 23:47:32 +00:00
}else if(!visible && con->indexOf(target) != -1){
target->setVisible(false);
con->removeTab(con->indexOf(target));
}
}
void MainWindow::splitter_layout_save(const QList<QString> &path_type, const QList<int> &size)
{
auto convert = [](QList<int> 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");
2022-11-29 03:47:12 +00:00
// 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");
2022-11-29 03:47:12 +00:00
// 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<int>{
QList<int> 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)
{
// 关闭事件
2023-03-11 07:01:28 +00:00
if (project_manager->isOpen()) {
disp_core->postCommand(CloseProject());
}
uilayout_save();
QMainWindow::closeEvent(event);
2022-11-15 12:58:12 +00:00
}
2022-11-18 23:47:32 +00:00
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
auto ev = event->type();
switch(ev){
2022-11-29 03:47:12 +00:00
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;
2022-11-18 23:47:32 +00:00
}
return QMainWindow::eventFilter(watched, event);
}