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