Salome HOME
Modify creation of curves: 1) using QDockWidget instead of QDialog; 2) selection...
[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   createMenu( separator(), aHydroId );
142   createMenu( TestSplitId, aHydroId, -1, -1 );
143 }
144
145 void HYDROGUI_Module::createPopups()
146 {
147 }
148
149 void HYDROGUI_Module::createToolbars()
150 {
151   int aToolBar = createTool( tr( "MEN_DESK_HYDRO" ) );
152   createTool( UndoId, aToolBar );
153   createTool( RedoId, aToolBar );
154 }
155
156 void HYDROGUI_Module::createUndoRedoActions()
157 {
158   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
159
160   QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
161     aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
162     Qt::CTRL + Qt::Key_Z, application()->desktop() );
163     
164   QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
165     aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
166     Qt::CTRL + Qt::Key_Y, application()->desktop() );
167   
168   registerAction( UndoId, anEditUndo );
169   registerAction( RedoId, anEditRedo );
170
171   anEditUndo->setComment( tr( "STB_UNDO" ) );
172   anEditRedo->setComment( tr( "STB_REDO" ) );
173
174   connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
175   connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
176
177   QAction* aTestSplit = new QAction( "Test split", application()->desktop() );
178   registerAction( TestSplitId, aTestSplit );
179   connect( aTestSplit, SIGNAL( triggered() ), this, SLOT( onTestSplit() ) );
180 }
181
182 void HYDROGUI_Module::updateUndoRedoControls()
183 {
184   HYDROGUI_DataModel* aModel = getDataModel();
185
186   QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
187   QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
188
189   bool aCanUndo = aModel->canUndo();
190   bool aCanRedo = aModel->canRedo();
191
192   if( aCanUndo )
193     aUndoAction->addNames( aModel->undoNames() );
194   aUndoAction->setEnabled( aCanUndo );
195
196   if( aCanRedo )
197     aRedoAction->addNames( aModel->redoNames() );
198   aRedoAction->setEnabled( aCanRedo );
199 }
200
201 void HYDROGUI_Module::onOperation()
202 {
203   const QAction* anAction = dynamic_cast<const QAction*>( sender() );
204   int anId = actionId( anAction );
205   if( anId >= 0 )
206     startOperation( anId );
207 }
208
209 bool HYDROGUI_Module::onUndo( int theNumActions )
210 {
211   QApplication::setOverrideCursor( Qt::WaitCursor );
212   bool anIsOk = true;
213   HYDROGUI_DataModel* aModel = getDataModel();
214   if( aModel )
215   {
216     while( theNumActions > 0 )
217     {
218       if( !aModel->undo() )
219       {
220         anIsOk = false;
221         break;
222       }
223       theNumActions--;
224     }
225     update( UF_All );
226   }
227   QApplication::restoreOverrideCursor();
228   return anIsOk;
229 }
230
231 bool HYDROGUI_Module::onRedo( int theNumActions )
232 {
233   QApplication::setOverrideCursor( Qt::WaitCursor );
234   bool anIsOk = true;
235   HYDROGUI_DataModel* aModel = getDataModel();
236   if( aModel )
237   {
238     while( theNumActions > 0 )
239     {
240       if( !aModel->redo() )
241       {
242         anIsOk = false;
243         break;
244       }
245       theNumActions--;
246     }
247     update( UF_All );
248   }
249   QApplication::restoreOverrideCursor();
250   return anIsOk;
251 }
252
253 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
254 {
255   LightApp_Operation* anOp = 0;
256   HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
257   switch( theId )
258   {
259   case SaveVisualStateId:
260   case LoadVisualStateId:
261     anOp = new HYDROGUI_VisualStateOp( aModule, theId == LoadVisualStateId );
262     break;
263   case CopyId:
264   case PasteId:
265     anOp = new HYDROGUI_CopyPasteOp( aModule, theId == PasteId );
266     break;
267   case ImportImageId:
268   case EditImportedImageId:
269     anOp = new HYDROGUI_ImportImageOp( aModule, theId == EditImportedImageId );
270     break;
271   case ObserveImageId:
272     anOp = new HYDROGUI_ObserveImageOp( aModule );
273     break;
274   case ExportImageId:
275     anOp = new HYDROGUI_ExportImageOp( aModule );
276     break;
277   case UpdateImageId:
278     anOp = new HYDROGUI_UpdateImageOp( aModule );
279     break;
280   case CreatePolylineId:
281   case EditPolylineId:
282     anOp = new HYDROGUI_PolylineOp( aModule, theId == EditPolylineId );
283     break;
284   case ImportBathymetryId:
285     anOp = new HYDROGUI_ImportBathymetryOp( aModule );
286     break;
287   case CreateZoneId:
288   case EditZoneId:
289     anOp = new HYDROGUI_ZoneOp( aModule, theId == EditZoneId );
290     break;
291   case CreateCalculationId:
292   case EditCalculationId:
293     anOp = new HYDROGUI_CalculationOp( aModule, theId == EditCalculationId );
294     break;
295   case FuseImagesId:
296   case EditFusedImageId:
297     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse, theId == EditFusedImageId );
298     break;
299   case CutImagesId:
300   case EditCutImageId:
301     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut, theId == EditCutImageId );
302     break;
303   case SplitImageId:
304   case EditSplittedImageId:
305     anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Split, theId == EditSplittedImageId );
306     break;
307   case DeleteId:
308     anOp = new HYDROGUI_DeleteOp( aModule );
309     break;
310   case ShowId:
311   case ShowOnlyId:
312   case ShowAllId:
313   case HideId:
314   case HideAllId:
315     anOp = new HYDROGUI_ShowHideOp( aModule, theId );
316     break;
317   }
318
319   if( !anOp )
320     anOp = LightApp_Module::createOperation( theId );
321
322   return anOp;
323 }
324
325 //-----------------------------------------------------------------------------
326 // Test splitting
327 //-----------------------------------------------------------------------------
328 #include <HYDROData_Iterator.h>
329 #include <HYDROGUI_Tool.h>
330 void HYDROGUI_Module::onTestSplit()
331 {
332   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( getStudyId() );
333
334   aDocument->StartOperation();
335
336   HYDROData_SequenceOfObjects aZoneList;
337   HYDROData_Iterator anIterator( aDocument, KIND_ZONE );
338   for( ; anIterator.More(); anIterator.Next() )
339     aZoneList.Append( anIterator.Current() );
340
341   HYDROGUI_SplitZonesTool::SplitDataList aSplitDataList =
342     HYDROGUI_SplitZonesTool::SplitZones( aZoneList, NULL );
343
344   int anIndex = 0;
345   HYDROGUI_SplitZonesTool::SplitDataListIterator anIter( aSplitDataList );
346   while( anIter.hasNext() )
347   {
348     const HYDROGUI_SplitZonesTool::SplitData& aSplitData = anIter.next();
349     const QPainterPath& aPath = aSplitData.Path;
350     const QStringList& aZoneNames = aSplitData.ZoneNames;
351
352     Handle(HYDROData_Polyline) aPolyline =
353       Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
354     if( !aPolyline.IsNull() )
355     {
356       aPolyline->SetName( QString( "SplitPolyline_%1" ).arg( ++anIndex ) );
357       aPolyline->setDimension( 2 );
358
359       QList<PolylineSection> aPolylineData;
360       for( int i = 0, n = aPath.elementCount(); i < n; i++ )
361       {
362         const QPainterPath::Element anElement = aPath.elementAt( i );
363         switch( anElement.type )
364         {
365           case QPainterPath::MoveToElement:
366             aPolylineData.append( PolylineSection() );
367             break;
368           case QPainterPath::LineToElement:
369             if( !aPolylineData.isEmpty() )
370             {
371               PolylineSection& aSection = aPolylineData.last();
372               aSection.myCoords << anElement.x;
373               aSection.myCoords << anElement.y;
374             }
375             break;
376           case QPainterPath::CurveToElement: // currently not supported
377           default:
378             break;
379         }
380       }
381       aPolyline->setPolylineData( aPolylineData );
382     }
383
384     Handle(HYDROData_Zone) aZone =
385       Handle(HYDROData_Zone)::DownCast( aDocument->CreateObject( KIND_ZONE ) );
386     if( !aZone.IsNull() )
387     {
388       aZone->SetName( QString( "SplitZone_%1" ).arg( anIndex ) );
389       aZone->SetPolyline( aPolyline );
390
391       aZone->SetBorderColor( Qt::black );
392
393       int aCounter = 0;
394       int aR = 0, aG = 0, aB = 0;
395       QStringListIterator aZoneNameIter( aZoneNames );
396       while( aZoneNameIter.hasNext() )
397       {
398         const QString& aZoneName = aZoneNameIter.next();
399         Handle(HYDROData_Zone) aRefZone = Handle(HYDROData_Zone)::DownCast(
400           HYDROGUI_Tool::FindObjectByName( this, aZoneName, KIND_ZONE ) );
401         if( !aRefZone.IsNull() )
402         {
403           QColor aRefColor = aRefZone->GetFillingColor();
404           aR += aRefColor.red();
405           aG += aRefColor.green();
406           aB += aRefColor.blue();
407           aCounter++;
408         }
409       }
410       if( aCounter > 0 )
411       {
412         QColor aFillingColor( aR / aCounter, aG / aCounter, aB / aCounter );
413         aZone->SetFillingColor( aFillingColor );
414       }
415     }
416   }
417
418   aDocument->CommitOperation();
419   update( UF_Model );
420 }