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