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