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