1 // Copyright (C) 2016-2020 CEA/DEN, EDF R&D
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
22 #include <PyEditor_Editor.h>
25 #include "Py2YacsDialog_raw.hxx"
27 Py2YacsDialog_raw::Py2YacsDialog_raw( QWidget* parent)
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);
39 _pyEditor = new PyEditor_Editor(this);
41 _pyEditor = new QTextEdit(this);
44 QPushButton * applyButton = new QPushButton(tr("A&pply"));
45 QTextEdit *errorMessages = new QTextEdit(this);
46 errorMessages->setReadOnly(true);
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);
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);
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);
68 QGroupBox *messageWidget = new QGroupBox(tr("Messages:"));
69 QVBoxLayout *messageLayout = new QVBoxLayout;
70 messageLayout->addWidget(errorMessages);
71 messageWidget->setLayout(messageLayout);
73 QSplitter * splitterW = new QSplitter(Qt::Vertical);
74 splitterW->addWidget(editWidget);
75 splitterW->addWidget(messageWidget);
77 QVBoxLayout *mainLayout = new QVBoxLayout;
78 mainLayout->addWidget(splitterW);
79 mainLayout->addLayout(validationLayout);
80 setLayout(mainLayout);
81 setWindowTitle(tr("Python to YACS schema editor"));
84 _saveButton->setEnabled(false);
85 connect(_pyEditor, SIGNAL(textChanged()), this, SLOT(invalidModel()));
86 connect(applyButton,SIGNAL(clicked()),this, SLOT(onApply()));
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()));
104 void Py2YacsDialog_raw::onFunctionNamesChange(std::list<std::string> validFunctionNames)
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++)
113 _functionChosen->addItem(it->c_str());
114 if(lastChoice == it->c_str())
118 _functionChosen->setCurrentIndex(new_index);
121 void Py2YacsDialog_raw::onLoad()
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..."),
129 tr("Python script (*.py);;"));
131 if (!fileName.isEmpty())
133 QFile file(fileName);
134 settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
136 _model.loadFile(fileName.toStdString());
137 _saveButton->setEnabled(_model.savePossible());
142 void Py2YacsDialog_raw::onExport()
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..."),
151 QString("%1 (*.xml)" ).arg( tr("xml files")));
152 if (!fileName.isEmpty())
154 if (!fileName.endsWith(".xml"))
156 QFile file(fileName);
157 settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
158 if(_model.exportToXml(fileName.toStdString()))
160 _yacsFile = fileName;
166 void Py2YacsDialog_raw::onApply()
168 _model.setScript(_pyEditor->toPlainText().toStdString());
172 void Py2YacsDialog_raw::invalidModel()
174 _okButton->setEnabled(false);
175 _exportButton->setEnabled(false);
176 _functionChosen->setEnabled(false);
179 void Py2YacsDialog_raw::checkModel()
181 bool modelState = _model.schemaAvailable();
182 _okButton->setEnabled(modelState);
183 _exportButton->setEnabled(modelState);
184 _functionChosen->setEnabled(modelState);
187 void Py2YacsDialog_raw::onSave()
189 _model.setScript(_pyEditor->toPlainText().toStdString());
194 void Py2YacsDialog_raw::onSaveAs()
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..."),
203 QString("%1 (*.py)" ).arg( tr("python files")));
204 if (!fileName.isEmpty())
206 if (!fileName.endsWith(".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());
217 YACS::ENGINE::Proc* Py2YacsDialog_raw::getYacsSchema()
219 return _model.getProc();
222 QString Py2YacsDialog_raw::getYacsFile()