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