Salome HOME
Bugs 153, 154.
[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   createAction( EditImportedBathymetryId, "EDIT_IMPORTED_BATHYMETRY" );
126
127   createAction( CreateImmersibleZoneId, "CREATE_IMMERSIBLE_ZONE" );
128   createAction( EditImmersibleZoneId, "EDIT_IMMERSIBLE_ZONE" );
129
130   createAction( CreateStreamId, "CREATE_STREAM" );
131   createAction( EditStreamId, "EDIT_STREAM" );
132
133   createAction( CreateChannelId, "CREATE_CHANNEL" );
134   createAction( EditChannelId, "EDIT_CHANNEL" );
135
136   createAction( ImportObstacleFromFileId, "IMPORT_OBSTACLE_FROM_FILE" );
137   createAction( ImportGeomObjectId, "IMPORT_GEOM_OBJECT" );
138   createAction( CreateBoxId, "CREATE_BOX" );
139   createAction( CreateCylinderId, "CREATE_CYLINDER" );
140
141   createAction( CreateCalculationId, "CREATE_CALCULATION" );
142   createAction( EditCalculationId, "EDIT_CALCULATION" );
143   createAction( ExportCalculationId, "EXPORT_CALCULATION" );
144
145   createAction( FuseImagesId, "FUSE_IMAGES" );
146   createAction( EditFusedImageId, "EDIT_FUSED_IMAGE" );
147
148   createAction( CutImagesId, "CUT_IMAGES" );
149   createAction( EditCutImageId, "EDIT_CUT_IMAGE" );
150
151   createAction( SplitImageId, "SPLIT_IMAGE" );
152   createAction( EditSplittedImageId, "EDIT_SPLITTED_IMAGE" );
153
154   createAction( DeleteId, "DELETE", "", Qt::Key_Delete, false,
155                 SLOT( onDelete() ) );
156
157   createAction( SetColorId, "COLOR" );
158
159   createAction( ShowId, "SHOW" );
160   createAction( ShowOnlyId, "SHOW_ONLY" );
161   createAction( ShowAllId, "SHOW_ALL" );
162   createAction( HideId, "HIDE" );
163   createAction( HideAllId, "HIDE_ALL" );
164 }
165
166 void HYDROGUI_Module::createMenus()
167 {
168   int aFileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
169   int aCustomPos = 5; // to insert custom actions after "Save as" and before "Preferences"
170   createMenu( SaveVisualStateId, aFileMenu, aCustomPos );
171   createMenu( separator(), aFileMenu, -1, aCustomPos );
172
173   int anEditMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 5 );
174   createMenu( UndoId, anEditMenu );
175   createMenu( RedoId, anEditMenu );
176   createMenu( separator(), anEditMenu );
177   createMenu( CopyId, anEditMenu );
178   createMenu( PasteId, anEditMenu );
179
180   int aHydroMenu = 6; // Edit menu id == 5, View menu id == 10
181   int aHydroId = createMenu( tr( "MEN_DESK_HYDRO" ), -1, -1, aHydroMenu );
182   createMenu( ImportImageId, aHydroId, -1, -1 );
183   createMenu( ImportBathymetryId, aHydroId, -1, -1 );
184   createMenu( CreatePolylineId, aHydroId, -1, -1 );
185   createMenu( CreatePolyline3DId, aHydroId, -1, -1 );
186
187   int aNewProfileId = createMenu( tr( "MEN_DESK_PROFILE" ), aHydroId, -1 );
188   createMenu( CreateProfileId, aNewProfileId, -1, -1 );
189   createMenu( ImportProfilesId, aNewProfileId, -1, -1 );
190   createMenu( AllGeoreferencementId, aNewProfileId, -1, -1 );
191
192   int anArtificialMenuId = createMenu( tr( "MEN_DESK_ARTIFICIAL" ), aHydroId, -1 );
193   createMenu( CreateChannelId, anArtificialMenuId, -1, -1 );
194
195   int aNaturalMenuId = createMenu( tr( "MEN_DESK_NATURAL" ), aHydroId, -1 );
196   createMenu( CreateImmersibleZoneId, aNaturalMenuId, -1, -1 );
197   createMenu( CreateStreamId, aNaturalMenuId, -1, -1 );
198
199   int anObstacleMenuId = createMenu( tr( "MEN_DESK_OBSTACLE" ), aHydroId, -1 );
200   createMenu( ImportObstacleFromFileId, anObstacleMenuId, -1, -1 );
201   createMenu( CreateBoxId, anObstacleMenuId, -1, -1 );
202   createMenu( CreateCylinderId, anObstacleMenuId, -1, -1 );
203
204   createMenu( CreateCalculationId, aHydroId, -1, -1 );
205   createMenu( separator(), aHydroId );
206   createMenu( FuseImagesId, aHydroId, -1, -1 );
207   createMenu( CutImagesId, aHydroId, -1, -1 );
208   createMenu( SplitImageId, aHydroId, -1, -1 );
209   createMenu( separator(), aHydroId );
210 }
211
212 void HYDROGUI_Module::createPopups()
213 {
214 }
215
216 void HYDROGUI_Module::createToolbars()
217 {
218   int aToolBar = createTool( tr( "MEN_DESK_HYDRO" ) );
219   createTool( UndoId, aToolBar );
220   createTool( RedoId, aToolBar );
221 }
222
223 void HYDROGUI_Module::createUndoRedoActions()
224 {
225   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
226
227   QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
228     aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
229     Qt::CTRL + Qt::Key_Z, application()->desktop() );
230     
231   QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
232     aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
233     Qt::CTRL + Qt::Key_Y, application()->desktop() );
234   
235   registerAction( UndoId, anEditUndo );
236   registerAction( RedoId, anEditRedo );
237
238   anEditUndo->setComment( tr( "STB_UNDO" ) );
239   anEditRedo->setComment( tr( "STB_REDO" ) );
240
241   connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
242   connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
243 }
244
245 void HYDROGUI_Module::updateUndoRedoControls()
246 {
247   HYDROGUI_DataModel* aModel = getDataModel();
248
249   QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
250   QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
251
252   bool aCanUndo = aModel->canUndo();
253   bool aCanRedo = aModel->canRedo();
254
255   if( aCanUndo )
256     aUndoAction->addNames( aModel->undoNames() );
257   aUndoAction->setEnabled( aCanUndo );
258
259   if( aCanRedo )
260     aRedoAction->addNames( aModel->redoNames() );
261   aRedoAction->setEnabled( aCanRedo );
262 }
263
264 void HYDROGUI_Module::onOperation()
265 {
266   const QAction* anAction = dynamic_cast<const QAction*>( sender() );
267   int anId = actionId( anAction );
268   if( anId >= 0 )
269     startOperation( anId );
270 }
271
272 void HYDROGUI_Module::onDelete()
273 {
274   SUIT_Operation* anOp = application()->activeStudy()->activeOperation();
275   HYDROGUI_PolylineOp* aPolylineOp = dynamic_cast<HYDROGUI_PolylineOp*>( anOp );
276   if ( aPolylineOp && aPolylineOp->deleteEnabled() )
277     aPolylineOp->deleteSelected();
278   else
279     startOperation( DeleteId );
280 }
281
282 bool HYDROGUI_Module::onUndo( int theNumActions )
283 {
284   QApplication::setOverrideCursor( Qt::WaitCursor );
285   bool anIsOk = true;
286   HYDROGUI_DataModel* aModel = getDataModel();
287   if( aModel )
288   {
289     while( theNumActions > 0 )
290     {
291       if( !aModel->undo() )
292       {
293         anIsOk = false;
294         break;
295       }
296       theNumActions--;
297     }
298     update( UF_All );
299   }
300   QApplication::restoreOverrideCursor();
301   return anIsOk;
302 }
303
304 bool HYDROGUI_Module::onRedo( int theNumActions )
305 {
306   QApplication::setOverrideCursor( Qt::WaitCursor );
307   bool anIsOk = true;
308   HYDROGUI_DataModel* aModel = getDataModel();
309   if( aModel )
310   {
311     while( theNumActions > 0 )
312     {
313       if( !aModel->redo() )
314       {
315         anIsOk = false;
316         break;
317       }
318       theNumActions--;
319     }
320     update( UF_All );
321   }
322   QApplication::restoreOverrideCursor();
323   return anIsOk;
324 }
325
326 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
327 {
328   LightApp_Operation* anOp = 0;
329   HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
330   switch( theId )
331   {
332   case SaveVisualStateId:
333   case LoadVisualStateId:
334     anOp = new HYDROGUI_VisualStateOp( aModule, theId == LoadVisualStateId );
335     break;
336   case CopyId:
337   case PasteId:
338     anOp = new HYDROGUI_CopyPasteOp( aModule, theId == PasteId );
339     break;
340   case ImportImageId:
341   case EditImportedImageId:
342     anOp = new HYDROGUI_ImportImageOp( aModule, theId == EditImportedImageId );
343     break;
344   case ObserveImageId:
345     anOp = new HYDROGUI_ObserveImageOp( aModule );
346     break;
347   case ExportImageId:
348     anOp = new HYDROGUI_ExportImageOp( aModule );
349     break;
350   case UpdateImageId:
351     anOp = new HYDROGUI_UpdateImageOp( aModule );
352     break;
353   case RemoveImageRefsId:
354     anOp = new HYDROGUI_RemoveImageRefsOp( aModule );
355     break;
356   case CreatePolyline3DId:
357   case EditPolyline3DId:
358     anOp = new HYDROGUI_Poly3DOp( aModule, theId == EditPolyline3DId );
359     break;
360   case CreatePolylineId:
361   case EditPolylineId:
362     anOp = new HYDROGUI_PolylineOp( aModule, theId == EditPolylineId );
363     break;
364   case CreateProfileId:
365   case EditProfileId:
366     anOp = new HYDROGUI_ProfileOp( aModule, theId == EditProfileId );
367     break;
368   case ImportProfilesId:
369     anOp = new HYDROGUI_ImportProfilesOp( aModule ) ;
370     break;
371   case AllGeoreferencementId:
372     anOp = new HYDROGUI_GeoreferencementOp( aModule, HYDROGUI_GeoreferencementOp::All ) ;
373     break;
374   case SelectedGeoreferencementId:
375     anOp = new HYDROGUI_GeoreferencementOp( aModule, HYDROGUI_GeoreferencementOp::Selected ) ;
376     break;
377   case ImportBathymetryId:
378   case EditImportedBathymetryId:
379     anOp = new HYDROGUI_ImportBathymetryOp( aModule, theId == EditImportedBathymetryId  );
380     break;
381   case CreateImmersibleZoneId:
382   case EditImmersibleZoneId:
383     anOp = new HYDROGUI_ImmersibleZoneOp( aModule, theId == EditImmersibleZoneId );
384     break;
385   case CreateStreamId:
386   case EditStreamId:
387     anOp = new HYDROGUI_StreamOp( aModule, theId == EditStreamId );
388     break;
389   case CreateChannelId:
390   case EditChannelId:
391     anOp = new HYDROGUI_ChannelOp( aModule, theId == EditChannelId );
392     break;
393   case CreateCalculationId:
394   case EditCalculationId:
395     anOp = new HYDROGUI_CalculationOp( aModule, theId == EditCalculationId );
396     break;
397   case ExportCalculationId:
398     anOp = new HYDROGUI_ExportCalculationOp( aModule );
399     break;
400   case FuseImagesId:
401   case EditFusedImageId:
402     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse, theId == EditFusedImageId );
403     break;
404   case CutImagesId:
405   case EditCutImageId:
406     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut, theId == EditCutImageId );
407     break;
408   case SplitImageId:
409   case EditSplittedImageId:
410     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Split, theId == EditSplittedImageId );
411     break;
412   case ImportObstacleFromFileId:
413     anOp = new HYDROGUI_ImportObstacleFromFileOp( aModule );
414     break;
415   case ImportCreatedPrimitiveId:
416     anOp = new HYDROGUI_ImportGeomObjectOp( aModule, HYDROGUI_ImportGeomObjectOp::ImportCreated );
417     break;
418   case ImportGeomObjectId:
419     anOp = new HYDROGUI_ImportGeomObjectOp( aModule, HYDROGUI_ImportGeomObjectOp::ImportSelected );
420     break;
421   case CreateBoxId:
422     application()->activateOperation( "Geometry", GEOMOp::OpBox );
423     break;
424   case CreateCylinderId:
425     application()->activateOperation( "Geometry", GEOMOp::OpCylinder );
426     break;
427   case DeleteId:
428     anOp = new HYDROGUI_DeleteOp( aModule );
429     break;
430   case SetColorId:
431     anOp = new HYDROGUI_SetColorOp( aModule );
432     break;
433   case ShowId:
434   case ShowOnlyId:
435   case ShowAllId:
436   case HideId:
437   case HideAllId:
438     anOp = new HYDROGUI_ShowHideOp( aModule, theId );
439     break;
440   }
441
442   if( !anOp )
443     anOp = LightApp_Module::createOperation( theId );
444
445   return anOp;
446 }
447
448 bool HYDROGUI_Module::reusableOperation( const int id )
449 {
450   if ( id == ImportGeomObjectId ) {
451     return false;
452   }
453
454   return LightApp_Module::reusableOperation( id );
455 }
456
457 /**
458  * Called when the operation perfomed by another module is finished.
459  * \param theModuleName the name of the module which perfomed the operation
460  * \param theOperationName the operation name
461  * \param theEntryList the list of the created objects entries
462  */
463 void HYDROGUI_Module::onExternalOperationFinished( const QString& theModuleName,
464                                                    const QString& theOperationName,
465                                                    const QStringList& theEntryList )
466 {
467   // Process "Geometry" module operations with non-empty list of created objects only
468   if ( theModuleName != "Geometry" || theEntryList.isEmpty() ) {
469     return;
470   }
471   
472   // Start import GEOM object operation
473   myGeomObjectsToImport = theEntryList;
474   startOperation( ImportCreatedPrimitiveId );
475   myGeomObjectsToImport.clear();
476 }
477
478 /**
479  * Returns the list of entries of GEOM objects to be imported.
480  */
481 QStringList HYDROGUI_Module::GetGeomObjectsToImport()
482 {
483   return myGeomObjectsToImport;
484 }
485
486 /**
487  * Returns true if the object with the given entry can be renamed.
488  * @param theEntry the object entry
489  */
490 bool HYDROGUI_Module::renameAllowed( const QString& theEntry ) const
491 {
492   // Allow to rename all HYDRO objects
493   Handle(HYDROData_Entity) anEntity = getDataModel()->objectByEntry( theEntry );
494   return !anEntity.IsNull();
495 }
496 /**
497  * Returns true if the object with the given entry is renamed.
498  * @param theEntry the object entry
499  * @param theName the new name
500  */
501 bool HYDROGUI_Module::renameObject( const QString& theEntry, const QString& theName )
502 {
503   Handle(HYDROData_Entity) anEntity = getDataModel()->objectByEntry( theEntry );
504   bool aRes = !anEntity.IsNull();
505   if ( aRes )
506   {
507     HYDROGUI_DataModel* aModel = getDataModel();
508     if( aModel )
509     {
510       if( anEntity->GetName() != theName )
511       {
512         // check that there are no other objects with the same name in the document
513         Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( this, theName );
514         aRes = anObject.IsNull();
515         if ( aRes )
516         {
517           aRes = aModel->rename( anEntity, theName );
518         }
519         else
520         {
521           // Inform the user that the name is already used
522           QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
523           QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
524           SUIT_MessageBox::critical( getApp()->desktop(), aTitle, aMessage );
525         }
526       }
527     }
528   }
529   return aRes;
530 }