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