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