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