Salome HOME
avoid viewer update on each erase of shape
[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
54 #include <HYDROData_Document.h>
55 #include <HYDROData_Obstacle.h>
56 #include <HYDROData_SplitToZonesTool.h>
57
58 #include <GeometryGUI.h>
59 #include <GeometryGUI_Operations.h>
60 #include <GEOMBase.h>
61
62 #include <SalomeApp_Study.h>
63
64 #include <LightApp_Application.h>
65
66 #include <CAM_Application.h>
67
68 #include <QtxListAction.h>
69
70 #include <SUIT_Desktop.h>
71 #include <SUIT_ResourceMgr.h>
72 #include <SUIT_Session.h>
73
74 #include <QAction>
75 #include <QApplication>
76
77 QAction* HYDROGUI_Module::createAction( const int theId, const QString& theSuffix, const QString& theImg,
78                                         const int theKey, const bool isToggle, const QString& theSlot )
79 {
80   QString aSlot = theSlot;
81   if( aSlot.isEmpty() )
82     aSlot = SLOT( onOperation() );
83   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
84   std::string anImg = theImg.toStdString();
85   QPixmap aPixmap = theImg.isEmpty() ? QPixmap() : aResMgr->loadPixmap( "HYDRO", tr( anImg.c_str() ) );
86   std::string aMenu    = ( "MEN_" + theSuffix ).toStdString();
87   std::string aDesktop = ( "DSK_" + theSuffix ).toStdString();
88   std::string aToolbar = ( "STB_" + theSuffix ).toStdString();
89   std::string aSlotStr = aSlot.toStdString();
90   return LightApp_Module::createAction( theId, tr( aDesktop.c_str() ), aPixmap,
91     tr( aMenu.c_str() ), tr( aToolbar.c_str() ),
92                 theKey, application()->desktop(), isToggle, this, aSlotStr.c_str() );
93 }
94
95 void HYDROGUI_Module::createActions()
96 {
97   createAction( SaveVisualStateId, "SAVE_VISUAL_STATE" );
98   createAction( LoadVisualStateId, "LOAD_VISUAL_STATE" );
99
100   createAction( CopyId, "COPY", "", Qt::CTRL + Qt::Key_C );
101   createAction( PasteId, "PASTE", "", Qt::CTRL + Qt::Key_V );
102
103   createAction( ImportImageId, "IMPORT_IMAGE", "", Qt::CTRL + Qt::Key_I );
104   createAction( EditImportedImageId, "EDIT_IMPORTED_IMAGE" );
105   createAction( ObserveImageId, "OBSERVE_IMAGE" );
106   createAction( ExportImageId, "EXPORT_IMAGE" );
107   createAction( UpdateImageId, "UPDATE_IMAGE" );
108   createAction( RemoveImageRefsId, "REMOVE_IMAGE_REFERENCE" );
109
110   createAction( CreatePolylineId, "CREATE_POLYLINE" );
111   createAction( EditPolylineId, "EDIT_POLYLINE" ); 
112
113   createAction( CreatePolyline3DId, "CREATE_POLYLINE_3D" );
114   createAction( EditPolyline3DId, "EDIT_POLYLINE_3D" ); 
115
116   createAction( CreateProfileId, "CREATE_PROFILE" );
117   createAction( ImportProfilesId, "IMPORT_PROFILES" );
118   createAction( EditProfileId, "EDIT_PROFILE" ); 
119   createAction( AllGeoreferencementId, "GEOREFERENCEMENT" ); 
120   createAction( SelectedGeoreferencementId, "GEOREFERENCEMENT" ); 
121   
122   createAction( ImportBathymetryId, "IMPORT_BATHYMETRY", "", Qt::CTRL + Qt::Key_B );
123
124   createAction( CreateImmersibleZoneId, "CREATE_IMMERSIBLE_ZONE" );
125   createAction( EditImmersibleZoneId, "EDIT_IMMERSIBLE_ZONE" );
126
127   createAction( CreateStreamId, "CREATE_STREAM" );
128   createAction( EditStreamId, "EDIT_STREAM" );
129
130   createAction( CreateChannelId, "CREATE_CHANNEL" );
131   createAction( EditChannelId, "EDIT_CHANNEL" );
132
133   createAction( ImportObstacleFromFileId, "IMPORT_OBSTACLE_FROM_FILE" );
134   createAction( ImportGeomObjectId, "IMPORT_GEOM_OBJECT" );
135   createAction( CreateBoxId, "CREATE_BOX" );
136   createAction( CreateCylinderId, "CREATE_CYLINDER" );
137
138   createAction( CreateCalculationId, "CREATE_CALCULATION" );
139   createAction( EditCalculationId, "EDIT_CALCULATION" );
140   createAction( ExportCalculationId, "EXPORT_CALCULATION" );
141
142   createAction( FuseImagesId, "FUSE_IMAGES" );
143   createAction( EditFusedImageId, "EDIT_FUSED_IMAGE" );
144
145   createAction( CutImagesId, "CUT_IMAGES" );
146   createAction( EditCutImageId, "EDIT_CUT_IMAGE" );
147
148   createAction( SplitImageId, "SPLIT_IMAGE" );
149   createAction( EditSplittedImageId, "EDIT_SPLITTED_IMAGE" );
150
151   createAction( DeleteId, "DELETE", "", Qt::Key_Delete, false,
152                 SLOT( onDelete() ) );
153
154   createAction( SetColorId, "COLOR" );
155
156   createAction( ShowId, "SHOW" );
157   createAction( ShowOnlyId, "SHOW_ONLY" );
158   createAction( ShowAllId, "SHOW_ALL" );
159   createAction( HideId, "HIDE" );
160   createAction( HideAllId, "HIDE_ALL" );
161 }
162
163 void HYDROGUI_Module::createMenus()
164 {
165   int aFileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
166   int aCustomPos = 5; // to insert custom actions after "Save as" and before "Preferences"
167   createMenu( SaveVisualStateId, aFileMenu, aCustomPos );
168   createMenu( separator(), aFileMenu, -1, aCustomPos );
169
170   int anEditMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 5 );
171   createMenu( UndoId, anEditMenu );
172   createMenu( RedoId, anEditMenu );
173   createMenu( separator(), anEditMenu );
174   createMenu( CopyId, anEditMenu );
175   createMenu( PasteId, anEditMenu );
176
177   int aHydroMenu = 6; // Edit menu id == 5, View menu id == 10
178   int aHydroId = createMenu( tr( "MEN_DESK_HYDRO" ), -1, -1, aHydroMenu );
179   createMenu( ImportImageId, aHydroId, -1, -1 );
180   createMenu( ImportBathymetryId, aHydroId, -1, -1 );
181   createMenu( CreatePolylineId, aHydroId, -1, -1 );
182   createMenu( CreatePolyline3DId, aHydroId, -1, -1 );
183
184   int aNewProfileId = createMenu( tr( "MEN_DESK_PROFILE" ), aHydroId, -1 );
185   createMenu( CreateProfileId, aNewProfileId, -1, -1 );
186   createMenu( ImportProfilesId, aNewProfileId, -1, -1 );
187   createMenu( AllGeoreferencementId, aNewProfileId, -1, -1 );
188
189   int anArtificialMenuId = createMenu( tr( "MEN_DESK_ARTIFICIAL" ), aHydroId, -1 );
190   createMenu( CreateImmersibleZoneId, anArtificialMenuId, -1, -1 );
191   createMenu( CreateStreamId, anArtificialMenuId, -1, -1 );
192
193   int aNaturalMenuId = createMenu( tr( "MEN_DESK_NATURAL" ), aHydroId, -1 );
194   createMenu( CreateChannelId, aNaturalMenuId, -1, -1 );
195
196   int anObstacleMenuId = createMenu( tr( "MEN_DESK_OBSTACLE" ), aHydroId, -1 );
197   createMenu( ImportObstacleFromFileId, anObstacleMenuId, -1, -1 );
198   createMenu( CreateBoxId, anObstacleMenuId, -1, -1 );
199   createMenu( CreateCylinderId, anObstacleMenuId, -1, -1 );
200
201   createMenu( CreateCalculationId, aHydroId, -1, -1 );
202   createMenu( separator(), aHydroId );
203   createMenu( FuseImagesId, aHydroId, -1, -1 );
204   createMenu( CutImagesId, aHydroId, -1, -1 );
205   createMenu( SplitImageId, aHydroId, -1, -1 );
206   createMenu( separator(), aHydroId );
207 }
208
209 void HYDROGUI_Module::createPopups()
210 {
211 }
212
213 void HYDROGUI_Module::createToolbars()
214 {
215   int aToolBar = createTool( tr( "MEN_DESK_HYDRO" ) );
216   createTool( UndoId, aToolBar );
217   createTool( RedoId, aToolBar );
218 }
219
220 void HYDROGUI_Module::createUndoRedoActions()
221 {
222   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
223
224   QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
225     aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
226     Qt::CTRL + Qt::Key_Z, application()->desktop() );
227     
228   QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
229     aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
230     Qt::CTRL + Qt::Key_Y, application()->desktop() );
231   
232   registerAction( UndoId, anEditUndo );
233   registerAction( RedoId, anEditRedo );
234
235   anEditUndo->setComment( tr( "STB_UNDO" ) );
236   anEditRedo->setComment( tr( "STB_REDO" ) );
237
238   connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
239   connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
240 }
241
242 void HYDROGUI_Module::updateUndoRedoControls()
243 {
244   HYDROGUI_DataModel* aModel = getDataModel();
245
246   QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
247   QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
248
249   bool aCanUndo = aModel->canUndo();
250   bool aCanRedo = aModel->canRedo();
251
252   if( aCanUndo )
253     aUndoAction->addNames( aModel->undoNames() );
254   aUndoAction->setEnabled( aCanUndo );
255
256   if( aCanRedo )
257     aRedoAction->addNames( aModel->redoNames() );
258   aRedoAction->setEnabled( aCanRedo );
259 }
260
261 void HYDROGUI_Module::onOperation()
262 {
263   const QAction* anAction = dynamic_cast<const QAction*>( sender() );
264   int anId = actionId( anAction );
265   if( anId >= 0 )
266     startOperation( anId );
267 }
268
269 void HYDROGUI_Module::onDelete()
270 {
271   SUIT_Operation* anOp = application()->activeStudy()->activeOperation();
272   HYDROGUI_PolylineOp* aPolylineOp = dynamic_cast<HYDROGUI_PolylineOp*>( anOp );
273   if ( aPolylineOp && aPolylineOp->deleteEnabled() )
274     aPolylineOp->deleteSelected();
275   else
276     startOperation( DeleteId );
277 }
278
279 bool HYDROGUI_Module::onUndo( int theNumActions )
280 {
281   QApplication::setOverrideCursor( Qt::WaitCursor );
282   bool anIsOk = true;
283   HYDROGUI_DataModel* aModel = getDataModel();
284   if( aModel )
285   {
286     while( theNumActions > 0 )
287     {
288       if( !aModel->undo() )
289       {
290         anIsOk = false;
291         break;
292       }
293       theNumActions--;
294     }
295     update( UF_All );
296   }
297   QApplication::restoreOverrideCursor();
298   return anIsOk;
299 }
300
301 bool HYDROGUI_Module::onRedo( int theNumActions )
302 {
303   QApplication::setOverrideCursor( Qt::WaitCursor );
304   bool anIsOk = true;
305   HYDROGUI_DataModel* aModel = getDataModel();
306   if( aModel )
307   {
308     while( theNumActions > 0 )
309     {
310       if( !aModel->redo() )
311       {
312         anIsOk = false;
313         break;
314       }
315       theNumActions--;
316     }
317     update( UF_All );
318   }
319   QApplication::restoreOverrideCursor();
320   return anIsOk;
321 }
322
323 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
324 {
325   LightApp_Operation* anOp = 0;
326   HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
327   switch( theId )
328   {
329   case SaveVisualStateId:
330   case LoadVisualStateId:
331     anOp = new HYDROGUI_VisualStateOp( aModule, theId == LoadVisualStateId );
332     break;
333   case CopyId:
334   case PasteId:
335     anOp = new HYDROGUI_CopyPasteOp( aModule, theId == PasteId );
336     break;
337   case ImportImageId:
338   case EditImportedImageId:
339     anOp = new HYDROGUI_ImportImageOp( aModule, theId == EditImportedImageId );
340     break;
341   case ObserveImageId:
342     anOp = new HYDROGUI_ObserveImageOp( aModule );
343     break;
344   case ExportImageId:
345     anOp = new HYDROGUI_ExportImageOp( aModule );
346     break;
347   case UpdateImageId:
348     anOp = new HYDROGUI_UpdateImageOp( aModule );
349     break;
350   case RemoveImageRefsId:
351     anOp = new HYDROGUI_RemoveImageRefsOp( aModule );
352     break;
353   case CreatePolyline3DId:
354   case EditPolyline3DId:
355     anOp = new HYDROGUI_Poly3DOp( aModule, theId == EditPolyline3DId );
356     break;
357   case CreatePolylineId:
358   case EditPolylineId:
359     anOp = new HYDROGUI_PolylineOp( aModule, theId == EditPolylineId );
360     break;
361   case CreateProfileId:
362   case EditProfileId:
363     anOp = new HYDROGUI_ProfileOp( aModule, theId == EditProfileId );
364     break;
365   case ImportProfilesId:
366     anOp = new HYDROGUI_ImportProfilesOp( aModule ) ;
367     break;
368   case AllGeoreferencementId:
369     anOp = new HYDROGUI_GeoreferencementOp( aModule, HYDROGUI_GeoreferencementOp::All ) ;
370     break;
371   case SelectedGeoreferencementId:
372     anOp = new HYDROGUI_GeoreferencementOp( aModule, HYDROGUI_GeoreferencementOp::Selected ) ;
373     break;
374   case ImportBathymetryId:
375     anOp = new HYDROGUI_ImportBathymetryOp( aModule );
376     break;
377   case CreateImmersibleZoneId:
378   case EditImmersibleZoneId:
379     anOp = new HYDROGUI_ImmersibleZoneOp( aModule, theId == EditImmersibleZoneId );
380     break;
381   case CreateStreamId:
382   case EditStreamId:
383     anOp = new HYDROGUI_StreamOp( aModule, theId == EditStreamId );
384     break;
385   case CreateChannelId:
386   case EditChannelId:
387     anOp = new HYDROGUI_ChannelOp( aModule, theId == EditChannelId );
388     break;
389   case CreateCalculationId:
390   case EditCalculationId:
391     anOp = new HYDROGUI_CalculationOp( aModule, theId == EditCalculationId );
392     break;
393   case ExportCalculationId:
394     anOp = new HYDROGUI_ExportCalculationOp( aModule );
395     break;
396   case FuseImagesId:
397   case EditFusedImageId:
398     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse, theId == EditFusedImageId );
399     break;
400   case CutImagesId:
401   case EditCutImageId:
402     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut, theId == EditCutImageId );
403     break;
404   case SplitImageId:
405   case EditSplittedImageId:
406     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Split, theId == EditSplittedImageId );
407     break;
408   case ImportObstacleFromFileId:
409     anOp = new HYDROGUI_ImportObstacleFromFileOp( aModule );
410     break;
411   case ImportCreatedPrimitiveId:
412     anOp = new HYDROGUI_ImportGeomObjectOp( aModule, HYDROGUI_ImportGeomObjectOp::ImportCreated );
413     break;
414   case ImportGeomObjectId:
415     anOp = new HYDROGUI_ImportGeomObjectOp( aModule, HYDROGUI_ImportGeomObjectOp::ImportSelected );
416     break;
417   case CreateBoxId:
418     application()->activateOperation( "Geometry", GEOMOp::OpBox );
419     break;
420   case CreateCylinderId:
421     application()->activateOperation( "Geometry", GEOMOp::OpCylinder );
422     break;
423   case DeleteId:
424     anOp = new HYDROGUI_DeleteOp( aModule );
425     break;
426   case SetColorId:
427     anOp = new HYDROGUI_SetColorOp( aModule );
428     break;
429   case ShowId:
430   case ShowOnlyId:
431   case ShowAllId:
432   case HideId:
433   case HideAllId:
434     anOp = new HYDROGUI_ShowHideOp( aModule, theId );
435     break;
436   }
437
438   if( !anOp )
439     anOp = LightApp_Module::createOperation( theId );
440
441   return anOp;
442 }
443
444 bool HYDROGUI_Module::reusableOperation( const int id )
445 {
446   if ( id == ImportGeomObjectId ) {
447     return false;
448   }
449
450   return LightApp_Module::reusableOperation( id );
451 }
452
453 /**
454  * Called when the operation perfomed by another module is finished.
455  * \param theModuleName the name of the module which perfomed the operation
456  * \param theOperationName the operation name
457  * \param theEntryList the list of the created objects entries
458  */
459 void HYDROGUI_Module::onExternalOperationFinished( const QString& theModuleName,
460                                                    const QString& theOperationName,
461                                                    const QStringList& theEntryList )
462 {
463   // Process "Geometry" module operations with non-empty list of created objects only
464   if ( theModuleName != "Geometry" || theEntryList.isEmpty() ) {
465     return;
466   }
467   
468   // Start import GEOM object operation
469   myGeomObjectsToImport = theEntryList;
470   startOperation( ImportCreatedPrimitiveId );
471   myGeomObjectsToImport.clear();
472 }
473
474 /**
475  * Returns the list of entries of GEOM objects to be imported.
476  */
477 QStringList HYDROGUI_Module::GetGeomObjectsToImport()
478 {
479   return myGeomObjectsToImport;
480 }