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