Salome HOME
Fix for the bug #40: Fatal error when edit image.
[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 #include <HYDROData_Tool.h>
34
35 #include <GraphicsView_ViewManager.h>
36 #include <GraphicsView_ViewPort.h>
37 #include <GraphicsView_Viewer.h>
38
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
41
42 #include <STD_TabDesktop.h>
43 #include <SUIT_Desktop.h>
44 #include <QtxWorkstack.h>
45 #include <QApplication>
46
47 HYDROGUI_ImportImageOp::HYDROGUI_ImportImageOp( HYDROGUI_Module* theModule,
48                                                 const bool theIsEdit )
49 : HYDROGUI_Operation( theModule ),
50   myIsEdit( theIsEdit ),
51   myEditedObject( 0 ),
52   myActiveViewManager( 0 ),
53   myPreviewViewManager( 0 ),
54   myRefViewManager( 0 ),
55   myPreviewPrs( 0 ),
56   myRefPreviewPrs( 0 ),
57   myPointType( HYDROGUI_PrsImage::None )
58 {
59   setName( theIsEdit ? tr( "EDIT_IMPORTED_IMAGE" ) : tr( "IMPORT_IMAGE" ) );
60 }
61
62 HYDROGUI_ImportImageOp::~HYDROGUI_ImportImageOp()
63 {
64 }
65
66 void HYDROGUI_ImportImageOp::startOperation()
67 {
68   HYDROGUI_Operation::startOperation();
69
70   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
71   aPanel->reset();
72   aPanel->setIsEdit( myIsEdit );
73
74   QString aRefImageName;
75
76   if( myIsEdit )
77   {
78     myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
79     if( !myEditedObject.IsNull() )
80     {
81       QImage anImage = myEditedObject->Image();
82       bool anIsByTwoPoints = myEditedObject->IsByTwoPoints();
83
84       QPoint aLocalPointA, aLocalPointB, aLocalPointC;
85       myEditedObject->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
86
87       // Create the edited image preview presentation in the viewer
88       onCreatePreview( anImage );
89
90       // Set transformation local points A,B,C to the image preview presentation
91       setPresentationTrsfPoints( myPreviewPrs, anIsByTwoPoints, aLocalPointA, 
92         aLocalPointB, aLocalPointC );
93
94       // Build the existing image local and global points mapping 
95       // according to the current transformation mode.
96       HYDROData_Image::TransformationMode aTrsfMode = myEditedObject->GetTrsfMode();
97       QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
98       HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
99       if ( aTrsfMode == HYDROData_Image::ReferenceImage )
100       {
101         // Compute global points using the transformation matrix of the reference image
102         Handle(HYDROData_Image) aRefImage = myEditedObject->GetTrsfReferenceImage();
103         if ( !aRefImage.IsNull() )
104         {
105           QTransform aRefTrsf = aRefImage->Trsf(); // The reference image transformation matrix
106           aTrsfPointA = aRefTrsf.map( aLocalPointA ); // Compute the global point A
107           aTrsfPointB = aRefTrsf.map( aLocalPointB ); // Compute the global point B
108           if ( !anIsByTwoPoints )
109           {
110             aTrsfPointC = aRefTrsf.map( aLocalPointC ); // Compute the global point C if used
111           }
112           // Build the local-global points map
113           // Use the reference image transformation mode for interpreting global points
114           computeTrsfData( aRefImage->GetTrsfMode(), anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
115             aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
116         }
117       }
118       else
119       {
120         // Get global points from the edited image
121         myEditedObject->GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC );
122         // Build the local-global points map
123         computeTrsfData( aTrsfMode, anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
124           aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
125       }
126
127       // Initialize the dialog mode, local and global coordinates 
128       // except coordinates on the reference image
129       aPanel->setTransformationMode( aTrsfMode );
130       aPanel->setTransformationDataMap( aDataMap );
131       aPanel->setByTwoPoints( anIsByTwoPoints );
132
133       // Set points of the reference image
134       if ( aTrsfMode == HYDROData_Image::ReferenceImage )
135       {
136         Handle(HYDROData_Image) aRefImage;
137         myEditedObject->GetReferencePoints( aRefImage,
138                                          aTrsfPointA, aTrsfPointB, aTrsfPointC );
139         if ( !aRefImage.IsNull() )
140         {
141           aRefImageName = aRefImage->GetName();
142
143           // Create the reference image presentation in the viewer
144           onRefImageActivated( aRefImageName );
145
146           // Set transformation points A,B,C to the reference image presentation
147           setPresentationTrsfPoints( myRefPreviewPrs, anIsByTwoPoints, aTrsfPointA.toPoint(), 
148             aTrsfPointB.toPoint(), aTrsfPointC.toPoint() );
149
150           // Prepare A, B, C points on the reference image
151           HYDROGUI_ImportImageDlg::TransformationDataMap aRefDataMap;
152           aRefDataMap[ HYDROGUI_PrsImage::PointA ] = 
153             HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointA.toPoint() );
154           aRefDataMap[ HYDROGUI_PrsImage::PointB ] = 
155             HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointB.toPoint() );
156           if ( !anIsByTwoPoints )
157             aRefDataMap[ HYDROGUI_PrsImage::PointC ] = 
158             HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointC.toPoint() );
159
160           // Initialize the dialog's A, B, C points coordinates of the reference image
161           aPanel->setTransformationDataMap( aRefDataMap, true, true );
162         }
163       }
164     }
165   }
166
167   // Collect information about existing images and initialize the combobox 
168   // reference image selector in the dialog.
169   HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
170   HYDROData_Iterator anIterator( doc(), KIND_IMAGE );
171   for( ; anIterator.More(); anIterator.Next() )
172   {
173     Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
174     if( !anImageObj.IsNull() )
175     {
176       if( myIsEdit && IsEqual( anImageObj, myEditedObject ) )
177         continue;
178
179       QPoint aLocalPointA, aLocalPointB, aLocalPointC;
180       anImageObj->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
181       
182       HYDROData_Image::TransformationMode aImgTrsfMode;
183       QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
184       anImageObj->GetGlobalPoints( aImgTrsfMode,
185                                    aTrsfPointA, aTrsfPointB, aTrsfPointC );
186
187       HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
188       computeTrsfData( aImgTrsfMode, false, aLocalPointA, aLocalPointB, aLocalPointC,
189         aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
190
191       HYDROGUI_ImportImageDlg::PrsPointData aPrsPointData( anImageObj->GetName(), aDataMap );
192       aPrsPointDataList.append( aPrsPointData );
193     }
194   }
195
196   // Initialize the combobox in the dialog with the list of available reference images
197   aPanel->setPrsPointDataList( aPrsPointDataList );
198   // Select the current reference image in the dialog combobox
199   aPanel->setRefImageName( aRefImageName );
200 }
201
202 void HYDROGUI_ImportImageOp::setPresentationTrsfPoints( HYDROGUI_PrsImage* thePrs, 
203                                                         bool theIsByTwoPoints,
204                                                         const QPoint theLocalPointA, 
205                                                         const QPoint theLocalPointB, 
206                                                         const QPoint theLocalPointC )
207 {
208   // Set transformation points A,B,C to the image presentation
209   if( thePrs )
210   {
211     HYDROGUI_PrsImage::TransformationPointMap aPointMap =
212       thePrs->getTransformationPointMap();
213     if( !aPointMap.isEmpty() )
214     {
215       aPointMap[ HYDROGUI_PrsImage::PointA ].Point = theLocalPointA;
216       aPointMap[ HYDROGUI_PrsImage::PointB ].Point = theLocalPointB;
217       if ( !theIsByTwoPoints )
218         aPointMap[ HYDROGUI_PrsImage::PointC ].Point = theLocalPointC;
219
220       thePrs->setTransformationPointMap( aPointMap );
221     }
222   }
223 }
224
225 void HYDROGUI_ImportImageOp::computeTrsfData( HYDROData_Image::TransformationMode theTrsfMode,
226                                               bool theIsByTwoPoints,
227                                               const QPoint& theLocalPointA,
228                                               const QPoint& theLocalPointB,
229                                               const QPoint& theLocalPointC,
230                                               const QPointF& theGlobalPointA,
231                                               const QPointF& theGlobalPointB,
232                                               const QPointF& theGlobalPointC,
233                                               HYDROGUI_ImportImageDlg::TransformationDataMap& theDataMap ) const
234 {
235   // Build the local-global points map
236   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
237   theDataMap[ HYDROGUI_PrsImage::PointA ] = 
238     HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointA, theGlobalPointA );
239   theDataMap[ HYDROGUI_PrsImage::PointB ] =
240     HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointB, theGlobalPointB );
241   if ( !theIsByTwoPoints )
242   {
243     theDataMap[ HYDROGUI_PrsImage::PointC ] =
244       HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointC, theGlobalPointC );
245   }
246 }
247
248 void HYDROGUI_ImportImageOp::abortOperation()
249 {
250   closePreview();
251
252   HYDROGUI_Operation::abortOperation();
253 }
254
255 void HYDROGUI_ImportImageOp::commitOperation()
256 {
257   closePreview();
258
259   HYDROGUI_Operation::commitOperation();
260 }
261
262 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
263 {
264   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
265   connect( aPanel, SIGNAL( createPreview( QImage ) ),
266            this, SLOT( onCreatePreview( QImage ) ) );
267   connect( aPanel, SIGNAL( activatePointSelection( int ) ),
268            this, SLOT( onActivatePointSelection( int ) ) );
269   connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ),
270            this, SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
271   connect( aPanel, SIGNAL( modeActivated( int ) ),
272            this, SLOT( onModeActivated( int ) ) );
273   connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
274            this, SLOT( onRefImageActivated( const QString& ) ) );
275   return aPanel;
276 }
277
278 bool HYDROGUI_ImportImageOp::checkPoints( const QPointF& thePointA,
279                                           const QPointF& thePointB,
280                                           const QPointF& thePointC,
281                                           const bool     theIsByTwoPoints,
282                                           const QString& theLineErrMsg,
283                                           const QString& thePoinErrMsg,
284                                           QString&       theErrorMsg,
285                                           const bool     theIsToCheckInvertibles ) const
286 {
287   double xa = thePointA.x();
288   double ya = thePointA.y();
289   double xb = thePointB.x();
290   double yb = thePointB.y();
291   double xc = thePointC.x();
292   double yc = thePointC.y();
293
294   if ( !theIsByTwoPoints )
295   {
296     // check that three input points don't belong to a single line
297     if ( ValuesEquals( ( yb - ya ) * ( xc - xa ), ( yc - ya ) * ( xb - xa ) ) )
298     {
299       theErrorMsg = theLineErrMsg;
300       return false;
301     }
302   }
303   else 
304   {
305     // check that two points are not identical
306     if ( ValuesEquals( xa, ya ) && ValuesEquals( xb, yb ) )
307     {
308       theErrorMsg = thePoinErrMsg;
309       return false;
310     }
311   }
312
313   if ( theIsToCheckInvertibles )
314   {
315     QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
316
317     bool anIsInvertible = false;
318     QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
319     if( !anIsInvertible )
320     {
321       theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
322       return false;
323     }
324   }
325
326   return true;
327 }
328
329 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
330                                            QString& theErrorMsg )
331 {
332   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
333
334   QString anImageName = aPanel->getImageName();
335   if( anImageName.isEmpty() )
336     return false;
337
338   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
339   {
340     // check that there are no other objects with the same name in the document
341     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
342     if( !anObject.IsNull() )
343     {
344       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
345       return false;
346     }
347   }
348
349   HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
350   if( !aPanel->getTransformationDataMap( aMap ) )
351     return false;
352
353   bool anIsByTwoPoints = aPanel->isByTwoPoints();
354
355   QPoint aPointA = aMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
356   QPoint aPointB = aMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
357   QPoint aPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) :
358                                      aMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
359
360   // first, we check correctness of image points
361   if ( !checkPoints( aPointA, aPointB, aPointC, anIsByTwoPoints, 
362                      tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
363                      tr( "POINTS_A_B_ARE_IDENTICAL" ),
364                      theErrorMsg, true ) )
365     return false;
366
367   Handle(HYDROData_Image) aRefImageObj;
368   QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC( INT_MIN, INT_MIN );
369
370   HYDROData_Image::TransformationMode aTransformationMode = 
371     (HYDROData_Image::TransformationMode)aPanel->getTransformationMode();
372   if ( aTransformationMode == HYDROData_Image::ReferenceImage )
373   {
374     QString aRefImageName = aPanel->getRefImageName();
375     if( aRefImageName.isEmpty() )
376     {
377       theErrorMsg = tr( "REFERENCE_IMAGE_IS_NOT_SELECTED" );
378       return false;
379     }
380
381     HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
382     if( !aPanel->getTransformationDataMap( aRefMap, true ) )
383       return false;
384
385     aRefImageObj = Handle(HYDROData_Image)::DownCast(
386       HYDROGUI_Tool::FindObjectByName( module(), aRefImageName, KIND_IMAGE ) );
387     if( aRefImageObj.IsNull() )
388       return false;
389
390     aTrsfPointA = aRefMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
391     aTrsfPointB = aRefMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
392     if ( !anIsByTwoPoints )
393       aTrsfPointC = aRefMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
394
395     // the same check of correctness for the reference points
396     if ( !checkPoints( aTrsfPointA, aTrsfPointB, aTrsfPointC, anIsByTwoPoints, 
397                        tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
398                        tr( "REFERENCE_POINTS_A_B_ARE_IDENTICAL" ),
399                        theErrorMsg, false ) )
400       return false;
401   }
402   else
403   {
404     if ( aTransformationMode == HYDROData_Image::ManualGeodesic )
405     {
406       aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].GeodesicPoint;
407       aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].GeodesicPoint;
408       if ( !anIsByTwoPoints )
409         aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].GeodesicPoint;
410     }
411     else
412     {
413       aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].CartesianPoint;
414       aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].CartesianPoint;
415       if ( !anIsByTwoPoints )
416         aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].CartesianPoint;
417     }
418   }
419
420   Handle(HYDROData_Image) anImageObj;
421   if( myIsEdit )
422     anImageObj = myEditedObject;
423   else
424     anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
425
426   if( anImageObj.IsNull() )
427     return false;
428
429   anImageObj->SetName( anImageName );
430   anImageObj->SetImage( myImage );
431   anImageObj->SetLocalPoints( aPointA, aPointB, aPointC, false );
432
433   if ( aTransformationMode == HYDROData_Image::ReferenceImage )
434   {
435     anImageObj->SetReferencePoints( aRefImageObj,
436                                     aTrsfPointA, aTrsfPointB, aTrsfPointC );
437   }
438   else
439   {
440     anImageObj->SetGlobalPoints( aTransformationMode,
441                                  aTrsfPointA, aTrsfPointB, aTrsfPointC );
442   }
443
444   if( !myIsEdit )
445   {
446     // Set imported file name for image
447     QString aFilePath = aPanel->getFileName();
448     anImageObj->SetFilePath( aFilePath );
449   }
450    
451   // must be done after all checks and before calling SetVisible() method below
452   closePreview();
453
454   if( !myIsEdit )
455     module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
456
457   if( myIsEdit )
458     anImageObj->Update();
459
460   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
461   return true;
462 }
463
464 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
465 {
466   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
467
468   myImage = theImage;
469
470   if( myPreviewPrs ) // if the image is changed by choosing another file
471   {
472     myPreviewPrs->setImage( myImage );
473     myPreviewPrs->compute();
474
475     if( myPreviewViewManager )
476     {
477       if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
478       {
479         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
480         {
481           aViewPort->onBoundingRectChanged();
482           aViewPort->fitAll();
483         }
484       }
485     }
486   }
487   else
488   {
489     LightApp_Application* anApp = module()->getApp();
490
491     myActiveViewManager = anApp->activeViewManager();
492
493     myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
494     myPreviewPrs->setImage( myImage );
495     myPreviewPrs->compute();
496
497     myPreviewViewManager =
498       dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
499     if( myPreviewViewManager )
500     {
501       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
502                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
503
504       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
505       myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
506       if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
507       {
508         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
509         {
510           //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
511
512           aViewPort->addItem( myPreviewPrs );
513           aViewPort->fitAll();
514
515           myPreviewPrs->setIsTransformationPointPreview( true );
516         }
517         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
518                  this, SLOT( onPointSelected() ) );
519       }
520     }
521
522     QString anImageName;
523     if( myIsEdit )
524     {
525       if( !myEditedObject.IsNull() )
526         anImageName = myEditedObject->GetName();
527     }
528     else
529       anImageName = HYDROGUI_Tool::GenerateObjectName( module(), "Image" );
530     aPanel->setImageName( anImageName );
531   }
532
533   aPanel->setImageSize( myImage.size() );
534
535   aPanel->initializePointSelection();
536   onPointSelected( false );
537 }
538
539 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
540 {
541   myPointType = thePointType;
542   if( myPreviewPrs )
543     myPreviewPrs->setTransformationPointType( thePointType );
544   if( myRefPreviewPrs )
545     myRefPreviewPrs->setTransformationPointType( thePointType );
546 }
547
548 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
549                                                   int thePointType,
550                                                   bool theIsY,
551                                                   int theValue )
552 {
553   if( !theIsRef && myPreviewPrs )
554     myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
555   else if( theIsRef && myRefPreviewPrs )
556     myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
557 }
558
559 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
560 {
561   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
562
563   QString aRefImageName;
564   if( theMode == HYDROData_Image::ReferenceImage )
565   {
566     aRefImageName = aPanel->getRefImageName();
567     if( aRefImageName.isEmpty() )
568       return; // do nothing in this case to avoid visual moving of preview prs
569     onRefImageActivated( aRefImageName );
570   }
571   else
572   {
573     if( myRefViewManager )
574     {
575       closeView( myRefViewManager );
576     }
577   }
578 }
579
580 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
581 {
582   GraphicsView_ViewPort* aViewPort = 0;
583
584   LightApp_Application* anApp = module()->getApp();
585   ///// Get a view port for the reference image preview
586   if( myRefViewManager )
587   {
588     if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
589     {
590       aViewPort = aViewer->getActiveViewPort();
591     }
592   }
593   else
594   {
595     anApp = module()->getApp();
596     // Init reference image preview
597     myRefViewManager =
598       dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
599     if( myRefViewManager )
600     {
601       connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
602                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
603
604       module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
605       myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
606       if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
607       {
608         aViewPort = aViewer->getActiveViewPort();
609         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
610                  this, SLOT( onRefPointSelected() ) );
611       }
612     }
613   }
614
615   if( !aViewPort )
616     return;
617
618   // Remove the old presentation of the reference image if any
619   if( myRefPreviewPrs )
620   {
621     myRefPreviewPrs->setCaption( QString() );
622     aViewPort->removeItem( myRefPreviewPrs );
623
624     delete myRefPreviewPrs;
625     myRefPreviewPrs = 0;
626   }
627
628   // Create a new reference image presentation
629   QImage anImage;
630   Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
631     HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
632   if( !anImageObj.IsNull() )
633   {
634     anImage = anImageObj->Image();
635
636     myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
637     myRefPreviewPrs->setImage( anImage );
638     myRefPreviewPrs->compute();
639
640     myRefPreviewPrs->setIsTransformationPointPreview( true );
641     myRefPreviewPrs->setTransformationPointType( myPointType );
642
643     // Add the new reference image presentation to the appropriate view
644     aViewPort->addItem( myRefPreviewPrs );
645   }
646
647   aViewPort->fitAll();
648
649   // Split views horizontally
650   if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
651   {
652     qApp->processEvents();
653     QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
654     aViewPort->activateWindow();
655     aViewPort->show();
656     aViewPort->setFocus(Qt::ActiveWindowFocusReason);
657     aWorkstack->splitHorizontal();
658   }
659
660   // Initialize the dialog
661   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
662   aPanel->setImageSize( anImage.size(), true );
663   aPanel->initializePointSelection();
664   onPointSelected( true );
665 }
666
667 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
668 {
669   closePreview();
670 }
671
672 void HYDROGUI_ImportImageOp::onPointSelected()
673 {
674   onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
675 }
676
677 void HYDROGUI_ImportImageOp::onRefPointSelected()
678 {
679   onPointSelected( true );
680 }
681
682 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
683 {
684   HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
685   if( !aPrs )
686     return;
687
688   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
689
690   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
691     aPrs->getTransformationPointMap();
692   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
693   while( anIter.hasNext() )
694   {
695     int aPointType = anIter.next().key();
696     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
697     const QPoint& aPoint = aTransformationPoint.Point;
698
699     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
700     aDataMap[ aPointType ] = aData;
701   }
702
703   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
704 }
705
706 void HYDROGUI_ImportImageOp::closePreview()
707 {
708   closeView( myPreviewViewManager );
709   closeView( myRefViewManager );
710
711   if( myActiveViewManager )
712     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
713 }
714
715 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
716 {
717   if( aViewMgr )
718   {
719     GraphicsView_ViewPort* aViewPort = 0;
720     if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
721     {
722       aViewPort = aViewer->getActiveViewPort();
723     }
724     disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
725                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
726
727     // Nullify appropriate presentation pointer
728     HYDROGUI_PrsImage* aPrs = 0;
729     switch ( module()->getViewManagerRole( aViewMgr ) )
730     {
731       case HYDROGUI_Module::VMR_ReferenceImage:
732         aPrs = myRefPreviewPrs;
733         myRefPreviewPrs = 0;
734         break;
735       case HYDROGUI_Module::VMR_TransformImage:
736         aPrs = myPreviewPrs;
737         myPreviewPrs = 0;
738     }
739
740     // Remove the appropriate presentation from the view
741     if( aPrs && aViewPort )
742     {
743       aViewPort->removeItem( aPrs );
744       delete aPrs;
745     }
746
747     // Delete the view
748     module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here
749     aViewMgr = 0;
750   }
751 }