Salome HOME
Bug fix: don't set "Loading" state for MacroNodes in InitialState() function (called...
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_Service.cxx
index 6b148e5f833214ed401cf23036fc3a72fa63be8d..6213aa42ed97c30cc6dc590b4cafcdb612a3eab1 100644 (file)
@@ -35,6 +35,7 @@ using namespace std;
 #include "SALOME_NamingService.hxx"
 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
 #include <qlayout.h>
+#include <qhbox.h>
 #include <qtextstream.h>
 
 
@@ -152,14 +153,13 @@ SUPERVGUI_Service::SUPERVGUI_Service(SALOME_NamingService* ns):
   QHGroupBox* aAddBox2 = new QHGroupBox(tr("TIT_ADDNODE"), aPythonPane);
   aAddBox2->setInsideSpacing(20);
 
-  QLabel* aTypeLbl = new QLabel(tr("LBL_NODETYPE"), aAddBox2);
+  /*QLabel* aTypeLbl = */new QLabel(tr("LBL_NODETYPE"), aAddBox2);
 
   myTypeCombo = new QComboBox(aAddBox2);
-  myTypeCombo->insertItem("Computation");
-  myTypeCombo->insertItem("Switch");
-  myTypeCombo->insertItem("Loop");
-  myTypeCombo->insertItem("GoTo");
-  //myTypeCombo->insertItem("Label");
+  myTypeCombo->insertItem( tr( "INLINE_COMP" ) );
+  myTypeCombo->insertItem( tr( "INLINE_SWTC" ) );
+  myTypeCombo->insertItem( tr( "INLINE_LOOP" ) );
+  myTypeCombo->insertItem( tr( "INLINE_GOTO") );
   connect(myTypeCombo, SIGNAL(activated(int)), this, SLOT(typeNodeSelected(int)));
 
   aPythonLayout->addWidget(aAddBox2);
@@ -700,8 +700,7 @@ void SUPERVGUI_Service::tabChanged(QWidget* theWidget) {
 //  Pane for Python script editing
 //*****************************************************
 SUPERVGUI_PythonEditPane::SUPERVGUI_PythonEditPane(QWidget* theParent) 
-  : QFrame(theParent),
-    myIStream(0)
+  : QFrame(theParent)
 {
   QGridLayout* aEditLayout = new QGridLayout(this, 2, 4);
 
@@ -725,6 +724,45 @@ SUPERVGUI_PythonEditPane::SUPERVGUI_PythonEditPane(QWidget* theParent)
   aEditLayout->addMultiCellWidget(myText, 1, 1, 0, 3);
 }
    
+/**
+ * Searches for text beginning with "def" and ending with any meanful character at 
+ * beginning of line.  Starts searching with current position of given stream.
+ * Fills the myPyFunctions with strings corresponding to found fucntions.
+ */
+void SUPERVGUI_PythonEditPane::initPyFunctions( QTextStream& theStream ) {
+
+  if ( theStream.atEnd() )
+    return;
+
+  QString aPyFunction;
+  QString aLine = theStream.readLine(); 
+
+  while ( !theStream.atEnd() ) { // not EOF
+
+    // asv : 23.11.04 : fix for PAL6870 : skip empty lines in the beginning or  between functions
+    //       find("def)==0 -> analizing only global function definitions which start with 0 indentation
+    while ( !aLine.isNull() && aLine.find( "def" ) != 0 ) 
+      aLine = theStream.readLine();
+    
+    if ( !aLine.isNull() && aLine.find("def") == 0 ) { 
+      aPyFunction += aLine;
+      aPyFunction += '\n'; 
+
+      // read function body
+      aLine = theStream.readLine();
+      // asv : 23.11.04 : added "|| aLine.isEmpty()" - fix for PAL6870. readLine() returns an empty
+      //       string for "\n" string, it trails \n caracter.  but we accept such lines..
+      while ( !aLine.isNull() && ( aLine.isEmpty() || aLine[0].isSpace() ) ) {
+       aPyFunction += aLine;
+       aPyFunction += '\n'; 
+       aLine = theStream.readLine();
+      }
+
+      myPyFunctions << aPyFunction;
+      aPyFunction.setLength( 0 );
+    }
+  }
+}
 
 /**
  * Load existing Python script
@@ -737,77 +775,50 @@ void SUPERVGUI_PythonEditPane::loadFile() {
                                               true);
   if (aFileName.isEmpty()) return;
 
-  myFile = new QFile(aFileName);
-  if (!myFile->open(IO_ReadOnly)) {
+  QFile aFile( aFileName );
+  if (!aFile.open(IO_ReadOnly)) {
     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), 
                         tr("MSG_CANT_LOADSCRIPT"));
     return;
   }
-  myIStream = new QTextStream(myFile);
 
-  if (myIStream->atEnd()) {
-    QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), 
-                        tr("MSG_EMTY_FILE"));
-    myNextBtn->setEnabled(false);
-    delete myIStream;
-    myIStream = 0;
-    myFile->close();
-    delete myFile;
+  myPyFunctions.clear();
+  myPyIndex = -1;
+  myNextBtn->setEnabled( false );
+  myText->clear();
+
+  QTextStream aFileReader(&aFile);
+  if ( aFileReader.atEnd() ) {
+    QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_EMTY_FILE"));
+    aFile.close();
     return;
   }
-  myNextBtn->setEnabled(true);
-  readFunction();
+
+  initPyFunctions( aFileReader );
+
+  if ( myPyFunctions.size() ) {
+    myNextBtn->setEnabled( true );
+    myPyIndex = 0;
+    readFunction();
+  }
 }
   
-
 /**
  * Finds and Reads a function from current position in the opened file
+ * asv : Comment above is old! Present model: just take an already read string = PyFunction
+ * from the list which is filled in loadFile().
  */
 void SUPERVGUI_PythonEditPane::readFunction() {
-  if (!myIStream) return;
-
-  while (!myIStream->atEnd()) {
-    QString aLine = myIStream->readLine();
-    if (aLine.isNull()) return;
-
-    // find first function
-    int aDefPos = aLine.find("def");
-    if (aDefPos == 0) { // only not a class members
-      myText->clear();
-      // find name
-      /*int aStart = aLine.find(" ", aDefPos);
-      int aEnd = aLine.find("(", aStart);
-      QString aName = aLine.mid(aStart, (aEnd-aStart));
-      aName = aName.stripWhiteSpace();
-      myNameEdt->setText(aName);*/
-      
-      // find input params
-      /*aStart = aEnd+1;
-      aEnd = aLine.find(")", aStart);
-      QString aParams = aLine.mid(aStart, (aEnd-aStart));
-      aParams = aParams.stripWhiteSpace();
-      myParamEdt->setText(aParams);*/
-
-      myText->append(aLine);
-      // read body
-      QString aBodyLine = myIStream->readLine();
-      while ((!aBodyLine.isNull()) && aBodyLine[0].isSpace()) {
-       myText->append(aBodyLine);
-       aBodyLine = myIStream->readLine();
-      }
-      return;
-    }
-  }
-  QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), 
-                        tr("MSG_NOMORE_FUNCTIONS"));
-  delete myIStream;
-  myIStream = 0;
-  myNextBtn->setEnabled(false);
-  myFile->close();
-  delete myFile;
+  myText->clear();
+  if ( myPyIndex != -1 && myPyFunctions.size() && myPyFunctions.size() > myPyIndex )
+    myText->append( myPyFunctions[ myPyIndex++ ] );
+  if ( myPyFunctions.size() <= myPyIndex ) // last index was reached
+    myNextBtn->setEnabled( false );
 }
 
-
+/**
+ * Returns the text after "def" and before "(" -- first defined function name
+ */
 QString SUPERVGUI_PythonEditPane::getFuncName() {
   QString aName("");
   for (int i=0; i < myText->paragraphs(); i++) {
@@ -823,14 +834,20 @@ QString SUPERVGUI_PythonEditPane::getFuncName() {
   return aName;
 }
 
-
+/**
+ * Returns the contents of the myText widget without trailing spacing 
+ */
 SUPERV_Strings SUPERVGUI_PythonEditPane::getFunction() {
   SUPERV_Strings aStrings = new SUPERV::ListOfStrings();
   aStrings->length(myText->paragraphs());
   for (int i=0; i < myText->paragraphs(); i++) {
     QString aLine = myText->text(i);
-    if (!aLine.right(1).compare(" "))
-      aLine = aLine.remove(aLine.length()-1,1);
+    // asv : 30.11.04 - why do we have to remove trailing spaces??  
+    // it's user's responsibility to enter correct Python code, we don't do anything with it.
+    // if (..) -- initial, while(..) -- my improvement, but also commented out -- needless.
+    //if (!aLine.right(1).compare(" ")) // replaced with the line below -- loop
+    //while (aLine.at(aLine.length()-1).isSpace()) // remove trailing spaces
+    //  aLine = aLine.remove(aLine.length()-1,1);
     aStrings[i] = CORBA::string_dup(aLine.latin1());
   }
   return aStrings._retn();
@@ -841,6 +858,12 @@ void SUPERVGUI_PythonEditPane::setFunction(SUPERV_Strings theStr) {
   int aLen = theStr->length();
   for (int i=0; i < aLen; i++)
     myText->append(QString(theStr[i]));
+
+  // asv : 30.11.04 : 2.7 - Inline node function editor improvement
+  // 2.7.1 - set focus to editor widget on editor window opening
+  // 2.7.2 - scroll to the beginning of function on editor window opening
+  myText->setFocus();
+  myText->ensureVisible( 0,0 );
 }