2022-11-22 03:42:48 +00:00
|
|
|
|
#include "storyunitsourceedit.h"
|
|
|
|
|
#include "keywordshightlighter.h"
|
|
|
|
|
#include <QComboBox>
|
|
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QSpinBox>
|
2022-12-31 05:26:58 +00:00
|
|
|
|
#include <QSplitter>
|
2022-11-22 03:42:48 +00:00
|
|
|
|
#include <QVBoxLayout>
|
2022-12-31 05:26:58 +00:00
|
|
|
|
#include <QDebug>
|
2022-11-22 03:42:48 +00:00
|
|
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
|
using namespace Parse::Result;
|
|
|
|
|
using namespace Components;
|
|
|
|
|
using namespace Enhancement;
|
|
|
|
|
|
|
|
|
|
StoryUnitSourceEdit::StoryUnitSourceEdit(FileExtensionFactory *factory)
|
2022-12-31 05:26:58 +00:00
|
|
|
|
: refers_descriptions(new QTableView()), refers_model(new QStandardItemModel()),
|
|
|
|
|
split(new QSplitter()),
|
|
|
|
|
words_highlighter(new KeywordsHightlighter(this)), factory_ins(factory)
|
2022-11-22 03:42:48 +00:00
|
|
|
|
{
|
2022-11-25 07:30:33 +00:00
|
|
|
|
words_highlighter->setDocument(this->textDocument());
|
2022-12-31 05:26:58 +00:00
|
|
|
|
refers_descriptions->setModel(refers_model);
|
2022-11-22 03:42:48 +00:00
|
|
|
|
|
2022-11-25 07:30:33 +00:00
|
|
|
|
this->setContexMenuProcess([this](QMenu *menu){
|
2022-11-22 03:42:48 +00:00
|
|
|
|
menu->addSeparator();
|
|
|
|
|
menu->addAction("刷新", [this](){words_highlighter->rehighlight();});
|
|
|
|
|
});
|
2022-12-31 05:26:58 +00:00
|
|
|
|
|
|
|
|
|
connect(edit_square, &QTextEdit::cursorPositionChanged, this, &StoryUnitSourceEdit::cursor_contex_query);
|
2023-01-01 05:10:31 +00:00
|
|
|
|
|
|
|
|
|
auto vins = FormattedTextEdit::textView();
|
|
|
|
|
split->addWidget(vins);
|
|
|
|
|
split->addWidget(this->refers_descriptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StoryUnitSourceEdit::~StoryUnitSourceEdit()
|
|
|
|
|
{
|
|
|
|
|
delete this->refers_descriptions;
|
|
|
|
|
|
|
|
|
|
split->deleteLater();
|
2022-11-22 03:42:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileExtensionFactory *StoryUnitSourceEdit::factory() const
|
|
|
|
|
{
|
|
|
|
|
return factory_ins;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 03:47:12 +00:00
|
|
|
|
void StoryUnitSourceEdit::reloadConfigrations(QList<Config::Configration *> configs)
|
2022-11-22 03:42:48 +00:00
|
|
|
|
{
|
|
|
|
|
// TODO: 设置格式
|
|
|
|
|
auto font_family = Config::ConfigHelper::getConfigAsDefaultSequence
|
2022-12-31 05:26:58 +00:00
|
|
|
|
(configs, {"sourcecode_unitedit","default_font","font_family"}, "微软雅黑");
|
2022-11-22 03:42:48 +00:00
|
|
|
|
auto font_size = Config::ConfigHelper::getConfigAsDefaultSequence(
|
2022-12-31 05:26:58 +00:00
|
|
|
|
configs, {"sourcecode_unitedit","default_font","font_sizept"}, "20");
|
2022-11-22 03:42:48 +00:00
|
|
|
|
QFont default_font;
|
|
|
|
|
default_font.setFamily(font_family);
|
|
|
|
|
default_font.setPointSize(font_size.toInt());
|
|
|
|
|
|
2022-11-25 07:30:33 +00:00
|
|
|
|
this->textDocument()->setDefaultFont(default_font);
|
2022-11-22 03:42:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StoryUnitSourceEdit::modeReset(const QString &) const
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<QString> StoryUnitSourceEdit::modes() const
|
|
|
|
|
{
|
|
|
|
|
return QList<QString>() << "源码";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString StoryUnitSourceEdit::currentMode() const
|
|
|
|
|
{
|
|
|
|
|
return modes()[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString StoryUnitSourceEdit::title() const
|
|
|
|
|
{
|
|
|
|
|
return source_target.fileName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StoryUnitSourceEdit::rehighlighter()
|
|
|
|
|
{
|
|
|
|
|
this->words_highlighter->rehighlight();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 03:47:12 +00:00
|
|
|
|
void StoryUnitSourceEdit::initSource(Core::AppCore *core, const QFileInfo &src, QWidget *parent)
|
2022-11-22 03:42:48 +00:00
|
|
|
|
{
|
|
|
|
|
this->source_target = src;
|
2022-11-22 06:15:36 +00:00
|
|
|
|
this->core_ins = core;
|
2022-12-31 05:26:58 +00:00
|
|
|
|
this->refers_descriptions->setParent(parent);
|
|
|
|
|
this->refers_model->setParent(parent);
|
|
|
|
|
this->split->setParent(parent);
|
|
|
|
|
|
2022-11-29 03:47:12 +00:00
|
|
|
|
FormattedTextEdit::initSource(core, src, parent);
|
2022-11-22 03:42:48 +00:00
|
|
|
|
static_cast<KeywordsHightlighter*>(words_highlighter)
|
|
|
|
|
->reset(core->parseCore()->queryDocument(src));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-31 05:26:58 +00:00
|
|
|
|
void StoryUnitSourceEdit::cursor_contex_query()
|
|
|
|
|
{
|
|
|
|
|
refers_model->clear();
|
|
|
|
|
refers_model->setHorizontalHeaderLabels(QStringList() << "故事" << "内容");
|
|
|
|
|
|
|
|
|
|
auto cursor = edit_square->textCursor();
|
2022-12-31 13:05:58 +00:00
|
|
|
|
auto doc = this->core_ins->parseCore()->queryDocument(QFileInfo(absoluteFilePath()));
|
2022-12-31 05:26:58 +00:00
|
|
|
|
auto words = doc->getWords(cursor.block().blockNumber());
|
|
|
|
|
|
|
|
|
|
QList<DesNode*> frags;
|
|
|
|
|
for (auto &w : words) {
|
|
|
|
|
QHash<uint, Parse::Result::DesNode*> nodes;
|
|
|
|
|
|
|
|
|
|
auto xhost = w->host();
|
|
|
|
|
while (xhost != nullptr){
|
|
|
|
|
nodes[xhost->typeValue()] = xhost;
|
|
|
|
|
xhost = xhost->parent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!nodes.contains(NODE_STORYFRAGMENT))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
xhost = nodes[NODE_STORYFRAGMENT];
|
|
|
|
|
if(!frags.contains(xhost))
|
|
|
|
|
frags << xhost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto less_equal_than = [](std::pair<uint, uint> a, std::pair<uint, uint> b)->bool{
|
|
|
|
|
if(a.first < b.first)
|
|
|
|
|
return true;
|
|
|
|
|
else if(a.first == b.first)
|
|
|
|
|
return a.second <= b.second;
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
for(auto x : frags){
|
|
|
|
|
auto first = x->refered().first();
|
|
|
|
|
auto last = x->refered().last();
|
|
|
|
|
|
|
|
|
|
auto tcursor = std::make_pair(cursor.block().blockNumber(), cursor.positionInBlock());
|
|
|
|
|
auto fcursor = std::make_pair(first->row(), first->column());
|
|
|
|
|
auto lcursor = std::make_pair(last->row(), last->column()+last->length());
|
|
|
|
|
|
|
|
|
|
if(less_equal_than(fcursor, tcursor) && less_equal_than(tcursor, lcursor))
|
|
|
|
|
{
|
|
|
|
|
auto name = static_cast<NamedNode*>(x)->name();
|
|
|
|
|
present_refersed_tips(name[1], name[0]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StoryUnitSourceEdit::present_refersed_tips(const QString &unit, const QString &frag)
|
|
|
|
|
{
|
|
|
|
|
QHash<QString, DesNode*> nodes;
|
|
|
|
|
|
|
|
|
|
auto storys = core_ins->parseCore()->allStoryBoards();
|
|
|
|
|
for (auto s : storys) {
|
|
|
|
|
auto xins = static_cast<NamedNode*>(s);
|
|
|
|
|
auto refs_x = s->children();
|
|
|
|
|
for(auto &ref : refs_x)
|
|
|
|
|
if(ref->typeValue() == NODE_FRAGMENTREFERENCE){
|
|
|
|
|
auto nm_ref = static_cast<NamedNode*>(ref);
|
|
|
|
|
|
|
|
|
|
if(nm_ref->name()[0] == frag && nm_ref->name()[1] == unit){
|
|
|
|
|
nodes[xins->name()[0]] = ref;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(auto &nm : nodes.keys())
|
|
|
|
|
{
|
|
|
|
|
QList<QStandardItem*> row;
|
|
|
|
|
row << new QStandardItem(nm);
|
|
|
|
|
row.last()->setEditable(false);
|
|
|
|
|
row << new QStandardItem(nodes[nm]->toString());
|
|
|
|
|
row.last()->setEditable(false);
|
|
|
|
|
|
|
|
|
|
refers_model->appendRow(row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
refers_descriptions->resizeColumnsToContents();
|
|
|
|
|
refers_descriptions->resizeRowsToContents();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-22 03:42:48 +00:00
|
|
|
|
void StoryUnitSourceEdit::jumpTo(const QList<QString> &path)
|
|
|
|
|
{
|
2022-12-31 13:05:58 +00:00
|
|
|
|
auto fpath = this->absoluteFilePath();
|
2022-11-22 06:15:36 +00:00
|
|
|
|
auto core = core_ins->parseCore();
|
|
|
|
|
if(path.size()){
|
|
|
|
|
auto storynode = core->queryStoryUnit(path[0]).first();
|
|
|
|
|
auto first_word = storynode->refered()[0];
|
|
|
|
|
|
2022-11-25 07:30:33 +00:00
|
|
|
|
auto textblock = this->textDocument()->findBlockByNumber(first_word->row());
|
|
|
|
|
auto cur = this->getTextCursor();
|
2022-11-22 06:15:36 +00:00
|
|
|
|
cur.setPosition(textblock.position());
|
|
|
|
|
cur.select(QTextCursor::SelectionType::LineUnderCursor);
|
2022-11-25 07:30:33 +00:00
|
|
|
|
this->setTextCursor(cur);
|
2022-11-22 06:15:36 +00:00
|
|
|
|
|
|
|
|
|
if(path.size() > 1){
|
|
|
|
|
auto storypoint = core->queryStoryFragment(storynode, path[1]).first();
|
|
|
|
|
first_word = storypoint->refered()[0];
|
|
|
|
|
|
2022-11-25 07:30:33 +00:00
|
|
|
|
auto textblock = this->textDocument()->findBlockByNumber(first_word->row());
|
2022-11-22 06:15:36 +00:00
|
|
|
|
cur.setPosition(textblock.position());
|
|
|
|
|
cur.select(QTextCursor::SelectionType::LineUnderCursor);
|
2022-11-25 07:30:33 +00:00
|
|
|
|
this->setTextCursor(cur);
|
2022-11-22 06:15:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 07:30:33 +00:00
|
|
|
|
textView()->setFocus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StoryUnitSourceEdit::resetProcsType(const QString &suffix)
|
|
|
|
|
{
|
|
|
|
|
|
2022-11-22 03:42:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-31 05:26:58 +00:00
|
|
|
|
QWidget *StoryUnitSourceEdit::textView() const
|
|
|
|
|
{
|
|
|
|
|
return split;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 01:18:54 +00:00
|
|
|
|
Extension *StoryUnitSourceEditFactory::newInstance(Core::AppCore *core)
|
2022-11-22 03:42:48 +00:00
|
|
|
|
{
|
|
|
|
|
return new StoryUnitSourceEdit(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<Scale> StoryUnitSourceEditFactory::configs() const
|
|
|
|
|
{
|
|
|
|
|
return QList<Scale>() << Scale::Project << Scale::Global;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 09:44:18 +00:00
|
|
|
|
namespace __impl_temp {
|
2022-11-22 03:42:48 +00:00
|
|
|
|
class UnitConfigration : public QWidget
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
UnitConfigration(Config::Configration* port)
|
|
|
|
|
{
|
|
|
|
|
auto layout = new QVBoxLayout(this);
|
|
|
|
|
|
|
|
|
|
auto fontfamily = new QComboBox(this);
|
|
|
|
|
auto families = QFontDatabase().families(QFontDatabase::WritingSystem::SimplifiedChinese);
|
|
|
|
|
fontfamily->addItems(families);
|
|
|
|
|
auto family = Config::ConfigHelper::getConfigAsDefault(
|
2022-12-31 05:26:58 +00:00
|
|
|
|
port, {"sourcecode_unitedit","default_font","font_family"}, "微软雅黑");
|
2022-11-22 03:42:48 +00:00
|
|
|
|
fontfamily->setCurrentText(family);
|
|
|
|
|
connect(fontfamily, &QComboBox::currentTextChanged, [port](const QString &text){
|
|
|
|
|
port->setConfig({"sourcecode_unitedit","default_font","font_family"}, text);
|
|
|
|
|
});
|
|
|
|
|
layout->addWidget(fontfamily);
|
|
|
|
|
|
|
|
|
|
auto fontsize = new QSpinBox(this);
|
|
|
|
|
fontsize->setRange(10, 200);
|
|
|
|
|
fontsize->setSingleStep(1);
|
|
|
|
|
auto size = Config::ConfigHelper::getConfigAsDefault(
|
2022-12-31 05:26:58 +00:00
|
|
|
|
port,{"sourcecode_unitedit","default_font","font_sizept"}, "20");
|
2022-11-22 03:42:48 +00:00
|
|
|
|
fontsize->setValue(size.toInt());
|
|
|
|
|
connect(fontsize, QOverload<int>::of(&QSpinBox::valueChanged), [port](int val){
|
2022-12-31 05:26:58 +00:00
|
|
|
|
port->setConfig({"sourcecode_unitedit","default_font","font_sizept"}, QString("%1").arg(val));
|
2022-11-22 03:42:48 +00:00
|
|
|
|
});
|
|
|
|
|
layout->addWidget(fontsize);
|
|
|
|
|
|
|
|
|
|
layout->addWidget(new QWidget(this), 1);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
QWidget *StoryUnitSourceEditFactory::getNewPanel(Config::Configration *config)
|
|
|
|
|
{
|
2023-02-25 09:44:18 +00:00
|
|
|
|
return new __impl_temp::UnitConfigration(config);
|
2022-11-22 03:42:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString StoryUnitSourceEditFactory::extensionName() const
|
|
|
|
|
{
|
|
|
|
|
return "单元编辑器";
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 01:32:42 +00:00
|
|
|
|
QList<QString> StoryUnitSourceEditFactory::suffixPeers() const
|
2022-11-22 03:42:48 +00:00
|
|
|
|
{
|
2022-11-25 01:32:42 +00:00
|
|
|
|
return QList<QString>() << "storyunit";
|
2022-11-22 03:42:48 +00:00
|
|
|
|
}
|