Salome HOME
Update copyrights
[modules/yacs.git] / src / py2yacsgui / Py2YacsDialog_raw.cxx
1 // Copyright (C) 2016-2019  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 #include <QtWidgets>
20 #include <QSettings>
21 #ifdef HAS_PYEDITOR
22 #include <PyEditor_Editor.h>
23 #endif
24
25 #include "Py2YacsDialog_raw.hxx"
26
27 Py2YacsDialog_raw::Py2YacsDialog_raw( QWidget* parent)
28 : QDialog(parent)
29 {
30   QHBoxLayout *fileLayout = new QHBoxLayout;
31   QPushButton *loadButton = new QPushButton(tr("&Load"));
32   _saveButton = new QPushButton(tr("&Save"));
33   QPushButton *saveAsButton = new QPushButton(tr("Save &as ..."));
34   fileLayout->addWidget(loadButton);
35   fileLayout->addWidget(_saveButton);
36   fileLayout->addWidget(saveAsButton);
37
38 #ifdef HAS_PYEDITOR
39   _pyEditor = new PyEditor_Editor(this);
40 #else
41   _pyEditor = new QTextEdit(this);
42 #endif
43
44   QPushButton * applyButton = new QPushButton(tr("A&pply"));
45   QTextEdit *errorMessages = new QTextEdit(this);
46   errorMessages->setReadOnly(true);
47
48   QHBoxLayout *exportLayout = new QHBoxLayout;
49   _functionChosen = new QComboBox(this);
50   _exportButton = new QPushButton(tr("E&xport to YACS schema..."));
51   exportLayout->addWidget(new QLabel(tr("Function to run:")));
52   exportLayout->addWidget(_functionChosen);
53
54   QHBoxLayout *validationLayout = new QHBoxLayout;
55   _okButton = new QPushButton(tr("Save YACS schema and &quit"));
56   QPushButton * cancelButton = new QPushButton(tr("&Cancel"));
57   validationLayout->addWidget(_okButton);
58   validationLayout->addWidget(cancelButton);
59
60   QGroupBox *editWidget = new QGroupBox(tr("Python script:"));
61   QVBoxLayout *editLayout = new QVBoxLayout;
62   editLayout->addLayout(fileLayout);
63   editLayout->addWidget(_pyEditor);
64   editLayout->addWidget(applyButton);
65   editLayout->addLayout(exportLayout);
66   editWidget->setLayout(editLayout);
67
68   QGroupBox *messageWidget = new QGroupBox(tr("Messages:"));
69   QVBoxLayout *messageLayout = new QVBoxLayout;
70   messageLayout->addWidget(errorMessages);
71   messageWidget->setLayout(messageLayout);
72
73   QSplitter * splitterW = new QSplitter(Qt::Vertical);
74   splitterW->addWidget(editWidget);
75   splitterW->addWidget(messageWidget);
76
77   QVBoxLayout *mainLayout = new QVBoxLayout;
78   mainLayout->addWidget(splitterW);
79   mainLayout->addLayout(validationLayout);
80   setLayout(mainLayout);
81   setWindowTitle(tr("Python to YACS schema editor"));
82
83   invalidModel();
84   _saveButton->setEnabled(false);
85   connect(_pyEditor, SIGNAL(textChanged()), this, SLOT(invalidModel()));
86   connect(applyButton,SIGNAL(clicked()),this, SLOT(onApply()));
87
88   connect(&_model, SIGNAL(scriptChanged(const QString&)),
89           _pyEditor, SLOT(setText(const QString&)));
90   connect(&_model, SIGNAL(errorChanged(const QString&)),
91           errorMessages, SLOT(setText(const QString&)));
92   connect(&_model, SIGNAL(functionsChanged(std::list<std::string>)),
93           this, SLOT(onFunctionNamesChange(std::list<std::string>)));
94   connect(_functionChosen,SIGNAL(currentIndexChanged(const QString &)),
95           &_model, SLOT(setFunctionName(const QString&)));
96   connect(loadButton,SIGNAL(clicked()),this, SLOT(onLoad()));
97   connect(_saveButton,SIGNAL(clicked()),this, SLOT(onSave()));
98   connect(saveAsButton,SIGNAL(clicked()),this, SLOT(onSaveAs()));
99   connect(_exportButton,SIGNAL(clicked()),this, SLOT(onExport()));
100   connect(cancelButton,SIGNAL(clicked()),this, SLOT(reject()));
101   connect(_okButton,SIGNAL(clicked()),this, SLOT(onExport()));
102 }
103
104 void Py2YacsDialog_raw::onFunctionNamesChange(std::list<std::string> validFunctionNames)
105 {
106   int new_index = 0;
107   int count = 0;
108   QString lastChoice = _functionChosen->currentText();
109   _functionChosen->clear();
110   std::list<std::string>::const_iterator it;
111   for(it=validFunctionNames.begin(); it!=validFunctionNames.end(); it++)
112   {
113     _functionChosen->addItem(it->c_str());
114     if(lastChoice == it->c_str())
115       new_index = count;
116     count++;
117   }
118   _functionChosen->setCurrentIndex(new_index);
119 }
120
121 void Py2YacsDialog_raw::onLoad()
122 {
123   QSettings settings;
124   QString currentDir = settings.value("currentDir").toString();
125   if (currentDir.isEmpty())
126     currentDir = QDir::homePath();
127   QString fileName = QFileDialog::getOpenFileName(this, tr("Python script to import..."),
128                      currentDir,
129                      tr("Python script (*.py);;"));
130
131   if (!fileName.isEmpty())
132   {
133     QFile file(fileName);
134     settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
135     
136     _model.loadFile(fileName.toStdString());
137     _saveButton->setEnabled(_model.savePossible());
138     checkModel();
139   }
140 }
141
142 void Py2YacsDialog_raw::onExport()
143 {
144   QSettings settings;
145   QString currentDir = settings.value("currentDir").toString();
146   if (currentDir.isEmpty())
147     currentDir = QDir::homePath();
148   QString fileName = QFileDialog::getSaveFileName(this,
149                                   tr("Save to YACS schema..."),
150                                   currentDir,
151                                   QString("%1 (*.xml)" ).arg( tr("xml files")));
152   if (!fileName.isEmpty())
153   {
154     if (!fileName.endsWith(".xml"))
155       fileName += ".xml";
156     QFile file(fileName);
157     settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
158     if(_model.exportToXml(fileName.toStdString()))
159     {
160       _yacsFile = fileName;
161       accept();
162     }
163   }
164 }
165
166 void Py2YacsDialog_raw::onApply()
167 {
168   _model.setScript(_pyEditor->toPlainText().toStdString());
169   checkModel();
170 }
171
172 void Py2YacsDialog_raw::invalidModel()
173 {
174   _okButton->setEnabled(false);
175   _exportButton->setEnabled(false);
176   _functionChosen->setEnabled(false);
177 }
178
179 void Py2YacsDialog_raw::checkModel()
180 {
181   bool modelState = _model.schemaAvailable();
182   _okButton->setEnabled(modelState);
183   _exportButton->setEnabled(modelState);
184   _functionChosen->setEnabled(modelState);
185 }
186
187 void Py2YacsDialog_raw::onSave()
188 {
189   _model.setScript(_pyEditor->toPlainText().toStdString());
190   _model.save();
191   checkModel();
192 }
193
194 void Py2YacsDialog_raw::onSaveAs()
195 {
196   QSettings settings;
197   QString currentDir = settings.value("currentDir").toString();
198   if (currentDir.isEmpty())
199     currentDir = QDir::homePath();
200   QString fileName = QFileDialog::getSaveFileName(this,
201                                   tr("Save to python file..."),
202                                   currentDir,
203                                   QString("%1 (*.py)" ).arg( tr("python files")));
204   if (!fileName.isEmpty())
205   {
206     if (!fileName.endsWith(".py"))
207       fileName += ".py";
208     QFile file(fileName);
209     settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
210     _model.setScript(_pyEditor->toPlainText().toStdString());
211     _model.saveAs(fileName.toStdString());
212     _saveButton->setEnabled(_model.savePossible());
213     checkModel();
214   }
215 }
216
217 YACS::ENGINE::Proc* Py2YacsDialog_raw::getYacsSchema()
218 {
219   return _model.getProc();
220 }
221
222 QString Py2YacsDialog_raw::getYacsFile()
223 {
224   return _yacsFile;
225 }