Salome HOME
Modify creation of curves: 1) using QDockWidget instead of QDialog; 2) selection...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Operations.cxx
index 8f717cad837c00459dcf7f400234b4de970b8f2b..72c51c9a2d295398b918fcdf6d1bf11aa49e494d 100644 (file)
 
 #include "HYDROGUI_Operations.h"
 
+#include "HYDROGUI_CopyPasteOp.h"
 #include "HYDROGUI_DataModel.h"
+#include "HYDROGUI_DeleteOp.h"
+#include "HYDROGUI_ExportImageOp.h"
 #include "HYDROGUI_ImportImageOp.h"
+#include "HYDROGUI_ImportBathymetryOp.h"
+#include "HYDROGUI_CalculationOp.h"
 #include "HYDROGUI_Module.h"
+#include "HYDROGUI_ObserveImageOp.h"
+#include "HYDROGUI_PolylineOp.h"
+#include "HYDROGUI_ShowHideOp.h"
+#include "HYDROGUI_SplitZonesTool.h"
 #include "HYDROGUI_TwoImagesOp.h"
+#include "HYDROGUI_UpdateFlags.h"
+#include "HYDROGUI_UpdateImageOp.h"
+#include "HYDROGUI_VisualStateOp.h"
+#include "HYDROGUI_ZoneOp.h"
 
 #include <CAM_Application.h>
 
+#include <QtxListAction.h>
+
 #include <SUIT_Desktop.h>
 #include <SUIT_ResourceMgr.h>
+#include <SUIT_Session.h>
 
 #include <QAction>
+#include <QApplication>
 
-QAction* HYDROGUI_Module::CreateAction( const int theId, const QString& theSuffix, const QString& theImg,
+QAction* HYDROGUI_Module::createAction( const int theId, const QString& theSuffix, const QString& theImg,
                                         const int theKey, const bool isToggle, const QString& theSlot )
 {
   QString aSlot = theSlot;
   if( aSlot.isEmpty() )
     aSlot = SLOT( onOperation() );
-  SUIT_ResourceMgr* aMgr = application()->resourceMgr();
+  SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
   std::string anImg = theImg.toStdString();
-  QPixmap aPixmap = theImg.isEmpty() ? QPixmap() : aMgr->loadPixmap( "HYDROGUI", tr( anImg.c_str() ) );
+  QPixmap aPixmap = theImg.isEmpty() ? QPixmap() : aResMgr->loadPixmap( "HYDRO", tr( anImg.c_str() ) );
   std::string aMenu    = ( "MEN_" + theSuffix ).toStdString();
   std::string aDesktop = ( "DSK_" + theSuffix ).toStdString();
   std::string aToolbar = ( "STB_" + theSuffix ).toStdString();
@@ -52,28 +69,133 @@ QAction* HYDROGUI_Module::CreateAction( const int theId, const QString& theSuffi
                theKey, application()->desktop(), isToggle, this, aSlotStr.c_str() );
 }
 
-void HYDROGUI_Module::CreateActions()
+void HYDROGUI_Module::createActions()
 {
-  CreateAction( ImportImageId, "IMPORT_IMAGE", "", Qt::CTRL + Qt::Key_I );
-  CreateAction( FuseId, "FUSE_IMAGES" );
-  CreateAction( CutId, "CUT_IMAGES" );
+  createAction( SaveVisualStateId, "SAVE_VISUAL_STATE" );
+  createAction( LoadVisualStateId, "LOAD_VISUAL_STATE" );
+
+  createAction( CopyId, "COPY", "", Qt::CTRL + Qt::Key_C );
+  createAction( PasteId, "PASTE", "", Qt::CTRL + Qt::Key_V );
+
+  createAction( ImportImageId, "IMPORT_IMAGE", "", Qt::CTRL + Qt::Key_I );
+  createAction( EditImportedImageId, "EDIT_IMPORTED_IMAGE" );
+  createAction( ObserveImageId, "OBSERVE_IMAGE" );
+  createAction( ExportImageId, "EXPORT_IMAGE" );
+  createAction( UpdateImageId, "UPDATE_IMAGE" );
+
+  createAction( CreatePolylineId, "CREATE_POLYLINE" );
+  createAction( EditPolylineId, "EDIT_POLYLINE" ); 
+
+  createAction( ImportBathymetryId, "IMPORT_BATHYMETRY", "", Qt::CTRL + Qt::Key_B );
+
+  createAction( CreateZoneId, "CREATE_ZONE" );
+  createAction( EditZoneId, "EDIT_ZONE" );
+
+  createAction( CreateCalculationId, "CREATE_CALCULATION" );
+  createAction( EditCalculationId, "EDIT_CALCULATION" );
+
+  createAction( FuseImagesId, "FUSE_IMAGES" );
+  createAction( EditFusedImageId, "EDIT_FUSED_IMAGE" );
+
+  createAction( CutImagesId, "CUT_IMAGES" );
+  createAction( EditCutImageId, "EDIT_CUT_IMAGE" );
+
+  createAction( SplitImageId, "SPLIT_IMAGE" );
+  createAction( EditSplittedImageId, "EDIT_SPLITTED_IMAGE" );
+
+  createAction( DeleteId, "DELETE", "", Qt::Key_Delete );
+
+  createAction( ShowId, "SHOW" );
+  createAction( ShowOnlyId, "SHOW_ONLY" );
+  createAction( ShowAllId, "SHOW_ALL" );
+  createAction( HideId, "HIDE" );
+  createAction( HideAllId, "HIDE_ALL" );
 }
 
-void HYDROGUI_Module::CreateMenus()
+void HYDROGUI_Module::createMenus()
 {
-  int aHydroMenuIndex = 6; // Edit menu id == 5, View menu id == 10
-  int aHydroId = createMenu( tr( "MEN_DESK_HYDRO" ), -1, -1, aHydroMenuIndex );
+  int aFileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
+  int aCustomPos = 5; // to insert custom actions after "Save as" and before "Preferences"
+  createMenu( SaveVisualStateId, aFileMenu, aCustomPos );
+  createMenu( separator(), aFileMenu, -1, aCustomPos );
+
+  int anEditMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 5 );
+  createMenu( UndoId, anEditMenu );
+  createMenu( RedoId, anEditMenu );
+  createMenu( separator(), anEditMenu );
+  createMenu( CopyId, anEditMenu );
+  createMenu( PasteId, anEditMenu );
+
+  int aHydroMenu = 6; // Edit menu id == 5, View menu id == 10
+  int aHydroId = createMenu( tr( "MEN_DESK_HYDRO" ), -1, -1, aHydroMenu );
   createMenu( ImportImageId, aHydroId, -1, -1 );
-  createMenu( FuseId, aHydroId, -1, -1 );
-  createMenu( CutId, aHydroId, -1, -1 );
+  createMenu( ImportBathymetryId, aHydroId, -1, -1 );
+  createMenu( CreatePolylineId, aHydroId, -1, -1 );
+  createMenu( CreateZoneId, aHydroId, -1, -1 );
+  createMenu( CreateCalculationId, aHydroId, -1, -1 );
+  createMenu( separator(), aHydroId );
+  createMenu( FuseImagesId, aHydroId, -1, -1 );
+  createMenu( CutImagesId, aHydroId, -1, -1 );
+  createMenu( SplitImageId, aHydroId, -1, -1 );
+
+  createMenu( separator(), aHydroId );
+  createMenu( TestSplitId, aHydroId, -1, -1 );
 }
 
-void HYDROGUI_Module::CreatePopups()
+void HYDROGUI_Module::createPopups()
 {
 }
 
-void HYDROGUI_Module::CreateToolbars()
+void HYDROGUI_Module::createToolbars()
 {
+  int aToolBar = createTool( tr( "MEN_DESK_HYDRO" ) );
+  createTool( UndoId, aToolBar );
+  createTool( RedoId, aToolBar );
+}
+
+void HYDROGUI_Module::createUndoRedoActions()
+{
+  SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
+
+  QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
+    aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
+    Qt::CTRL + Qt::Key_Z, application()->desktop() );
+    
+  QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
+    aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
+    Qt::CTRL + Qt::Key_Y, application()->desktop() );
+  
+  registerAction( UndoId, anEditUndo );
+  registerAction( RedoId, anEditRedo );
+
+  anEditUndo->setComment( tr( "STB_UNDO" ) );
+  anEditRedo->setComment( tr( "STB_REDO" ) );
+
+  connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
+  connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
+
+  QAction* aTestSplit = new QAction( "Test split", application()->desktop() );
+  registerAction( TestSplitId, aTestSplit );
+  connect( aTestSplit, SIGNAL( triggered() ), this, SLOT( onTestSplit() ) );
+}
+
+void HYDROGUI_Module::updateUndoRedoControls()
+{
+  HYDROGUI_DataModel* aModel = getDataModel();
+
+  QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
+  QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
+
+  bool aCanUndo = aModel->canUndo();
+  bool aCanRedo = aModel->canRedo();
+
+  if( aCanUndo )
+    aUndoAction->addNames( aModel->undoNames() );
+  aUndoAction->setEnabled( aCanUndo );
+
+  if( aCanRedo )
+    aRedoAction->addNames( aModel->redoNames() );
+  aRedoAction->setEnabled( aCanRedo );
 }
 
 void HYDROGUI_Module::onOperation()
@@ -84,20 +206,113 @@ void HYDROGUI_Module::onOperation()
     startOperation( anId );
 }
 
+bool HYDROGUI_Module::onUndo( int theNumActions )
+{
+  QApplication::setOverrideCursor( Qt::WaitCursor );
+  bool anIsOk = true;
+  HYDROGUI_DataModel* aModel = getDataModel();
+  if( aModel )
+  {
+    while( theNumActions > 0 )
+    {
+      if( !aModel->undo() )
+      {
+        anIsOk = false;
+        break;
+      }
+      theNumActions--;
+    }
+    update( UF_All );
+  }
+  QApplication::restoreOverrideCursor();
+  return anIsOk;
+}
+
+bool HYDROGUI_Module::onRedo( int theNumActions )
+{
+  QApplication::setOverrideCursor( Qt::WaitCursor );
+  bool anIsOk = true;
+  HYDROGUI_DataModel* aModel = getDataModel();
+  if( aModel )
+  {
+    while( theNumActions > 0 )
+    {
+      if( !aModel->redo() )
+      {
+        anIsOk = false;
+        break;
+      }
+      theNumActions--;
+    }
+    update( UF_All );
+  }
+  QApplication::restoreOverrideCursor();
+  return anIsOk;
+}
+
 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
 {
   LightApp_Operation* anOp = 0;
   HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
   switch( theId )
   {
+  case SaveVisualStateId:
+  case LoadVisualStateId:
+    anOp = new HYDROGUI_VisualStateOp( aModule, theId == LoadVisualStateId );
+    break;
+  case CopyId:
+  case PasteId:
+    anOp = new HYDROGUI_CopyPasteOp( aModule, theId == PasteId );
+    break;
   case ImportImageId:
-    anOp = new HYDROGUI_ImportImageOp( aModule );
+  case EditImportedImageId:
+    anOp = new HYDROGUI_ImportImageOp( aModule, theId == EditImportedImageId );
+    break;
+  case ObserveImageId:
+    anOp = new HYDROGUI_ObserveImageOp( aModule );
+    break;
+  case ExportImageId:
+    anOp = new HYDROGUI_ExportImageOp( aModule );
+    break;
+  case UpdateImageId:
+    anOp = new HYDROGUI_UpdateImageOp( aModule );
+    break;
+  case CreatePolylineId:
+  case EditPolylineId:
+    anOp = new HYDROGUI_PolylineOp( aModule, theId == EditPolylineId );
+    break;
+  case ImportBathymetryId:
+    anOp = new HYDROGUI_ImportBathymetryOp( aModule );
+    break;
+  case CreateZoneId:
+  case EditZoneId:
+    anOp = new HYDROGUI_ZoneOp( aModule, theId == EditZoneId );
+    break;
+  case CreateCalculationId:
+  case EditCalculationId:
+    anOp = new HYDROGUI_CalculationOp( aModule, theId == EditCalculationId );
+    break;
+  case FuseImagesId:
+  case EditFusedImageId:
+    anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse, theId == EditFusedImageId );
+    break;
+  case CutImagesId:
+  case EditCutImageId:
+    anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut, theId == EditCutImageId );
     break;
-  case FuseId:
-    anOp = new HYDROGUI_TwoImagesOp( aModule, tr( "FUSE_OP" ) );
+  case SplitImageId:
+  case EditSplittedImageId:
+    anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Split, theId == EditSplittedImageId );
     break;
-  case CutId:
-    anOp = new HYDROGUI_TwoImagesOp( aModule, tr( "CUT_OP" ) );
+  case DeleteId:
+    anOp = new HYDROGUI_DeleteOp( aModule );
+    break;
+  case ShowId:
+  case ShowOnlyId:
+  case ShowAllId:
+  case HideId:
+  case HideAllId:
+    anOp = new HYDROGUI_ShowHideOp( aModule, theId );
     break;
   }
 
@@ -106,3 +321,100 @@ LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
 
   return anOp;
 }
+
+//-----------------------------------------------------------------------------
+// Test splitting
+//-----------------------------------------------------------------------------
+#include <HYDROData_Iterator.h>
+#include <HYDROGUI_Tool.h>
+void HYDROGUI_Module::onTestSplit()
+{
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( getStudyId() );
+
+  aDocument->StartOperation();
+
+  HYDROData_SequenceOfObjects aZoneList;
+  HYDROData_Iterator anIterator( aDocument, KIND_ZONE );
+  for( ; anIterator.More(); anIterator.Next() )
+    aZoneList.Append( anIterator.Current() );
+
+  HYDROGUI_SplitZonesTool::SplitDataList aSplitDataList =
+    HYDROGUI_SplitZonesTool::SplitZones( aZoneList, NULL );
+
+  int anIndex = 0;
+  HYDROGUI_SplitZonesTool::SplitDataListIterator anIter( aSplitDataList );
+  while( anIter.hasNext() )
+  {
+    const HYDROGUI_SplitZonesTool::SplitData& aSplitData = anIter.next();
+    const QPainterPath& aPath = aSplitData.Path;
+    const QStringList& aZoneNames = aSplitData.ZoneNames;
+
+    Handle(HYDROData_Polyline) aPolyline =
+      Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
+    if( !aPolyline.IsNull() )
+    {
+      aPolyline->SetName( QString( "SplitPolyline_%1" ).arg( ++anIndex ) );
+      aPolyline->setDimension( 2 );
+
+      QList<PolylineSection> aPolylineData;
+      for( int i = 0, n = aPath.elementCount(); i < n; i++ )
+      {
+        const QPainterPath::Element anElement = aPath.elementAt( i );
+        switch( anElement.type )
+        {
+          case QPainterPath::MoveToElement:
+            aPolylineData.append( PolylineSection() );
+            break;
+          case QPainterPath::LineToElement:
+            if( !aPolylineData.isEmpty() )
+            {
+              PolylineSection& aSection = aPolylineData.last();
+              aSection.myCoords << anElement.x;
+              aSection.myCoords << anElement.y;
+            }
+            break;
+          case QPainterPath::CurveToElement: // currently not supported
+          default:
+            break;
+        }
+      }
+      aPolyline->setPolylineData( aPolylineData );
+    }
+
+    Handle(HYDROData_Zone) aZone =
+      Handle(HYDROData_Zone)::DownCast( aDocument->CreateObject( KIND_ZONE ) );
+    if( !aZone.IsNull() )
+    {
+      aZone->SetName( QString( "SplitZone_%1" ).arg( anIndex ) );
+      aZone->SetPolyline( aPolyline );
+
+      aZone->SetBorderColor( Qt::black );
+
+      int aCounter = 0;
+      int aR = 0, aG = 0, aB = 0;
+      QStringListIterator aZoneNameIter( aZoneNames );
+      while( aZoneNameIter.hasNext() )
+      {
+        const QString& aZoneName = aZoneNameIter.next();
+        Handle(HYDROData_Zone) aRefZone = Handle(HYDROData_Zone)::DownCast(
+          HYDROGUI_Tool::FindObjectByName( this, aZoneName, KIND_ZONE ) );
+        if( !aRefZone.IsNull() )
+        {
+          QColor aRefColor = aRefZone->GetFillingColor();
+          aR += aRefColor.red();
+          aG += aRefColor.green();
+          aB += aRefColor.blue();
+          aCounter++;
+        }
+      }
+      if( aCounter > 0 )
+      {
+        QColor aFillingColor( aR / aCounter, aG / aCounter, aB / aCounter );
+        aZone->SetFillingColor( aFillingColor );
+      }
+    }
+  }
+
+  aDocument->CommitOperation();
+  update( UF_Model );
+}