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