Salome HOME
portage V8_5_0
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Operation.cxx
index 0d7310d6e56317fe7f5de5c13d5771e4342c8a38..30caab310ad1451e56a17b3c4a5b576259ce93c6 100644 (file)
@@ -1,12 +1,8 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
-//
-// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-//
+// Copyright (C) 2014-2015  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.
+// 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
 //
 
 #include "HYDROGUI_Operation.h"
-#include "HYDROGUI_Module.h"
+
 #include "HYDROGUI_InputPanel.h"
+#include "HYDROGUI_Module.h"
+#include "HYDROGUI_Tool.h"
+#include "HYDROGUI_OCCDisplayer.h"
+#include "HYDROGUI_Shape.h"
 
 #include <HYDROData_Document.h>
 #include <HYDROData_Iterator.h>
 #include <LightApp_SelectionMgr.h>
 
 #include <SUIT_Desktop.h>
+#include <SUIT_MessageBox.h>
 #include <SUIT_Study.h>
 
+#include <OCCViewer_ViewManager.h>
+#include <OCCViewer_ViewWindow.h>
+#include <OCCViewer_ViewPort3d.h>
+
+#include <QApplication>
+
 HYDROGUI_Operation::HYDROGUI_Operation( HYDROGUI_Module* theModule )
-: LightApp_Operation(), myModule( theModule ), myPanel( 0 )
+: LightApp_Operation(),
+  myModule( theModule ),
+  myPanel( 0 ),
+  myIsPrintErrorMessage( true ),
+  myIsTransactionOpened( false ),
+  myPreviewManager( 0 ),
+  myPreviewZLayer( -1 ),
+  myIsApplyAndClose( true )
 {
+  connect( this, SIGNAL( helpContextModule( const QString&, const QString&,
+                                            const QString& ) ),
+           theModule->application(), SLOT( onHelpContextModule( const QString&,
+                                            const QString&, const QString& ) ) );
 }
 
 HYDROGUI_Operation::~HYDROGUI_Operation()
 {
 }
 
+void HYDROGUI_Operation::setName( const QString& theName )
+{
+  myName = theName;
+}
+
+const QString& HYDROGUI_Operation::getName() const
+{
+  return myName;
+}
+
+HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
+{
+  if( !myPanel )
+  {
+    ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
+    connect( myPanel, SIGNAL( panelApplyAndClose() ), this, SLOT( onApplyAndClose() ) );
+    connect( myPanel, SIGNAL( panelApply() ),  this, SLOT( onApply() ) );
+    connect( myPanel, SIGNAL( panelCancel() ), this, SLOT( onCancel() ) );
+    connect( myPanel, SIGNAL( panelHelp() ), this, SLOT( onHelp() ) );
+  }
+  return myPanel;
+}
+
 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
 {
   return myModule->getApp()->selectionMgr();
@@ -52,52 +93,378 @@ HYDROGUI_Module* HYDROGUI_Operation::module() const
   return myModule;
 }
 
-void HYDROGUI_Operation::startOperation()
+/**
+  * Returns Z layer of the operation preview.
+   \ returns a layer position
+ */
+int HYDROGUI_Operation::getPreviewZLayer() const
+{
+  return myPreviewZLayer;
+}
+
+/**
+ * Update Z layer for the operation preview.
+   \param theLayer a layer position
+ */
+void HYDROGUI_Operation::updatePreviewZLayer( int theLayer )
 {
-  doc()->StartOperation();
+  setPreviewZLayer( theLayer );
 
-  if( inputPanel() )
+  HYDROGUI_Shape* aPreview = getPreviewShape();
+  if ( aPreview )
+    aPreview->setZLayer( getPreviewZLayer() );
+}
+
+/**
+ * Set Z layer for the operation preview.
+ \param theLayer a layer position
+ */
+void HYDROGUI_Operation::setPreviewZLayer( int theLayer )
+{
+  if ( theLayer != myPreviewZLayer )
+    myPreviewZLayer = theLayer;
+}
+
+/**
+ * Returns a shape preview of the operation
+ */
+HYDROGUI_Shape* HYDROGUI_Operation::getPreviewShape() const
+{
+  return 0;
+}
+
+/**
+ * Return the operation preview manager
+ */
+OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager()
+{
+  return myPreviewManager;
+}
+
+/**
+ * Set the preview manager
+ */
+void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager )
+{
+  //No good: preview Z layer could be used by usual presentations
+  //if ( !theManager && myPreviewManager )
+  //  module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
+
+  myPreviewManager = theManager;
+
+  if ( myPreviewManager )
+    setPreviewZLayer( module()->getOCCDisplayer()->AddPreviewZLayer( myPreviewManager ) );
+}
+
+void HYDROGUI_Operation::setCursor()
+{
+  if ( myPreviewManager )
   {
-    connect( inputPanel(), SIGNAL( panelApply() ),  this, SLOT( OnApply() ) );
-    connect( inputPanel(), SIGNAL( panelCancel() ), this, SLOT( OnCancel() ) );
+    QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
+    for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
+    {
+      OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
+      if ( occWin )
+      {
+        OCCViewer_ViewPort3d* vp = occWin->getViewPort();
+        if ( vp )
+        {
+          // Save old cursor
+          myCursor = vp->cursor();
+          // Set specific cursor chosen in preferences
+          QCursor aCursor = module()->getPrefEditCursor();
+          vp->setDefaultCursor( aCursor.shape() );
+          vp->setCursor( *vp->getDefaultCursor() );
+        }
+      }
+    }
+  }
+}
 
-    inputPanel()->show();
+void HYDROGUI_Operation::restoreCursor()
+{
+  if ( myPreviewManager )
+  {
+    QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
+    for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
+    {
+      OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
+      if ( occWin )
+      {
+        OCCViewer_ViewPort3d* vp = occWin->getViewPort();
+        if ( vp )
+        {
+          // Restore old cursor
+          vp->setDefaultCursor( myCursor.shape() );
+          vp->setCursor( myCursor );
+        }
+      }
+    }
+  }
+}
+
+void HYDROGUI_Operation::setIsApplyAndClose( const bool theFlag )
+{
+  myIsApplyAndClose = theFlag;
+}
+
+bool HYDROGUI_Operation::isApplyAndClose() const
+{
+  return myIsApplyAndClose;
+}
+
+void HYDROGUI_Operation::apply()
+{
+  QApplication::setOverrideCursor( Qt::WaitCursor );
+
+  startDocOperation();
+
+  int anUpdateFlags = 0;
+  QString anErrorMsg;
+
+  bool aResult = false;
+  QStringList aBrowseObjectsEntries;
+
+  try
+  {
+    aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
+  }
+  catch ( Standard_Failure )
+  {
+    Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
+    anErrorMsg = aFailure->GetMessageString();
+    aResult = false;
+  }
+  catch ( ... )
+  {
+    aResult = false;
+  }
+  
+  QApplication::restoreOverrideCursor();
+
+  if ( aResult )
+  {
+    module()->update( anUpdateFlags );
+    commitDocOperation();
+    commit();
+    browseObjects( aBrowseObjectsEntries, myIsApplyAndClose );
+
+    if ( !myIsApplyAndClose && inputPanel() )
+      start();
+  }
+  else if( !anErrorMsg.isEmpty() )
+  {
+    // Abort document opeartion only if requested
+    if ( isToAbortOnApply() )
+      abortDocOperation();
+
+    printErrorMessage( anErrorMsg );
+    // If the operation has no input panel - do abort
+    if ( !inputPanel() ) {
+      abort();
+    }
+  } 
+}
+
+void HYDROGUI_Operation::startOperation()
+{
+  LightApp_Operation::startOperation();
+  myModule->getActiveOperations().push( this );
+
+  if( myIsApplyAndClose && inputPanel() )
+  {
     myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
+    inputPanel()->show();
   }
 }
 
-HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
+void HYDROGUI_Operation::abortOperation()
+{
+  LightApp_Operation::abortOperation();
+  closeInputPanel();
+}
+
+void HYDROGUI_Operation::commitOperation()
+{
+  LightApp_Operation::commitOperation();
+  if ( myIsApplyAndClose )
+    closeInputPanel();
+}
+
+void HYDROGUI_Operation::stopOperation()
+{
+  LightApp_Operation::stopOperation();
+
+  // pop the operation from the cached map of active operations
+  QStack<HYDROGUI_Operation*>& anOperations = myModule->getActiveOperations();
+  if ( !anOperations.empty() ) {
+    if ( anOperations.top() == this )
+      anOperations.pop();
+    else
+    {
+      // find in the stack the current operation and remove it from the stack
+      QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
+      aVIt.toBack();
+      aVIt.previous(); // skip the top show/hide operation
+      while ( aVIt.hasPrevious() )
+      {
+        HYDROGUI_Operation* anOp = aVIt.previous();
+        if ( anOp == this )
+          anOperations.remove( anOperations.lastIndexOf( anOp ) );
+      }
+    }
+  }
+  // release the preview manager with removing the added preview Z layer
+  setPreviewManager( 0 );
+}
+
+void HYDROGUI_Operation::setDialogActive( const bool active )
+{
+  LightApp_Operation::setDialogActive( active );
+  if( myPanel )
+  {
+    if( active )
+    {
+      myPanel->show();
+    }
+  }
+}
+
+HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
+{
+  return NULL;
+}
+
+void HYDROGUI_Operation::closeInputPanel()
+{
+  if( myPanel )
+  {
+    myModule->getApp()->desktop()->removeDockWidget( myPanel );
+    delete myPanel;
+    myPanel = 0;
+  }
+}
+
+bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
+                                       QString& theErrorMsg,
+                                       QStringList& theBrowseObjectsEntries )
+{
+  return false;
+}
+
+void HYDROGUI_Operation::processCancel()
 {
-  if( !myPanel )
-    ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
-  return myPanel;
 }
 
-Handle_HYDROData_Document HYDROGUI_Operation::doc() const
+void HYDROGUI_Operation::startDocOperation()
 {
-  int aStudyId = myModule->application()->activeStudy()->id();
-  return HYDROData_Document::Document( aStudyId );
+  // Open transaction in the model document only if it not
+  // already opened by other operation (intended for nested operations)
+  if ( !doc()->IsOperation() )
+  {
+    doc()->StartOperation();
+    myIsTransactionOpened = true;
+  }
+}
+
+void HYDROGUI_Operation::abortDocOperation()
+{
+  // Abort transaction in the model document only if it was 
+  // opened by this operation (intended for nested operations)
+  if ( myIsTransactionOpened && doc()->IsOperation() )
+  {
+    doc()->AbortOperation();
+    myIsTransactionOpened = false;
+  }
 }
 
-void HYDROGUI_Operation::OnApply()
+void HYDROGUI_Operation::commitDocOperation()
 {
-  doc()->CommitOperation();
-  inputPanel()->hide();
+  // Commit transaction in the model document only if it was 
+  // opened by this operation (intended for nested operations)
+  if ( myIsTransactionOpened && doc()->IsOperation() )
+  {
+    doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
+    myIsTransactionOpened = false;
+  }
 }
 
-void HYDROGUI_Operation::OnCancel()
+Handle(HYDROData_Document) HYDROGUI_Operation::doc() const
 {
-  doc()->AbortOperation();
-  inputPanel()->hide();
+  return HYDROData_Document::Document( myModule->getStudyId() );
 }
 
-Handle_HYDROData_Object HYDROGUI_Operation::FindObjectByName( const QString& theName, int theKind ) const
+void HYDROGUI_Operation::onApplyAndClose()
 {
-  HYDROData_Iterator anIt( doc(), theKind );
-  for( ; anIt.More(); anIt.Next() )
+  myIsApplyAndClose = true;
+  apply();
+}
+
+void HYDROGUI_Operation::onApply()
+{
+  myIsApplyAndClose = false;
+  apply();
+}
+
+void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
+{
+  myIsPrintErrorMessage = theIsPrint;
+}
+
+void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
+{
+  if ( myIsPrintErrorMessage )
   {
-    if( anIt.Current()->GetName() == theName )
-      return anIt.Current();
+    QString aMsg = tr( "INPUT_VALID_DATA" );
+    if( !theErrorMsg.isEmpty() )
+      aMsg.prepend( theErrorMsg + "\n" );
+    SUIT_MessageBox::critical( module()->getApp()->desktop(),
+                               tr( "INSUFFICIENT_INPUT_DATA" ),
+                               aMsg );
+
   }
-  return Handle_HYDROData_Object();
+  
+  myIsPrintErrorMessage = true;
+}
+
+void HYDROGUI_Operation::onCancel()
+{
+  processCancel();
+  abort();
+  if ( myPanel )
+    abortOperation();
+  myIsApplyAndClose = true;
+}
+
+void HYDROGUI_Operation::onHelp()
+{
+   emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
+}
+
+QString HYDROGUI_Operation::getHelpComponent() const
+{
+   return module()->moduleName();
+}
+
+QString HYDROGUI_Operation::getHelpFile() const
+{
+  QString aFileName = ((myName.isEmpty())? operationName() : myName);
+  aFileName = aFileName.toLower();
+  aFileName = aFileName.replace(QRegExp("\\s"), "_").append(".html");
+  aFileName.remove( "create_" );
+  aFileName.remove( "edit_" );
+  return aFileName;
+}
+
+QString HYDROGUI_Operation::getHelpContext() const
+{
+   return QString();
+}
+
+void HYDROGUI_Operation::browseObjects( const QStringList& theBrowseObjectsEntries,
+                                        const bool theIsApplyAndClose/* = true*/ )
+{
+  bool isOptimizedBrowse = true;
+  module()->getApp()->browseObjects( theBrowseObjectsEntries, theIsApplyAndClose, isOptimizedBrowse );
 }