Salome HOME
Dump Image data to python script (Feature #13).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportImageOp.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_ImportImageOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_ImportImageDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_PrsImage.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
31
32 #include <HYDROData_Iterator.h>
33
34 #include <HYDROOperations_Factory.h>
35
36 #include <GraphicsView_ViewManager.h>
37 #include <GraphicsView_ViewPort.h>
38 #include <GraphicsView_Viewer.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
42
43 HYDROGUI_ImportImageOp::HYDROGUI_ImportImageOp( HYDROGUI_Module* theModule,
44                                                 const bool theIsEdit )
45 : HYDROGUI_Operation( theModule ),
46   myIsEdit( theIsEdit ),
47   myEditedObject( 0 ),
48   myActiveViewManager( 0 ),
49   myPreviewViewManager( 0 ),
50   myPreviewPrs( 0 ),
51   myRefPreviewPrs( 0 ),
52   myPointType( HYDROGUI_PrsImage::None )
53 {
54   setName( theIsEdit ? tr( "EDIT_IMPORTED_IMAGE" ) : tr( "IMPORT_IMAGE" ) );
55 }
56
57 HYDROGUI_ImportImageOp::~HYDROGUI_ImportImageOp()
58 {
59 }
60
61 void HYDROGUI_ImportImageOp::startOperation()
62 {
63   HYDROGUI_Operation::startOperation();
64
65   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
66   aPanel->reset();
67   aPanel->setIsEdit( myIsEdit );
68
69   if( myIsEdit )
70   {
71     myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
72     if( !myEditedObject.IsNull() )
73     {
74       QImage anImage = myEditedObject->Image();
75
76       QPoint aPointA1, aPointB1, aPointC1;
77       QPointF aPointA2, aPointB2, aPointC2;
78       myEditedObject->TrsfPoints( aPointA1, aPointB1, aPointC1,
79                                   aPointA2, aPointB2, aPointC2 );
80
81       onCreatePreview( anImage );
82
83       if( myPreviewPrs )
84       {
85         HYDROGUI_PrsImage::TransformationPointMap aPointMap =
86           myPreviewPrs->getTransformationPointMap();
87         if( !aPointMap.isEmpty() )
88         {
89           aPointMap[ HYDROGUI_PrsImage::PointA ].Point = aPointA1;
90           aPointMap[ HYDROGUI_PrsImage::PointB ].Point = aPointB1;
91           aPointMap[ HYDROGUI_PrsImage::PointC ].Point = aPointC1;
92           myPreviewPrs->setTransformationPointMap( aPointMap );
93         }
94       }
95
96       HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
97       aDataMap[ HYDROGUI_PrsImage::PointA ] =
98         HYDROGUI_ImportImageDlg::TransformationData( aPointA1, aPointA2 );
99       aDataMap[ HYDROGUI_PrsImage::PointB ] =
100         HYDROGUI_ImportImageDlg::TransformationData( aPointB1, aPointB2 );
101       aDataMap[ HYDROGUI_PrsImage::PointC ] =
102         HYDROGUI_ImportImageDlg::TransformationData( aPointC1, aPointC2 );
103       ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap );
104     }
105   }
106
107   // collect information about existing images
108   HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
109   HYDROData_Iterator anIterator( doc(), KIND_IMAGE );
110   for( ; anIterator.More(); anIterator.Next() )
111   {
112     Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
113     if( !anImageObj.IsNull() )
114     {
115       if( myIsEdit && HYDROGUI_Tool::IsEqual( anImageObj, myEditedObject ) )
116         continue;
117
118       QPoint aPointA1, aPointB1, aPointC1;
119       QPointF aPointA2, aPointB2, aPointC2;
120       anImageObj->TrsfPoints( aPointA1, aPointB1, aPointC1,
121                               aPointA2, aPointB2, aPointC2 );
122
123       HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
124       aDataMap[ HYDROGUI_PrsImage::PointA ] =
125         HYDROGUI_ImportImageDlg::TransformationData( aPointA1, aPointA2 );
126       aDataMap[ HYDROGUI_PrsImage::PointB ] =
127         HYDROGUI_ImportImageDlg::TransformationData( aPointB1, aPointB2 );
128       aDataMap[ HYDROGUI_PrsImage::PointC ] =
129         HYDROGUI_ImportImageDlg::TransformationData( aPointC1, aPointC2 );
130
131       HYDROGUI_ImportImageDlg::PrsPointData aPrsPointData( anImageObj->GetName(), aDataMap );
132       aPrsPointDataList.append( aPrsPointData );
133     }
134   }
135   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setPrsPointDataList( aPrsPointDataList );
136 }
137
138 void HYDROGUI_ImportImageOp::abortOperation()
139 {
140   closePreview();
141
142   HYDROGUI_Operation::abortOperation();
143 }
144
145 void HYDROGUI_ImportImageOp::commitOperation()
146 {
147   closePreview();
148
149   HYDROGUI_Operation::commitOperation();
150 }
151
152 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
153 {
154   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
155   connect( aPanel, SIGNAL( createPreview( QImage ) ),
156            this, SLOT( onCreatePreview( QImage ) ) );
157   connect( aPanel, SIGNAL( activatePointSelection( int ) ),
158            this, SLOT( onActivatePointSelection( int ) ) );
159   connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ),
160            this, SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
161   connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
162            this, SLOT( onRefImageActivated( const QString& ) ) );
163   return aPanel;
164 }
165
166 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
167                                            QString& theErrorMsg )
168 {
169   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
170
171   QString anImageName = aPanel->getImageName();
172   if( anImageName.isEmpty() )
173     return false;
174
175   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
176   {
177     // check that there are no other objects with the same name in the document
178     Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
179     if( !anObject.IsNull() )
180     {
181       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
182       return false;
183     }
184   }
185
186   if( !myPreviewPrs )
187     return false;
188
189   QImage anImage = myPreviewPrs->getImage();
190
191   bool anIsRefImage = aPanel->getTransformationMode() == HYDROGUI_ImportImageDlg::RefImage;
192
193   QTransform aRefTransform;
194   if( anIsRefImage && myRefPreviewPrs )
195   {
196     Handle(HYDROData_Image) aRefImageObj =
197       Handle(HYDROData_Image)::DownCast( myRefPreviewPrs->getObject() );
198     if( !aRefImageObj.IsNull() )
199       aRefTransform = aRefImageObj->Trsf();
200   }
201
202   HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
203   if( !aPanel->getTransformationDataMap( aMap ) )
204     return false;
205
206   HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
207   if( anIsRefImage && !aPanel->getTransformationDataMap( aRefMap, true ) )
208     return false;
209
210   QPoint aPointA1 = aMap[ HYDROGUI_PrsImage::PointA ].first;
211   QPoint aPointB1 = aMap[ HYDROGUI_PrsImage::PointB ].first;
212   QPoint aPointC1 = aMap[ HYDROGUI_PrsImage::PointC ].first;
213
214   QPointF aPointA2 = aMap[ HYDROGUI_PrsImage::PointA ].second;
215   QPointF aPointB2 = aMap[ HYDROGUI_PrsImage::PointB ].second;
216   QPointF aPointC2 = aMap[ HYDROGUI_PrsImage::PointC ].second;
217
218   QPoint aPointA3, aPointB3, aPointC3;
219   if( anIsRefImage )
220   {
221     aPointA3 = aRefMap[ HYDROGUI_PrsImage::PointA ].first;
222     aPointB3 = aRefMap[ HYDROGUI_PrsImage::PointB ].first;
223     aPointC3 = aRefMap[ HYDROGUI_PrsImage::PointC ].first;
224   }
225
226   int xa1 = aPointA1.x();
227   int ya1 = aPointA1.y();
228   int xb1 = aPointB1.x();
229   int yb1 = aPointB1.y();
230   int xc1 = aPointC1.x();
231   int yc1 = aPointC1.y();
232
233   double xa2 = aPointA2.x();
234   double ya2 = aPointA2.y();
235   double xb2 = aPointB2.x();
236   double yb2 = aPointB2.y();
237   double xc2 = aPointC2.x();
238   double yc2 = aPointC2.y();
239
240   int xa3 = aPointA3.x();
241   int ya3 = aPointA3.y();
242   int xb3 = aPointB3.x();
243   int yb3 = aPointB3.y();
244   int xc3 = aPointC3.x();
245   int yc3 = aPointC3.y();
246
247   // first, check that three input points don't belong to a single line
248   if( ( yb1 - ya1 ) * ( xc1 - xa1 ) == ( yc1 - ya1 ) * ( xb1 - xa1 ) )
249   {
250     theErrorMsg = tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" );
251     return false;
252   }
253
254   // the same check for the reference points
255   if( anIsRefImage && ( ( yb3 - ya3 ) * ( xc3 - xa3 ) == ( yc3 - ya3 ) * ( xb3 - xa3 ) ) )
256   {
257     theErrorMsg = tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" );
258     return false;
259   }
260
261   QTransform aTransform1( xa1, ya1, 1, xb1, yb1, 1, xc1, yc1, 1 );
262   QTransform aTransform2( xa2, ya2, 1, xb2, yb2, 1, xc2, yc2, 1 );
263   QTransform aTransform3( xa3, ya3, 1, xb3, yb3, 1, xc3, yc3, 1 );
264
265   bool anIsInvertible = false;
266   QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
267   if( !anIsInvertible )
268   {
269     theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
270     return false;
271   }
272
273   QTransform aTransform;
274   if( anIsRefImage )
275     aTransform = aTransform1Inverted * aTransform3 * aRefTransform;
276   else
277     aTransform = aTransform1Inverted * aTransform2;
278
279   Handle(HYDROData_Image) anImageObj;
280   if( myIsEdit )
281     anImageObj = myEditedObject;
282   else
283     anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
284
285   if( anImageObj.IsNull() )
286     return false;
287
288   anImageObj->SetName( anImageName );
289
290   anImageObj->SetImage( anImage );
291   anImageObj->SetTrsf( aTransform );
292
293   if( anIsRefImage )
294   {
295     aPointA2 = QPointF( aTransform.map( aPointA1 ) );
296     aPointB2 = QPointF( aTransform.map( aPointB1 ) );
297     aPointC2 = QPointF( aTransform.map( aPointC1 ) );
298   }
299
300   anImageObj->SetTrsfPoints( aPointA1, aPointB1, aPointC1,
301                              aPointA2, aPointB2, aPointC2 );
302
303   if( !myIsEdit )
304   {
305     // Set imported file name for image
306     QString aFilePath = aPanel->getFileName();
307     anImageObj->SetFilePath( aFilePath );
308   }
309    
310   // must be done after all checks and before calling SetVisible() method below
311   closePreview();
312
313   if( !myIsEdit )
314     module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
315
316   if( myIsEdit )
317     if( HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory() )
318       aFactory->UpdateImage( doc(), anImageObj );
319
320   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced;
321   return true;
322 }
323
324 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
325 {
326   LightApp_Application* anApp = module()->getApp();
327
328   myActiveViewManager = anApp->activeViewManager();
329
330   myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
331   myPreviewPrs->setImage( theImage );
332   myPreviewPrs->compute();
333
334   myPreviewViewManager =
335     dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
336   if( myPreviewViewManager )
337   {
338     module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
339     myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
340     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
341     {
342       if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
343       {
344         //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
345
346         aViewPort->addItem( myPreviewPrs );
347         aViewPort->fitAll();
348
349         myPreviewPrs->setIsTransformationPointPreview( true );
350       }
351       connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
352                this, SLOT( onPointSelected() ) );
353     }
354   }
355
356   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
357
358   QString anImageName;
359   if( myIsEdit )
360   {
361     if( !myEditedObject.IsNull() )
362       anImageName = myEditedObject->GetName();
363   }
364   else
365     anImageName = HYDROGUI_Tool::GenerateObjectName( module(), "Image" );
366   aPanel->setImageName( anImageName );
367
368   aPanel->setImageSize( theImage.size() );
369
370   aPanel->initializePointSelection();
371   onPointSelected( false );
372 }
373
374 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
375 {
376   myPointType = thePointType;
377   if( myPreviewPrs )
378     myPreviewPrs->setTransformationPointType( thePointType );
379   if( myRefPreviewPrs )
380     myRefPreviewPrs->setTransformationPointType( thePointType );
381 }
382
383 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
384                                                   int thePointType,
385                                                   bool theIsY,
386                                                   int theValue )
387 {
388   if( !theIsRef && myPreviewPrs )
389     myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
390   else if( theIsRef && myRefPreviewPrs )
391     myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
392 }
393
394 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
395 {
396   GraphicsView_ViewPort* aViewPort = 0;
397   if( myPreviewViewManager )
398     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
399       aViewPort = aViewer->getActiveViewPort();
400
401   if( !aViewPort )
402     return;
403
404   if( myPreviewPrs )
405     myPreviewPrs->setCaption( QString() );
406
407   if( myRefPreviewPrs )
408   {
409     myRefPreviewPrs->setCaption( QString() );
410     aViewPort->removeItem( myRefPreviewPrs );
411
412     delete myRefPreviewPrs;
413     myRefPreviewPrs = 0;
414   }
415
416   QImage anImage;
417   Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
418     HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
419   if( !anImageObj.IsNull() )
420   {
421     anImage = anImageObj->Image();
422
423     myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
424     myRefPreviewPrs->setImage( anImage );
425     myRefPreviewPrs->compute();
426
427     myRefPreviewPrs->setIsTransformationPointPreview( true );
428     myRefPreviewPrs->setTransformationPointType( myPointType );
429
430     // vertically shift the reference prs relatively to the main prs
431     if( myPreviewPrs )
432     {
433       myPreviewPrs->setCaption( tr( "IMPORTED_IMAGE" ) );
434
435       QImage anImage = myPreviewPrs->getImage();
436       myRefPreviewPrs->moveBy( 0, anImage.height() + 60 );
437       myRefPreviewPrs->setCaption( tr( "REFERENCE_IMAGE" ) );
438     }
439
440     aViewPort->addItem( myRefPreviewPrs );
441   }
442
443   aViewPort->fitAll();
444
445   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
446
447   aPanel->setImageSize( anImage.size(), true );
448
449   aPanel->initializePointSelection();
450   onPointSelected( true );
451 }
452
453 void HYDROGUI_ImportImageOp::onPointSelected()
454 {
455   onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
456 }
457
458 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
459 {
460   HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
461   if( !aPrs )
462     return;
463
464   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
465
466   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
467     aPrs->getTransformationPointMap();
468   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
469   while( anIter.hasNext() )
470   {
471     int aPointType = anIter.next().key();
472     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
473     const QPoint& aPoint = aTransformationPoint.Point;
474
475     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF() );
476     aDataMap[ aPointType ] = aData;
477   }
478
479   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
480 }
481
482 void HYDROGUI_ImportImageOp::closePreview()
483 {
484   if( myPreviewPrs )
485   {
486     delete myPreviewPrs;
487     myPreviewPrs = 0;
488   }
489
490   if( myRefPreviewPrs )
491   {
492     delete myRefPreviewPrs;
493     myRefPreviewPrs = 0;
494   }
495
496   if( myPreviewViewManager )
497   {
498     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
499     myPreviewViewManager = 0;
500   }
501
502   if( myActiveViewManager )
503     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
504 }