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