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