Salome HOME
de2587d5fd6a841012ccef2219de54a878c8412b
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Operations.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Operations.h"
24
25 #include "HYDROGUI_CopyPasteOp.h"
26 #include "HYDROGUI_CalculationOp.h"
27 #include "HYDROGUI_ChannelOp.h"
28 #include "HYDROGUI_DataModel.h"
29 #include "HYDROGUI_DeleteOp.h"
30 #include "HYDROGUI_ExportImageOp.h"
31 #include "HYDROGUI_ImportImageOp.h"
32 #include "HYDROGUI_ImportBathymetryOp.h"
33 #include "HYDROGUI_ImmersibleZoneOp.h"
34 #include "HYDROGUI_Module.h"
35 #include "HYDROGUI_ObserveImageOp.h"
36 #include "HYDROGUI_PolylineOp.h"
37 #include "HYDROGUI_Poly3DOp.h"
38 #include "HYDROGUI_ProfileOp.h"
39 #include "HYDROGUI_RemoveImageRefsOp.h"
40 #include "HYDROGUI_ShowHideOp.h"
41 #include "HYDROGUI_StreamOp.h"
42 #include "HYDROGUI_TwoImagesOp.h"
43 #include "HYDROGUI_UpdateFlags.h"
44 #include "HYDROGUI_UpdateImageOp.h"
45 #include "HYDROGUI_VisualStateOp.h"
46 #include "HYDROGUI_ImmersibleZoneOp.h"
47 #include "HYDROGUI_ImportGeomObjectOp.h"
48 #include "HYDROGUI_ImportObstacleFromFileOp.h"
49 #include "HYDROGUI_ExportCalculationOp.h"
50 #include "HYDROGUI_ImportProfilesOp.h"
51 #include "HYDROGUI_GeoreferencementOp.h"
52 #include "HYDROGUI_SetColorOp.h"
53 #include "HYDROGUI_Tool.h"
54
55 #include <HYDROData_Document.h>
56 #include <HYDROData_Obstacle.h>
57 #include <HYDROData_SplitToZonesTool.h>
58
59 #include <GeometryGUI.h>
60 #include <GeometryGUI_Operations.h>
61 #include <GEOMBase.h>
62
63 #include <SalomeApp_Study.h>
64
65 #include <LightApp_Application.h>
66
67 #include <CAM_Application.h>
68
69 #include <QtxListAction.h>
70
71 #include <SUIT_Desktop.h>
72 #include <SUIT_ResourceMgr.h>
73 #include <SUIT_Session.h>
74 #include <SUIT_MessageBox.h>
75
76 #include <QAction>
77 #include <QApplication>
78
79 QAction* HYDROGUI_Module::createAction( const int theId, const QString& theSuffix, const QString& theImg,
80                                         const int theKey, const bool isToggle, const QString& theSlot )
81 {
82   QString aSlot = theSlot;
83   if( aSlot.isEmpty() )
84     aSlot = SLOT( onOperation() );
85   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
86   std::string anImg = theImg.toStdString();
87   QPixmap aPixmap = theImg.isEmpty() ? QPixmap() : aResMgr->loadPixmap( "HYDRO", tr( anImg.c_str() ) );
88   std::string aMenu    = ( "MEN_" + theSuffix ).toStdString();
89   std::string aDesktop = ( "DSK_" + theSuffix ).toStdString();
90   std::string aToolbar = ( "STB_" + theSuffix ).toStdString();
91   std::string aSlotStr = aSlot.toStdString();
92   return LightApp_Module::createAction( theId, tr( aDesktop.c_str() ), aPixmap,
93     tr( aMenu.c_str() ), tr( aToolbar.c_str() ),
94                 theKey, application()->desktop(), isToggle, this, aSlotStr.c_str() );
95 }
96
97 void HYDROGUI_Module::createActions()
98 {
99   createAction( SaveVisualStateId, "SAVE_VISUAL_STATE" );
100   createAction( LoadVisualStateId, "LOAD_VISUAL_STATE" );
101
102   createAction( CopyId, "COPY", "", Qt::CTRL + Qt::Key_C );
103   createAction( PasteId, "PASTE", "", Qt::CTRL + Qt::Key_V );
104
105   createAction( ImportImageId, "IMPORT_IMAGE", "", Qt::CTRL + Qt::Key_I );
106   createAction( EditImportedImageId, "EDIT_IMPORTED_IMAGE" );
107   createAction( ObserveImageId, "OBSERVE_IMAGE" );
108   createAction( ExportImageId, "EXPORT_IMAGE" );
109   createAction( UpdateImageId, "UPDATE_IMAGE" );
110   createAction( RemoveImageRefsId, "REMOVE_IMAGE_REFERENCE" );
111
112   createAction( CreatePolylineId, "CREATE_POLYLINE" );
113   createAction( EditPolylineId, "EDIT_POLYLINE" ); 
114
115   createAction( CreatePolyline3DId, "CREATE_POLYLINE_3D" );
116   createAction( EditPolyline3DId, "EDIT_POLYLINE_3D" ); 
117
118   createAction( CreateProfileId, "CREATE_PROFILE" );
119   createAction( ImportProfilesId, "IMPORT_PROFILES" );
120   createAction( EditProfileId, "EDIT_PROFILE" ); 
121   createAction( AllGeoreferencementId, "GEOREFERENCEMENT" ); 
122   createAction( SelectedGeoreferencementId, "GEOREFERENCEMENT" ); 
123   
124   createAction( ImportBathymetryId, "IMPORT_BATHYMETRY", "", Qt::CTRL + Qt::Key_B );
125
126   createAction( CreateImmersibleZoneId, "CREATE_IMMERSIBLE_ZONE" );
127   createAction( EditImmersibleZoneId, "EDIT_IMMERSIBLE_ZONE" );
128
129   createAction( CreateStreamId, "CREATE_STREAM" );
130   createAction( EditStreamId, "EDIT_STREAM" );
131
132   createAction( CreateChannelId, "CREATE_CHANNEL" );
133   createAction( EditChannelId, "EDIT_CHANNEL" );
134
135   createAction( ImportObstacleFromFileId, "IMPORT_OBSTACLE_FROM_FILE" );
136   createAction( ImportGeomObjectId, "IMPORT_GEOM_OBJECT" );
137   createAction( CreateBoxId, "CREATE_BOX" );
138   createAction( CreateCylinderId, "CREATE_CYLINDER" );
139
140   createAction( CreateCalculationId, "CREATE_CALCULATION" );
141   createAction( EditCalculationId, "EDIT_CALCULATION" );
142   createAction( ExportCalculationId, "EXPORT_CALCULATION" );
143
144   createAction( FuseImagesId, "FUSE_IMAGES" );
145   createAction( EditFusedImageId, "EDIT_FUSED_IMAGE" );
146
147   createAction( CutImagesId, "CUT_IMAGES" );
148   createAction( EditCutImageId, "EDIT_CUT_IMAGE" );
149
150   createAction( SplitImageId, "SPLIT_IMAGE" );
151   createAction( EditSplittedImageId, "EDIT_SPLITTED_IMAGE" );
152
153   createAction( DeleteId, "DELETE", "", Qt::Key_Delete, false,
154                 SLOT( onDelete() ) );
155
156   createAction( SetColorId, "COLOR" );
157
158   createAction( ShowId, "SHOW" );
159   createAction( ShowOnlyId, "SHOW_ONLY" );
160   createAction( ShowAllId, "SHOW_ALL" );
161   createAction( HideId, "HIDE" );
162   createAction( HideAllId, "HIDE_ALL" );
163 }
164
165 void HYDROGUI_Module::createMenus()
166 {
167   int aFileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
168   int aCustomPos = 5; // to insert custom actions after "Save as" and before "Preferences"
169   createMenu( SaveVisualStateId, aFileMenu, aCustomPos );
170   createMenu( separator(), aFileMenu, -1, aCustomPos );
171
172   int anEditMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 5 );
173   createMenu( UndoId, anEditMenu );
174   createMenu( RedoId, anEditMenu );
175   createMenu( separator(), anEditMenu );
176   createMenu( CopyId, anEditMenu );
177   createMenu( PasteId, anEditMenu );
178
179   int aHydroMenu = 6; // Edit menu id == 5, View menu id == 10
180   int aHydroId = createMenu( tr( "MEN_DESK_HYDRO" ), -1, -1, aHydroMenu );
181   createMenu( ImportImageId, aHydroId, -1, -1 );
182   createMenu( ImportBathymetryId, aHydroId, -1, -1 );
183   createMenu( CreatePolylineId, aHydroId, -1, -1 );
184   createMenu( CreatePolyline3DId, aHydroId, -1, -1 );
185
186   int aNewProfileId = createMenu( tr( "MEN_DESK_PROFILE" ), aHydroId, -1 );
187   createMenu( CreateProfileId, aNewProfileId, -1, -1 );
188   createMenu( ImportProfilesId, aNewProfileId, -1, -1 );
189   createMenu( AllGeoreferencementId, aNewProfileId, -1, -1 );
190
191   int anArtificialMenuId = createMenu( tr( "MEN_DESK_ARTIFICIAL" ), aHydroId, -1 );
192   createMenu( CreateImmersibleZoneId, anArtificialMenuId, -1, -1 );
193   createMenu( CreateStreamId, anArtificialMenuId, -1, -1 );
194
195   int aNaturalMenuId = createMenu( tr( "MEN_DESK_NATURAL" ), aHydroId, -1 );
196   createMenu( CreateChannelId, aNaturalMenuId, -1, -1 );
197
198   int anObstacleMenuId = createMenu( tr( "MEN_DESK_OBSTACLE" ), aHydroId, -1 );
199   createMenu( ImportObstacleFromFileId, anObstacleMenuId, -1, -1 );
200   createMenu( CreateBoxId, anObstacleMenuId, -1, -1 );
201   createMenu( CreateCylinderId, anObstacleMenuId, -1, -1 );
202
203   createMenu( CreateCalculationId, aHydroId, -1, -1 );
204   createMenu( separator(), aHydroId );
205   createMenu( FuseImagesId, aHydroId, -1, -1 );
206   createMenu( CutImagesId, aHydroId, -1, -1 );
207   createMenu( SplitImageId, aHydroId, -1, -1 );
208   createMenu( separator(), aHydroId );
209 }
210
211 void HYDROGUI_Module::createPopups()
212 {
213 }
214
215 void HYDROGUI_Module::createToolbars()
216 {
217   int aToolBar = createTool( tr( "MEN_DESK_HYDRO" ) );
218   createTool( UndoId, aToolBar );
219   createTool( RedoId, aToolBar );
220 }
221
222 void HYDROGUI_Module::createUndoRedoActions()
223 {
224   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
225
226   QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
227     aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
228     Qt::CTRL + Qt::Key_Z, application()->desktop() );
229     
230   QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
231     aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
232     Qt::CTRL + Qt::Key_Y, application()->desktop() );
233   
234   registerAction( UndoId, anEditUndo );
235   registerAction( RedoId, anEditRedo );
236
237   anEditUndo->setComment( tr( "STB_UNDO" ) );
238   anEditRedo->setComment( tr( "STB_REDO" ) );
239
240   connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
241   connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
242 }
243
244 void HYDROGUI_Module::updateUndoRedoControls()
245 {
246   HYDROGUI_DataModel* aModel = getDataModel();
247
248   QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
249   QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
250
251   bool aCanUndo = aModel->canUndo();
252   bool aCanRedo = aModel->canRedo();
253
254   if( aCanUndo )
255     aUndoAction->addNames( aModel->undoNames() );
256   aUndoAction->setEnabled( aCanUndo );
257
258   if( aCanRedo )
259     aRedoAction->addNames( aModel->redoNames() );
260   aRedoAction->setEnabled( aCanRedo );
261 }
262
263 void HYDROGUI_Module::onOperation()
264 {
265   const QAction* anAction = dynamic_cast<const QAction*>( sender() );
266   int anId = actionId( anAction );
267   if( anId >= 0 )
268     startOperation( anId );
269 }
270
271 void HYDROGUI_Module::onDelete()
272 {
273   SUIT_Operation* anOp = application()->activeStudy()->activeOperation();
274   HYDROGUI_PolylineOp* aPolylineOp = dynamic_cast<HYDROGUI_PolylineOp*>( anOp );
275   if ( aPolylineOp && aPolylineOp->deleteEnabled() )
276     aPolylineOp->deleteSelected();
277   else
278     startOperation( DeleteId );
279 }
280
281 bool HYDROGUI_Module::onUndo( int theNumActions )
282 {
283   QApplication::setOverrideCursor( Qt::WaitCursor );
284   bool anIsOk = true;
285   HYDROGUI_DataModel* aModel = getDataModel();
286   if( aModel )
287   {
288     while( theNumActions > 0 )
289     {
290       if( !aModel->undo() )
291       {
292         anIsOk = false;
293         break;
294       }
295       theNumActions--;
296     }
297     update( UF_All );
298   }
299   QApplication::restoreOverrideCursor();
300   return anIsOk;
301 }
302
303 bool HYDROGUI_Module::onRedo( int theNumActions )
304 {
305   QApplication::setOverrideCursor( Qt::WaitCursor );
306   bool anIsOk = true;
307   HYDROGUI_DataModel* aModel = getDataModel();
308   if( aModel )
309   {
310     while( theNumActions > 0 )
311     {
312       if( !aModel->redo() )
313       {
314         anIsOk = false;
315         break;
316       }
317       theNumActions--;
318     }
319     update( UF_All );
320   }
321   QApplication::restoreOverrideCursor();
322   return anIsOk;
323 }
324
325 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
326 {
327   LightApp_Operation* anOp = 0;
328   HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
329   switch( theId )
330   {
331   case SaveVisualStateId:
332   case LoadVisualStateId:
333     anOp = new HYDROGUI_VisualStateOp( aModule, theId == LoadVisualStateId );
334     break;
335   case CopyId:
336   case PasteId:
337     anOp = new HYDROGUI_CopyPasteOp( aModule, theId == PasteId );
338     break;
339   case ImportImageId:
340   case EditImportedImageId:
341     anOp = new HYDROGUI_ImportImageOp( aModule, theId == EditImportedImageId );
342     break;
343   case ObserveImageId:
344     anOp = new HYDROGUI_ObserveImageOp( aModule );
345     break;
346   case ExportImageId:
347     anOp = new HYDROGUI_ExportImageOp( aModule );
348     break;
349   case UpdateImageId:
350     anOp = new HYDROGUI_UpdateImageOp( aModule );
351     break;
352   case RemoveImageRefsId:
353     anOp = new HYDROGUI_RemoveImageRefsOp( aModule );
354     break;
355   case CreatePolyline3DId:
356   case EditPolyline3DId:
357     anOp = new HYDROGUI_Poly3DOp( aModule, theId == EditPolyline3DId );
358     break;
359   case CreatePolylineId:
360   case EditPolylineId:
361     anOp = new HYDROGUI_PolylineOp( aModule, theId == EditPolylineId );
362     break;
363   case CreateProfileId:
364   case EditProfileId:
365     anOp = new HYDROGUI_ProfileOp( aModule, theId == EditProfileId );
366     break;
367   case ImportProfilesId:
368     anOp = new HYDROGUI_ImportProfilesOp( aModule ) ;
369     break;
370   case AllGeoreferencementId:
371     anOp = new HYDROGUI_GeoreferencementOp( aModule, HYDROGUI_GeoreferencementOp::All ) ;
372     break;
373   case SelectedGeoreferencementId:
374     anOp = new HYDROGUI_GeoreferencementOp( aModule, HYDROGUI_GeoreferencementOp::Selected ) ;
375     break;
376   case ImportBathymetryId:
377     anOp = new HYDROGUI_ImportBathymetryOp( aModule );
378     break;
379   case CreateImmersibleZoneId:
380   case EditImmersibleZoneId:
381     anOp = new HYDROGUI_ImmersibleZoneOp( aModule, theId == EditImmersibleZoneId );
382     break;
383   case CreateStreamId:
384   case EditStreamId:
385     anOp = new HYDROGUI_StreamOp( aModule, theId == EditStreamId );
386     break;
387   case CreateChannelId:
388   case EditChannelId:
389     anOp = new HYDROGUI_ChannelOp( aModule, theId == EditChannelId );
390     break;
391   case CreateCalculationId:
392   case EditCalculationId:
393     anOp = new HYDROGUI_CalculationOp( aModule, theId == EditCalculationId );
394     break;
395   case ExportCalculationId:
396     anOp = new HYDROGUI_ExportCalculationOp( aModule );
397     break;
398   case FuseImagesId:
399   case EditFusedImageId:
400     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse, theId == EditFusedImageId );
401     break;
402   case CutImagesId:
403   case EditCutImageId:
404     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut, theId == EditCutImageId );
405     break;
406   case SplitImageId:
407   case EditSplittedImageId:
408     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Split, theId == EditSplittedImageId );
409     break;
410   case ImportObstacleFromFileId:
411     anOp = new HYDROGUI_ImportObstacleFromFileOp( aModule );
412     break;
413   case ImportCreatedPrimitiveId:
414     anOp = new HYDROGUI_ImportGeomObjectOp( aModule, HYDROGUI_ImportGeomObjectOp::ImportCreated );
415     break;
416   case ImportGeomObjectId:
417     anOp = new HYDROGUI_ImportGeomObjectOp( aModule, HYDROGUI_ImportGeomObjectOp::ImportSelected );
418     break;
419   case CreateBoxId:
420     application()->activateOperation( "Geometry", GEOMOp::OpBox );
421     break;
422   case CreateCylinderId:
423     application()->activateOperation( "Geometry", GEOMOp::OpCylinder );
424     break;
425   case DeleteId:
426     anOp = new HYDROGUI_DeleteOp( aModule );
427     break;
428   case SetColorId:
429     anOp = new HYDROGUI_SetColorOp( aModule );
430     break;
431   case ShowId:
432   case ShowOnlyId:
433   case ShowAllId:
434   case HideId:
435   case HideAllId:
436     anOp = new HYDROGUI_ShowHideOp( aModule, theId );
437     break;
438   }
439
440   if( !anOp )
441     anOp = LightApp_Module::createOperation( theId );
442
443   return anOp;
444 }
445
446 bool HYDROGUI_Module::reusableOperation( const int id )
447 {
448   if ( id == ImportGeomObjectId ) {
449     return false;
450   }
451
452   return LightApp_Module::reusableOperation( id );
453 }
454
455 /**
456  * Called when the operation perfomed by another module is finished.
457  * \param theModuleName the name of the module which perfomed the operation
458  * \param theOperationName the operation name
459  * \param theEntryList the list of the created objects entries
460  */
461 void HYDROGUI_Module::onExternalOperationFinished( const QString& theModuleName,
462                                                    const QString& theOperationName,
463                                                    const QStringList& theEntryList )
464 {
465   // Process "Geometry" module operations with non-empty list of created objects only
466   if ( theModuleName != "Geometry" || theEntryList.isEmpty() ) {
467     return;
468   }
469   
470   // Start import GEOM object operation
471   myGeomObjectsToImport = theEntryList;
472   startOperation( ImportCreatedPrimitiveId );
473   myGeomObjectsToImport.clear();
474 }
475
476 /**
477  * Returns the list of entries of GEOM objects to be imported.
478  */
479 QStringList HYDROGUI_Module::GetGeomObjectsToImport()
480 {
481   return myGeomObjectsToImport;
482 }
483
484 /**
485  * Returns true if the object with the given entry can be renamed.
486  * @param theEntry the object entry
487  */
488 bool HYDROGUI_Module::renameAllowed( const QString& theEntry ) const
489 {
490   // Allow to rename all HYDRO objects
491   Handle(HYDROData_Entity) anEntity = getDataModel()->objectByEntry( theEntry );
492   return !anEntity.IsNull();
493 }
494 /**
495  * Returns true if the object with the given entry is renamed.
496  * @param theEntry the object entry
497  * @param theName the new name
498  */
499 bool HYDROGUI_Module::renameObject( const QString& theEntry, const QString& theName )
500 {
501   Handle(HYDROData_Entity) anEntity = getDataModel()->objectByEntry( theEntry );
502   bool aRes = !anEntity.IsNull();
503   if ( aRes )
504   {
505     HYDROGUI_DataModel* aModel = getDataModel();
506     if( aModel )
507     {
508       if( anEntity->GetName() != theName )
509       {
510         // check that there are no other objects with the same name in the document
511         Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( this, theName );
512         aRes = anObject.IsNull();
513         if ( aRes )
514         {
515           aRes = aModel->rename( anEntity, theName );
516         }
517         else
518         {
519           // Inform the user that the name is already used
520           QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
521           QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
522           SUIT_MessageBox::critical( getApp()->desktop(), aTitle, aMessage );
523         }
524       }
525     }
526   }
527   return aRes;
528 }