1 // Copyright (C) 2016-2017 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.hxx"
27 Py2YacsDialog::Py2YacsDialog( 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);
53 exportLayout->addWidget(_exportButton);
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);
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);
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(exportLayout);
80 mainLayout->addLayout(validationLayout);
81 setLayout(mainLayout);
82 setWindowTitle(tr("Python to YACS schema editor"));
85 _saveButton->setEnabled(false);
86 connect(_pyEditor, SIGNAL(textChanged()), this, SLOT(invalidModel()));
87 connect(applyButton,SIGNAL(clicked()),this, SLOT(onApply()));
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()));
105 void Py2YacsDialog::onFunctionNamesChange(std::list<std::string> validFunctionNames)
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++)
114 _functionChosen->addItem(it->c_str());
115 if(lastChoice == it->c_str())
119 _functionChosen->setCurrentIndex(new_index);
122 void Py2YacsDialog::onLoad()
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..."),
130 tr("Python script (*.py);;"));
132 if (!fileName.isEmpty())
134 QFile file(fileName);
135 settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
137 model.loadFile(fileName.toStdString());
138 _saveButton->setEnabled(model.savePossible());
143 void Py2YacsDialog::onExport()
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..."),
152 QString("%1 (*.xml)" ).arg( tr("xml files")));
153 if (!fileName.isEmpty())
155 if (!fileName.endsWith(".xml"))
157 QFile file(fileName);
158 settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
160 model.exportToXml(fileName.toStdString());
164 void Py2YacsDialog::onApply()
166 model.setScript(_pyEditor->toPlainText().toStdString());
170 void Py2YacsDialog::invalidModel()
172 _okButton->setEnabled(false);
173 _exportButton->setEnabled(false);
174 _functionChosen->setEnabled(false);
177 void Py2YacsDialog::checkModel()
179 bool modelState = model.schemaAvailable();
180 _okButton->setEnabled(modelState);
181 _exportButton->setEnabled(modelState);
182 _functionChosen->setEnabled(modelState);
185 void Py2YacsDialog::onSave()
187 model.setScript(_pyEditor->toPlainText().toStdString());
192 void Py2YacsDialog::onSaveAs()
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..."),
201 QString("%1 (*.py)" ).arg( tr("python files")));
202 if (!fileName.isEmpty())
204 if (!fileName.endsWith(".py"))
206 QFile file(fileName);
207 settings.setValue("currentDir", QFileInfo(fileName).absolutePath());
209 model.setScript(_pyEditor->toPlainText().toStdString());
210 model.saveAs(fileName.toStdString());
211 _saveButton->setEnabled(model.savePossible());
216 YACS::ENGINE::Proc* Py2YacsDialog::getYacsSchema()
218 return model.getProc();