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