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