From 7a83c194e8c7fcd31085084c268f9d02bf0e53b5 Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Tue, 26 Feb 2019 16:48:17 +0100 Subject: [PATCH] Py2YacsDialog for future use in OTGUI Add an option to Py2YacsDialog in order to avoid the creation of the yacs file. The dialog box can be used to edit a python file and verify that it respects the format for py2yacs. --- src/py2yacsgui/Py2YacsDialog.cxx | 54 +++++++++++++++++++------------- src/py2yacsgui/Py2YacsDialog.hxx | 5 ++- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/py2yacsgui/Py2YacsDialog.cxx b/src/py2yacsgui/Py2YacsDialog.cxx index d2490561c..ccf3777f1 100644 --- a/src/py2yacsgui/Py2YacsDialog.cxx +++ b/src/py2yacsgui/Py2YacsDialog.cxx @@ -22,12 +22,14 @@ #include #include -Py2YacsDialog::Py2YacsDialog( QWidget* parent) +Py2YacsDialog::Py2YacsDialog( QWidget* parent, bool exportXml) : QDialog(parent), _yacsFile(), _pyEditorWindow(0), _errorMessages(0), - _okButton(0) + _okButton(0), + _exportXml(exportXml), + _pyScript() { QVBoxLayout *mainLayout = new QVBoxLayout; _pyEditorWindow = new PyEditor_Window; @@ -70,32 +72,37 @@ void Py2YacsDialog::onExport() } Py2yacs converter; - std::string text = pyEdit->text().toStdString(); + _pyScript = pyEdit->text().toStdString(); try { - converter.load(text); + converter.load(_pyScript); // _exec -> default name for OPENTURNS functions std::string errors = converter.getFunctionErrors("_exec"); if(errors.empty()) { - QSettings settings; - QString currentDir = settings.value("currentDir").toString(); - if (currentDir.isEmpty()) - currentDir = QDir::homePath(); - QString fileName = QFileDialog::getSaveFileName(this, - tr("Save to YACS schema..."), - currentDir, - QString("%1 (*.xml)" ).arg( tr("xml files"))); - if (!fileName.isEmpty()) + if(_exportXml) { - if (!fileName.endsWith(".xml")) - fileName += ".xml"; - QFile file(fileName); - settings.setValue("currentDir", QFileInfo(fileName).absolutePath()); - converter.save(fileName.toStdString(), "_exec"); - _yacsFile = fileName; - accept(); + QSettings settings; + QString currentDir = settings.value("currentDir").toString(); + if (currentDir.isEmpty()) + currentDir = QDir::homePath(); + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save to YACS schema..."), + currentDir, + QString("%1 (*.xml)" ).arg( tr("xml files"))); + if (!fileName.isEmpty()) + { + if (!fileName.endsWith(".xml")) + fileName += ".xml"; + QFile file(fileName); + settings.setValue("currentDir", QFileInfo(fileName).absolutePath()); + converter.save(fileName.toStdString(), "_exec"); + _yacsFile = fileName; + accept(); + } } + else + accept(); } else { @@ -115,4 +122,9 @@ void Py2YacsDialog::onExport() QString Py2YacsDialog::getYacsFile() { return _yacsFile; -} \ No newline at end of file +} + +std::string Py2YacsDialog::getScriptText() +{ + return _pyScript; +} diff --git a/src/py2yacsgui/Py2YacsDialog.hxx b/src/py2yacsgui/Py2YacsDialog.hxx index 6144ae17e..782533738 100644 --- a/src/py2yacsgui/Py2YacsDialog.hxx +++ b/src/py2yacsgui/Py2YacsDialog.hxx @@ -27,8 +27,9 @@ class PY2YACSGUILIB_EXPORT Py2YacsDialog : public QDialog { Q_OBJECT public: - Py2YacsDialog( QWidget* parent=0); + Py2YacsDialog( QWidget* parent=0, bool exportXml = true); QString getYacsFile(); + std::string getScriptText(); public slots: virtual void onExport(); @@ -38,6 +39,8 @@ class PY2YACSGUILIB_EXPORT Py2YacsDialog : public QDialog PyEditor_Window *_pyEditorWindow; QTextEdit * _errorMessages; QPushButton *_okButton; + bool _exportXml; + std::string _pyScript; }; #endif // PY2YACSDIALOG_HXX -- 2.39.2