From e2b7b63bbc98ea8773a58495787e7fb7233baa5a Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Wed, 16 Aug 2017 17:12:46 +0200 Subject: [PATCH] New Py2YacsDialog using PyEditor_Window. --- src/py2yacs/py2yacs.cxx | 72 ++++++++ src/py2yacs/py2yacs.hxx | 20 +++ src/py2yacsgui/CMakeLists.txt | 7 + src/py2yacsgui/Py2YacsDialog.cxx | 235 +++++++-------------------- src/py2yacsgui/Py2YacsDialog.hxx | 34 +--- src/py2yacsgui/Py2YacsDialog_raw.cxx | 225 +++++++++++++++++++++++++ src/py2yacsgui/Py2YacsDialog_raw.hxx | 64 ++++++++ src/py2yacsgui/Py2YacsModel.cxx | 32 +--- src/py2yacsgui/py2yacsgui.cxx | 4 +- src/py2yacsgui/py2yacsgui_rich.cxx | 44 +++++ 10 files changed, 504 insertions(+), 233 deletions(-) create mode 100644 src/py2yacsgui/Py2YacsDialog_raw.cxx create mode 100644 src/py2yacsgui/Py2YacsDialog_raw.hxx create mode 100644 src/py2yacsgui/py2yacsgui_rich.cxx diff --git a/src/py2yacs/py2yacs.cxx b/src/py2yacs/py2yacs.cxx index 5789ef408..5ee96e586 100644 --- a/src/py2yacs/py2yacs.cxx +++ b/src/py2yacs/py2yacs.cxx @@ -382,4 +382,76 @@ YACS::ENGINE::Proc* Py2yacs::createProc(const std::string& python_function)const node->setExecutionMode(YACS::ENGINE::InlineNode::REMOTE_STR); node->setContainer(schema->containerMap["DefaultContainer"]); return schema; +} + +std::string Py2yacs::getAllErrors()const +{ + std::stringstream buffer; + buffer.clear(); + if(! _global_errors.empty()) + { + buffer << "Global errors:" << std::endl; + std::list::const_iterator it; + for(it=_global_errors.begin(); it!=_global_errors.end(); it++) + { + buffer << *it << std::endl; + } + buffer << "-----------------------------------------" << std::endl; + } + + std::list::const_iterator it_fp; + for(it_fp=_functions.begin();it_fp!=_functions.end();it_fp++) + { + if(! it_fp->_errors.empty()) + { + buffer << "Function " << it_fp->_name << " has errors:" << std::endl; + std::list::const_iterator it; + buffer << "Errors :" ; + for(it=it_fp->_errors.begin();it!=it_fp->_errors.end();it++) + buffer << *it << std::endl; + buffer << "-----------------------------------------" << std::endl; + } + } + return buffer.str(); +} + +std::string Py2yacs::getFunctionErrors(const std::string& functionName)const +{ + std::stringstream buffer; + buffer.clear(); + if(! _global_errors.empty()) + { + buffer << "Global errors:" << std::endl; + std::list::const_iterator it; + for(it=_global_errors.begin(); it!=_global_errors.end(); it++) + { + buffer << *it << std::endl; + } + buffer << "-----------------------------------------" << std::endl; + } + + bool nameFound = false; + std::list::const_iterator it_fp; + for(it_fp=_functions.begin(); it_fp!=_functions.end() && !nameFound; it_fp++) + { + if(it_fp->_name == functionName) + { + nameFound = true; + if(! it_fp->_errors.empty()) + { + buffer << "Function " << it_fp->_name << " has errors:" << std::endl; + std::list::const_iterator it; + buffer << "Errors :" ; + for(it=it_fp->_errors.begin();it!=it_fp->_errors.end();it++) + buffer << *it << std::endl; + buffer << "-----------------------------------------" << std::endl; + } + } + } + + if(!nameFound) + { + buffer << "Function " << functionName << " not found." << std::endl; + } + return buffer.str(); } \ No newline at end of file diff --git a/src/py2yacs/py2yacs.hxx b/src/py2yacs/py2yacs.hxx index ee052cdf3..76d82dede 100644 --- a/src/py2yacs/py2yacs.hxx +++ b/src/py2yacs/py2yacs.hxx @@ -104,9 +104,29 @@ class PY2YACSLIB_EXPORT Py2yacs */ YACS::ENGINE::Proc* createProc(const std::string& python_function)const; + /*! + * Syntax errors when parsing python and py2yacs global errors. + * \return a list of errors. + */ const std::list& getGlobalErrors() const; + const std::list& getFunctionProperties()const; + /*! + * Get a string containing global errors and errors specific to py2yacs for + * every function in python script. + * An empty string means there is no error. + */ + std::string getAllErrors()const; + + /*! + * Same as getAllErrors but only for one function. + * py2yacs errors for other functions are ignored. + * If the function name is not found, you get an error message in the + * returned string. + */ + std::string getFunctionErrors(const std::string& functionName)const; + private: std::string _python_parser_module; std::string _python_parser_function; diff --git a/src/py2yacsgui/CMakeLists.txt b/src/py2yacsgui/CMakeLists.txt index b3d2c437c..34c9d437b 100644 --- a/src/py2yacsgui/CMakeLists.txt +++ b/src/py2yacsgui/CMakeLists.txt @@ -45,6 +45,7 @@ SET(_link_LIBRARIES SET(_moc_HEADERS Py2YacsDialog.hxx Py2YacsModel.hxx + Py2YacsDialog_raw.hxx ) # sources / moc wrappings @@ -54,11 +55,13 @@ SET(py2yacsgui_HEADERS Py2YacsModel.hxx Py2YacsDialog.hxx config_py2yacsgui.hxx + Py2YacsDialog_raw.hxx ) SET(py2yacsgui_SOURCES Py2YacsDialog.cxx Py2YacsModel.cxx + Py2YacsDialog_raw.cxx ${_moc_SOURCES} ) @@ -67,6 +70,10 @@ TARGET_LINK_LIBRARIES(py2yacsguilib ${_link_LIBRARIES}) ADD_EXECUTABLE(py2yacsgui py2yacsgui.cxx) TARGET_LINK_LIBRARIES(py2yacsgui py2yacsguilib) +ADD_EXECUTABLE(py2yacsgui_rich py2yacsgui_rich.cxx) +TARGET_LINK_LIBRARIES(py2yacsgui_rich py2yacsguilib) + INSTALL(TARGETS py2yacsguilib EXPORT ${PROJECT_NAME}TargetGroup DESTINATION ${SALOME_INSTALL_LIBS}) INSTALL(TARGETS py2yacsgui DESTINATION ${SALOME_INSTALL_BINS}) +INSTALL(TARGETS py2yacsgui_rich DESTINATION ${SALOME_INSTALL_BINS}) INSTALL(FILES ${py2yacsgui_HEADERS} DESTINATION ${SALOME_INSTALL_HEADERS}) diff --git a/src/py2yacsgui/Py2YacsDialog.cxx b/src/py2yacsgui/Py2YacsDialog.cxx index df01c18fc..9e597a97a 100644 --- a/src/py2yacsgui/Py2YacsDialog.cxx +++ b/src/py2yacsgui/Py2YacsDialog.cxx @@ -16,211 +16,102 @@ // // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#include -#include -#ifdef HAS_PYEDITOR -#include -#endif #include "Py2YacsDialog.hxx" +#include +#include +#include Py2YacsDialog::Py2YacsDialog( QWidget* parent) -: QDialog(parent) +: QDialog(parent), + _yacsFile(), + _pyEditorWindow(0), + _errorMessages(0), + _okButton(0) { - QHBoxLayout *fileLayout = new QHBoxLayout; - QPushButton *loadButton = new QPushButton(tr("&Load")); - _saveButton = new QPushButton(tr("&Save")); - QPushButton *saveAsButton = new QPushButton(tr("Save &as ...")); - fileLayout->addWidget(loadButton); - fileLayout->addWidget(_saveButton); - fileLayout->addWidget(saveAsButton); - -#ifdef HAS_PYEDITOR - _pyEditor = new PyEditor_Editor(this); -#else - _pyEditor = new QTextEdit(this); -#endif + QVBoxLayout *mainLayout = new QVBoxLayout; + _pyEditorWindow = new PyEditor_Window; - QPushButton * applyButton = new QPushButton(tr("A&pply")); - QTextEdit *errorMessages = new QTextEdit(this); - errorMessages->setReadOnly(true); + _errorMessages = new QTextEdit(this); + _errorMessages->setReadOnly(true); + _errorMessages->hide(); + QSplitter * splitterW = new QSplitter(Qt::Vertical); + splitterW->addWidget(_pyEditorWindow); + splitterW->addWidget(_errorMessages); + mainLayout->addWidget(splitterW); - QHBoxLayout *exportLayout = new QHBoxLayout; - _functionChosen = new QComboBox(this); - _exportButton = new QPushButton(tr("E&xport to YACS schema...")); - exportLayout->addWidget(new QLabel(tr("Function to run:"))); - exportLayout->addWidget(_functionChosen); - QHBoxLayout *validationLayout = new QHBoxLayout; - _okButton = new QPushButton(tr("Save YACS schema and &quit")); + _okButton = new QPushButton(tr("py -> &YACS")); QPushButton * cancelButton = new QPushButton(tr("&Cancel")); validationLayout->addWidget(_okButton); validationLayout->addWidget(cancelButton); - - QGroupBox *editWidget = new QGroupBox(tr("Python script:")); - QVBoxLayout *editLayout = new QVBoxLayout; - editLayout->addLayout(fileLayout); - editLayout->addWidget(_pyEditor); - editLayout->addWidget(applyButton); - editLayout->addLayout(exportLayout); - editWidget->setLayout(editLayout); - - QGroupBox *messageWidget = new QGroupBox(tr("Messages:")); - QVBoxLayout *messageLayout = new QVBoxLayout; - messageLayout->addWidget(errorMessages); - messageWidget->setLayout(messageLayout); - - QSplitter * splitterW = new QSplitter(Qt::Vertical); - splitterW->addWidget(editWidget); - splitterW->addWidget(messageWidget); - - QVBoxLayout *mainLayout = new QVBoxLayout; - mainLayout->addWidget(splitterW); mainLayout->addLayout(validationLayout); + setLayout(mainLayout); setWindowTitle(tr("Python to YACS schema editor")); - - invalidModel(); - _saveButton->setEnabled(false); - connect(_pyEditor, SIGNAL(textChanged()), this, SLOT(invalidModel())); - connect(applyButton,SIGNAL(clicked()),this, SLOT(onApply())); - - connect(&_model, SIGNAL(scriptChanged(const QString&)), - _pyEditor, SLOT(setText(const QString&))); - connect(&_model, SIGNAL(errorChanged(const QString&)), - errorMessages, SLOT(setText(const QString&))); - connect(&_model, SIGNAL(functionsChanged(std::list)), - this, SLOT(onFunctionNamesChange(std::list))); - connect(_functionChosen,SIGNAL(currentIndexChanged(const QString &)), - &_model, SLOT(setFunctionName(const QString&))); - connect(loadButton,SIGNAL(clicked()),this, SLOT(onLoad())); - connect(_saveButton,SIGNAL(clicked()),this, SLOT(onSave())); - connect(saveAsButton,SIGNAL(clicked()),this, SLOT(onSaveAs())); - connect(_exportButton,SIGNAL(clicked()),this, SLOT(onExport())); + connect(cancelButton,SIGNAL(clicked()),this, SLOT(reject())); connect(_okButton,SIGNAL(clicked()),this, SLOT(onExport())); -} -void Py2YacsDialog::onFunctionNamesChange(std::list validFunctionNames) -{ - int new_index = 0; - int count = 0; - QString lastChoice = _functionChosen->currentText(); - _functionChosen->clear(); - std::list::const_iterator it; - for(it=validFunctionNames.begin(); it!=validFunctionNames.end(); it++) - { - _functionChosen->addItem(it->c_str()); - if(lastChoice == it->c_str()) - new_index = count; - count++; - } - _functionChosen->setCurrentIndex(new_index); + // PyEditor_Window has a button "exit". + // Trigger "cancel" when the editor is closed. + _pyEditorWindow->setAttribute(Qt::WA_DeleteOnClose); + connect(_pyEditorWindow,SIGNAL(destroyed()),this, SLOT(reject())); } -void Py2YacsDialog::onLoad() +void Py2YacsDialog::onExport() { - QSettings settings; - QString currentDir = settings.value("currentDir").toString(); - if (currentDir.isEmpty()) - currentDir = QDir::homePath(); - QString fileName = QFileDialog::getOpenFileName(this, tr("Python script to import..."), - currentDir, - tr("Python script (*.py);;")); - - if (!fileName.isEmpty()) + PyEditor_Widget* pyEdit = dynamic_cast + (_pyEditorWindow->centralWidget()); + if(!pyEdit) { - QFile file(fileName); - settings.setValue("currentDir", QFileInfo(fileName).absolutePath()); - - _model.loadFile(fileName.toStdString()); - _saveButton->setEnabled(_model.savePossible()); - checkModel(); + reject(); + return; } -} -void Py2YacsDialog::onExport() -{ - QSettings settings; - QString currentDir = settings.value("currentDir").toString(); - if (currentDir.isEmpty()) - currentDir = QDir::homePath(); - QString fileName = QFileDialog::getSaveFileName(this, + Py2yacs converter; + std::string text = pyEdit->text().toStdString(); + try + { + converter.load(text); + // _exec -> default name for OPENTURNS functions + std::string errors = converter.getFunctionErrors("_exec"); + if(errors.empty()) + { + QSettings settings; + QString currentDir = settings.value("currentDir").toString(); + if (currentDir.isEmpty()) + currentDir = QDir::homePath(); + QString fileName = QFileDialog::getSaveFileName(this, tr("Save to YACS schema..."), currentDir, QString("%1 (*.xml)" ).arg( tr("xml files"))); - if (!fileName.isEmpty()) - { - if (!fileName.endsWith(".xml")) - fileName += ".xml"; - QFile file(fileName); - settings.setValue("currentDir", QFileInfo(fileName).absolutePath()); - - if(_model.exportToXml(fileName.toStdString())) + if (!fileName.isEmpty()) + { + if (!fileName.endsWith(".xml")) + fileName += ".xml"; + QFile file(fileName); + settings.setValue("currentDir", QFileInfo(fileName).absolutePath()); + converter.save(fileName.toStdString(), "_exec"); + _yacsFile = fileName; + accept(); + } + } + else { - _yacsFile = fileName; - accept(); + _errorMessages->show(); + _errorMessages->setText(errors.c_str()); } } -} - -void Py2YacsDialog::onApply() -{ - _model.setScript(_pyEditor->toPlainText().toStdString()); - checkModel(); -} - -void Py2YacsDialog::invalidModel() -{ - _okButton->setEnabled(false); - _exportButton->setEnabled(false); - _functionChosen->setEnabled(false); -} - -void Py2YacsDialog::checkModel() -{ - bool modelState = _model.schemaAvailable(); - _okButton->setEnabled(modelState); - _exportButton->setEnabled(modelState); - _functionChosen->setEnabled(modelState); -} - -void Py2YacsDialog::onSave() -{ - _model.setScript(_pyEditor->toPlainText().toStdString()); - _model.save(); - checkModel(); -} - -void Py2YacsDialog::onSaveAs() -{ - QSettings settings; - QString currentDir = settings.value("currentDir").toString(); - if (currentDir.isEmpty()) - currentDir = QDir::homePath(); - QString fileName = QFileDialog::getSaveFileName(this, - tr("Save to python file..."), - currentDir, - QString("%1 (*.py)" ).arg( tr("python files"))); - if (!fileName.isEmpty()) + catch(Py2yacsException& e) { - if (!fileName.endsWith(".py")) - fileName += ".py"; - QFile file(fileName); - settings.setValue("currentDir", QFileInfo(fileName).absolutePath()); - - _model.setScript(_pyEditor->toPlainText().toStdString()); - _model.saveAs(fileName.toStdString()); - _saveButton->setEnabled(_model.savePossible()); - checkModel(); + const char * error = e.what(); + _errorMessages->show(); + _errorMessages->setText(QString(error)); + return; } } -YACS::ENGINE::Proc* Py2YacsDialog::getYacsSchema() -{ - return _model.getProc(); -} - QString Py2YacsDialog::getYacsFile() { return _yacsFile; diff --git a/src/py2yacsgui/Py2YacsDialog.hxx b/src/py2yacsgui/Py2YacsDialog.hxx index ccb2c8d67..4139fcfb7 100644 --- a/src/py2yacsgui/Py2YacsDialog.hxx +++ b/src/py2yacsgui/Py2YacsDialog.hxx @@ -20,45 +20,23 @@ #define PY2YACSDIALOG_HXX #include "config_py2yacsgui.hxx" -#include "Py2YacsModel.hxx" -#include - -#ifdef HAS_PYEDITOR - class PyEditor_Editor; -#else - class QTextEdit; -#endif - class QComboBox; - class QPushButton; +#include +class PyEditor_Window; class PY2YACSGUILIB_EXPORT Py2YacsDialog : public QDialog { Q_OBJECT public: Py2YacsDialog( QWidget* parent=0); - YACS::ENGINE::Proc* getYacsSchema(); QString getYacsFile(); - + public slots: - virtual void onFunctionNamesChange(std::list validFunctionNames); - virtual void onLoad(); virtual void onExport(); - virtual void checkModel(); - virtual void invalidModel(); - virtual void onApply(); - virtual void onSave(); - virtual void onSaveAs(); + private: - Py2YacsModel _model; QString _yacsFile; -#ifdef HAS_PYEDITOR - PyEditor_Editor *_pyEditor; -#else - QTextEdit *_pyEditor; -#endif - QComboBox *_functionChosen; - QPushButton *_saveButton; - QPushButton *_exportButton; + PyEditor_Window *_pyEditorWindow; + QTextEdit * _errorMessages; QPushButton *_okButton; }; diff --git a/src/py2yacsgui/Py2YacsDialog_raw.cxx b/src/py2yacsgui/Py2YacsDialog_raw.cxx new file mode 100644 index 000000000..53a897c7a --- /dev/null +++ b/src/py2yacsgui/Py2YacsDialog_raw.cxx @@ -0,0 +1,225 @@ +// Copyright (C) 2016-2017 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +#include +#include +#ifdef HAS_PYEDITOR +#include +#endif + +#include "Py2YacsDialog_raw.hxx" + +Py2YacsDialog_raw::Py2YacsDialog_raw( QWidget* parent) +: QDialog(parent) +{ + QHBoxLayout *fileLayout = new QHBoxLayout; + QPushButton *loadButton = new QPushButton(tr("&Load")); + _saveButton = new QPushButton(tr("&Save")); + QPushButton *saveAsButton = new QPushButton(tr("Save &as ...")); + fileLayout->addWidget(loadButton); + fileLayout->addWidget(_saveButton); + fileLayout->addWidget(saveAsButton); + +#ifdef HAS_PYEDITOR + _pyEditor = new PyEditor_Editor(this); +#else + _pyEditor = new QTextEdit(this); +#endif + + QPushButton * applyButton = new QPushButton(tr("A&pply")); + QTextEdit *errorMessages = new QTextEdit(this); + errorMessages->setReadOnly(true); + + QHBoxLayout *exportLayout = new QHBoxLayout; + _functionChosen = new QComboBox(this); + _exportButton = new QPushButton(tr("E&xport to YACS schema...")); + exportLayout->addWidget(new QLabel(tr("Function to run:"))); + exportLayout->addWidget(_functionChosen); + + QHBoxLayout *validationLayout = new QHBoxLayout; + _okButton = new QPushButton(tr("Save YACS schema and &quit")); + QPushButton * cancelButton = new QPushButton(tr("&Cancel")); + validationLayout->addWidget(_okButton); + validationLayout->addWidget(cancelButton); + + QGroupBox *editWidget = new QGroupBox(tr("Python script:")); + QVBoxLayout *editLayout = new QVBoxLayout; + editLayout->addLayout(fileLayout); + editLayout->addWidget(_pyEditor); + editLayout->addWidget(applyButton); + editLayout->addLayout(exportLayout); + editWidget->setLayout(editLayout); + + QGroupBox *messageWidget = new QGroupBox(tr("Messages:")); + QVBoxLayout *messageLayout = new QVBoxLayout; + messageLayout->addWidget(errorMessages); + messageWidget->setLayout(messageLayout); + + QSplitter * splitterW = new QSplitter(Qt::Vertical); + splitterW->addWidget(editWidget); + splitterW->addWidget(messageWidget); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(splitterW); + mainLayout->addLayout(validationLayout); + setLayout(mainLayout); + setWindowTitle(tr("Python to YACS schema editor")); + + invalidModel(); + _saveButton->setEnabled(false); + connect(_pyEditor, SIGNAL(textChanged()), this, SLOT(invalidModel())); + connect(applyButton,SIGNAL(clicked()),this, SLOT(onApply())); + + connect(&_model, SIGNAL(scriptChanged(const QString&)), + _pyEditor, SLOT(setText(const QString&))); + connect(&_model, SIGNAL(errorChanged(const QString&)), + errorMessages, SLOT(setText(const QString&))); + connect(&_model, SIGNAL(functionsChanged(std::list)), + this, SLOT(onFunctionNamesChange(std::list))); + connect(_functionChosen,SIGNAL(currentIndexChanged(const QString &)), + &_model, SLOT(setFunctionName(const QString&))); + connect(loadButton,SIGNAL(clicked()),this, SLOT(onLoad())); + connect(_saveButton,SIGNAL(clicked()),this, SLOT(onSave())); + connect(saveAsButton,SIGNAL(clicked()),this, SLOT(onSaveAs())); + connect(_exportButton,SIGNAL(clicked()),this, SLOT(onExport())); + connect(cancelButton,SIGNAL(clicked()),this, SLOT(reject())); + connect(_okButton,SIGNAL(clicked()),this, SLOT(onExport())); +} + +void Py2YacsDialog_raw::onFunctionNamesChange(std::list validFunctionNames) +{ + int new_index = 0; + int count = 0; + QString lastChoice = _functionChosen->currentText(); + _functionChosen->clear(); + std::list::const_iterator it; + for(it=validFunctionNames.begin(); it!=validFunctionNames.end(); it++) + { + _functionChosen->addItem(it->c_str()); + if(lastChoice == it->c_str()) + new_index = count; + count++; + } + _functionChosen->setCurrentIndex(new_index); +} + +void Py2YacsDialog_raw::onLoad() +{ + QSettings settings; + QString currentDir = settings.value("currentDir").toString(); + if (currentDir.isEmpty()) + currentDir = QDir::homePath(); + QString fileName = QFileDialog::getOpenFileName(this, tr("Python script to import..."), + currentDir, + tr("Python script (*.py);;")); + + if (!fileName.isEmpty()) + { + QFile file(fileName); + settings.setValue("currentDir", QFileInfo(fileName).absolutePath()); + + _model.loadFile(fileName.toStdString()); + _saveButton->setEnabled(_model.savePossible()); + checkModel(); + } +} + +void Py2YacsDialog_raw::onExport() +{ + QSettings settings; + QString currentDir = settings.value("currentDir").toString(); + if (currentDir.isEmpty()) + currentDir = QDir::homePath(); + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save to YACS schema..."), + currentDir, + QString("%1 (*.xml)" ).arg( tr("xml files"))); + if (!fileName.isEmpty()) + { + if (!fileName.endsWith(".xml")) + fileName += ".xml"; + QFile file(fileName); + settings.setValue("currentDir", QFileInfo(fileName).absolutePath()); + if(_model.exportToXml(fileName.toStdString())) + { + _yacsFile = fileName; + accept(); + } + } +} + +void Py2YacsDialog_raw::onApply() +{ + _model.setScript(_pyEditor->toPlainText().toStdString()); + checkModel(); +} + +void Py2YacsDialog_raw::invalidModel() +{ + _okButton->setEnabled(false); + _exportButton->setEnabled(false); + _functionChosen->setEnabled(false); +} + +void Py2YacsDialog_raw::checkModel() +{ + bool modelState = _model.schemaAvailable(); + _okButton->setEnabled(modelState); + _exportButton->setEnabled(modelState); + _functionChosen->setEnabled(modelState); +} + +void Py2YacsDialog_raw::onSave() +{ + _model.setScript(_pyEditor->toPlainText().toStdString()); + _model.save(); + checkModel(); +} + +void Py2YacsDialog_raw::onSaveAs() +{ + QSettings settings; + QString currentDir = settings.value("currentDir").toString(); + if (currentDir.isEmpty()) + currentDir = QDir::homePath(); + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save to python file..."), + currentDir, + QString("%1 (*.py)" ).arg( tr("python files"))); + if (!fileName.isEmpty()) + { + if (!fileName.endsWith(".py")) + fileName += ".py"; + QFile file(fileName); + settings.setValue("currentDir", QFileInfo(fileName).absolutePath()); + _model.setScript(_pyEditor->toPlainText().toStdString()); + _model.saveAs(fileName.toStdString()); + _saveButton->setEnabled(_model.savePossible()); + checkModel(); + } +} + +YACS::ENGINE::Proc* Py2YacsDialog_raw::getYacsSchema() +{ + return _model.getProc(); +} + +QString Py2YacsDialog_raw::getYacsFile() +{ + return _yacsFile; +} \ No newline at end of file diff --git a/src/py2yacsgui/Py2YacsDialog_raw.hxx b/src/py2yacsgui/Py2YacsDialog_raw.hxx new file mode 100644 index 000000000..1de373216 --- /dev/null +++ b/src/py2yacsgui/Py2YacsDialog_raw.hxx @@ -0,0 +1,64 @@ +// Copyright (C) 2016-2017 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +#ifndef PY2YACSDIALOG_RAW_HXX +#define PY2YACSDIALOG_RAW_HXX + +#include "config_py2yacsgui.hxx" +#include "Py2YacsModel.hxx" +#include + +#ifdef HAS_PYEDITOR + class PyEditor_Editor; +#else + class QTextEdit; +#endif + class QComboBox; + class QPushButton; + +class PY2YACSGUILIB_EXPORT Py2YacsDialog_raw : public QDialog +{ + Q_OBJECT + public: + Py2YacsDialog_raw( QWidget* parent=0); + YACS::ENGINE::Proc* getYacsSchema(); + QString getYacsFile(); + public slots: + virtual void onFunctionNamesChange(std::list validFunctionNames); + virtual void onLoad(); + virtual void onExport(); + virtual void checkModel(); + virtual void invalidModel(); + virtual void onApply(); + virtual void onSave(); + virtual void onSaveAs(); + private: + Py2YacsModel _model; + QString _yacsFile; +#ifdef HAS_PYEDITOR + PyEditor_Editor *_pyEditor; +#else + QTextEdit *_pyEditor; +#endif + QComboBox *_functionChosen; + QPushButton *_saveButton; + QPushButton *_exportButton; + QPushButton *_okButton; +}; + +#endif // PY2YACSDIALOG_RAW_HXX diff --git a/src/py2yacsgui/Py2YacsModel.cxx b/src/py2yacsgui/Py2YacsModel.cxx index 9a0c7f27e..844f7ccb1 100644 --- a/src/py2yacsgui/Py2YacsModel.cxx +++ b/src/py2yacsgui/Py2YacsModel.cxx @@ -82,43 +82,13 @@ void Py2YacsModel::saveAs(const std::string& path) void Py2YacsModel::setScript(const std::string& texte) { - std::stringstream buffer; _script = texte; emit scriptChanged(_script.c_str()); _functionName = ""; try { _data.load(_script); - - // get the errors - buffer.clear(); - const std::list& globalErrors = _data.getGlobalErrors(); - if(! globalErrors.empty()) - { - buffer << "Global errors:" << std::endl; - std::list::const_iterator it; - for(it=globalErrors.begin(); it!=globalErrors.end(); it++) - { - buffer << *it << std::endl; - } - buffer << "-----------------------------------------" << std::endl; - } - - std::list::const_iterator it_fp; - const std::list& functions = _data.getFunctionProperties(); - for(it_fp=functions.begin();it_fp!=functions.end();it_fp++) - { - if(! it_fp->_errors.empty()) - { - buffer << "Function " << it_fp->_name << " has errors:" << std::endl; - std::list::const_iterator it; - buffer << "Errors :" ; - for(it=it_fp->_errors.begin();it!=it_fp->_errors.end();it++) - buffer << *it << std::endl; - buffer << "-----------------------------------------" << std::endl; - } - } - _lastError = buffer.str(); + _lastError = _data.getAllErrors(); } catch(Py2yacsException& e) { diff --git a/src/py2yacsgui/py2yacsgui.cxx b/src/py2yacsgui/py2yacsgui.cxx index 107d59d79..c1f40633f 100644 --- a/src/py2yacsgui/py2yacsgui.cxx +++ b/src/py2yacsgui/py2yacsgui.cxx @@ -16,7 +16,7 @@ // // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#include "Py2YacsDialog.hxx" +#include "Py2YacsDialog_raw.hxx" #include #include "RuntimeSALOME.hxx" #include "Proc.hxx" @@ -26,7 +26,7 @@ int main(int argc, char *argv[]) { QApplication app(argc,argv); YACS::ENGINE::RuntimeSALOME::setRuntime(); - Py2YacsDialog mygui; + Py2YacsDialog_raw mygui; if(mygui.exec()) { std::cout << "Accepted" << std::endl; diff --git a/src/py2yacsgui/py2yacsgui_rich.cxx b/src/py2yacsgui/py2yacsgui_rich.cxx new file mode 100644 index 000000000..f31011c5b --- /dev/null +++ b/src/py2yacsgui/py2yacsgui_rich.cxx @@ -0,0 +1,44 @@ +// Copyright (C) 2016-2017 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +#include "Py2YacsDialog.hxx" +#include +#include "RuntimeSALOME.hxx" +#include "Proc.hxx" +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc,argv); + app.setOrganizationName( "salome" ); + app.setOrganizationDomain( "www.salome-platform.org" ); + app.setApplicationName( "py2yacs" ); + + YACS::ENGINE::RuntimeSALOME::setRuntime(); + Py2YacsDialog mygui; + if(mygui.exec()) + { + std::cout << "Accepted:" << mygui.getYacsFile().toStdString() << std::endl; + } + else + { + std::cout << "Not accepted" << std::endl; + } + return 0; +} -- 2.39.2