QtNovelUI/WordsIDE/srcedit_storyboard.cpp

153 lines
4.1 KiB
C++

#include "srcedit_storyboard.h"
#include "keywordshighlighter.h"
#include <QComboBox>
#include <QMenu>
#include <QSpinBox>
#include <QTextStream>
#include <QVBoxLayout>
using namespace Components;
using namespace Enhancement;
using namespace Core;
class Impl_EditError2 : public MakeTools::EditException {
public:
Impl_EditError2(const QString &title, const QString &reason) {
this->t = title;
this->r = reason;
buffer = r.toLocal8Bit();
}
// exception interface
public:
virtual const char *what() const override { return buffer; }
// ParseException interface
public:
virtual QString reason() const override { return r; }
virtual QString title() const override { return t; }
private:
QString r, t;
QByteArray buffer;
};
QString StorySourceEdit::file_suffix = "storyboard";
StorySourceEdit::StorySourceEdit(QObject *parent)
: MakeTools::FilePresent(parent),
// words_highlighter(new KeywordsHighlighter(this)),
edit_square(new QTextEdit) {
// words_highlighter->setDocument(this->edit_square->document());
connect(edit_square, &QTextEdit::textChanged, [this](){
emit this->dataChanged(filepath_store);
});
}
void StorySourceEdit::contexBinding(Core::AppCore *app) { this->core_temp = app; }
void StorySourceEdit::renderRepeat() const {
// this->words_highlighter->rehighlight();
}
QWidget *StorySourceEdit::widget() const
{
return edit_square;
}
QString StorySourceEdit::name() const
{
return this->name_store;
}
void StorySourceEdit::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 StorySourceEdit::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 StorySourceEdit::relativeTargetPath(const QDir &base) const
{
return base.relativeFilePath(filepath_store);
}
QString StorySourceEdit::absoluteTargetPath() const
{
return filepath_store;
}
QString StorySourceEdit::getText() const
{
return edit_square->toPlainText();
}
bool StorySourceEdit::isModified() const
{
return true;
}
void StorySourceEdit::applySetting(const QString &name, Core::AppCore *core)
{
this->name_store = name;
}
void StorySourceEdit::jumpTo(const QList<QString> &path) {
// auto fpath = this->absoluteTargetPath();
// auto core = core_temp->parseCore();
// if(path.size()){
// auto storynode = core->queryStoryBoard(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());
// this->edit_square->setTextCursor(cur);
// }
}
MakeTools::FilePresent::Features StorySourceEdit::features()
{
return Feature::Compile | Feature::HighLight;
}
void StorySourceEditFactory::create(const QFileInfo &target) {
QFile base_f(target.canonicalFilePath());
if (!base_f.exists()) {
throw new Impl_EditError2("新建错误", "指定路径不合法,文件不存在:" + target.absoluteFilePath());
}
if (!base_f.open(QIODevice::WriteOnly | QIODevice::Text))
throw new Impl_EditError2("新建错误", QString("指定文件无法建立:" + target.absoluteFilePath()));
base_f.write(" ");
base_f.flush();
base_f.close();
}