feat(editor): delete script document if script is deleted. also prevent duplicates
This commit is contained in:
parent
3b60b3b0ec
commit
de0acda8ac
4 changed files with 35 additions and 1 deletions
|
@ -484,13 +484,30 @@ std::optional<std::string> MainWindow::openFileDialog(QString filter, QString de
|
|||
}
|
||||
|
||||
void MainWindow::openScriptDocument(std::shared_ptr<Script> script) {
|
||||
// Document already exists, don't open it
|
||||
if (scriptDocuments.count(script) > 0) {
|
||||
ui->mdiArea->setActiveSubWindow(scriptDocuments[script]);
|
||||
return;
|
||||
}
|
||||
|
||||
ScriptDocument* doc = new ScriptDocument(script);
|
||||
scriptDocuments[script] = doc;
|
||||
doc->setAttribute(Qt::WA_DeleteOnClose, true);
|
||||
ui->mdiArea->addSubWindow(doc);
|
||||
ui->mdiArea->setActiveSubWindow(doc);
|
||||
doc->showMaximized();
|
||||
}
|
||||
|
||||
void MainWindow::closeScriptDocument(std::shared_ptr<Script> script) {
|
||||
if (scriptDocuments.count(script) == 0) return;
|
||||
|
||||
ScriptDocument* doc = scriptDocuments[script];
|
||||
ui->mdiArea->removeSubWindow(doc);
|
||||
ui->mdiArea->activeSubWindow()->showMaximized();
|
||||
scriptDocuments.erase(script);
|
||||
doc->deleteLater();
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
delete ui;
|
||||
|
|
|
@ -5,8 +5,10 @@
|
|||
#include "placedocument.h"
|
||||
#include "qbasictimer.h"
|
||||
#include "qcoreevent.h"
|
||||
#include "script/scriptdocument.h"
|
||||
#include <QMainWindow>
|
||||
#include <QLineEdit>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <qfiledialog.h>
|
||||
|
||||
|
@ -53,10 +55,12 @@ public:
|
|||
bool editSoundEffects = true;
|
||||
|
||||
void openScriptDocument(std::shared_ptr<Script>);
|
||||
void closeScriptDocument(std::shared_ptr<Script>);
|
||||
|
||||
Ui::MainWindow *ui;
|
||||
private:
|
||||
PlaceDocument* placeDocument;
|
||||
std::map<std::shared_ptr<Script>, ScriptDocument*> scriptDocuments;
|
||||
|
||||
void updateToolbars();
|
||||
void closeEvent(QCloseEvent* evt) override;
|
||||
|
|
|
@ -5,13 +5,16 @@
|
|||
#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 "mainwindow.h"
|
||||
#include "objects/script.h"
|
||||
#include "datatypes/meta.h"
|
||||
|
||||
std::map<int, const QColor> DARK_MODE_COLOR_SCHEME = {{
|
||||
{QsciLexerLua::Comment, QColor("#808080")},
|
||||
|
@ -37,6 +40,15 @@ ScriptDocument::ScriptDocument(std::shared_ptr<Script> script, QWidget* 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);
|
||||
|
@ -74,7 +86,6 @@ ScriptDocument::ScriptDocument(std::shared_ptr<Script> script, QWidget* parent):
|
|||
|
||||
// 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
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "datatypes/signal.h"
|
||||
#include <Qsci/qsciscintilla.h>
|
||||
#include <memory>
|
||||
#include <qmdisubwindow.h>
|
||||
|
@ -8,6 +9,7 @@ class Script;
|
|||
|
||||
class ScriptDocument : public QMdiSubWindow {
|
||||
std::shared_ptr<Script> script;
|
||||
SignalConnectionHolder scriptDeletionHandler;
|
||||
|
||||
QsciScintilla* scintilla;
|
||||
public:
|
||||
|
|
Loading…
Add table
Reference in a new issue