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