#include "storychain_sourceeditor.h" #include "keywordshightlighter.h" #include #include #include #include #include using namespace Enhancement; using namespace Parse::Result; using namespace Core; using namespace Components; StorychainSourceEdit::StorychainSourceEdit() : highter_ins(new KeywordsHightlighter(this)), edit_square(new QTextEdit) { connect(edit_square, &QTextEdit::textChanged, [this](){ emit this->dataChanged(filepath_store); }); highter_ins->setDocument(edit_square->document()); } void StorychainSourceEdit::contexBind(AppCore *app, Parse::Result::DocCore *core) { core_ins = core; app_core = app; } void StorychainSourceEdit::reHighlighter() const { highter_ins->rehighlight(); } void StorychainSourceEdit::modeReset(const QString &) const{} QList StorychainSourceEdit::modes() const { return QList() << "源码"; } QString StorychainSourceEdit::currentMode() const { return modes()[0]; } QList StorychainSourceEdit::suffixes() const { return QList() << "storychain"; } std::tuple StorychainSourceEdit::create(const QDir &target, const QString &name, const QString &suffix) { auto path = target.filePath(QString("%1.%2").arg(name, suffix)); if(QFileInfo::exists(path)) return std::make_tuple(false, path); QFile nwf(path); if(!nwf.open(QIODevice::WriteOnly|QIODevice::Text)) throw new SimpleException("指定文件"); return std::make_tuple(true, target.relativeFilePath(path)); } MakeTools::ContentPresent *StorychainSourceEdit::newInst() const { return new StorychainSourceEdit(); } QWidget *StorychainSourceEdit::widget() const { return edit_square; } void StorychainSourceEdit::applySetting(const QString &name, Core::AppCore *core) { this->name_store = name; } QString StorychainSourceEdit::name() const { return name_store; } void StorychainSourceEdit::load(const QFileInfo &target_file) { if(!target_file.exists()) throw new SimpleException("参数错误:传入的文件不存在"); if(!target_file.isFile()) throw new SimpleException("参数错误:传入参数非文件类型"); filepath_store = target_file.absoluteFilePath(); QFile bin(filepath_store); if(!bin.open(QIODevice::ReadOnly|QIODevice::Text)) throw new SimpleException("参数错误:指定的文件无法打开"); QTextStream tin(&bin); tin.setCodec("UTF-8"); this->edit_square->setText(tin.readAll()); } void StorychainSourceEdit::saveAs(const QString &path) { if(!path.isEmpty()) filepath_store = path; QFile bout(filepath_store); if(!bout.open(QIODevice::WriteOnly|QIODevice::Text)) throw new SimpleException("参数错误:指定的文件无法打开"); QTextStream tout(&bout); tout.setCodec("UTF-8"); tout << this->edit_square->toPlainText(); tout.flush(); } QString StorychainSourceEdit::targetPath(const QDir &base) const { return base.relativeFilePath(filepath_store); } QString StorychainSourceEdit::absulateTargetPath() const { return filepath_store; } QString StorychainSourceEdit::getText() const { return edit_square->toPlainText(); } bool StorychainSourceEdit::isModified() const { return true; } void StorychainSourceEdit::jumpTo(const QList &path) { auto fpath = this->absulateTargetPath(); auto core = app_core->parseCore(); if(path.size()){ auto storynode = core->queryStoryChain(path[0]).first(); auto first_word = storynode->refered()[0]; auto textblock = this->edit_square->document()->findBlockByNumber(first_word->row()); auto cur = this->edit_square->textCursor(); cur.setPosition(textblock.position()); cur.select(QTextCursor::SelectionType::LineUnderCursor); this->edit_square->setTextCursor(cur); if(path.size() > 1){ auto storypoint = core->queryStoryPoint(storynode, path[1]).first(); first_word = storypoint->refered()[0]; auto textblock = this->edit_square->document()->findBlockByNumber(first_word->row()); cur.setPosition(textblock.position()); cur.select(QTextCursor::SelectionType::LineUnderCursor); this->edit_square->setTextCursor(cur); } } this->widget()->setFocus(); }