QtNovelUI/WordsIDE/storychainspresent.cpp

187 lines
5.6 KiB
C++
Raw Normal View History

2022-11-18 05:43:27 +00:00
#include "storychainspresent.h"
2022-12-31 13:05:58 +00:00
#include "DocsManager.h"
2023-03-10 13:01:19 +00:00
#include "command_list.h"
2023-03-10 15:18:46 +00:00
#include "manager_docs.h"
2022-11-18 05:43:27 +00:00
#include <QSplitter>
#include <QVBoxLayout>
using namespace Components;
using namespace Parse;
using namespace Parse::Result;
2022-12-01 13:54:04 +00:00
using namespace Tools;
2023-03-10 13:01:19 +00:00
using namespace CommandList;
2022-11-18 05:43:27 +00:00
2023-03-10 13:01:19 +00:00
StorychainsPresentModel::StorychainsPresentModel(Core::AppCore *core)
2023-03-10 15:18:46 +00:00
: core_ins(core), model_base(new QStandardItemModel),
details_base(new QTextDocument) {
sync_tools = new ModelSyncs<DesNode *>(
2023-03-10 13:01:19 +00:00
model_base,
2023-03-10 15:18:46 +00:00
[](DesNode *const &d, QStandardItem *it) -> bool {
return static_cast<NamedNode *>(d)->name().first() == it->text();
},
2023-03-10 13:01:19 +00:00
[](DesNode *const &d, QStandardItem *it) {
2023-03-10 15:18:46 +00:00
it->setText(static_cast<NamedNode *>(d)->name().first());
});
2023-03-10 13:01:19 +00:00
}
2023-03-10 15:18:46 +00:00
StorychainsPresentModel::~StorychainsPresentModel() {
2023-03-10 13:01:19 +00:00
delete model_base;
delete details_base;
delete sync_tools;
}
2023-03-10 15:18:46 +00:00
QStandardItemModel *StorychainsPresentModel::treeModel() const {
2023-03-10 13:01:19 +00:00
return model_base;
}
2023-03-10 15:18:46 +00:00
QTextDocument *const StorychainsPresentModel::detailsBackend() const {
2023-03-10 13:01:19 +00:00
return details_base;
2022-11-18 05:43:27 +00:00
}
2023-03-10 15:18:46 +00:00
void StorychainsPresentModel::refreshTree() {
sync_tools->presentSync([this](DesNode *p) -> QList<DesNode *> {
if (p) {
QList<DesNode *> retv;
for (auto &point : p->children()) {
if (point->typeValue() == NODE_STORYPOINT) {
retv << point;
2022-12-01 13:54:04 +00:00
}
2022-11-18 05:43:27 +00:00
}
2023-03-10 15:18:46 +00:00
return retv;
} else
return this->core_ins->parseCore()->allStoryChains();
});
2022-11-18 05:43:27 +00:00
}
2023-03-10 13:01:19 +00:00
using namespace Operate;
2023-03-10 15:18:46 +00:00
void StorychainsPresentModel::showDetails(const QList<QString> &chain_path) {
2023-03-10 13:01:19 +00:00
// 获取第一级初始集合
2023-03-10 15:18:46 +00:00
QList<QStandardItem *> item_list =
OpStream<QStandardItem *>([this](int &cnt, int idx) -> QStandardItem * {
cnt = model_base->rowCount();
if (cnt <= 0)
return nullptr;
return model_base->item(idx);
})
.select()
.toList();
2023-03-10 13:01:19 +00:00
// 迭代路径获取故事链节点
2023-03-10 15:18:46 +00:00
for (auto it : chain_path) {
auto result = OpStream<QStandardItem *>(item_list)
.filter([it](QStandardItem *const &xit) {
return it == xit->text();
})
.toList();
if (!result.size()) {
throw new SimpleException("指定的Storychain路径无效" +
chain_path.join("/"));
2023-03-10 13:01:19 +00:00
}
2023-03-10 15:18:46 +00:00
item_list = OpStream<QStandardItem *>(
[&result](int &cnt, int idx) -> QStandardItem * {
cnt = result[0]->rowCount();
if (cnt <= 0)
return nullptr;
return result[0]->child(idx);
})
.select()
.toList();
2023-03-10 13:01:19 +00:00
}
2022-11-18 05:43:27 +00:00
2023-03-10 13:01:19 +00:00
// 填充详细内容
auto item = item_list[0];
QString text_lines;
2023-03-10 15:18:46 +00:00
if (item->parent() == nullptr) {
auto node = core_ins->parseCore()->queryStoryChain(item->text());
2022-11-18 05:43:27 +00:00
auto children = node.first()->children();
2023-03-10 15:18:46 +00:00
for (auto &it : children) {
if (it->typeValue() != NODE_STORYPOINT) {
2023-03-10 13:01:19 +00:00
text_lines += it->toString() + "\n";
2022-11-18 05:43:27 +00:00
}
}
2023-03-10 15:18:46 +00:00
} else {
auto node =
core_ins->parseCore()->queryStoryChain(item->parent()->text());
auto point =
core_ins->parseCore()->queryStoryPoint(node.first(), item->text());
2022-11-18 05:43:27 +00:00
auto children = point.first()->children();
2023-03-10 15:18:46 +00:00
for (auto &it : children) {
2023-03-10 13:01:19 +00:00
text_lines += it->toString() + "\n";
2022-11-18 05:43:27 +00:00
}
}
2023-03-10 13:01:19 +00:00
details_base->setPlainText(text_lines);
}
2023-03-10 15:18:46 +00:00
QString StorychainsPresentModel::name() const {
2023-03-10 13:01:19 +00:00
return NAME(StorychainsPresentModel);
}
2023-03-10 15:18:46 +00:00
StorychainsPresent::StorychainsPresent(Schedule::CommandsDispatcher *disp,
QWidget *parent)
: QWidget(parent), disp_core(disp), tree_view(new QTreeView(this)),
details_show(new QPlainTextEdit) {
auto backend =
disp->get<StorychainsPresentModel>(NAME(StorychainsPresentModel));
2023-03-10 13:01:19 +00:00
tree_view->setModel(backend->treeModel());
tree_view->setHeaderHidden(true);
details_show->setDocument(backend->detailsBackend());
details_show->setReadOnly(true);
auto layoutx = new QVBoxLayout(this);
auto split = new QSplitter(Qt::Vertical, this);
layoutx->addWidget(split);
split->addWidget(tree_view);
split->addWidget(details_show);
layoutx->setMargin(0);
2023-03-10 15:18:46 +00:00
connect(tree_view, &QTreeView::clicked, this,
&StorychainsPresent::detail_show);
connect(tree_view, &QTreeView::doubleClicked, this,
&StorychainsPresent::jump_to);
2023-03-10 13:01:19 +00:00
}
2023-03-10 15:18:46 +00:00
QString StorychainsPresent::name() const { return NAME(StorychainsPresent); }
2022-11-18 05:43:27 +00:00
2023-03-10 15:18:46 +00:00
void StorychainsPresent::jump_to(const QModelIndex &curr) {
if (!curr.isValid())
return;
QList<QString> path;
2023-03-10 13:27:46 +00:00
auto node = curr;
2023-03-10 13:01:19 +00:00
while (true) {
2023-03-10 13:27:46 +00:00
auto node_name = node.data().toString();
2023-03-10 13:01:19 +00:00
2023-03-10 13:27:46 +00:00
if (node_name.isEmpty())
2023-03-10 13:01:19 +00:00
break;
2023-03-10 13:27:46 +00:00
path.insert(0, node_name);
node = node.parent();
}
2023-03-10 13:01:19 +00:00
disp_core->postCommand(StorychainJumpTo(path));
}
2023-03-10 15:18:46 +00:00
void StorychainsPresent::detail_show(const QModelIndex &curr) {
if (!curr.isValid())
2023-03-10 13:01:19 +00:00
return;
2023-03-10 13:01:19 +00:00
QList<QString> path;
2023-03-10 13:27:46 +00:00
auto node = curr;
2023-03-10 13:01:19 +00:00
while (true) {
2023-03-10 13:27:46 +00:00
auto node_name = node.data().toString();
2023-03-10 13:01:19 +00:00
2023-03-10 13:27:46 +00:00
if (node_name.isEmpty())
2023-03-10 13:01:19 +00:00
break;
2023-03-10 13:27:46 +00:00
path.insert(0, node_name);
node = node.parent();
}
2023-03-10 13:01:19 +00:00
disp_core->postCommand(StorychainDetailShow(path));
}