280 lines
8.6 KiB
C++
280 lines
8.6 KiB
C++
#include "storyunitsourceedit.h"
|
||
#include "keywordshightlighter.h"
|
||
#include <QComboBox>
|
||
#include <QMenu>
|
||
#include <QSpinBox>
|
||
#include <QSplitter>
|
||
#include <QVBoxLayout>
|
||
#include <QDebug>
|
||
|
||
using namespace Core;
|
||
using namespace Parse::Result;
|
||
using namespace Components;
|
||
using namespace Enhancement;
|
||
|
||
StoryUnitSourceEdit::StoryUnitSourceEdit(FileExtensionFactory *factory)
|
||
: refers_descriptions(new QTableView()), refers_model(new QStandardItemModel()),
|
||
split(new QSplitter()),
|
||
words_highlighter(new KeywordsHightlighter(this)), factory_ins(factory)
|
||
{
|
||
words_highlighter->setDocument(this->textDocument());
|
||
refers_descriptions->setModel(refers_model);
|
||
|
||
this->setContexMenuProcess([this](QMenu *menu){
|
||
menu->addSeparator();
|
||
menu->addAction("刷新", [this](){words_highlighter->rehighlight();});
|
||
});
|
||
|
||
connect(edit_square, &QTextEdit::cursorPositionChanged, this, &StoryUnitSourceEdit::cursor_contex_query);
|
||
|
||
auto vins = FormattedTextEdit::textView();
|
||
split->addWidget(vins);
|
||
split->addWidget(this->refers_descriptions);
|
||
}
|
||
|
||
StoryUnitSourceEdit::~StoryUnitSourceEdit()
|
||
{
|
||
delete this->refers_descriptions;
|
||
|
||
split->deleteLater();
|
||
}
|
||
|
||
FileExtensionFactory *StoryUnitSourceEdit::factory() const
|
||
{
|
||
return factory_ins;
|
||
}
|
||
|
||
void StoryUnitSourceEdit::reloadConfigrations(QList<Config::Configration *> configs)
|
||
{
|
||
// TODO: 设置格式
|
||
auto font_family = Config::ConfigHelper::getConfigAsDefaultSequence
|
||
(configs, {"sourcecode_unitedit","default_font","font_family"}, "微软雅黑");
|
||
auto font_size = Config::ConfigHelper::getConfigAsDefaultSequence(
|
||
configs, {"sourcecode_unitedit","default_font","font_sizept"}, "20");
|
||
QFont default_font;
|
||
default_font.setFamily(font_family);
|
||
default_font.setPointSize(font_size.toInt());
|
||
|
||
this->textDocument()->setDefaultFont(default_font);
|
||
}
|
||
|
||
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();
|
||
}
|
||
|
||
void StoryUnitSourceEdit::initSource(Core::AppCore *core, const QFileInfo &src, QWidget *parent)
|
||
{
|
||
this->source_target = src;
|
||
this->core_ins = core;
|
||
this->refers_descriptions->setParent(parent);
|
||
this->refers_model->setParent(parent);
|
||
this->split->setParent(parent);
|
||
|
||
FormattedTextEdit::initSource(core, src, parent);
|
||
static_cast<KeywordsHightlighter*>(words_highlighter)
|
||
->reset(core->parseCore()->queryDocument(src));
|
||
}
|
||
|
||
void StoryUnitSourceEdit::cursor_contex_query()
|
||
{
|
||
refers_model->clear();
|
||
refers_model->setHorizontalHeaderLabels(QStringList() << "故事" << "内容");
|
||
|
||
auto cursor = edit_square->textCursor();
|
||
auto doc = this->core_ins->parseCore()->queryDocument(QFileInfo(absoluteFilePath()));
|
||
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();
|
||
}
|
||
|
||
void StoryUnitSourceEdit::jumpTo(const QList<QString> &path)
|
||
{
|
||
auto fpath = this->absoluteFilePath();
|
||
auto core = core_ins->parseCore();
|
||
if(path.size()){
|
||
auto storynode = core->queryStoryUnit(path[0]).first();
|
||
auto first_word = storynode->refered()[0];
|
||
|
||
auto textblock = this->textDocument()->findBlockByNumber(first_word->row());
|
||
auto cur = this->getTextCursor();
|
||
cur.setPosition(textblock.position());
|
||
cur.select(QTextCursor::SelectionType::LineUnderCursor);
|
||
this->setTextCursor(cur);
|
||
|
||
if(path.size() > 1){
|
||
auto storypoint = core->queryStoryFragment(storynode, path[1]).first();
|
||
first_word = storypoint->refered()[0];
|
||
|
||
auto textblock = this->textDocument()->findBlockByNumber(first_word->row());
|
||
cur.setPosition(textblock.position());
|
||
cur.select(QTextCursor::SelectionType::LineUnderCursor);
|
||
this->setTextCursor(cur);
|
||
}
|
||
}
|
||
|
||
textView()->setFocus();
|
||
}
|
||
|
||
void StoryUnitSourceEdit::resetProcsType(const QString &suffix)
|
||
{
|
||
|
||
}
|
||
|
||
QWidget *StoryUnitSourceEdit::textView() const
|
||
{
|
||
return split;
|
||
}
|
||
|
||
Extension *StoryUnitSourceEditFactory::newInstance(Core::AppCore *core)
|
||
{
|
||
return new StoryUnitSourceEdit(this);
|
||
}
|
||
|
||
QList<Scale> StoryUnitSourceEditFactory::configs() const
|
||
{
|
||
return QList<Scale>() << Scale::Project << Scale::Global;
|
||
}
|
||
|
||
namespace __temp {
|
||
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(
|
||
port, {"sourcecode_unitedit","default_font","font_family"}, "微软雅黑");
|
||
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(
|
||
port,{"sourcecode_unitedit","default_font","font_sizept"}, "20");
|
||
fontsize->setValue(size.toInt());
|
||
connect(fontsize, QOverload<int>::of(&QSpinBox::valueChanged), [port](int val){
|
||
port->setConfig({"sourcecode_unitedit","default_font","font_sizept"}, QString("%1").arg(val));
|
||
});
|
||
layout->addWidget(fontsize);
|
||
|
||
layout->addWidget(new QWidget(this), 1);
|
||
}
|
||
};
|
||
}
|
||
QWidget *StoryUnitSourceEditFactory::getNewPanel(Config::Configration *config)
|
||
{
|
||
return new __temp::UnitConfigration(config);
|
||
}
|
||
|
||
QString StoryUnitSourceEditFactory::extensionName() const
|
||
{
|
||
return "单元编辑器";
|
||
}
|
||
|
||
QList<QString> StoryUnitSourceEditFactory::suffixPeers() const
|
||
{
|
||
return QList<QString>() << "storyunit";
|
||
}
|