Salome HOME
Fixed bug for the case when the imported image is changed by choosing another file.
[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   bool anIsRefImage = aPanel->getTransformationMode() == HYDROGUI_ImportImageDlg::RefImage;
187
188   HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
189   if( !aPanel->getTransformationDataMap( aMap ) )
190     return false;
191
192   HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
193   if( anIsRefImage && !aPanel->getTransformationDataMap( aRefMap, true ) )
194     return false;
195
196   QPoint aPointA1 = aMap[ HYDROGUI_PrsImage::PointA ].first;
197   QPoint aPointB1 = aMap[ HYDROGUI_PrsImage::PointB ].first;
198   QPoint aPointC1 = aMap[ HYDROGUI_PrsImage::PointC ].first;
199
200   QPointF aPointA2 = aMap[ HYDROGUI_PrsImage::PointA ].second;
201   QPointF aPointB2 = aMap[ HYDROGUI_PrsImage::PointB ].second;
202   QPointF aPointC2 = aMap[ HYDROGUI_PrsImage::PointC ].second;
203
204   QPoint aPointA3, aPointB3, aPointC3;
205   if( anIsRefImage )
206   {
207     aPointA3 = aRefMap[ HYDROGUI_PrsImage::PointA ].first;
208     aPointB3 = aRefMap[ HYDROGUI_PrsImage::PointB ].first;
209     aPointC3 = aRefMap[ HYDROGUI_PrsImage::PointC ].first;
210   }
211
212   int xa1 = aPointA1.x();
213   int ya1 = aPointA1.y();
214   int xb1 = aPointB1.x();
215   int yb1 = aPointB1.y();
216   int xc1 = aPointC1.x();
217   int yc1 = aPointC1.y();
218
219   double xa2 = aPointA2.x();
220   double ya2 = aPointA2.y();
221   double xb2 = aPointB2.x();
222   double yb2 = aPointB2.y();
223   double xc2 = aPointC2.x();
224   double yc2 = aPointC2.y();
225
226   int xa3 = aPointA3.x();
227   int ya3 = aPointA3.y();
228   int xb3 = aPointB3.x();
229   int yb3 = aPointB3.y();
230   int xc3 = aPointC3.x();
231   int yc3 = aPointC3.y();
232
233   // first, check that three input points don't belong to a single line
234   if( ( yb1 - ya1 ) * ( xc1 - xa1 ) == ( yc1 - ya1 ) * ( xb1 - xa1 ) )
235   {
236     theErrorMsg = tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" );
237     return false;
238   }
239
240   // the same check for the reference points
241   if( anIsRefImage && ( ( yb3 - ya3 ) * ( xc3 - xa3 ) == ( yc3 - ya3 ) * ( xb3 - xa3 ) ) )
242   {
243     theErrorMsg = tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" );
244     return false;
245   }
246
247   QTransform aTransform1( xa1, ya1, 1, xb1, yb1, 1, xc1, yc1, 1 );
248   QTransform aTransform2( xa2, ya2, 1, xb2, yb2, 1, xc2, yc2, 1 );
249   QTransform aTransform3( xa3, ya3, 1, xb3, yb3, 1, xc3, yc3, 1 );
250
251   bool anIsInvertible = false;
252   QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
253   if( !anIsInvertible )
254   {
255     theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
256     return false;
257   }
258
259   QTransform aTransform;
260   if( anIsRefImage )
261     aTransform = aTransform1Inverted * aTransform3 * myRefTransform;
262   else
263     aTransform = aTransform1Inverted * aTransform2;
264
265   Handle(HYDROData_Image) anImageObj;
266   if( myIsEdit )
267     anImageObj = myEditedObject;
268   else
269     anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
270
271   if( anImageObj.IsNull() )
272     return false;
273
274   anImageObj->SetName( anImageName );
275
276   anImageObj->SetImage( myImage );
277   anImageObj->SetTrsf( aTransform );
278
279   if( anIsRefImage )
280   {
281     aPointA2 = QPointF( aTransform.map( aPointA1 ) );
282     aPointB2 = QPointF( aTransform.map( aPointB1 ) );
283     aPointC2 = QPointF( aTransform.map( aPointC1 ) );
284   }
285
286   anImageObj->SetTrsfPoints( aPointA1, aPointB1, aPointC1,
287                              aPointA2, aPointB2, aPointC2 );
288
289   if( !myIsEdit )
290   {
291     // Set imported file name for image
292     QString aFilePath = aPanel->getFileName();
293     anImageObj->SetFilePath( aFilePath );
294   }
295    
296   // must be done after all checks and before calling SetVisible() method below
297   closePreview();
298
299   if( !myIsEdit )
300     module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
301
302   if( myIsEdit )
303     anImageObj->Update();
304
305   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced;
306   return true;
307 }
308
309 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
310 {
311   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
312
313   myImage = theImage;
314
315   if( myPreviewPrs ) // if the image is changed by choosing another file
316   {
317     myPreviewPrs->setImage( myImage );
318     myPreviewPrs->compute();
319
320     if( myPreviewViewManager )
321     {
322       if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
323       {
324         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
325         {
326           aViewPort->onBoundingRectChanged();
327           aViewPort->fitAll();
328         }
329       }
330     }
331   }
332   else
333   {
334     LightApp_Application* anApp = module()->getApp();
335
336     myActiveViewManager = anApp->activeViewManager();
337
338     myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
339     myPreviewPrs->setImage( myImage );
340     myPreviewPrs->compute();
341
342     myPreviewViewManager =
343       dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
344     if( myPreviewViewManager )
345     {
346       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
347                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
348
349       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
350       myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
351       if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
352       {
353         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
354         {
355           //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
356
357           aViewPort->addItem( myPreviewPrs );
358           aViewPort->fitAll();
359
360           myPreviewPrs->setIsTransformationPointPreview( true );
361         }
362         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
363                  this, SLOT( onPointSelected() ) );
364       }
365     }
366
367     QString anImageName;
368     if( myIsEdit )
369     {
370       if( !myEditedObject.IsNull() )
371         anImageName = myEditedObject->GetName();
372     }
373     else
374       anImageName = HYDROGUI_Tool::GenerateObjectName( module(), "Image" );
375     aPanel->setImageName( anImageName );
376   }
377
378   aPanel->setImageSize( myImage.size() );
379
380   aPanel->initializePointSelection();
381   onPointSelected( false );
382 }
383
384 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
385 {
386   myPointType = thePointType;
387   if( myPreviewPrs )
388     myPreviewPrs->setTransformationPointType( thePointType );
389   if( myRefPreviewPrs )
390     myRefPreviewPrs->setTransformationPointType( thePointType );
391 }
392
393 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
394                                                   int thePointType,
395                                                   bool theIsY,
396                                                   int theValue )
397 {
398   if( !theIsRef && myPreviewPrs )
399     myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
400   else if( theIsRef && myRefPreviewPrs )
401     myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
402 }
403
404 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
405 {
406   myRefTransform.reset();
407
408   GraphicsView_ViewPort* aViewPort = 0;
409   if( myPreviewViewManager )
410     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
411       aViewPort = aViewer->getActiveViewPort();
412
413   if( !aViewPort )
414     return;
415
416   if( myPreviewPrs )
417     myPreviewPrs->setCaption( QString() );
418
419   if( myRefPreviewPrs )
420   {
421     myRefPreviewPrs->setCaption( QString() );
422     aViewPort->removeItem( myRefPreviewPrs );
423
424     delete myRefPreviewPrs;
425     myRefPreviewPrs = 0;
426   }
427
428   QImage anImage;
429   Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
430     HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
431   if( !anImageObj.IsNull() )
432   {
433     anImage = anImageObj->Image();
434     myRefTransform = anImageObj->Trsf();
435
436     myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
437     myRefPreviewPrs->setImage( anImage );
438     myRefPreviewPrs->compute();
439
440     myRefPreviewPrs->setIsTransformationPointPreview( true );
441     myRefPreviewPrs->setTransformationPointType( myPointType );
442
443     // vertically shift the reference prs relatively to the main prs
444     if( myPreviewPrs )
445     {
446       myPreviewPrs->setCaption( tr( "IMPORTED_IMAGE" ) );
447
448       QImage anImage = myPreviewPrs->getImage();
449       myRefPreviewPrs->moveBy( 0, anImage.height() + 60 );
450       myRefPreviewPrs->setCaption( tr( "REFERENCE_IMAGE" ) );
451     }
452
453     aViewPort->addItem( myRefPreviewPrs );
454   }
455
456   aViewPort->fitAll();
457
458   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
459
460   aPanel->setImageSize( anImage.size(), true );
461
462   aPanel->initializePointSelection();
463   onPointSelected( true );
464 }
465
466 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
467 {
468   closePreview();
469 }
470
471 void HYDROGUI_ImportImageOp::onPointSelected()
472 {
473   onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
474 }
475
476 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
477 {
478   HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
479   if( !aPrs )
480     return;
481
482   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
483
484   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
485     aPrs->getTransformationPointMap();
486   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
487   while( anIter.hasNext() )
488   {
489     int aPointType = anIter.next().key();
490     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
491     const QPoint& aPoint = aTransformationPoint.Point;
492
493     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF() );
494     aDataMap[ aPointType ] = aData;
495   }
496
497   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
498 }
499
500 void HYDROGUI_ImportImageOp::closePreview()
501 {
502   // It's very strange, but without calling this method (it's quite safe) a crash is stably reproduced.
503   // Scenario: create any non-Graphics view, activate import op, click apply.
504   // Result: a few SIGSEGVs coming from processEvents(), then crash.
505   if( myPreviewViewManager )
506     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
507       if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
508         aViewPort->onBoundingRectChanged();
509
510   if( myPreviewPrs )
511   {
512     delete myPreviewPrs;
513     myPreviewPrs = 0;
514   }
515
516   if( myRefPreviewPrs )
517   {
518     delete myRefPreviewPrs;
519     myRefPreviewPrs = 0;
520   }
521
522   if( myPreviewViewManager )
523   {
524     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
525                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
526
527     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
528     myPreviewViewManager = 0;
529   }
530
531   if( myActiveViewManager )
532     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
533 }