From 684a33ee35a691d1906247b119ae3d20df3e236a Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 22 Oct 2024 04:07:35 +0300 Subject: [PATCH] Force saving of the script to TRIK Studio save file --- qrgui/mainWindow/mainWindow.cpp | 23 +++++++++++++++++-- qrgui/mainWindow/mainWindow.h | 1 + qrgui/mainWindow/mainWindow.ui | 9 ++++++++ .../projectManager/projectManagerWrapper.cpp | 4 ++++ .../projectManager/projectManagerWrapper.h | 1 + .../projectManagementInterface.h | 2 ++ .../components/projectManager.cpp | 20 ++++++++++++++++ .../systemFacade/components/projectManager.h | 1 + .../projectManagementInterfaceMock.h | 1 + qrtranslations/fr/qrgui_mainWindow_fr.ts | 20 ++++++++++------ qrtranslations/fr/qrgui_system_facade_fr.ts | 8 +++---- qrtranslations/ru/qrgui_mainWindow_ru.ts | 20 ++++++++++------ qrtranslations/ru/qrgui_system_facade_ru.ts | 8 +++---- 13 files changed, 94 insertions(+), 24 deletions(-) diff --git a/qrgui/mainWindow/mainWindow.cpp b/qrgui/mainWindow/mainWindow.cpp index c84c4b02f0..e4e68f1a32 100644 --- a/qrgui/mainWindow/mainWindow.cpp +++ b/qrgui/mainWindow/mainWindow.cpp @@ -210,6 +210,7 @@ void MainWindow::connectActions() connect(mUi->actionSave, &QAction::triggered, this, &MainWindow::tryToSave); connect(mUi->actionSave_as, &QAction::triggered, &*mProjectManager, &ProjectManagerWrapper::suggestToSaveAs); connect(mUi->actionSave_diagram_as_a_picture, &QAction::triggered, this, &MainWindow::saveDiagramAsAPicture); + connect(mUi->actionSave_script, &QAction::triggered, this, &MainWindow::saveScript); connect(mUi->actionPrint, &QAction::triggered, this, &MainWindow::print); connect(mUi->actionMakeSvg, &QAction::triggered, this, &MainWindow::makeSvg); @@ -1270,15 +1271,16 @@ void MainWindow::currentTabChanged(int newIndex) const bool isStartTab = dynamic_cast(mUi->tabs->widget(newIndex)); const bool isGesturesTab = dynamic_cast(mUi->tabs->widget(newIndex)); const bool isDecorativeTab = isStartTab || isGesturesTab; + const bool isCodeTab = dynamic_cast(mUi->tabs->widget(newIndex)); - mUi->actionSave->setEnabled(!isDecorativeTab); + mUi->actionSave->setEnabled(!isDecorativeTab && !isCodeTab); mUi->actionSave_as->setEnabled(!isDecorativeTab); mUi->actionSave_diagram_as_a_picture->setEnabled(isEditorTab); mUi->actionPrint->setEnabled(!isDecorativeTab); mUi->actionFind->setEnabled(!isDecorativeTab); mUi->actionFind_and_replace->setEnabled(!isDecorativeTab); mUi->actionGesturesShow->setEnabled(qReal::SettingsManager::value("gesturesEnabled").toBool()); - + mUi->actionSave_script->setEnabled(isCodeTab); updateUndoRedoState(); if (isEditorTab) { @@ -2377,3 +2379,20 @@ void MainWindow::endPaletteModification() scene->update(); } } + +void MainWindow::saveScript() +{ + auto * const area = dynamic_cast(currentTab()); + if (!area) { + mErrorReporter->addWarning(tr("")); + return; + } + + auto extension = area->currentLanguage().extension; + auto code = area->text(); + if (!mProjectManager->saveScriptToProject(code, extension)) { + mErrorReporter->addWarning(tr("")); + return; + } +} + diff --git a/qrgui/mainWindow/mainWindow.h b/qrgui/mainWindow/mainWindow.h index 75ced6cba7..f6459ac1e7 100644 --- a/qrgui/mainWindow/mainWindow.h +++ b/qrgui/mainWindow/mainWindow.h @@ -276,6 +276,7 @@ private slots: void updatePaletteIcons(); void setTextChanged(qReal::text::QScintillaTextEdit *editor, bool changed); + void saveScript(); private: /// Initializes a tab if it is a diagram --- sets its logical and graphical diff --git a/qrgui/mainWindow/mainWindow.ui b/qrgui/mainWindow/mainWindow.ui index 753e85cc0a..32dc838261 100644 --- a/qrgui/mainWindow/mainWindow.ui +++ b/qrgui/mainWindow/mainWindow.ui @@ -45,6 +45,7 @@ + @@ -711,6 +712,14 @@ Save diagram as a picture... + + + + Save script + + + Ctrl+S + diff --git a/qrgui/mainWindow/projectManager/projectManagerWrapper.cpp b/qrgui/mainWindow/projectManager/projectManagerWrapper.cpp index 7d19b71566..46f816afce 100644 --- a/qrgui/mainWindow/projectManager/projectManagerWrapper.cpp +++ b/qrgui/mainWindow/projectManager/projectManagerWrapper.cpp @@ -294,6 +294,10 @@ bool ProjectManagerWrapper::saveText() { return mTextManager->saveText(false); } +bool ProjectManagerWrapper::saveScriptToProject(const QString &code, const QString &extension) { + return ProjectManager::saveScriptToProject(code, extension) && ProjectManagerWrapper::saveText(); +} + bool ProjectManagerWrapper::suggestToSaveAs() { if (mTextManager->saveText(true)) { diff --git a/qrgui/mainWindow/projectManager/projectManagerWrapper.h b/qrgui/mainWindow/projectManager/projectManagerWrapper.h index 1b9ec3a5af..6501f87e1e 100644 --- a/qrgui/mainWindow/projectManager/projectManagerWrapper.h +++ b/qrgui/mainWindow/projectManager/projectManagerWrapper.h @@ -53,6 +53,7 @@ public slots: void refreshWindowTitleAccordingToSaveFile(); bool saveText(); + bool saveScriptToProject(const QString &code, const QString &extension) override; bool askQuestion(const QString &title, const QString &question) const override; diff --git a/qrgui/plugins/toolPluginInterface/usedInterfaces/projectManagementInterface.h b/qrgui/plugins/toolPluginInterface/usedInterfaces/projectManagementInterface.h index 9644ae086f..fc351109e0 100644 --- a/qrgui/plugins/toolPluginInterface/usedInterfaces/projectManagementInterface.h +++ b/qrgui/plugins/toolPluginInterface/usedInterfaces/projectManagementInterface.h @@ -62,6 +62,8 @@ public slots: /// Similarly @see save(), if specified project-file, similarly @see suggestToSaveAs() o/w virtual bool saveOrSuggestToSaveAs() = 0; + virtual bool saveScriptToProject(const QString &code, const QString &extension) = 0; + public: /// Returns true if some .qrs project is currently opened in system. Otherwise returns false. virtual bool somethingOpened() const = 0; diff --git a/qrgui/systemFacade/components/projectManager.cpp b/qrgui/systemFacade/components/projectManager.cpp index 88de0f93c1..3a48f672db 100644 --- a/qrgui/systemFacade/components/projectManager.cpp +++ b/qrgui/systemFacade/components/projectManager.cpp @@ -191,6 +191,7 @@ bool ProjectManager::import(const QString &fileName) bool ProjectManager::saveFileExists(const QString &fileName) const { + qDebug() << __PRETTY_FUNCTION__ << "saveFileExists"; if (!QFile::exists(fileName)) { fileNotFoundMessage(fileName); return false; @@ -304,6 +305,7 @@ bool ProjectManager::saveTo(const QString &fileName) bool ProjectManager::save() { + qDebug() << __PRETTY_FUNCTION__ << "SAVE"; // Do not change the method to saveAll - in the current implementation, an empty project in the repository is // created to initialize the file name with an empty string, which allows the internal state of the file // name = "" Attempt to save the project in this case result in @@ -316,6 +318,24 @@ bool ProjectManager::save() return false; } +bool ProjectManager::saveScriptToProject(const QString &script, const QString &extension) +{ + if (!script.isEmpty()) { + auto& repo = mModels.mutableLogicalRepoApi(); + repo.setMetaInformation("activeCode", script); + repo.setMetaInformation("activeCodeLanguageExtension", extension); + if (!mModels.repoControlApi().saveAll()) { + QLOG_INFO() << ""; + return false; + } + + return true; + } + + QLOG_INFO() << ""; + return false; +} + bool ProjectManager::restoreIncorrectlyTerminated() { return mAutosaver.checkTempFile(); diff --git a/qrgui/systemFacade/components/projectManager.h b/qrgui/systemFacade/components/projectManager.h index 03315cb03f..8c028f6913 100644 --- a/qrgui/systemFacade/components/projectManager.h +++ b/qrgui/systemFacade/components/projectManager.h @@ -43,6 +43,7 @@ public slots: bool saveAs(const QString &fileName) override; bool suggestToSaveAs() override; bool saveOrSuggestToSaveAs() override; + bool saveScriptToProject(const QString &code, const QString &extension) override; void setUnsavedIndicator(bool isUnsaved) override; diff --git a/qrtest/unitTests/mocks/qrgui/mainWindow/projectManager/projectManagementInterfaceMock.h b/qrtest/unitTests/mocks/qrgui/mainWindow/projectManager/projectManagementInterfaceMock.h index be17a1712b..f7de08cc17 100644 --- a/qrtest/unitTests/mocks/qrgui/mainWindow/projectManager/projectManagementInterfaceMock.h +++ b/qrtest/unitTests/mocks/qrgui/mainWindow/projectManager/projectManagementInterfaceMock.h @@ -40,6 +40,7 @@ class ProjectManagementInterfaceMock : public qReal::ProjectManagementInterface MOCK_METHOD0(openEmptyWithSuggestToSaveChanges, bool()); MOCK_METHOD1(open, bool(const QString &fileName)); MOCK_METHOD0(suggestToSaveChangesOrCancel, bool()); + MOCK_METHOD2(saveScriptToProject, bool(const QString &code, const QString &extension)); MOCK_METHOD1(setUnsavedIndicator, void(bool isUnsaved)); }; diff --git a/qrtranslations/fr/qrgui_mainWindow_fr.ts b/qrtranslations/fr/qrgui_mainWindow_fr.ts index 99ff9c30a2..2076e6172d 100644 --- a/qrtranslations/fr/qrgui_mainWindow_fr.ts +++ b/qrtranslations/fr/qrgui_mainWindow_fr.ts @@ -80,7 +80,7 @@ &Fichier - + &View &Affichage @@ -258,11 +258,12 @@ + Ctrl+S Ctrl+S - + Save as... Enregistrer sous... @@ -415,6 +416,11 @@ + Save script + + + + Close project Fermer le projet @@ -708,7 +714,7 @@ qReal::MainWindow - + Could not save file, try to save it to another place @@ -717,7 +723,7 @@ À propo de QReal - + Restore default settings @@ -729,7 +735,7 @@ WARNING: The settings will be restored after application restart - + Error Erreur @@ -837,7 +843,7 @@ WARNING: The settings will be restored after application restart - + Gestures Show Afficher les gestes @@ -915,7 +921,7 @@ WARNING: The settings will be restored after application restart [modifié] - + Select file to save current metamodel to Choisir un fichier pour enregister le metamodèle courant diff --git a/qrtranslations/fr/qrgui_system_facade_fr.ts b/qrtranslations/fr/qrgui_system_facade_fr.ts index 5bcff6c7f0..d5c65797e0 100644 --- a/qrtranslations/fr/qrgui_system_facade_fr.ts +++ b/qrtranslations/fr/qrgui_system_facade_fr.ts @@ -29,7 +29,7 @@ - + Can`t open project file Le fichier du projet ne peut pas être ouvert @@ -90,7 +90,7 @@ Ouvrir un projet existant - + Open project @@ -111,7 +111,7 @@ Choisir le fichier avec la sauvegarde pour importer - + There are missing plugins Manque d'extensions @@ -132,7 +132,7 @@ Le fichier du projet ne peut pas être ouvert - + Select file to save current model to Choisissez le ficher pour enregister le modèle diff --git a/qrtranslations/ru/qrgui_mainWindow_ru.ts b/qrtranslations/ru/qrgui_mainWindow_ru.ts index 62ee5f5e24..ac4eb0ce0a 100644 --- a/qrtranslations/ru/qrgui_mainWindow_ru.ts +++ b/qrtranslations/ru/qrgui_mainWindow_ru.ts @@ -80,7 +80,7 @@ Ф&айл - + &View &Вид @@ -258,11 +258,12 @@ + Ctrl+S Ctrl+S - + Save as... Сохранить как... @@ -415,6 +416,11 @@ + Save script + + + + Close project Закрыть проект @@ -735,7 +741,7 @@ О QReal - + Error Ошибка @@ -788,7 +794,7 @@ Создать диаграмму - + Restore default settings Восстановить настройки по-умолчанию @@ -802,7 +808,7 @@ WARNING: The settings will be restored after application restart ВНИМАНИЕ: Настройки будут сброшены после перезапуска приложения - + Could not save file, try to save it to another place Не удалось сохранить файл, попробуйте сохранить его в другое место @@ -862,7 +868,7 @@ WARNING: The settings will be restored after application restart Показать/спрятать панель ошибок - + Gestures Show Жесты мышью @@ -1029,7 +1035,7 @@ WARNING: The settings will be restored after application restart [изменён] - + Select file to save current metamodel to Выберите файл для сохранения метамодели diff --git a/qrtranslations/ru/qrgui_system_facade_ru.ts b/qrtranslations/ru/qrgui_system_facade_ru.ts index 9f8bd60224..c547fe71c4 100644 --- a/qrtranslations/ru/qrgui_system_facade_ru.ts +++ b/qrtranslations/ru/qrgui_system_facade_ru.ts @@ -29,7 +29,7 @@ - + Can`t open project file Не могу открыть проект @@ -90,7 +90,7 @@ Открыть проект - + Open project Открыть проект @@ -111,7 +111,7 @@ Выберите файл для импорта - + There are missing plugins Не хватает плагинов @@ -133,7 +133,7 @@ Не могу открыть проект - + Select file to save current model to Выберите файл для сохранения модели