Salome HOME
Implement panel for obstacles creation/modification.
[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 );
131
132   createAction( SetColorId, "COLOR" );
133
134   createAction( ShowId, "SHOW" );
135   createAction( ShowOnlyId, "SHOW_ONLY" );
136   createAction( ShowAllId, "SHOW_ALL" );
137   createAction( HideId, "HIDE" );
138   createAction( HideAllId, "HIDE_ALL" );
139 }
140
141 void HYDROGUI_Module::createMenus()
142 {
143   int aFileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
144   int aCustomPos = 5; // to insert custom actions after "Save as" and before "Preferences"
145   createMenu( SaveVisualStateId, aFileMenu, aCustomPos );
146   createMenu( separator(), aFileMenu, -1, aCustomPos );
147
148   int anEditMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 5 );
149   createMenu( UndoId, anEditMenu );
150   createMenu( RedoId, anEditMenu );
151   createMenu( separator(), anEditMenu );
152   createMenu( CopyId, anEditMenu );
153   createMenu( PasteId, anEditMenu );
154
155   int aHydroMenu = 6; // Edit menu id == 5, View menu id == 10
156   int aHydroId = createMenu( tr( "MEN_DESK_HYDRO" ), -1, -1, aHydroMenu );
157   createMenu( ImportImageId, aHydroId, -1, -1 );
158   createMenu( ImportBathymetryId, aHydroId, -1, -1 );
159   createMenu( CreatePolylineId, aHydroId, -1, -1 );
160   createMenu( CreateImmersibleZoneId, aHydroId, -1, -1 );
161
162   int aNewObstacleId = createMenu( tr( "MEN_OBSTACLE" ), aHydroId, -1 );
163   createMenu( ImportObstacleFromFileId, aNewObstacleId, -1, -1 );
164   createMenu( CreateBoxId, aNewObstacleId, -1, -1 );
165   createMenu( CreateCylinderId, aNewObstacleId, -1, -1 );
166
167   createMenu( CreateCalculationId, aHydroId, -1, -1 );
168   createMenu( separator(), aHydroId );
169   createMenu( FuseImagesId, aHydroId, -1, -1 );
170   createMenu( CutImagesId, aHydroId, -1, -1 );
171   createMenu( SplitImageId, aHydroId, -1, -1 );
172   createMenu( separator(), aHydroId );
173 }
174
175 void HYDROGUI_Module::createPopups()
176 {
177 }
178
179 void HYDROGUI_Module::createToolbars()
180 {
181   int aToolBar = createTool( tr( "MEN_DESK_HYDRO" ) );
182   createTool( UndoId, aToolBar );
183   createTool( RedoId, aToolBar );
184 }
185
186 void HYDROGUI_Module::createUndoRedoActions()
187 {
188   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
189
190   QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
191     aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
192     Qt::CTRL + Qt::Key_Z, application()->desktop() );
193     
194   QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
195     aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
196     Qt::CTRL + Qt::Key_Y, application()->desktop() );
197   
198   registerAction( UndoId, anEditUndo );
199   registerAction( RedoId, anEditRedo );
200
201   anEditUndo->setComment( tr( "STB_UNDO" ) );
202   anEditRedo->setComment( tr( "STB_REDO" ) );
203
204   connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
205   connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
206 }
207
208 void HYDROGUI_Module::updateUndoRedoControls()
209 {
210   HYDROGUI_DataModel* aModel = getDataModel();
211
212   QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
213   QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
214
215   bool aCanUndo = aModel->canUndo();
216   bool aCanRedo = aModel->canRedo();
217
218   if( aCanUndo )
219     aUndoAction->addNames( aModel->undoNames() );
220   aUndoAction->setEnabled( aCanUndo );
221
222   if( aCanRedo )
223     aRedoAction->addNames( aModel->redoNames() );
224   aRedoAction->setEnabled( aCanRedo );
225 }
226
227 void HYDROGUI_Module::onOperation()
228 {
229   const QAction* anAction = dynamic_cast<const QAction*>( sender() );
230   int anId = actionId( anAction );
231   if( anId >= 0 )
232     startOperation( anId );
233 }
234
235 bool HYDROGUI_Module::onUndo( int theNumActions )
236 {
237   QApplication::setOverrideCursor( Qt::WaitCursor );
238   bool anIsOk = true;
239   HYDROGUI_DataModel* aModel = getDataModel();
240   if( aModel )
241   {
242     while( theNumActions > 0 )
243     {
244       if( !aModel->undo() )
245       {
246         anIsOk = false;
247         break;
248       }
249       theNumActions--;
250     }
251     update( UF_All );
252   }
253   QApplication::restoreOverrideCursor();
254   return anIsOk;
255 }
256
257 bool HYDROGUI_Module::onRedo( int theNumActions )
258 {
259   QApplication::setOverrideCursor( Qt::WaitCursor );
260   bool anIsOk = true;
261   HYDROGUI_DataModel* aModel = getDataModel();
262   if( aModel )
263   {
264     while( theNumActions > 0 )
265     {
266       if( !aModel->redo() )
267       {
268         anIsOk = false;
269         break;
270       }
271       theNumActions--;
272     }
273     update( UF_All );
274   }
275   QApplication::restoreOverrideCursor();
276   return anIsOk;
277 }
278
279 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
280 {
281   LightApp_Operation* anOp = 0;
282   HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
283   switch( theId )
284   {
285   case SaveVisualStateId:
286   case LoadVisualStateId:
287     anOp = new HYDROGUI_VisualStateOp( aModule, theId == LoadVisualStateId );
288     break;
289   case CopyId:
290   case PasteId:
291     anOp = new HYDROGUI_CopyPasteOp( aModule, theId == PasteId );
292     break;
293   case ImportImageId:
294   case EditImportedImageId:
295     anOp = new HYDROGUI_ImportImageOp( aModule, theId == EditImportedImageId );
296     break;
297   case ObserveImageId:
298     anOp = new HYDROGUI_ObserveImageOp( aModule );
299     break;
300   case ExportImageId:
301     anOp = new HYDROGUI_ExportImageOp( aModule );
302     break;
303   case UpdateImageId:
304     anOp = new HYDROGUI_UpdateImageOp( aModule );
305     break;
306   case RemoveImageRefsId:
307     anOp = new HYDROGUI_RemoveImageRefsOp( aModule );
308     break;
309   case CreatePolylineId:
310   case EditPolylineId:
311     anOp = new HYDROGUI_PolylineOp( aModule, theId == EditPolylineId );
312     break;
313   case ImportBathymetryId:
314     anOp = new HYDROGUI_ImportBathymetryOp( aModule );
315     break;
316   case CreateImmersibleZoneId:
317   case EditImmersibleZoneId:
318     anOp = new HYDROGUI_ImmersibleZoneOp( aModule, theId == EditImmersibleZoneId );
319     break;
320   case CreateCalculationId:
321   case EditCalculationId:
322     anOp = new HYDROGUI_CalculationOp( aModule, theId == EditCalculationId );
323     break;
324   case ExportCalculationId:
325     anOp = new HYDROGUI_ExportCalculationOp( aModule );
326     break;
327   case FuseImagesId:
328   case EditFusedImageId:
329     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse, theId == EditFusedImageId );
330     break;
331   case CutImagesId:
332   case EditCutImageId:
333     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut, theId == EditCutImageId );
334     break;
335   case SplitImageId:
336   case EditSplittedImageId:
337     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Split, theId == EditSplittedImageId );
338     break;
339   case ImportObstacleFromFileId:
340     anOp = new HYDROGUI_ImportObstacleFromFileOp( aModule );
341     break;
342   case ImportCreatedPrimitiveId:
343     anOp = new HYDROGUI_ImportGeomObjectOp( aModule, HYDROGUI_ImportGeomObjectOp::ImportCreated );
344     break;
345   case ImportGeomObjectId:
346     anOp = new HYDROGUI_ImportGeomObjectOp( aModule, HYDROGUI_ImportGeomObjectOp::ImportSelected );
347     break;
348   case CreateBoxId:
349     application()->activateOperation( "Geometry", GEOMOp::OpBox );
350     break;
351   case CreateCylinderId:
352     application()->activateOperation( "Geometry", GEOMOp::OpCylinder );
353     break;
354   case DeleteId:
355     anOp = new HYDROGUI_DeleteOp( aModule );
356     break;
357   case SetColorId:
358     anOp = new HYDROGUI_SetColorOp( aModule );
359     break;
360   case ShowId:
361   case ShowOnlyId:
362   case ShowAllId:
363   case HideId:
364   case HideAllId:
365     anOp = new HYDROGUI_ShowHideOp( aModule, theId );
366     break;
367   }
368
369   if( !anOp )
370     anOp = LightApp_Module::createOperation( theId );
371
372   return anOp;
373 }
374
375 bool HYDROGUI_Module::reusableOperation( const int id )
376 {
377   if ( id == ImportGeomObjectId ) {
378     return false;
379   }
380
381   return LightApp_Module::reusableOperation( id );
382 }
383
384 /**
385  * Called when the operation perfomed by another module is finished.
386  * \param theModuleName the name of the module which perfomed the operation
387  * \param theOperationName the operation name
388  * \param theEntryList the list of the created objects entries
389  */
390 void HYDROGUI_Module::onExternalOperationFinished( const QString& theModuleName,
391                                                    const QString& theOperationName,
392                                                    const QStringList& theEntryList )
393 {
394   // Process "Geometry" module operations with non-empty list of created objects only
395   if ( theModuleName != "Geometry" || theEntryList.isEmpty() ) {
396     return;
397   }
398   
399   // Start import GEOM object operation
400   myGeomObjectsToImport = theEntryList;
401   startOperation( ImportCreatedPrimitiveId );
402   myGeomObjectsToImport.clear();
403 }
404
405 /**
406  * Returns the list of entries of GEOM objects to be imported.
407  */
408 QStringList HYDROGUI_Module::GetGeomObjectsToImport()
409 {
410   return myGeomObjectsToImport;
411 }