104 lines
3.7 KiB
C++
104 lines
3.7 KiB
C++
#include "scriptdocument.h"
|
|
|
|
#include <Qsci/qsciscintilla.h>
|
|
#include <Qsci/qscilexerlua.h>
|
|
#include <Qsci/qsciscintillabase.h>
|
|
#include <Qsci/qscistyle.h>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <qboxlayout.h>
|
|
#include <qcolor.h>
|
|
#include <qfont.h>
|
|
#include <qdebug.h>
|
|
#include <qglobal.h>
|
|
#include <qlayout.h>
|
|
#include <qtextformat.h>
|
|
#include "mainwindow.h"
|
|
#include "objects/script.h"
|
|
#include "datatypes/meta.h"
|
|
|
|
std::map<int, const QColor> DARK_MODE_COLOR_SCHEME = {{
|
|
{QsciLexerLua::Comment, QColor("#808080")},
|
|
{QsciLexerLua::LineComment, QColor("#808080")},
|
|
{QsciLexerLua::Number, QColor("#6897BB")},
|
|
{QsciLexerLua::Keyword, QColor("#CC7832")},
|
|
{QsciLexerLua::String, QColor("#6A8759")},
|
|
{QsciLexerLua::Character, QColor("#6A8759")},
|
|
{QsciLexerLua::LiteralString, QColor("#6A8759")},
|
|
{QsciLexerLua::Preprocessor, QColor("#FF00FF")}, // Obsolete since Lua 4.0, but whatever
|
|
{QsciLexerLua::Operator, QColor("#FFFFFF")},
|
|
{QsciLexerLua::Identifier, QColor("#FFFFFF")},
|
|
{QsciLexerLua::UnclosedString, QColor("#6A8759")},
|
|
{QsciLexerLua::BasicFunctions, QColor("#CC7832")},
|
|
{QsciLexerLua::StringTableMathsFunctions, QColor("#CC7832")},
|
|
{QsciLexerLua::CoroutinesIOSystemFacilities, QColor("#CC7832")},
|
|
{QsciLexerLua::Label, QColor("#FFFFFF")},
|
|
|
|
}};
|
|
|
|
ScriptDocument::ScriptDocument(std::shared_ptr<Script> script, QWidget* parent):
|
|
script(script), QMdiSubWindow(parent) {
|
|
|
|
setWindowTitle(QString::fromStdString(script->name));
|
|
|
|
// Add detector for script deletion to automatically close this document
|
|
scriptDeletionHandler = script->AncestryChanged->Connect([this, script](std::vector<Data::Variant> args) {
|
|
std::weak_ptr<Instance> child = args[0].get<Data::InstanceRef>();
|
|
std::weak_ptr<Instance> newParent = args[1].get<Data::InstanceRef>();
|
|
if (child.expired() || child.lock() != script || !newParent.expired()) return;
|
|
|
|
dynamic_cast<MainWindow*>(window())->closeScriptDocument(script);
|
|
});
|
|
|
|
// QFrame* frame = new QFrame;
|
|
// QVBoxLayout* frameLayout = new QVBoxLayout;
|
|
// frame->setLayout(frameLayout);
|
|
scintilla = new QsciScintilla;
|
|
// frameLayout->addWidget(scintilla);
|
|
// setWidget(frame);
|
|
setWidget(scintilla);
|
|
|
|
// https://forum.qt.io/post/803690
|
|
QFont findFont("<NONE>");
|
|
findFont.setStyleHint(QFont::Monospace);
|
|
QFontInfo info(findFont);
|
|
|
|
QFont font;
|
|
font.setFamily(info.family());
|
|
font.setPointSize(12);
|
|
font.setFixedPitch(true);
|
|
|
|
// scintilla->setMargins(2);
|
|
scintilla->setScrollWidth(1); // Hide scrollbars on empty document, it will grow automatically
|
|
scintilla->setMarginType(1, QsciScintilla::NumberMargin);
|
|
scintilla->setMarginWidth(1, "0000");
|
|
scintilla->setMarginsForegroundColor(palette().windowText().color());
|
|
scintilla->setMarginsBackgroundColor(palette().window().color());
|
|
scintilla->setCaretForegroundColor(palette().text().color());
|
|
scintilla->setFont(font);
|
|
scintilla->setTabWidth(4);
|
|
|
|
scintilla->setText(QString::fromStdString(script->source));
|
|
|
|
QsciLexerLua* lexer = new QsciLexerLua;
|
|
lexer->setFont(font);
|
|
scintilla->setLexer(lexer);
|
|
|
|
// Set color scheme
|
|
// https://stackoverflow.com/a/26318796/16255372
|
|
for (auto& [style, color] : DARK_MODE_COLOR_SCHEME) {
|
|
lexer->setColor(color, style);
|
|
}
|
|
|
|
// lexer->setAutoIndentStyle(QsciScintilla::AiOpening | QsciScintilla::AiMaintain | QsciScintilla::AiClosing);
|
|
// scintilla->setAutoIndent(true);
|
|
|
|
connect(scintilla, &QsciScintilla::textChanged, [this]() {
|
|
// this-> is important here, as otherwise it will refer to the
|
|
// parameter passed in, which will get gc'ed eventually
|
|
this->script->source = scintilla->text().toStdString();
|
|
});
|
|
}
|
|
|
|
ScriptDocument::~ScriptDocument() {
|
|
}
|