Salome HOME
New Py2YacsDialog using PyEditor_Window. V8_4_0a1 V8_4_0a2
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 16 Aug 2017 15:12:46 +0000 (17:12 +0200)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 16 Aug 2017 15:12:46 +0000 (17:12 +0200)
src/py2yacs/py2yacs.cxx
src/py2yacs/py2yacs.hxx
src/py2yacsgui/CMakeLists.txt
src/py2yacsgui/Py2YacsDialog.cxx
src/py2yacsgui/Py2YacsDialog.hxx
src/py2yacsgui/Py2YacsDialog_raw.cxx [new file with mode: 0644]
src/py2yacsgui/Py2YacsDialog_raw.hxx [new file with mode: 0644]
src/py2yacsgui/Py2YacsModel.cxx
src/py2yacsgui/py2yacsgui.cxx
src/py2yacsgui/py2yacsgui_rich.cxx [new file with mode: 0644]

index 5789ef408d6c118130a9482a7823c9c2f6de6ef8..5ee96e586958e1c83f9f72e859870fdafd9ca1df 100644 (file)
@@ -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<std::string>::const_iterator it;
+    for(it=_global_errors.begin(); it!=_global_errors.end(); it++)
+    {
+      buffer << *it << std::endl;
+    }
+    buffer << "-----------------------------------------" << std::endl;
+  }
+
+  std::list<FunctionProperties>::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<std::string>::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<std::string>::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<FunctionProperties>::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<std::string>::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
index ee052cdf31e8d86102538d43b4dd7686059e84da..76d82dede7773f666b27f937b2d260bbfef56059 100644 (file)
@@ -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<std::string>& getGlobalErrors() const;
+
     const std::list<FunctionProperties>& 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;
index b3d2c437c83806a568a295286fce4ee16b41fa14..34c9d437b65d9c105e3e0ddffeb9871cceb1f70f 100644 (file)
@@ -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})
index df01c18fc4d7c7ade84ad9e18faebdde54aec639..9e597a97afad3d78b788f7e36f7776da13c89629 100644 (file)
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-#include <QtWidgets>
-#include <QSettings>
-#ifdef HAS_PYEDITOR
-#include <PyEditor_Editor.h>
-#endif
 
 #include "Py2YacsDialog.hxx"
+#include <PyEditor_Window.h>
+#include <PyEditor_Widget.h>
+#include <py2yacs.hxx>
 
 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<std::string>)),
-          this, SLOT(onFunctionNamesChange(std::list<std::string>)));
-  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<std::string> validFunctionNames)
-{
-  int new_index = 0;
-  int count = 0;
-  QString lastChoice = _functionChosen->currentText();
-  _functionChosen->clear();
-  std::list<std::string>::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<PyEditor_Widget*>
+                                             (_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;
index ccb2c8d6704d4e270a0c9a8b39501acc0febe25a..4139fcfb779b88be87d34565a4431191f8661fdc 100644 (file)
 #define PY2YACSDIALOG_HXX
 
 #include "config_py2yacsgui.hxx"
-#include "Py2YacsModel.hxx"
-#include <QDialog>
-
-#ifdef HAS_PYEDITOR
- class PyEditor_Editor;
-#else
- class QTextEdit;
-#endif
- class QComboBox;
- class QPushButton;
+#include <QtWidgets>
 
+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<std::string> 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 (file)
index 0000000..53a897c
--- /dev/null
@@ -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 <QtWidgets>
+#include <QSettings>
+#ifdef HAS_PYEDITOR
+#include <PyEditor_Editor.h>
+#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<std::string>)),
+          this, SLOT(onFunctionNamesChange(std::list<std::string>)));
+  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<std::string> validFunctionNames)
+{
+  int new_index = 0;
+  int count = 0;
+  QString lastChoice = _functionChosen->currentText();
+  _functionChosen->clear();
+  std::list<std::string>::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 (file)
index 0000000..1de3732
--- /dev/null
@@ -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 <QDialog>
+
+#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<std::string> 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
index 9a0c7f27e90f8a645d504bf5edd2ad0fb7bc46e9..844f7ccb16f76fd125f0338db8afc4715c467ed6 100644 (file)
@@ -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<std::string>& globalErrors = _data.getGlobalErrors();
-    if(! globalErrors.empty())
-    {
-      buffer << "Global errors:" << std::endl;
-      std::list<std::string>::const_iterator it;
-      for(it=globalErrors.begin(); it!=globalErrors.end(); it++)
-      {
-        buffer << *it << std::endl;
-      }
-      buffer << "-----------------------------------------" << std::endl;
-    }
-    
-    std::list<FunctionProperties>::const_iterator it_fp;
-    const std::list<FunctionProperties>& 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<std::string>::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)
   {
index 107d59d79edb9264c7da863faf5511636b3a9c59..c1f40633ff6f3f3fb4b1ab4c750a3a400a34c144 100644 (file)
@@ -16,7 +16,7 @@
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-#include "Py2YacsDialog.hxx"
+#include "Py2YacsDialog_raw.hxx"
 #include <QApplication>
 #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 (file)
index 0000000..f31011c
--- /dev/null
@@ -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 <QApplication>
+#include "RuntimeSALOME.hxx"
+#include "Proc.hxx"
+#include <PyEditor_StdSettings.h>
+#include <iostream>
+
+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;
+}