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