]> SALOME platform Git repositories - modules/yacs.git/blob - src/py2yacsgui/Py2YacsDialog.cxx
Salome HOME
New Py2YacsDialog using PyEditor_Window.
[modules/yacs.git] / src / py2yacsgui / Py2YacsDialog.cxx
1 // Copyright (C) 2016-2017  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 {
32   QVBoxLayout *mainLayout = new QVBoxLayout;
33   _pyEditorWindow = new PyEditor_Window;
34
35   _errorMessages = new QTextEdit(this);
36   _errorMessages->setReadOnly(true);
37   _errorMessages->hide();
38   QSplitter * splitterW = new QSplitter(Qt::Vertical);
39   splitterW->addWidget(_pyEditorWindow);
40   splitterW->addWidget(_errorMessages);
41   mainLayout->addWidget(splitterW);
42   
43   QHBoxLayout *validationLayout = new QHBoxLayout;
44   _okButton = new QPushButton(tr("py -> &YACS"));
45   QPushButton * cancelButton = new QPushButton(tr("&Cancel"));
46   validationLayout->addWidget(_okButton);
47   validationLayout->addWidget(cancelButton);
48   mainLayout->addLayout(validationLayout);
49
50   setLayout(mainLayout);
51   setWindowTitle(tr("Python to YACS schema editor"));
52
53   connect(cancelButton,SIGNAL(clicked()),this, SLOT(reject()));
54   connect(_okButton,SIGNAL(clicked()),this, SLOT(onExport()));
55
56   // PyEditor_Window has a button "exit".
57   // Trigger "cancel" when the editor is closed.
58   _pyEditorWindow->setAttribute(Qt::WA_DeleteOnClose);
59   connect(_pyEditorWindow,SIGNAL(destroyed()),this, SLOT(reject()));
60 }
61
62 void Py2YacsDialog::onExport()
63 {
64   PyEditor_Widget* pyEdit = dynamic_cast<PyEditor_Widget*>
65                                              (_pyEditorWindow->centralWidget());
66   if(!pyEdit)
67   {
68     reject();
69     return;
70   }
71
72   Py2yacs converter;
73   std::string text = pyEdit->text().toStdString();
74   try
75   {
76     converter.load(text);
77     // _exec -> default name for OPENTURNS functions
78     std::string errors = converter.getFunctionErrors("_exec");
79     if(errors.empty())
80     {
81       QSettings settings;
82       QString currentDir = settings.value("currentDir").toString();
83       if (currentDir.isEmpty())
84         currentDir = QDir::homePath();
85       QString fileName = QFileDialog::getSaveFileName(this,
86                                   tr("Save to YACS schema..."),
87                                   currentDir,
88                                   QString("%1 (*.xml)" ).arg( tr("xml files")));
89       if (!fileName.isEmpty())
90       {
91         if (!fileName.endsWith(".xml"))
92           fileName += ".xml";
93         QFile file(fileName);
94         settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
95         converter.save(fileName.toStdString(), "_exec");
96         _yacsFile = fileName;
97         accept();
98       }
99     }
100     else
101     {
102       _errorMessages->show();
103       _errorMessages->setText(errors.c_str());
104     }
105   }
106   catch(Py2yacsException& e)
107   {
108     const char * error = e.what();
109     _errorMessages->show();
110     _errorMessages->setText(QString(error));
111     return;
112   }
113 }
114
115 QString Py2YacsDialog::getYacsFile()
116 {
117   return _yacsFile;
118 }