Salome HOME
updated copyright message
[modules/yacs.git] / src / ydfx_gui / YDFXGUISeqInit.cxx
index 500ec1ad398e67fc757935aba95955e902682e32..0f3d69f3167038d69f51cb8c1979a5be986b4031 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016  CEA/DEN, EDF R&D
+// Copyright (C) 2016-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -18,6 +18,7 @@
 //
 // Author : Anthony Geay (EDF R&D)
 
+#include "Python.h"
 #include "YDFXGUISeqInit.hxx"
 
 #include <QFile>
@@ -31,9 +32,8 @@
 #include <QFileDialog>
 #include <QApplication>
 
-#include "Python.h"
 
-#include "AutoGIL.hxx"
+#include "PythonCppUtils.hxx"
 
 #include "YDFXGUIWrap.hxx"
 #include "YDFXGUIPyThreadSaver.hxx"
 
 const char YDFXGUIStatus::OK_STR[]="OK !";
 
-class AutoPyRef
-{
-public:
-  AutoPyRef(PyObject *pyobj=0):_pyobj(pyobj) { }
-  ~AutoPyRef() { release(); }
-  AutoPyRef(const AutoPyRef& other):_pyobj(other._pyobj) { if(_pyobj) Py_XINCREF(_pyobj); }
-  AutoPyRef& operator=(const AutoPyRef& other) { if(_pyobj==other._pyobj) return *this; release(); _pyobj=other._pyobj; Py_XINCREF(_pyobj); return *this; }
-  operator PyObject *() { return _pyobj; }
-  void set(PyObject *pyobj) { if(pyobj==_pyobj) return ; release(); _pyobj=pyobj; }
-  PyObject *get() { return _pyobj; }
-  bool isNull() const { return _pyobj==0; }
-  PyObject *retn() { if(_pyobj) Py_XINCREF(_pyobj); return _pyobj; }
-private:
-  void release() { if(_pyobj) Py_XDECREF(_pyobj); _pyobj=0; }
-private:
-  PyObject *_pyobj;
-};
-
 ////////////////////////////
 
 void YDFXGUIDoubleVectHolder::applyOnInput(YACSEvalInputPort *inp) const
@@ -101,7 +83,7 @@ bool YDFXGUISeqSetterP::executeScript(int& sz)
   //
   if(_fileName.isEmpty())
     {
-      emit problemDetected(QString("For \"%1\" : no file defined !").arg(zeBossc->getName()));
+      Q_EMIT problemDetected(QString("For \"%1\" : no file defined !").arg(zeBossc->getName()));
       return false;
     }
   QFile file(_fileName);
@@ -117,7 +99,7 @@ bool YDFXGUISeqSetterP::executeScript(int& sz)
       double v(line2.toDouble(&isOK));
       if(!isOK)
         {
-          emit problemDetected(QString("For \"%1\" : At line %2 it is not a float !").arg(zeBossc->getName()).arg(i));
+          Q_EMIT problemDetected(QString("For \"%1\" : At line %2 it is not a float !").arg(zeBossc->getName()).arg(i));
           return false;
         }
       _vect.push_back(v);
@@ -199,26 +181,26 @@ bool YDFXGUISeqSetterT::executeScript(int& sz)
   std::string txt(toPlainText().toStdString());
   YDFXGUIPyThreadSaver::SaveContext(QApplication::instance()->thread());
   {
-    YACS::ENGINE::AutoGIL gal;
+    AutoGIL gal;
     AutoPyRef code(Py_CompileString(txt.c_str(),TMP_FILENAME, Py_file_input));
     if(code.get() == NULL)
       {
-        emit problemDetected(QString("For \"%1\" : python code is invalid !").arg(zeBossc->getName()));
+        Q_EMIT problemDetected(QString("For \"%1\" : python code is invalid !").arg(zeBossc->getName()));
         return false;
       }
     AutoPyRef context(PyDict_New());
     PyDict_SetItemString( context, "__builtins__", PyEval_GetBuiltins() );
-    AutoPyRef res(PyEval_EvalCode((PyCodeObject *)code.get(), context, context));
+    AutoPyRef res(PyEval_EvalCode(code.get(), context, context));
     PyObject *item(PyDict_GetItemString(context,name.c_str()));
     //
     if(!item)
       {
-        emit problemDetected(QString("For \"%1\" : Py var %1 is not defined !").arg(zeBossc->getName()));
+        Q_EMIT problemDetected(QString("For \"%1\" : Py var %1 is not defined !").arg(zeBossc->getName()));
         return false;
       }
     if(!PyList_Check(item))
       {
-        emit problemDetected(QString("For \"%1\" : Py var %1 must be a list !").arg(zeBossc->getName()));
+        Q_EMIT problemDetected(QString("For \"%1\" : Py var %1 must be a list !").arg(zeBossc->getName()));
         return false;
       }
     sz=PyList_Size(item);
@@ -228,7 +210,7 @@ bool YDFXGUISeqSetterT::executeScript(int& sz)
         PyObject *val(PyList_GetItem(item,i));
         if(!PyFloat_Check(val))
           {
-            emit problemDetected(QString("For \"%1\" : At pos %2 of python list, it is not a float !").arg(zeBossc->getName()).arg(i));
+            Q_EMIT problemDetected(QString("For \"%1\" : At pos %2 of python list, it is not a float !").arg(zeBossc->getName()).arg(i));
             return false;
           }
         _vect[i]=PyFloat_AS_DOUBLE(val);
@@ -292,7 +274,7 @@ YDFXGUISeqSetter::YDFXGUISeqSetter(QWidget *parent, const QString& name):QWidget
   QVBoxLayout *verticalLayout(new QVBoxLayout(this));
   _textEdit=new YDFXGUISeqSetterT(this); verticalLayout->addWidget(_textEdit);
   _push=new YDFXGUISeqSetterP(this); verticalLayout->addWidget(_push);
-  _textEdit->setText(QString("import math\n%1=[math.sqrt(float(elt)+0.) for elt in xrange(4)]").arg(name));
+  _textEdit->setText(QString("import math\n%1=[math.sqrt(float(elt)+0.) for elt in range(4)]").arg(name));
   _textEdit->hide();
   _push->hide();
 }
@@ -409,7 +391,7 @@ YDFXGUISeqLine::YDFXGUISeqLine(QWidget *parent, YACSEvalInputPort *inp):_combo(0
   connect(_combo,SIGNAL(currentIndexChanged(int)),this,SLOT(typeOfAssignmentChanged(int)));
   horizontalLayout->addWidget(_setter);
   _combo->setCurrentIndex(0);
-  emit _combo->currentIndexChanged(0);//to be sure to sync widgets
+  Q_EMIT _combo->currentIndexChanged(0);//to be sure to sync widgets
 }
 
 void YDFXGUISeqLine::loadState(const QMap<QString,QString>& state)
@@ -551,7 +533,7 @@ void YDFXGUISeqInitEff::assignButtonClicked()
 {
   int sz;
   bool verdict(checkConsistency(sz));
-  emit configurationIsOK(verdict);
+  Q_EMIT configurationIsOK(verdict);
 }
 
 void YDFXGUISeqInitEff::applyOnEFX()
@@ -572,7 +554,7 @@ bool YDFXGUISeqInitEff::checkConsistency(int& sz)
         refSz=locSz;
       if(locSz!=refSz)
         {
-          emit line->setter()->problemDetected(QString("Var %1 does not have the same number of elts than others !").arg(line->getName()));
+          Q_EMIT line->setter()->problemDetected(QString("Var %1 does not have the same number of elts than others !").arg(line->getName()));
           return false;
         }
     }