Salome HOME
Fix for the bug #42: point C is not activated, but point C is shown in preview in...
[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
44 #include <CAM_Application.h>
45
46 #include <QtxListAction.h>
47
48 #include <SUIT_Desktop.h>
49 #include <SUIT_ResourceMgr.h>
50 #include <SUIT_Session.h>
51
52 #include <QAction>
53 #include <QApplication>
54
55 QAction* HYDROGUI_Module::createAction( const int theId, const QString& theSuffix, const QString& theImg,
56                                         const int theKey, const bool isToggle, const QString& theSlot )
57 {
58   QString aSlot = theSlot;
59   if( aSlot.isEmpty() )
60     aSlot = SLOT( onOperation() );
61   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
62   std::string anImg = theImg.toStdString();
63   QPixmap aPixmap = theImg.isEmpty() ? QPixmap() : aResMgr->loadPixmap( "HYDRO", tr( anImg.c_str() ) );
64   std::string aMenu    = ( "MEN_" + theSuffix ).toStdString();
65   std::string aDesktop = ( "DSK_" + theSuffix ).toStdString();
66   std::string aToolbar = ( "STB_" + theSuffix ).toStdString();
67   std::string aSlotStr = aSlot.toStdString();
68   return LightApp_Module::createAction( theId, tr( aMenu.c_str() ), aPixmap,
69     tr( aDesktop.c_str() ), tr( aToolbar.c_str() ),
70                 theKey, application()->desktop(), isToggle, this, aSlotStr.c_str() );
71 }
72
73 void HYDROGUI_Module::createActions()
74 {
75   createAction( SaveVisualStateId, "SAVE_VISUAL_STATE" );
76   createAction( LoadVisualStateId, "LOAD_VISUAL_STATE" );
77
78   createAction( CopyId, "COPY", "", Qt::CTRL + Qt::Key_C );
79   createAction( PasteId, "PASTE", "", Qt::CTRL + Qt::Key_V );
80
81   createAction( ImportImageId, "IMPORT_IMAGE", "", Qt::CTRL + Qt::Key_I );
82   createAction( EditImportedImageId, "EDIT_IMPORTED_IMAGE" );
83   createAction( ObserveImageId, "OBSERVE_IMAGE" );
84   createAction( ExportImageId, "EXPORT_IMAGE" );
85   createAction( UpdateImageId, "UPDATE_IMAGE" );
86   createAction( RemoveImageRefsId, "REMOVE_IMAGE_REFERENCE" );
87
88   createAction( CreatePolylineId, "CREATE_POLYLINE" );
89   createAction( EditPolylineId, "EDIT_POLYLINE" ); 
90
91   createAction( ImportBathymetryId, "IMPORT_BATHYMETRY", "", Qt::CTRL + Qt::Key_B );
92
93   createAction( CreateImmersibleZoneId, "CREATE_IMMERSIBLE_ZONE" );
94   createAction( EditImmersibleZoneId, "EDIT_IMMERSIBLE_ZONE" );
95
96   createAction( CreateCalculationId, "CREATE_CALCULATION" );
97   createAction( EditCalculationId, "EDIT_CALCULATION" );
98
99   createAction( FuseImagesId, "FUSE_IMAGES" );
100   createAction( EditFusedImageId, "EDIT_FUSED_IMAGE" );
101
102   createAction( CutImagesId, "CUT_IMAGES" );
103   createAction( EditCutImageId, "EDIT_CUT_IMAGE" );
104
105   createAction( SplitImageId, "SPLIT_IMAGE" );
106   createAction( EditSplittedImageId, "EDIT_SPLITTED_IMAGE" );
107
108   createAction( DeleteId, "DELETE", "", Qt::Key_Delete );
109
110   createAction( ShowId, "SHOW" );
111   createAction( ShowOnlyId, "SHOW_ONLY" );
112   createAction( ShowAllId, "SHOW_ALL" );
113   createAction( HideId, "HIDE" );
114   createAction( HideAllId, "HIDE_ALL" );
115 }
116
117 void HYDROGUI_Module::createMenus()
118 {
119   int aFileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
120   int aCustomPos = 5; // to insert custom actions after "Save as" and before "Preferences"
121   createMenu( SaveVisualStateId, aFileMenu, aCustomPos );
122   createMenu( separator(), aFileMenu, -1, aCustomPos );
123
124   int anEditMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 5 );
125   createMenu( UndoId, anEditMenu );
126   createMenu( RedoId, anEditMenu );
127   createMenu( separator(), anEditMenu );
128   createMenu( CopyId, anEditMenu );
129   createMenu( PasteId, anEditMenu );
130
131   int aHydroMenu = 6; // Edit menu id == 5, View menu id == 10
132   int aHydroId = createMenu( tr( "MEN_DESK_HYDRO" ), -1, -1, aHydroMenu );
133   createMenu( ImportImageId, aHydroId, -1, -1 );
134   createMenu( ImportBathymetryId, aHydroId, -1, -1 );
135   createMenu( CreatePolylineId, aHydroId, -1, -1 );
136   createMenu( CreateImmersibleZoneId, aHydroId, -1, -1 );
137   createMenu( CreateCalculationId, aHydroId, -1, -1 );
138   createMenu( separator(), aHydroId );
139   createMenu( FuseImagesId, aHydroId, -1, -1 );
140   createMenu( CutImagesId, aHydroId, -1, -1 );
141   createMenu( SplitImageId, aHydroId, -1, -1 );
142 }
143
144 void HYDROGUI_Module::createPopups()
145 {
146 }
147
148 void HYDROGUI_Module::createToolbars()
149 {
150   int aToolBar = createTool( tr( "MEN_DESK_HYDRO" ) );
151   createTool( UndoId, aToolBar );
152   createTool( RedoId, aToolBar );
153 }
154
155 void HYDROGUI_Module::createUndoRedoActions()
156 {
157   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
158
159   QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
160     aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
161     Qt::CTRL + Qt::Key_Z, application()->desktop() );
162     
163   QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
164     aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
165     Qt::CTRL + Qt::Key_Y, application()->desktop() );
166   
167   registerAction( UndoId, anEditUndo );
168   registerAction( RedoId, anEditRedo );
169
170   anEditUndo->setComment( tr( "STB_UNDO" ) );
171   anEditRedo->setComment( tr( "STB_REDO" ) );
172
173   connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
174   connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
175 }
176
177 void HYDROGUI_Module::updateUndoRedoControls()
178 {
179   HYDROGUI_DataModel* aModel = getDataModel();
180
181   QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
182   QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
183
184   bool aCanUndo = aModel->canUndo();
185   bool aCanRedo = aModel->canRedo();
186
187   if( aCanUndo )
188     aUndoAction->addNames( aModel->undoNames() );
189   aUndoAction->setEnabled( aCanUndo );
190
191   if( aCanRedo )
192     aRedoAction->addNames( aModel->redoNames() );
193   aRedoAction->setEnabled( aCanRedo );
194 }
195
196 void HYDROGUI_Module::onOperation()
197 {
198   const QAction* anAction = dynamic_cast<const QAction*>( sender() );
199   int anId = actionId( anAction );
200   if( anId >= 0 )
201     startOperation( anId );
202 }
203
204 bool HYDROGUI_Module::onUndo( int theNumActions )
205 {
206   QApplication::setOverrideCursor( Qt::WaitCursor );
207   bool anIsOk = true;
208   HYDROGUI_DataModel* aModel = getDataModel();
209   if( aModel )
210   {
211     while( theNumActions > 0 )
212     {
213       if( !aModel->undo() )
214       {
215         anIsOk = false;
216         break;
217       }
218       theNumActions--;
219     }
220     update( UF_All );
221   }
222   QApplication::restoreOverrideCursor();
223   return anIsOk;
224 }
225
226 bool HYDROGUI_Module::onRedo( int theNumActions )
227 {
228   QApplication::setOverrideCursor( Qt::WaitCursor );
229   bool anIsOk = true;
230   HYDROGUI_DataModel* aModel = getDataModel();
231   if( aModel )
232   {
233     while( theNumActions > 0 )
234     {
235       if( !aModel->redo() )
236       {
237         anIsOk = false;
238         break;
239       }
240       theNumActions--;
241     }
242     update( UF_All );
243   }
244   QApplication::restoreOverrideCursor();
245   return anIsOk;
246 }
247
248 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
249 {
250   LightApp_Operation* anOp = 0;
251   HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
252   switch( theId )
253   {
254   case SaveVisualStateId:
255   case LoadVisualStateId:
256     anOp = new HYDROGUI_VisualStateOp( aModule, theId == LoadVisualStateId );
257     break;
258   case CopyId:
259   case PasteId:
260     anOp = new HYDROGUI_CopyPasteOp( aModule, theId == PasteId );
261     break;
262   case ImportImageId:
263   case EditImportedImageId:
264     anOp = new HYDROGUI_ImportImageOp( aModule, theId == EditImportedImageId );
265     break;
266   case ObserveImageId:
267     anOp = new HYDROGUI_ObserveImageOp( aModule );
268     break;
269   case ExportImageId:
270     anOp = new HYDROGUI_ExportImageOp( aModule );
271     break;
272   case UpdateImageId:
273     anOp = new HYDROGUI_UpdateImageOp( aModule );
274     break;
275   case RemoveImageRefsId:
276     anOp = new HYDROGUI_RemoveImageRefsOp( aModule );
277     break;
278   case CreatePolylineId:
279   case EditPolylineId:
280     anOp = new HYDROGUI_PolylineOp( aModule, theId == EditPolylineId );
281     break;
282   case ImportBathymetryId:
283     anOp = new HYDROGUI_ImportBathymetryOp( aModule );
284     break;
285   case CreateImmersibleZoneId:
286   case EditImmersibleZoneId:
287     anOp = new HYDROGUI_ImmersibleZoneOp( aModule, theId == EditImmersibleZoneId );
288     break;
289   case CreateCalculationId:
290   case EditCalculationId:
291     anOp = new HYDROGUI_CalculationOp( aModule, theId == EditCalculationId );
292     break;
293   case FuseImagesId:
294   case EditFusedImageId:
295     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse, theId == EditFusedImageId );
296     break;
297   case CutImagesId:
298   case EditCutImageId:
299     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut, theId == EditCutImageId );
300     break;
301   case SplitImageId:
302   case EditSplittedImageId:
303     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Split, theId == EditSplittedImageId );
304     break;
305   case DeleteId:
306     anOp = new HYDROGUI_DeleteOp( aModule );
307     break;
308   case ShowId:
309   case ShowOnlyId:
310   case ShowAllId:
311   case HideId:
312   case HideAllId:
313     anOp = new HYDROGUI_ShowHideOp( aModule, theId );
314     break;
315   }
316
317   if( !anOp )
318     anOp = LightApp_Module::createOperation( theId );
319
320   return anOp;
321 }