1 // Copyright (C) 2016-2024 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "Py2YacsDialog.hxx"
21 #include <PyEditor_Window.h>
22 #include <PyEditor_Widget.h>
23 #include <py2yacs.hxx>
25 Py2YacsDialog::Py2YacsDialog( QWidget* parent)
34 QVBoxLayout *mainLayout = new QVBoxLayout;
35 _pyEditorWindow = new PyEditor_Window;
37 _errorMessages = new QTextEdit(this);
38 _errorMessages->setReadOnly(true);
39 _errorMessages->hide();
40 QSplitter * splitterW = new QSplitter(Qt::Vertical);
41 splitterW->addWidget(_pyEditorWindow);
42 splitterW->addWidget(_errorMessages);
43 mainLayout->addWidget(splitterW);
45 QHBoxLayout *validationLayout = new QHBoxLayout;
46 _okButton = new QPushButton(tr("py -> &YACS"));
47 QPushButton * cancelButton = new QPushButton(tr("&Cancel"));
48 validationLayout->addWidget(_okButton);
49 validationLayout->addWidget(cancelButton);
50 mainLayout->addLayout(validationLayout);
52 setLayout(mainLayout);
53 setWindowTitle(tr("Python to YACS schema editor"));
55 connect(cancelButton,SIGNAL(clicked()),this, SLOT(reject()));
56 connect(_okButton,SIGNAL(clicked()),this, SLOT(onExport()));
58 // PyEditor_Window has a button "exit".
59 // Trigger "cancel" when the editor is closed.
60 _pyEditorWindow->setAttribute(Qt::WA_DeleteOnClose);
61 connect(_pyEditorWindow,SIGNAL(destroyed()),this, SLOT(reject()));
64 void Py2YacsDialog::onExport()
66 PyEditor_Widget* pyEdit = dynamic_cast<PyEditor_Widget*>
67 (_pyEditorWindow->centralWidget());
75 _pyScript = pyEdit->text().toStdString();
78 converter.load(_pyScript);
79 // _exec -> default name for OPENTURNS functions
80 std::string errors = converter.getFunctionErrors("_exec");
86 QString currentDir = settings.value("currentDir").toString();
87 if (currentDir.isEmpty())
88 currentDir = QDir::homePath();
89 QString fileName = QFileDialog::getSaveFileName(this,
90 tr("Save to YACS schema..."),
92 QString("%1 (*.xml)" ).arg( tr("xml files")));
93 if (!fileName.isEmpty())
95 if (!fileName.endsWith(".xml"))
98 settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
99 converter.save(fileName.toStdString(), "_exec");
100 _yacsFile = fileName;
109 _errorMessages->show();
110 _errorMessages->setText(errors.c_str());
113 catch(Py2yacsException& e)
115 const char * error = e.what();
116 _errorMessages->show();
117 _errorMessages->setText(QString(error));
122 QString Py2YacsDialog::getYacsFile()
127 std::string Py2YacsDialog::getScriptText()
132 void Py2YacsDialog::setScriptText(const std::string& pyScript)
134 PyEditor_Widget* pyEdit = dynamic_cast<PyEditor_Widget*>
135 (_pyEditorWindow->centralWidget());
136 if(pyEdit != nullptr)
137 pyEdit->setText(QString(pyScript.c_str()));
140 void Py2YacsDialog::setExportXml(bool yes)