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