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