Salome HOME
Create polyline operation was added
[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_PolylineOp.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( FuseId, "FUSE_IMAGES" );
67   createAction( CutId, "CUT_IMAGES" );
68
69   createAction( PolylineId, "POLYLINE" );
70
71
72   createAction( DeleteId, "DELETE", "", Qt::Key_Delete );
73
74   createAction( ShowId, "SHOW" );
75   createAction( ShowOnlyId, "SHOW_ONLY" );
76   createAction( ShowAllId, "SHOW_ALL" );
77   createAction( HideId, "HIDE" );
78   createAction( HideAllId, "HIDE_ALL" );
79 }
80
81 void HYDROGUI_Module::createMenus()
82 {
83   int anEditMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 5 );
84   createMenu( UndoId, anEditMenu );
85   createMenu( RedoId, anEditMenu );
86
87   int aHydroMenu = 6; // Edit menu id == 5, View menu id == 10
88   int aHydroId = createMenu( tr( "MEN_DESK_HYDRO" ), -1, -1, aHydroMenu );
89   createMenu( ImportImageId, aHydroId, -1, -1 );
90   createMenu( FuseId, aHydroId, -1, -1 );
91   createMenu( CutId, aHydroId, -1, -1 );
92   createMenu( PolylineId, aHydroId, -1, -1 );
93 }
94
95 void HYDROGUI_Module::createPopups()
96 {
97 }
98
99 void HYDROGUI_Module::createToolbars()
100 {
101   int aToolBar = createTool( tr( "HYDRO_TOOLBAR" ) );
102   createTool( UndoId, aToolBar );
103   createTool( RedoId, aToolBar );
104 }
105
106 void HYDROGUI_Module::createUndoRedoActions()
107 {
108   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
109
110   QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
111     aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
112     Qt::CTRL + Qt::Key_Z, application()->desktop() );
113     
114   QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
115     aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
116     Qt::CTRL + Qt::Key_Y, application()->desktop() );
117   
118   registerAction( UndoId, anEditUndo );
119   registerAction( RedoId, anEditRedo );
120
121   anEditUndo->setComment( tr( "STB_UNDO" ) );
122   anEditRedo->setComment( tr( "STB_REDO" ) );
123
124   connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
125   connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
126 }
127
128 void HYDROGUI_Module::updateUndoRedoControls()
129 {
130   HYDROGUI_DataModel* aModel = getDataModel();
131
132   QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
133   QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
134
135   bool aCanUndo = aModel->canUndo();
136   bool aCanRedo = aModel->canRedo();
137
138   if( aCanUndo )
139     aUndoAction->addNames( aModel->undoNames() );
140   aUndoAction->setEnabled( aCanUndo );
141
142   if( aCanRedo )
143     aRedoAction->addNames( aModel->redoNames() );
144   aRedoAction->setEnabled( aCanRedo );
145 }
146
147 void HYDROGUI_Module::onOperation()
148 {
149   const QAction* anAction = dynamic_cast<const QAction*>( sender() );
150   int anId = actionId( anAction );
151   if( anId >= 0 )
152     startOperation( anId );
153 }
154
155 bool HYDROGUI_Module::onUndo( int theNumActions )
156 {
157   QApplication::setOverrideCursor( Qt::WaitCursor );
158   bool anIsOk = true;
159   HYDROGUI_DataModel* aModel = getDataModel();
160   if( aModel )
161   {
162     while( theNumActions > 0 )
163     {
164       if( !aModel->undo() )
165       {
166         anIsOk = false;
167         break;
168       }
169       theNumActions--;
170     }
171     update( UF_All );
172   }
173   QApplication::restoreOverrideCursor();
174   return anIsOk;
175 }
176
177 bool HYDROGUI_Module::onRedo( int theNumActions )
178 {
179   QApplication::setOverrideCursor( Qt::WaitCursor );
180   bool anIsOk = true;
181   HYDROGUI_DataModel* aModel = getDataModel();
182   if( aModel )
183   {
184     while( theNumActions > 0 )
185     {
186       if( !aModel->redo() )
187       {
188         anIsOk = false;
189         break;
190       }
191       theNumActions--;
192     }
193     update( UF_All );
194   }
195   QApplication::restoreOverrideCursor();
196   return anIsOk;
197 }
198
199 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
200 {
201   LightApp_Operation* anOp = 0;
202   HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
203   switch( theId )
204   {
205   case ImportImageId:
206     anOp = new HYDROGUI_ImportImageOp( aModule );
207     break;
208   case FuseId:
209     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse );
210     break;
211   case CutId:
212     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut );
213     break;
214   case DeleteId:
215     anOp = new HYDROGUI_DeleteOp( aModule );
216     break;
217   case ShowId:
218   case ShowOnlyId:
219   case ShowAllId:
220   case HideId:
221   case HideAllId:
222     anOp = new HYDROGUI_ShowHideOp( aModule, theId );
223     break;
224   case PolylineId:
225     anOp = new HYDROGUI_PolylineOp( aModule );
226   }
227
228   if( !anOp )
229     anOp = LightApp_Module::createOperation( theId );
230
231   return anOp;
232 }