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