]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_Operations.cxx
Salome HOME
Copy/paste functionality for images, polylines and calculation cases.
[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::SHIFT + Qt::Key_I );
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( FuseImagesId, aHydroId, -1, -1 );
131   createMenu( CutImagesId, aHydroId, -1, -1 );
132   createMenu( SplitImageId, aHydroId, -1, -1 );
133 }
134
135 void HYDROGUI_Module::createPopups()
136 {
137 }
138
139 void HYDROGUI_Module::createToolbars()
140 {
141   int aToolBar = createTool( tr( "MEN_DESK_HYDRO" ) );
142   createTool( UndoId, aToolBar );
143   createTool( RedoId, aToolBar );
144 }
145
146 void HYDROGUI_Module::createUndoRedoActions()
147 {
148   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
149
150   QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
151     aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
152     Qt::CTRL + Qt::Key_Z, application()->desktop() );
153     
154   QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
155     aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
156     Qt::CTRL + Qt::Key_Y, application()->desktop() );
157   
158   registerAction( UndoId, anEditUndo );
159   registerAction( RedoId, anEditRedo );
160
161   anEditUndo->setComment( tr( "STB_UNDO" ) );
162   anEditRedo->setComment( tr( "STB_REDO" ) );
163
164   connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
165   connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
166 }
167
168 void HYDROGUI_Module::updateUndoRedoControls()
169 {
170   HYDROGUI_DataModel* aModel = getDataModel();
171
172   QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
173   QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
174
175   bool aCanUndo = aModel->canUndo();
176   bool aCanRedo = aModel->canRedo();
177
178   if( aCanUndo )
179     aUndoAction->addNames( aModel->undoNames() );
180   aUndoAction->setEnabled( aCanUndo );
181
182   if( aCanRedo )
183     aRedoAction->addNames( aModel->redoNames() );
184   aRedoAction->setEnabled( aCanRedo );
185 }
186
187 void HYDROGUI_Module::onOperation()
188 {
189   const QAction* anAction = dynamic_cast<const QAction*>( sender() );
190   int anId = actionId( anAction );
191   if( anId >= 0 )
192     startOperation( anId );
193 }
194
195 bool HYDROGUI_Module::onUndo( int theNumActions )
196 {
197   QApplication::setOverrideCursor( Qt::WaitCursor );
198   bool anIsOk = true;
199   HYDROGUI_DataModel* aModel = getDataModel();
200   if( aModel )
201   {
202     while( theNumActions > 0 )
203     {
204       if( !aModel->undo() )
205       {
206         anIsOk = false;
207         break;
208       }
209       theNumActions--;
210     }
211     update( UF_All );
212   }
213   QApplication::restoreOverrideCursor();
214   return anIsOk;
215 }
216
217 bool HYDROGUI_Module::onRedo( int theNumActions )
218 {
219   QApplication::setOverrideCursor( Qt::WaitCursor );
220   bool anIsOk = true;
221   HYDROGUI_DataModel* aModel = getDataModel();
222   if( aModel )
223   {
224     while( theNumActions > 0 )
225     {
226       if( !aModel->redo() )
227       {
228         anIsOk = false;
229         break;
230       }
231       theNumActions--;
232     }
233     update( UF_All );
234   }
235   QApplication::restoreOverrideCursor();
236   return anIsOk;
237 }
238
239 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
240 {
241   LightApp_Operation* anOp = 0;
242   HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
243   switch( theId )
244   {
245   case SaveVisualStateId:
246   case LoadVisualStateId:
247     anOp = new HYDROGUI_VisualStateOp( aModule, theId == LoadVisualStateId );
248     break;
249   case CopyId:
250   case PasteId:
251     anOp = new HYDROGUI_CopyPasteOp( aModule, theId == PasteId );
252     break;
253   case ImportImageId:
254   case EditImportedImageId:
255     anOp = new HYDROGUI_ImportImageOp( aModule, theId == EditImportedImageId );
256     break;
257   case ObserveImageId:
258     anOp = new HYDROGUI_ObserveImageOp( aModule );
259     break;
260   case ExportImageId:
261     anOp = new HYDROGUI_ExportImageOp( aModule );
262     break;
263   case UpdateImageId:
264     anOp = new HYDROGUI_UpdateImageOp( aModule );
265     break;
266   case CreatePolylineId:
267   case EditPolylineId:
268     anOp = new HYDROGUI_PolylineOp( aModule, theId == EditPolylineId );
269     break;
270   case ImportBathymetryId:
271     anOp = new HYDROGUI_ImportBathymetryOp( aModule );
272     break;
273   case CreateCalculationId:
274   case EditCalculationId:
275     anOp = new HYDROGUI_CalculationOp( aModule, theId == EditCalculationId );
276     break;
277   case FuseImagesId:
278   case EditFusedImageId:
279     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse, theId == EditFusedImageId );
280     break;
281   case CutImagesId:
282   case EditCutImageId:
283     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut, theId == EditCutImageId );
284     break;
285   case SplitImageId:
286   case EditSplittedImageId:
287     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Split, theId == EditSplittedImageId );
288     break;
289   case DeleteId:
290     anOp = new HYDROGUI_DeleteOp( aModule );
291     break;
292   case ShowId:
293   case ShowOnlyId:
294   case ShowAllId:
295   case HideId:
296   case HideAllId:
297     anOp = new HYDROGUI_ShowHideOp( aModule, theId );
298     break;
299   }
300
301   if( !anOp )
302     anOp = LightApp_Module::createOperation( theId );
303
304   return anOp;
305 }