]> SALOME platform Git repositories - modules/yacs.git/blob - src/py2yacsgui/Py2YacsDialog.cxx
Salome HOME
Add py2yacsgui.
[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 #include <QtWidgets>
20 #include <QSettings>
21 #ifdef HAS_PYEDITOR
22 #include <PyEditor_Editor.h>
23 #endif
24
25 #include "Py2YacsDialog.hxx"
26
27 Py2YacsDialog::Py2YacsDialog( 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   exportLayout->addWidget(_exportButton);
54
55   QHBoxLayout *validationLayout = new QHBoxLayout;
56   _okButton = new QPushButton(tr("O&k"));
57   QPushButton * cancelButton = new QPushButton(tr("&Cancel"));
58   validationLayout->addWidget(_okButton);
59   validationLayout->addWidget(cancelButton);
60   
61   QGroupBox *editWidget = new QGroupBox(tr("Python script:"));
62   QVBoxLayout *editLayout = new QVBoxLayout;
63   editLayout->addLayout(fileLayout);
64   editLayout->addWidget(_pyEditor);
65   editLayout->addWidget(applyButton);
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(exportLayout);
80   mainLayout->addLayout(validationLayout);
81   setLayout(mainLayout);
82   setWindowTitle(tr("Python to YACS schema editor"));
83   
84   invalidModel();
85   _saveButton->setEnabled(false);
86   connect(_pyEditor, SIGNAL(textChanged()), this, SLOT(invalidModel()));
87   connect(applyButton,SIGNAL(clicked()),this, SLOT(onApply()));
88   
89   connect(&model, SIGNAL(scriptChanged(const QString&)),
90           _pyEditor, SLOT(setText(const QString&)));
91   connect(&model, SIGNAL(errorChanged(const QString&)),
92           errorMessages, SLOT(setText(const QString&)));
93   connect(&model, SIGNAL(functionsChanged(std::list<std::string>)),
94           this, SLOT(onFunctionNamesChange(std::list<std::string>)));
95   connect(_functionChosen,SIGNAL(currentIndexChanged(const QString &)),
96           &model, SLOT(setFunctionName(const QString&)));
97   connect(loadButton,SIGNAL(clicked()),this, SLOT(onLoad()));
98   connect(_saveButton,SIGNAL(clicked()),this, SLOT(onSave()));
99   connect(saveAsButton,SIGNAL(clicked()),this, SLOT(onSaveAs()));
100   connect(_exportButton,SIGNAL(clicked()),this, SLOT(onExport()));
101   connect(cancelButton,SIGNAL(clicked()),this, SLOT(reject()));
102   connect(_okButton,SIGNAL(clicked()),this, SLOT(accept()));
103 }
104
105 void Py2YacsDialog::onFunctionNamesChange(std::list<std::string> validFunctionNames)
106 {
107   int new_index = 0;
108   int count = 0;
109   QString lastChoice = _functionChosen->currentText();
110   _functionChosen->clear();
111   std::list<std::string>::const_iterator it;
112   for(it=validFunctionNames.begin(); it!=validFunctionNames.end(); it++)
113   {
114     _functionChosen->addItem(it->c_str());
115     if(lastChoice == it->c_str())
116       new_index = count;
117     count++;
118   }
119   _functionChosen->setCurrentIndex(new_index);
120 }
121
122 void Py2YacsDialog::onLoad()
123 {
124   QSettings settings;
125   QString currentDir = settings.value("currentDir").toString();
126   if (currentDir.isEmpty())
127     currentDir = QDir::homePath();
128   QString fileName = QFileDialog::getOpenFileName(this, tr("Python script to import..."),
129                      currentDir,
130                      tr("Python script (*.py);;"));
131
132   if (!fileName.isEmpty())
133   {
134     QFile file(fileName);
135     settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
136     
137     model.loadFile(fileName.toStdString());
138     _saveButton->setEnabled(model.savePossible());
139     checkModel();
140   }
141 }
142
143 void Py2YacsDialog::onExport()
144 {
145   QSettings settings;
146   QString currentDir = settings.value("currentDir").toString();
147   if (currentDir.isEmpty())
148     currentDir = QDir::homePath();
149   QString fileName = QFileDialog::getSaveFileName(this,
150                                   tr("Save to YACS schema..."),
151                                   currentDir,
152                                   QString("%1 (*.xml)" ).arg( tr("xml files")));
153   if (!fileName.isEmpty())
154   {
155     if (!fileName.endsWith(".xml"))
156       fileName += ".xml";
157     QFile file(fileName);
158     settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
159     
160     model.exportToXml(fileName.toStdString());
161   }
162 }
163
164 void Py2YacsDialog::onApply()
165 {
166   model.setScript(_pyEditor->toPlainText().toStdString());
167   checkModel();
168 }
169
170 void Py2YacsDialog::invalidModel()
171 {
172   _okButton->setEnabled(false);
173   _exportButton->setEnabled(false);
174   _functionChosen->setEnabled(false);
175 }
176
177 void Py2YacsDialog::checkModel()
178 {
179   bool modelState = model.schemaAvailable();
180   _okButton->setEnabled(modelState);
181   _exportButton->setEnabled(modelState);
182   _functionChosen->setEnabled(modelState);
183 }
184
185 void Py2YacsDialog::onSave()
186 {
187   model.setScript(_pyEditor->toPlainText().toStdString());
188   model.save();
189   checkModel();
190 }
191
192 void Py2YacsDialog::onSaveAs()
193 {
194   QSettings settings;
195   QString currentDir = settings.value("currentDir").toString();
196   if (currentDir.isEmpty())
197     currentDir = QDir::homePath();
198   QString fileName = QFileDialog::getSaveFileName(this,
199                                   tr("Save to python file..."),
200                                   currentDir,
201                                   QString("%1 (*.py)" ).arg( tr("python files")));
202   if (!fileName.isEmpty())
203   {
204     if (!fileName.endsWith(".py"))
205       fileName += ".py";
206     QFile file(fileName);
207     settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
208     
209     model.setScript(_pyEditor->toPlainText().toStdString());
210     model.saveAs(fileName.toStdString());
211     _saveButton->setEnabled(model.savePossible());
212     checkModel();
213   }
214 }
215
216 YACS::ENGINE::Proc* Py2YacsDialog::getYacsSchema()
217 {
218   return model.getProc();
219 }