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