QtNovelUI/WordsIDE/storyunitspresent.cpp

159 lines
5.1 KiB
C++
Raw Normal View History

2022-11-18 23:47:32 +00:00
#include "storyunitspresent.h"
2022-12-31 13:05:58 +00:00
#include "DocsManager.h"
2023-03-10 15:18:46 +00:00
#include "command_list.h"
#include "opstream.h"
2022-11-18 23:47:32 +00:00
#include <QSplitter>
#include <QVBoxLayout>
using namespace Components;
using namespace Parse::Result;
2023-03-10 15:18:46 +00:00
using namespace Operate;
2023-03-11 07:01:28 +00:00
using namespace DataModel;
2022-11-18 23:47:32 +00:00
2023-03-10 13:01:19 +00:00
StoryunitsPresentModel::StoryunitsPresentModel(Core::AppCore *core)
: core_ins(core), model_ins(new QStandardItemModel),
details_backend(new QTextDocument) {}
2022-11-18 23:47:32 +00:00
2023-03-10 13:01:19 +00:00
StoryunitsPresentModel::~StoryunitsPresentModel() {
delete model_ins;
delete details_backend;
}
2022-11-18 23:47:32 +00:00
2023-03-10 13:01:19 +00:00
QStandardItemModel *StoryunitsPresentModel::treeModel() const {
return model_ins;
}
2022-11-18 23:47:32 +00:00
2023-03-10 13:01:19 +00:00
QTextDocument *StoryunitsPresentModel::detailsBackend() const {
return details_backend;
2022-11-18 23:47:32 +00:00
}
2023-03-10 15:18:46 +00:00
void StoryunitsPresentModel::refreshTree() {
2022-11-18 23:47:32 +00:00
model_ins->clear();
auto units = core_ins->parseCore()->allStoryUnits();
2023-03-10 13:01:19 +00:00
for (auto &unit : units) {
auto unit_node =
new QStandardItem(static_cast<NamedNode *>(unit)->name()[0]);
2022-11-18 23:47:32 +00:00
unit_node->setEditable(false);
model_ins->appendRow(unit_node);
auto children = unit->children();
2023-03-10 13:01:19 +00:00
for (auto &frag : children) {
if (frag->typeValue() == NODE_STORYFRAGMENT) {
auto frag_node = new QStandardItem(
static_cast<NamedNode *>(frag)->name()[0]);
2022-11-18 23:47:32 +00:00
frag_node->setEditable(false);
unit_node->appendRow(frag_node);
}
}
}
}
2023-03-10 15:18:46 +00:00
void StoryunitsPresentModel::details_show(const QList<QString> &path) {
2023-03-17 13:58:38 +00:00
QStandardItem *item = nullptr;
// 迭代路径获取故事链节点
for (auto &it : path) {
auto result = OpStream<QStandardItem *>([this, &item](int &cnt, int idx) -> QStandardItem * {
if (!item) {
cnt = model_ins->rowCount();
if (cnt <= 0)
return nullptr;
return model_ins->item(idx);
} else {
cnt = item->rowCount();
if (cnt <= 0)
return nullptr;
return item->child(idx);
}
}).filter([it](QStandardItem *const &xit) {
return it == xit->text();
}).toList();
if (!result.size()) {
throw new SimpleException("指定的Storychain路径无效" + path.join("/"));
} else {
item = result.at(0);
}
2023-03-10 15:18:46 +00:00
}
2022-11-18 23:47:32 +00:00
2023-03-10 15:18:46 +00:00
// 填充详细内容
QString contents;
2023-03-10 13:01:19 +00:00
if (item->parent() == nullptr) {
auto node = core_ins->parseCore()->queryStoryUnit(item->text());
2022-11-18 23:47:32 +00:00
auto children = node.first()->children();
2023-03-10 13:01:19 +00:00
for (auto &it : children) {
if (it->typeValue() != NODE_STORYFRAGMENT) {
2023-03-10 15:18:46 +00:00
contents += it->toString() + "\n";
2022-11-18 23:47:32 +00:00
}
}
2023-03-10 13:01:19 +00:00
} else {
auto node =
core_ins->parseCore()->queryStoryUnit(item->parent()->text());
auto point = core_ins->parseCore()->queryStoryFragment(node.first(),
item->text());
2022-11-18 23:47:32 +00:00
auto children = point.first()->children();
2023-03-10 13:01:19 +00:00
for (auto &it : children) {
2023-03-10 15:18:46 +00:00
contents += it->toString() + "\n";
2022-11-18 23:47:32 +00:00
}
}
2023-03-10 15:18:46 +00:00
details_backend->setPlainText(contents);
}
2023-03-17 13:58:38 +00:00
QString StoryunitsPresentModel::name() const { return NAME(StoryunitsPresentModel); }
2022-11-18 23:47:32 +00:00
2023-03-17 13:58:38 +00:00
StoryunitsPresent::StoryunitsPresent(Schedule::CommandsDispatcher *core, StoryunitsPresentModel *backend, QWidget *parent)
: QWidget(parent), disp_core(core), units_view(new QTreeView(this)), details_show(new QTextEdit(this)) {
2023-03-10 13:01:19 +00:00
units_view->setHeaderHidden(true);
units_view->setModel(backend->treeModel());
details_show->setReadOnly(true);
details_show->setDocument(backend->detailsBackend());
2022-11-18 23:47:32 +00:00
2023-03-10 13:01:19 +00:00
auto layout = new QVBoxLayout(this);
auto splitter = new QSplitter(Qt::Vertical, this);
layout->addWidget(splitter);
splitter->addWidget(units_view);
splitter->addWidget(details_show);
layout->setMargin(0);
2022-11-18 23:47:32 +00:00
2023-03-10 13:01:19 +00:00
connect(units_view, &QTreeView::clicked, this,
&StoryunitsPresent::show_details);
connect(units_view, &QTreeView::doubleClicked, this,
&StoryunitsPresent::jump_to);
}
2022-11-18 23:47:32 +00:00
2023-03-10 13:01:19 +00:00
QString StoryunitsPresent::name() const { return NAME(StoryunitsPresent); }
2023-03-10 15:18:46 +00:00
2023-03-17 13:58:38 +00:00
QWidget *StoryunitsPresent::widget() const { return (QWidget *)this; }
2023-03-10 15:18:46 +00:00
using namespace CommandList;
void StoryunitsPresent::jump_to(const QModelIndex &curr) {
QList<QString> path;
QModelIndex node = curr;
while (true) {
auto node_name = node.data().toString();
if (node_name.isEmpty())
break;
path.insert(0, node_name);
node = node.parent();
}
disp_core->postCommand(StoryunitJumpTo(path));
}
void StoryunitsPresent::show_details(const QModelIndex &curr) {
QList<QString> path;
QModelIndex node = curr;
while (true) {
auto node_name = node.data().toString();
if (node_name.isEmpty())
break;
path.insert(0, node_name);
node = node.parent();
}
disp_core->postCommand(StoryunitDetailShow(path));
}