diff --git a/src/UI/AbstractDocumentView.hh b/src/UI/AbstractDocumentView.hh index 43dc02e99877050426f3f9a227f4b57ce3e84692..29d22aedd82fa099e04d75dd0e6e36bc7c765fca 100644 --- a/src/UI/AbstractDocumentView.hh +++ b/src/UI/AbstractDocumentView.hh @@ -22,7 +22,7 @@ class AbstractDocumentView : public QMainWindow { public: enum class Type : quint64 { Vivy = Utils::toUnderlying(Utils::DocumentType::Vivy), - Script = Utils::toUnderlying(Utils::DocumentType::VivyScript), + Script = Utils::toUnderlying(Utils::DocumentType::VivyScript) }; public: diff --git a/src/UI/MainWindow.cc b/src/UI/MainWindow.cc index 9a64c713a75ca99874bc108114d4da283f521095..a375c4a4a2fefa93ef88d6a09cf0f0af0d4d8a40 100644 --- a/src/UI/MainWindow.cc +++ b/src/UI/MainWindow.cc @@ -119,24 +119,22 @@ MainWindow::MainWindow() noexcept } }; + { #define CONNECT_ENABLE(act, func) \ connect(documents, &QTabWidget::currentChanged, act, std::bind_front(func, act)); - connect(documents, &QTabWidget::currentChanged, this, - [this](int) noexcept -> void { documentViewActionsChanged(); }); + connect(documents, &QTabWidget::currentChanged, this, + [this](int) noexcept -> void { documentViewActionsChanged(); }); - CONNECT_ENABLE(saveFileAct, enableSaveOnDocument); - CONNECT_ENABLE(saveFileAsAct, enableSaveOnDocument); + CONNECT_ENABLE(saveFileAct, enableSaveOnDocument); + CONNECT_ENABLE(saveFileAsAct, enableSaveOnDocument); - CONNECT_ENABLE(loadSubDocumentAssAct, enableLoadSubOnDocument); - CONNECT_ENABLE(loadSubDocumentVideoAct, enableLoadSubOnDocument); - CONNECT_ENABLE(loadSubDocumentAudioAct, enableLoadSubOnDocument); + CONNECT_ENABLE(loadSubDocumentAssAct, enableLoadSubOnDocument); + CONNECT_ENABLE(loadSubDocumentVideoAct, enableLoadSubOnDocument); + CONNECT_ENABLE(loadSubDocumentAudioAct, enableLoadSubOnDocument); #undef CONNECT_ENABLE - - // Add a new empty document that will will be replaced if nothing is added - // to it. - newDocument(); + } // Main window has finished its construction statusBar()->showMessage("QSimulate has started"); @@ -279,16 +277,32 @@ MainWindow::openDocument() noexcept return; } - qDebug() << "Try to load document" << filename; - std::shared_ptr<VivyDocument> document = vivyApp->documentStore.loadDocument(filename); + // TODO: Better implementation + // Load script document + if (filename.endsWith(".lua") || filename.endsWith(".vvs")) { + qDebug() << "Try to load script" << filename; + try { + addTab(new ScriptDocumentView(filename, documents)); + } catch (const std::runtime_error &e) { + qCritical() << "Failed to load script" << filename << "with error:" << e.what(); + } + } - try { - qDebug() << "Add the view to the MainWindow for" << filename; - addTab(new VivyDocumentView(document)); - } catch (const std::runtime_error &e) { - qCritical() << "Failed to create the document view for" << QFileInfo(filename).baseName() - << "with path" << filename << "and error:" << e.what(); - vivyApp->documentStore.closeDocument(document->getUuid()); + // TODO: Better implementation + // Load Vivy document + else { + qDebug() << "Try to load document" << filename; + std::shared_ptr<VivyDocument> document = vivyApp->documentStore.loadDocument(filename); + + try { + qDebug() << "Add the view to the MainWindow for" << filename; + addTab(new VivyDocumentView(document)); + } catch (const std::runtime_error &e) { + qCritical() + << "Failed to create the document view for" << QFileInfo(filename).baseName() + << "with path" << filename << "and error:" << e.what(); + vivyApp->documentStore.closeDocument(document->getUuid()); + } } } @@ -327,17 +341,6 @@ MainWindow::loadSubDocumentAudio() noexcept void MainWindow::addTab(AbstractDocumentView *tab) -{ - const int index = documents->addTab(tab, tab->getDocumentTabIcon(), tab->getDocumentTabName()); - documents->setTabToolTip(index, tab->getDocumentTabToolTip()); - documents->setCurrentIndex(index); - connect(tab, &AbstractDocumentView::viewActionsChanged, this, - &MainWindow::documentViewActionsChanged); - documentViewActionsChanged(); -} - -void -MainWindow::addTab(VivyDocumentView *tab) { int index = -1; if (const int untouched_index = findFirstUntouchedDocument(); untouched_index >= 0) { diff --git a/src/UI/MainWindow.hh b/src/UI/MainWindow.hh index 06a77a12da624bc200fed2759ccadf643e9ae8de..21b59e32deb83d64e7b68977cf0b043616c7c1f3 100644 --- a/src/UI/MainWindow.hh +++ b/src/UI/MainWindow.hh @@ -28,7 +28,6 @@ class MainWindow final : public QMainWindow { public: explicit MainWindow() noexcept; - ~MainWindow() noexcept = default; AbstractDocument *getCurrentDocument() const noexcept; template <class Document> Document *getCurrentDocument() const noexcept @@ -43,7 +42,6 @@ public: private: void addTab(AbstractDocumentView *); - void addTab(VivyDocumentView *); AbstractDocumentView *getTab(const int) const noexcept; AbstractDocumentView *getCurrentDocumentView() const; diff --git a/src/UI/ScriptDocumentView.cc b/src/UI/ScriptDocumentView.cc index 661a1fbe5e23e8e193161cad68c3515426012511..f529e4f0026e82b7676152beae0d8cba85bb64ad 100644 --- a/src/UI/ScriptDocumentView.cc +++ b/src/UI/ScriptDocumentView.cc @@ -1,10 +1,22 @@ #include "ScriptDocumentView.hh" +#include "ScriptViews/ScriptEditor.hh" +#include "../VivyApplication.hh" + +#include <QVBoxLayout> using namespace Vivy; -ScriptDocumentView::ScriptDocumentView(QWidget *parent) noexcept +ScriptDocumentView::ScriptDocumentView(const QString &path, QWidget *parent) : AbstractDocumentView(AbstractDocumentView::Type::Script, parent) + , editor(new ScriptEditor(this)) { + QFile textFile(path); + if (!textFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + throw std::runtime_error("Failed to open script file"); + } + + editor->setPlainText(textFile.readAll()); + setCentralWidget(editor); } void @@ -27,7 +39,7 @@ ScriptDocumentView::getDocumentTabToolTip() const noexcept QIcon ScriptDocumentView::getDocumentTabIcon() const noexcept { - return QIcon::fromTheme("text-x-script"); + return QIcon(VIVY_ICON_FILE); } void diff --git a/src/UI/ScriptDocumentView.hh b/src/UI/ScriptDocumentView.hh index e1a6804b68aa59935167684a8489af8f96e571cc..530b9bfa60d864ad9f2b3eec624ee4c4c2fdae0f 100644 --- a/src/UI/ScriptDocumentView.hh +++ b/src/UI/ScriptDocumentView.hh @@ -13,12 +13,14 @@ namespace Vivy { +class ScriptEditor; + class ScriptDocumentView final : public AbstractDocumentView { Q_OBJECT VIVY_UNMOVABLE_OBJECT(ScriptDocumentView) public: - explicit ScriptDocumentView(QWidget *parent = nullptr) noexcept; + explicit ScriptDocumentView(const QString &path, QWidget *parent = nullptr); void closeDocument() noexcept override; void openProperties() noexcept override; @@ -31,6 +33,9 @@ public: { return nullptr; } + +private: + ScriptEditor *editor{ nullptr }; }; }