Salome HOME
7bf1c89534d635a8440212c3db491ef8a603e935
[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_CopyPasteOp.h"
26 #include "HYDROGUI_DataModel.h"
27 #include "HYDROGUI_DeleteOp.h"
28 #include "HYDROGUI_ExportImageOp.h"
29 #include "HYDROGUI_ImportImageOp.h"
30 #include "HYDROGUI_ImportBathymetryOp.h"
31 #include "HYDROGUI_CalculationOp.h"
32 #include "HYDROGUI_Module.h"
33 #include "HYDROGUI_ObserveImageOp.h"
34 #include "HYDROGUI_PolylineOp.h"
35 #include "HYDROGUI_ShowHideOp.h"
36 #include "HYDROGUI_SplitZonesTool.h"
37 #include "HYDROGUI_TwoImagesOp.h"
38 #include "HYDROGUI_UpdateFlags.h"
39 #include "HYDROGUI_UpdateImageOp.h"
40 #include "HYDROGUI_VisualStateOp.h"
41 #include "HYDROGUI_ZoneOp.h"
42
43 #include <CAM_Application.h>
44
45 #include <QtxListAction.h>
46
47 #include <SUIT_Desktop.h>
48 #include <SUIT_ResourceMgr.h>
49 #include <SUIT_Session.h>
50
51 #include <QAction>
52 #include <QApplication>
53
54 QAction* HYDROGUI_Module::createAction( const int theId, const QString& theSuffix, const QString& theImg,
55                                         const int theKey, const bool isToggle, const QString& theSlot )
56 {
57   QString aSlot = theSlot;
58   if( aSlot.isEmpty() )
59     aSlot = SLOT( onOperation() );
60   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
61   std::string anImg = theImg.toStdString();
62   QPixmap aPixmap = theImg.isEmpty() ? QPixmap() : aResMgr->loadPixmap( "HYDRO", tr( anImg.c_str() ) );
63   std::string aMenu    = ( "MEN_" + theSuffix ).toStdString();
64   std::string aDesktop = ( "DSK_" + theSuffix ).toStdString();
65   std::string aToolbar = ( "STB_" + theSuffix ).toStdString();
66   std::string aSlotStr = aSlot.toStdString();
67   return LightApp_Module::createAction( theId, tr( aMenu.c_str() ), aPixmap,
68     tr( aDesktop.c_str() ), tr( aToolbar.c_str() ),
69                 theKey, application()->desktop(), isToggle, this, aSlotStr.c_str() );
70 }
71
72 void HYDROGUI_Module::createActions()
73 {
74   createAction( SaveVisualStateId, "SAVE_VISUAL_STATE" );
75   createAction( LoadVisualStateId, "LOAD_VISUAL_STATE" );
76
77   createAction( CopyId, "COPY", "", Qt::CTRL + Qt::Key_C );
78   createAction( PasteId, "PASTE", "", Qt::CTRL + Qt::Key_V );
79
80   createAction( ImportImageId, "IMPORT_IMAGE", "", Qt::CTRL + Qt::Key_I );
81   createAction( EditImportedImageId, "EDIT_IMPORTED_IMAGE" );
82   createAction( ObserveImageId, "OBSERVE_IMAGE" );
83   createAction( ExportImageId, "EXPORT_IMAGE" );
84   createAction( UpdateImageId, "UPDATE_IMAGE" );
85
86   createAction( CreatePolylineId, "CREATE_POLYLINE" );
87   createAction( EditPolylineId, "EDIT_POLYLINE" ); 
88
89   createAction( ImportBathymetryId, "IMPORT_BATHYMETRY", "", Qt::CTRL + Qt::Key_B );
90
91   createAction( CreateZoneId, "CREATE_ZONE" );
92   createAction( EditZoneId, "EDIT_ZONE" );
93
94   createAction( CreateCalculationId, "CREATE_CALCULATION" );
95   createAction( EditCalculationId, "EDIT_CALCULATION" );
96
97   createAction( FuseImagesId, "FUSE_IMAGES" );
98   createAction( EditFusedImageId, "EDIT_FUSED_IMAGE" );
99
100   createAction( CutImagesId, "CUT_IMAGES" );
101   createAction( EditCutImageId, "EDIT_CUT_IMAGE" );
102
103   createAction( SplitImageId, "SPLIT_IMAGE" );
104   createAction( EditSplittedImageId, "EDIT_SPLITTED_IMAGE" );
105
106   createAction( DeleteId, "DELETE", "", Qt::Key_Delete );
107
108   createAction( ShowId, "SHOW" );
109   createAction( ShowOnlyId, "SHOW_ONLY" );
110   createAction( ShowAllId, "SHOW_ALL" );
111   createAction( HideId, "HIDE" );
112   createAction( HideAllId, "HIDE_ALL" );
113 }
114
115 void HYDROGUI_Module::createMenus()
116 {
117   int aFileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
118   int aCustomPos = 5; // to insert custom actions after "Save as" and before "Preferences"
119   createMenu( SaveVisualStateId, aFileMenu, aCustomPos );
120   createMenu( separator(), aFileMenu, -1, aCustomPos );
121
122   int anEditMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 5 );
123   createMenu( UndoId, anEditMenu );
124   createMenu( RedoId, anEditMenu );
125   createMenu( separator(), anEditMenu );
126   createMenu( CopyId, anEditMenu );
127   createMenu( PasteId, anEditMenu );
128
129   int aHydroMenu = 6; // Edit menu id == 5, View menu id == 10
130   int aHydroId = createMenu( tr( "MEN_DESK_HYDRO" ), -1, -1, aHydroMenu );
131   createMenu( ImportImageId, aHydroId, -1, -1 );
132   createMenu( ImportBathymetryId, aHydroId, -1, -1 );
133   createMenu( CreatePolylineId, aHydroId, -1, -1 );
134   createMenu( CreateZoneId, aHydroId, -1, -1 );
135   createMenu( CreateCalculationId, aHydroId, -1, -1 );
136   createMenu( separator(), aHydroId );
137   createMenu( FuseImagesId, aHydroId, -1, -1 );
138   createMenu( CutImagesId, aHydroId, -1, -1 );
139   createMenu( SplitImageId, aHydroId, -1, -1 );
140
141   //ouv: debug
142   //createMenu( separator(), aHydroId );
143   //createMenu( TestSplitId, aHydroId, -1, -1 );
144 }
145
146 void HYDROGUI_Module::createPopups()
147 {
148 }
149
150 void HYDROGUI_Module::createToolbars()
151 {
152   int aToolBar = createTool( tr( "MEN_DESK_HYDRO" ) );
153   createTool( UndoId, aToolBar );
154   createTool( RedoId, aToolBar );
155 }
156
157 void HYDROGUI_Module::createUndoRedoActions()
158 {
159   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
160
161   QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
162     aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
163     Qt::CTRL + Qt::Key_Z, application()->desktop() );
164     
165   QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
166     aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
167     Qt::CTRL + Qt::Key_Y, application()->desktop() );
168   
169   registerAction( UndoId, anEditUndo );
170   registerAction( RedoId, anEditRedo );
171
172   anEditUndo->setComment( tr( "STB_UNDO" ) );
173   anEditRedo->setComment( tr( "STB_REDO" ) );
174
175   connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
176   connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
177
178   QAction* aTestSplit = new QAction( "Test split", application()->desktop() );
179   registerAction( TestSplitId, aTestSplit );
180   connect( aTestSplit, SIGNAL( triggered() ), this, SLOT( onTestSplit() ) );
181 }
182
183 void HYDROGUI_Module::updateUndoRedoControls()
184 {
185   HYDROGUI_DataModel* aModel = getDataModel();
186
187   QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
188   QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
189
190   bool aCanUndo = aModel->canUndo();
191   bool aCanRedo = aModel->canRedo();
192
193   if( aCanUndo )
194     aUndoAction->addNames( aModel->undoNames() );
195   aUndoAction->setEnabled( aCanUndo );
196
197   if( aCanRedo )
198     aRedoAction->addNames( aModel->redoNames() );
199   aRedoAction->setEnabled( aCanRedo );
200 }
201
202 void HYDROGUI_Module::onOperation()
203 {
204   const QAction* anAction = dynamic_cast<const QAction*>( sender() );
205   int anId = actionId( anAction );
206   if( anId >= 0 )
207     startOperation( anId );
208 }
209
210 bool HYDROGUI_Module::onUndo( int theNumActions )
211 {
212   QApplication::setOverrideCursor( Qt::WaitCursor );
213   bool anIsOk = true;
214   HYDROGUI_DataModel* aModel = getDataModel();
215   if( aModel )
216   {
217     while( theNumActions > 0 )
218     {
219       if( !aModel->undo() )
220       {
221         anIsOk = false;
222         break;
223       }
224       theNumActions--;
225     }
226     update( UF_All );
227   }
228   QApplication::restoreOverrideCursor();
229   return anIsOk;
230 }
231
232 bool HYDROGUI_Module::onRedo( int theNumActions )
233 {
234   QApplication::setOverrideCursor( Qt::WaitCursor );
235   bool anIsOk = true;
236   HYDROGUI_DataModel* aModel = getDataModel();
237   if( aModel )
238   {
239     while( theNumActions > 0 )
240     {
241       if( !aModel->redo() )
242       {
243         anIsOk = false;
244         break;
245       }
246       theNumActions--;
247     }
248     update( UF_All );
249   }
250   QApplication::restoreOverrideCursor();
251   return anIsOk;
252 }
253
254 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
255 {
256   LightApp_Operation* anOp = 0;
257   HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
258   switch( theId )
259   {
260   case SaveVisualStateId:
261   case LoadVisualStateId:
262     anOp = new HYDROGUI_VisualStateOp( aModule, theId == LoadVisualStateId );
263     break;
264   case CopyId:
265   case PasteId:
266     anOp = new HYDROGUI_CopyPasteOp( aModule, theId == PasteId );
267     break;
268   case ImportImageId:
269   case EditImportedImageId:
270     anOp = new HYDROGUI_ImportImageOp( aModule, theId == EditImportedImageId );
271     break;
272   case ObserveImageId:
273     anOp = new HYDROGUI_ObserveImageOp( aModule );
274     break;
275   case ExportImageId:
276     anOp = new HYDROGUI_ExportImageOp( aModule );
277     break;
278   case UpdateImageId:
279     anOp = new HYDROGUI_UpdateImageOp( aModule );
280     break;
281   case CreatePolylineId:
282   case EditPolylineId:
283     anOp = new HYDROGUI_PolylineOp( aModule, theId == EditPolylineId );
284     break;
285   case ImportBathymetryId:
286     anOp = new HYDROGUI_ImportBathymetryOp( aModule );
287     break;
288   case CreateZoneId:
289   case EditZoneId:
290     anOp = new HYDROGUI_ZoneOp( aModule, theId == EditZoneId );
291     break;
292   case CreateCalculationId:
293   case EditCalculationId:
294     anOp = new HYDROGUI_CalculationOp( aModule, theId == EditCalculationId );
295     break;
296   case FuseImagesId:
297   case EditFusedImageId:
298     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse, theId == EditFusedImageId );
299     break;
300   case CutImagesId:
301   case EditCutImageId:
302     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut, theId == EditCutImageId );
303     break;
304   case SplitImageId:
305   case EditSplittedImageId:
306     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Split, theId == EditSplittedImageId );
307     break;
308   case DeleteId:
309     anOp = new HYDROGUI_DeleteOp( aModule );
310     break;
311   case ShowId:
312   case ShowOnlyId:
313   case ShowAllId:
314   case HideId:
315   case HideAllId:
316     anOp = new HYDROGUI_ShowHideOp( aModule, theId );
317     break;
318   }
319
320   if( !anOp )
321     anOp = LightApp_Module::createOperation( theId );
322
323   return anOp;
324 }
325
326 //-----------------------------------------------------------------------------
327 // Test splitting
328 //-----------------------------------------------------------------------------
329 #include <HYDROData_Iterator.h>
330 #include <HYDROGUI_Tool.h>
331 void HYDROGUI_Module::onTestSplit()
332 {
333   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( getStudyId() );
334
335   aDocument->StartOperation();
336
337   HYDROData_SequenceOfObjects aZoneList;
338   HYDROData_Iterator anIterator( aDocument, KIND_ZONE );
339   for( ; anIterator.More(); anIterator.Next() )
340     aZoneList.Append( anIterator.Current() );
341
342   HYDROGUI_SplitZonesTool::SplitDataList aSplitDataList =
343     HYDROGUI_SplitZonesTool::SplitZones( aZoneList, NULL );
344
345   int anIndex = 0;
346   HYDROGUI_SplitZonesTool::SplitDataListIterator anIter( aSplitDataList );
347   while( anIter.hasNext() )
348   {
349     const HYDROGUI_SplitZonesTool::SplitData& aSplitData = anIter.next();
350     const QPainterPath& aPath = aSplitData.Path;
351     const QStringList& aZoneNames = aSplitData.ZoneNames;
352
353     Handle(HYDROData_Polyline) aPolyline =
354       Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
355     if( !aPolyline.IsNull() )
356     {
357       aPolyline->SetName( QString( "SplitPolyline_%1" ).arg( ++anIndex ) );
358       aPolyline->setDimension( 2 );
359
360       QList<PolylineSection> aPolylineData;
361       for( int i = 0, n = aPath.elementCount(); i < n; i++ )
362       {
363         const QPainterPath::Element anElement = aPath.elementAt( i );
364         switch( anElement.type )
365         {
366           case QPainterPath::MoveToElement:
367             aPolylineData.append( PolylineSection() );
368             break;
369           case QPainterPath::LineToElement:
370             if( !aPolylineData.isEmpty() )
371             {
372               PolylineSection& aSection = aPolylineData.last();
373               aSection.myCoords << anElement.x;
374               aSection.myCoords << anElement.y;
375             }
376             break;
377           case QPainterPath::CurveToElement: // currently not supported
378           default:
379             break;
380         }
381       }
382       aPolyline->setPolylineData( aPolylineData );
383     }
384
385     Handle(HYDROData_Zone) aZone =
386       Handle(HYDROData_Zone)::DownCast( aDocument->CreateObject( KIND_ZONE ) );
387     if( !aZone.IsNull() )
388     {
389       aZone->SetName( QString( "SplitZone_%1" ).arg( anIndex ) );
390       aZone->SetPolyline( aPolyline );
391
392       aZone->SetBorderColor( Qt::black );
393
394       int aCounter = 0;
395       int aR = 0, aG = 0, aB = 0;
396       QStringListIterator aZoneNameIter( aZoneNames );
397       while( aZoneNameIter.hasNext() )
398       {
399         const QString& aZoneName = aZoneNameIter.next();
400         Handle(HYDROData_Zone) aRefZone = Handle(HYDROData_Zone)::DownCast(
401           HYDROGUI_Tool::FindObjectByName( this, aZoneName, KIND_ZONE ) );
402         if( !aRefZone.IsNull() )
403         {
404           QColor aRefColor = aRefZone->GetFillingColor();
405           aR += aRefColor.red();
406           aG += aRefColor.green();
407           aB += aRefColor.blue();
408           aCounter++;
409         }
410       }
411       if( aCounter > 0 )
412       {
413         QColor aFillingColor( aR / aCounter, aG / aCounter, aB / aCounter );
414         aZone->SetFillingColor( aFillingColor );
415       }
416     }
417   }
418
419   aDocument->CommitOperation();
420   update( UF_Model );
421 }