Salome HOME
Minor changes in various source files to remove compilation warnings (unused variable...
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI.cxx
index 64aa853ac8d7ad0b5122ed3e127ad89362dfd82f..11ac798d47d445cadf7000afc718016c998f015b 100644 (file)
@@ -74,7 +74,11 @@ SUPERVGUI::~SUPERVGUI() {
         factory = 0;
        //if (!engine->_is_nil()) CORBA::release(engine);         
         //info->close();
-        if (browser) browser->close();
+        if (browser) {
+         if (browser->isVisible())
+           browser->close();
+         delete browser;
+       }
         MESSAGE("SUPERVGUI Info : factory destroy");
     } else {
         MESSAGE("SUPERVGUI Error : another call to factory destructor");
@@ -104,10 +108,10 @@ QAD_StudyFrame* SUPERVGUI::createGraph() {
   QAD_StudyFrame* aStudyFrame = study->newWindow3d(study->getNextStudyFrameName(),
                                                   VIEW_GRAPHSUPERV, false);
   if (aStudyFrame) {
-    connect(aStudyFrame
-           SIGNAL(sfStudyFrameClosing(QAD_StudyFrame*)), 
+    connect(study
+           SIGNAL(supervStudyFrameClosing(QAD_ViewFrame*)), 
            &Supervision, 
-           SLOT(onGraphClosed(QAD_StudyFrame*)));
+           SLOT(onGraphClosed(QAD_ViewFrame*)));
   }
   return aStudyFrame;
 }
@@ -195,14 +199,50 @@ bool SUPERVGUI::isContains(QAD_Study* theStudy, const QString theKey) {
 }
 
 
-void SUPERVGUI::onGraphClosed(QAD_StudyFrame* theStudyFrame) {
-  QAD_ViewFrame* viewFrame = theStudyFrame->getRightFrame()->getViewFrame();
-  if(SUPERVGraph_ViewFrame* supervFrame = dynamic_cast<SUPERVGraph_ViewFrame*>(viewFrame)){
-    disconnect(theStudyFrame, 0 , this, 0);
+void SUPERVGUI::onGraphClosed(QAD_ViewFrame* theViewFrame) {
+  if(SUPERVGraph_ViewFrame* supervFrame = dynamic_cast<SUPERVGraph_ViewFrame*>(theViewFrame)){
+    disconnect(study, 0 , this, 0);
     SUPERVGraph_View* view = supervFrame->getViewWidget();
     SUPERVGUI_Main* aGraph = dynamic_cast<SUPERVGUI_Main*>(view);
     if (aGraph) {
       unregisterGraph(aGraph);
+      SUPERV_Graph aDataFlow = aGraph->getDataflow();
+      if ( !SUPERV_isNull( aDataFlow ) ) {
+       if ( aDataFlow->IsExecuting() ) {
+         const int aMsgResult = QMessageBox::warning(QAD_Application::getDesktop(),
+                                                     tr("WARNING"), 
+                                                     tr("MSG_DF_RUNNING"), 
+                                                     tr("MSG_DF_EXECUTION"), 
+                                                     tr("MSG_DF_KILL"));
+         if ( aMsgResult == 1 ) {
+           // KILL EXECUTION
+           if ( aDataFlow->Kill() ) {
+             // Kill() sends KillEvent and KillState to SUPERVGUI_Thread
+             // while sets myIsActive flag to false when it receives such event/state
+             // after myIsActive is false it calls QThread::exit() to terminate.
+
+             // why while() { qApp->processEvents() } ?
+             // because: SUPERVGUI_Thread::run() receives events, and calls myMain->execute()
+             // method using SALOME_Event paradigm, ProcessVoidEvent() function - 
+             // it puts this event to the main application event loop, in which we are being now.
+             // So if we block main GUI application thread (by calling aGraph->getMyThread()->wait() here)
+             // then we will have a deadlock of 2 threads waiting for one another 
+             while (aGraph->getMyThread()->running()) 
+               qApp->processEvents();
+           }
+         }
+         else { // BACKGROUND EXECUTION
+           emit KillMainThread(true); // set SUPERVGUI_Thread::myIsActive to false
+           while ( aGraph->getMyThread()->running() )
+             qApp->processEvents();
+         }
+       }
+       else { // EXECUTION IS FINISHED, JUST DESTROY THE SUPERVGUI_Thread object and return.
+         emit KillMainThread(true);
+         while ( aGraph->getMyThread()->running() ) 
+           qApp->processEvents();
+       }
+      }
     }
   }
 }
@@ -218,8 +258,10 @@ void SUPERVGUI::setMain(QWidget* w) {
   QAD_ViewFrame* viewFrame = aStudyFrame->getRightFrame()->getViewFrame();
   if(SUPERVGraph_ViewFrame* supervFrame = dynamic_cast<SUPERVGraph_ViewFrame*>(viewFrame)){
     SUPERVGraph_View* view = supervFrame->getViewWidget();
-    if (view)
+    if (view) {
       main = dynamic_cast<SUPERVGUI_Main*>(view);
+      connect(&Supervision, SIGNAL(KillMainThread(bool)), main->getMyThread(), SLOT(KillThread(bool)));
+    }
   }
   Trace("SUPERVGUI::setMain - main: "<<main);
 }
@@ -279,11 +321,22 @@ void SUPERVGUI::displayDataflow() {
                                 QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
          return;
       }
-      aDataFlow = engine->getStreamGraph(ior);
+      //aDataFlow = engine->getStreamGraph(ior);
+      aDataFlow = engine->getGraph(ior);
       if (SUPERV_isNull(aDataFlow)) {
        QMessageBox::warning(0, tr("ERROR"), tr("MSG_ACCESS_BAD_IOR"));
        return;
       }
+      /* ASV: 20.10.04: fix for 6896
+      if (aDataFlow->IsStreamGraph()) {
+       SUPERV_StreamGraph aDataFlowStream = aDataFlow->ToStreamGraph();
+       if (SUPERV_isNull(aDataFlowStream)) {
+         QMessageBox::warning(0, tr("ERROR"), tr("MSG_ACCESS_BAD_IOR"));
+         return;
+       }
+       aDataFlow = aDataFlowStream;
+      }
+      */
     }
     QAD_StudyFrame* aStudyFrame = createGraph();
     SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>
@@ -311,45 +364,104 @@ void SUPERVGUI::renameDataflow() {
       if (obj->FindAttribute(anAttr, "AttributeName")) {
        aName = SALOMEDS::AttributeName::_narrow(anAttr);
        QString nm = QString( aName->Value() );
-       bool ok = FALSE;
        nm = SALOMEGUI_NameDlg::getName( QAD_Application::getDesktop(), nm );
        if ( !nm.isEmpty() ) {
-         QApplication::setOverrideCursor( Qt::waitCursor );
+         // sak : 24.11.04 : fix for PAL6898 : if rename fails (study locked), 
+         // a message box is displayed, and cursor is "wait cursor".  We think that "wait cursor"
+         // is not neccessary here, because the rename operation is fast.
+         //QApplication::setOverrideCursor( Qt::waitCursor );
          study->renameIObject( IObject, nm );
-         QApplication::restoreOverrideCursor();
+         //QApplication::restoreOverrideCursor();
        }
       }
     }
   }
 }
 
-
 void SUPERVGUI::exportDataflow() {
     Trace("SUPERVGUI::exportDataflow")
-    if (main==0) {
-        QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_EXPORT"));
-    } else {
+    if ( main==0 ) {
+      QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_EXPORT"));
+    } 
+    else if ( SUPERV_isNull( main->getDataflow() ) ) { // should not normally happen..
+      QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_EXPORT"));
+    } 
+    else {
       QString f = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
                                           "",
                                           "*.xml",
                                           tr("TTL_EXPORT_DATAFLOW"),
                                           false);
-      if (!f.isEmpty()) {
-        if (main->exportDataflow(f)) {
+      if ( !f.isEmpty() ) {
+       
+       // asv : bug [VSR Bugs and Improvements in Supervisor] 1.8 : when exporting a file, 
+       // a backup copy of an existing file must be created (in case Export fails..)
+       QString aBackupFile = SUPERVGUI::createBackupFile( f );
+
+       if ( main->getDataflow()->Export(f.latin1()) ) {
          unregisterGraph(main);
          registerGraph(f, main);
+
+         // remove a backup file if export was successfull
+         if ( !aBackupFile.isNull() && !aBackupFile.isEmpty() )
+           QFile::remove( aBackupFile );
+       }
+       else {
+         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_WRITING").arg(aBackupFile));
        }
       }
     }
 }
 
+/** 
+ *If a file with theOriginalFileName exists, then the function tries to create 
+ * its backup copy of it (the_name + ".bakX", X = 0,1,2,..,99).  Returns backup file's name
+ * If a file with theOriginalFileName does not exist, or creation fails - Return empty string.
+ */
+QString SUPERVGUI::createBackupFile( const QString& theOriginalFileName ) {
+  QString theBackupFileName("");
+  if ( QFile::exists( theOriginalFileName ) ) {
+    // 1. construct a backup file name
+    int i = 0;
+    do {
+      theBackupFileName = theOriginalFileName;
+      theBackupFileName += ".bak";
+      theBackupFileName += QString::number(i++);
+    } 
+    while ( QFile::exists( theBackupFileName ) && i < 100 ); // max 99 backup files
+    // if *.bak99 exists -- it will be overwritten 
+    
+    // 2. copy the original file to theBackupFileName
+    QString cmd( "cp \"");
+    cmd += theOriginalFileName;
+    cmd += "\" \"";
+    cmd += theBackupFileName;
+    cmd += "\"";
+    bool res = system( cmd.latin1() );
+    if ( res ) 
+      theBackupFileName = QString("");
+  }
+  return theBackupFileName;
+}
+
 void SUPERVGUI::newDataflow() {
     Trace("SUPERVGUI::editDataflow");
+
+    //asv 20.10.04: removed 2 SUPERVGUI_Main constructors.  there is only ONE way 
+    //to create a Main object now: with a non-null DataFlow as a 3d parameter
+    //1. create a Graph object
+    SUPERV_Graph aNewDataFlow = engine->StreamGraph( MAIN_NEW );
+    if (SUPERV_isNull( aNewDataFlow )) {
+      QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_DF"));
+      return;
+    }
+    //2. create a ViewFrame object
     QAD_StudyFrame* aStudyFrame = createGraph();
     SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>
       (aStudyFrame->getRightFrame()->getViewFrame());
     if(aViewFrame){
-      main = new SUPERVGUI_Main(aViewFrame, desktop, false);
+      //3. bind Graph and ViewFrame -> create Main
+      main = new SUPERVGUI_Main(aViewFrame, desktop, aNewDataFlow );
       main->addNode();
       study->showFrame(aStudyFrame);
     }
@@ -403,6 +515,7 @@ void SUPERVGUI::runDataflow() {
     if (main==0) {
         QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_RUN"));
     } else {
+        main->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
         main->run();
     };
 }
@@ -412,6 +525,7 @@ void SUPERVGUI::stepByStep() {
     if (main==0) {
         QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_RUN"));
     } else {
+        main->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
         main->startExecute();
     }
 }
@@ -522,9 +636,16 @@ void SUPERVGUI::whatIsSelected(const Handle(SALOME_InteractiveObject)& theObj, b
          SALOMEDS::GenericAttribute_var anAttr;
          if (obj->FindAttribute(anAttr, "AttributeIOR")) {
            SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
-           SUPERV_Graph aDataFlow = engine->getStreamGraph(anIOR->Value());
-           if (!SUPERV_isNull(aDataFlow)) 
-             theIsDataflow = true;
+           //SUPERV_Graph aDataFlow = engine->getStreamGraph(anIOR->Value());
+           SUPERV_Graph aDataFlow = engine->getGraph(anIOR->Value());
+           if (!SUPERV_isNull(aDataFlow)) {
+             if (aDataFlow->IsStreamGraph()) {
+               if (!SUPERV_isNull(aDataFlow->ToStreamGraph()))
+                 theIsDataflow = true;
+             }
+             else
+               theIsDataflow = true;
+           }
          }
          CORBA::String_var anObjectID = obj->GetID();
          CORBA::String_var aComponentID = comp->GetID();
@@ -535,16 +656,23 @@ void SUPERVGUI::whatIsSelected(const Handle(SALOME_InteractiveObject)& theObj, b
   }
 }
 
-
 void SUPERVGUI::deleteObject() {
   SALOME_Selection* Sel = SALOME_Selection::Selection(study->getSelection() );
   if ((Sel==NULL) || (Sel->IObjectCount() == 0)) return;
    
+  // sak : 24.11.04 : fix for PAL6899 : if the study is locked - warn the user and return.
+  SALOMEDS::Study_var aStudy = study->getStudyDocument();
+  if ( aStudy->GetProperties()->IsLocked() ) {
+    QMessageBox::warning(QAD_Application::getDesktop(), tr("WRN_WARNING"), tr("WRN_STUDY_LOCKED"));
+    return;
+  }
+
+  // sak : 24.11.04 : fix for PAL6901 : Incorrect deletion of the objects : pressing ESC button
+  // emitted "YES" button click, now it is corrected, and treated as "NO" button click.
   if (QMessageBox::warning(QAD_Application::getDesktop(),
                           tr("WARNING"),
                           tr("MSG_ASK_DELETE"),
-                          QMessageBox::No,
-                          QMessageBox::Yes) == QMessageBox::No)
+                          QMessageBox::No, QMessageBox::Yes) != QMessageBox::Yes)
     return;
    
   SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
@@ -553,7 +681,6 @@ void SUPERVGUI::deleteObject() {
     bool aIsOwner, aIsDataflow;
     whatIsSelected(anIObj, aIsOwner, aIsDataflow);
     
-    SALOMEDS::Study_var aStudy = study->getStudyDocument();
     SALOMEDS::SObject_var  aObj = aStudy->FindObjectID( anIObj->getEntry() );
     if (!aObj->_is_nil()) {
       if (aIsDataflow) {