Salome HOME
7d9afc07c274a1858003e8115dacfea12a127fa0
[modules/yacs.git] / src / py2yacsgui / Py2YacsDialog.cxx
1 // Copyright (C) 2016-2023  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Py2YacsDialog.hxx"
21 #include <PyEditor_Window.h>
22 #include <PyEditor_Widget.h>
23 #include <py2yacs.hxx>
24
25 Py2YacsDialog::Py2YacsDialog( QWidget* parent)
26 : QDialog(parent),
27   _yacsFile(),
28   _pyEditorWindow(0),
29   _errorMessages(0),
30   _okButton(0),
31   _exportXml(true),
32   _pyScript()
33 {
34   QVBoxLayout *mainLayout = new QVBoxLayout;
35   _pyEditorWindow = new PyEditor_Window;
36
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);
44   
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);
51
52   setLayout(mainLayout);
53   setWindowTitle(tr("Python to YACS schema editor"));
54
55   connect(cancelButton,SIGNAL(clicked()),this, SLOT(reject()));
56   connect(_okButton,SIGNAL(clicked()),this, SLOT(onExport()));
57
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()));
62 }
63
64 void Py2YacsDialog::onExport()
65 {
66   PyEditor_Widget* pyEdit = dynamic_cast<PyEditor_Widget*>
67                                              (_pyEditorWindow->centralWidget());
68   if(!pyEdit)
69   {
70     reject();
71     return;
72   }
73
74   Py2yacs converter;
75   _pyScript = pyEdit->text().toStdString();
76   try
77   {
78     converter.load(_pyScript);
79     // _exec -> default name for OPENTURNS functions
80     std::string errors = converter.getFunctionErrors("_exec");
81     if(errors.empty())
82     {
83       if(_exportXml)
84       {
85         QSettings settings;
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..."),
91                                     currentDir,
92                                     QString("%1 (*.xml)" ).arg( tr("xml files")));
93         if (!fileName.isEmpty())
94         {
95           if (!fileName.endsWith(".xml"))
96             fileName += ".xml";
97           QFile file(fileName);
98           settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
99           converter.save(fileName.toStdString(), "_exec");
100           _yacsFile = fileName;
101           accept();
102         }
103       }
104       else
105         accept();
106     }
107     else
108     {
109       _errorMessages->show();
110       _errorMessages->setText(errors.c_str());
111     }
112   }
113   catch(Py2yacsException& e)
114   {
115     const char * error = e.what();
116     _errorMessages->show();
117     _errorMessages->setText(QString(error));
118     return;
119   }
120 }
121
122 QString Py2YacsDialog::getYacsFile()
123 {
124   return _yacsFile;
125 }
126
127 std::string Py2YacsDialog::getScriptText()
128 {
129   return _pyScript;
130 }
131
132 void Py2YacsDialog::setScriptText(const std::string& pyScript)
133 {
134   PyEditor_Widget* pyEdit = dynamic_cast<PyEditor_Widget*>
135                                              (_pyEditorWindow->centralWidget());
136   if(pyEdit != nullptr)
137     pyEdit->setText(QString(pyScript.c_str()));
138 }
139
140 void Py2YacsDialog::setExportXml(bool yes)
141 {
142   _exportXml = yes;
143 }