170 lines
5.4 KiB
C++
170 lines
5.4 KiB
C++
#include "storyunitsourceedit.h"
|
||
#include "keywordshightlighter.h"
|
||
#include <QComboBox>
|
||
#include <QMenu>
|
||
#include <QSpinBox>
|
||
#include <QVBoxLayout>
|
||
|
||
using namespace Core;
|
||
using namespace Parse::Result;
|
||
using namespace Components;
|
||
using namespace Enhancement;
|
||
|
||
StoryUnitSourceEdit::StoryUnitSourceEdit(FileExtensionFactory *factory)
|
||
: words_highlighter(new KeywordsHightlighter(this)), factory_ins(factory)
|
||
{
|
||
words_highlighter->setDocument(this->edit_square->document());
|
||
|
||
edit_square->setContextMenuPolicy(Qt::CustomContextMenu);
|
||
connect(edit_square, &QTextEdit::customContextMenuRequested, [this](const QPoint &pos){
|
||
auto menu = edit_square->createStandardContextMenu();
|
||
menu->addSeparator();
|
||
menu->addAction("刷新", [this](){words_highlighter->rehighlight();});
|
||
menu->exec(edit_square->mapToGlobal(pos));
|
||
});
|
||
}
|
||
|
||
FileExtensionFactory *StoryUnitSourceEdit::factory() const
|
||
{
|
||
return factory_ins;
|
||
}
|
||
|
||
void StoryUnitSourceEdit::reload(QList<Config::Configration *> configs)
|
||
{
|
||
this->configrations_host = 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->edit_square->document()->setDefaultFont(default_font);
|
||
this->rehighlighter();
|
||
}
|
||
|
||
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::setSource(Core::AppCore *core, const QFileInfo &src, QWidget *parent)
|
||
{
|
||
this->source_target = src;
|
||
this->core_ins = core;
|
||
FormattedTextEdit::setSource(core, src, parent);
|
||
static_cast<KeywordsHightlighter*>(words_highlighter)
|
||
->reset(core->parseCore()->queryDocument(src));
|
||
rehighlighter();
|
||
}
|
||
|
||
void StoryUnitSourceEdit::jumpTo(const QList<QString> &path)
|
||
{
|
||
auto fpath = this->filePath();
|
||
auto core = core_ins->parseCore();
|
||
if(path.size()){
|
||
auto storynode = core->queryStoryUnit(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->queryStoryFragment(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);
|
||
}
|
||
}
|
||
|
||
edit_square->setFocus();
|
||
}
|
||
|
||
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";
|
||
}
|