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