Salome HOME
Feature #86: The hierarchy in the Object Browser (T 19).
[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->setIsByTwoPoints( theIsByTwoPoints );
221       thePrs->setTransformationPointMap( aPointMap );
222     }
223   }
224 }
225
226 void HYDROGUI_ImportImageOp::computeTrsfData( HYDROData_Image::TransformationMode theTrsfMode,
227                                               bool theIsByTwoPoints,
228                                               const QPoint& theLocalPointA,
229                                               const QPoint& theLocalPointB,
230                                               const QPoint& theLocalPointC,
231                                               const QPointF& theGlobalPointA,
232                                               const QPointF& theGlobalPointB,
233                                               const QPointF& theGlobalPointC,
234                                               HYDROGUI_ImportImageDlg::TransformationDataMap& theDataMap ) const
235 {
236   // Build the local-global points map
237   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
238   theDataMap[ HYDROGUI_PrsImage::PointA ] = 
239     HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointA, theGlobalPointA );
240   theDataMap[ HYDROGUI_PrsImage::PointB ] =
241     HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointB, theGlobalPointB );
242   if ( !theIsByTwoPoints )
243   {
244     theDataMap[ HYDROGUI_PrsImage::PointC ] =
245       HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointC, theGlobalPointC );
246   }
247 }
248
249 void HYDROGUI_ImportImageOp::abortOperation()
250 {
251   closePreview();
252
253   HYDROGUI_Operation::abortOperation();
254 }
255
256 void HYDROGUI_ImportImageOp::commitOperation()
257 {
258   closePreview();
259
260   HYDROGUI_Operation::commitOperation();
261 }
262
263 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
264 {
265   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
266   connect( aPanel, SIGNAL( createPreview( QImage ) ), SLOT( onCreatePreview( QImage ) ) );
267   connect( aPanel, SIGNAL( activatePointSelection( int ) ), SLOT( onActivatePointSelection( int ) ) );
268   connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ), 
269                      SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
270   connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
271   connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
272                      SLOT( onRefImageActivated( const QString& ) ) );
273   connect( aPanel, SIGNAL( setCIsUsed( bool ) ), SLOT( onSetCIsUsed( bool ) ) );
274   return aPanel;
275 }
276
277 void HYDROGUI_ImportImageOp::onSetCIsUsed( bool theCIsUsed )
278 {
279   if ( myPreviewPrs )
280   {
281     myPreviewPrs->setIsByTwoPoints( !theCIsUsed );
282   }
283   if ( myRefPreviewPrs )
284   {
285     myRefPreviewPrs->setIsByTwoPoints( !theCIsUsed );
286   }
287 }
288
289 bool HYDROGUI_ImportImageOp::checkPoints( const QPointF& thePointA,
290                                           const QPointF& thePointB,
291                                           const QPointF& thePointC,
292                                           const bool     theIsByTwoPoints,
293                                           const QString& theLineErrMsg,
294                                           const QString& thePoinErrMsg,
295                                           QString&       theErrorMsg,
296                                           const bool     theIsToCheckInvertibles ) const
297 {
298   double xa = thePointA.x();
299   double ya = thePointA.y();
300   double xb = thePointB.x();
301   double yb = thePointB.y();
302   double xc = thePointC.x();
303   double yc = thePointC.y();
304
305   if ( !theIsByTwoPoints )
306   {
307     // check that three input points don't belong to a single line
308     if ( ValuesEquals( ( yb - ya ) * ( xc - xa ), ( yc - ya ) * ( xb - xa ) ) )
309     {
310       theErrorMsg = theLineErrMsg;
311       return false;
312     }
313   }
314   else 
315   {
316     // check that two points are not identical
317     if ( ValuesEquals( xa, ya ) && ValuesEquals( xb, yb ) )
318     {
319       theErrorMsg = thePoinErrMsg;
320       return false;
321     }
322   }
323
324   if ( theIsToCheckInvertibles )
325   {
326     QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
327
328     bool anIsInvertible = false;
329     QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
330     if( !anIsInvertible )
331     {
332       theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
333       return false;
334     }
335   }
336
337   return true;
338 }
339
340 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
341                                            QString& theErrorMsg )
342 {
343   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
344
345   QString anImageName = aPanel->getImageName();
346   if( anImageName.isEmpty() )
347     return false;
348
349   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
350   {
351     // check that there are no other objects with the same name in the document
352     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
353     if( !anObject.IsNull() )
354     {
355       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
356       return false;
357     }
358   }
359
360   HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
361   if( !aPanel->getTransformationDataMap( aMap ) )
362     return false;
363
364   bool anIsByTwoPoints = aPanel->isByTwoPoints();
365
366   QPoint aPointA = aMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
367   QPoint aPointB = aMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
368   QPoint aPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) :
369                                      aMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
370
371   // first, we check correctness of image points
372   if ( !checkPoints( aPointA, aPointB, aPointC, anIsByTwoPoints, 
373                      tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
374                      tr( "POINTS_A_B_ARE_IDENTICAL" ),
375                      theErrorMsg, true ) )
376     return false;
377
378   Handle(HYDROData_Image) aRefImageObj;
379   QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC( INT_MIN, INT_MIN );
380
381   HYDROData_Image::TransformationMode aTransformationMode = 
382     (HYDROData_Image::TransformationMode)aPanel->getTransformationMode();
383   if ( aTransformationMode == HYDROData_Image::ReferenceImage )
384   {
385     QString aRefImageName = aPanel->getRefImageName();
386     if( aRefImageName.isEmpty() )
387     {
388       theErrorMsg = tr( "REFERENCE_IMAGE_IS_NOT_SELECTED" );
389       return false;
390     }
391
392     HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
393     if( !aPanel->getTransformationDataMap( aRefMap, true ) )
394       return false;
395
396     aRefImageObj = Handle(HYDROData_Image)::DownCast(
397       HYDROGUI_Tool::FindObjectByName( module(), aRefImageName, KIND_IMAGE ) );
398     if( aRefImageObj.IsNull() )
399       return false;
400
401     aTrsfPointA = aRefMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
402     aTrsfPointB = aRefMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
403     if ( !anIsByTwoPoints )
404       aTrsfPointC = aRefMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
405
406     // the same check of correctness for the reference points
407     if ( !checkPoints( aTrsfPointA, aTrsfPointB, aTrsfPointC, anIsByTwoPoints, 
408                        tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
409                        tr( "REFERENCE_POINTS_A_B_ARE_IDENTICAL" ),
410                        theErrorMsg, false ) )
411       return false;
412   }
413   else
414   {
415     if ( aTransformationMode == HYDROData_Image::ManualGeodesic )
416     {
417       aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].GeodesicPoint;
418       aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].GeodesicPoint;
419       if ( !anIsByTwoPoints )
420         aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].GeodesicPoint;
421     }
422     else
423     {
424       aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].CartesianPoint;
425       aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].CartesianPoint;
426       if ( !anIsByTwoPoints )
427         aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].CartesianPoint;
428     }
429   }
430
431   Handle(HYDROData_Image) anImageObj;
432   if( myIsEdit )
433     anImageObj = myEditedObject;
434   else
435     anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
436
437   if( anImageObj.IsNull() )
438     return false;
439
440   anImageObj->SetName( anImageName );
441   anImageObj->SetImage( myImage );
442   anImageObj->SetLocalPoints( aPointA, aPointB, aPointC, false );
443
444   if ( aTransformationMode == HYDROData_Image::ReferenceImage )
445   {
446     anImageObj->SetReferencePoints( aRefImageObj,
447                                     aTrsfPointA, aTrsfPointB, aTrsfPointC );
448   }
449   else
450   {
451     anImageObj->SetGlobalPoints( aTransformationMode,
452                                  aTrsfPointA, aTrsfPointB, aTrsfPointC );
453   }
454
455   if( !myIsEdit )
456   {
457     // Set imported file name for image
458     QString aFilePath = aPanel->getFileName();
459     anImageObj->SetFilePath( aFilePath );
460   }
461    
462   // must be done after all checks and before calling SetVisible() method below
463   closePreview();
464
465   if( !myIsEdit )
466     module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
467
468   if( myIsEdit )
469     anImageObj->Update();
470
471   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
472   return true;
473 }
474
475 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
476 {
477   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
478
479   myImage = theImage;
480
481   if( myPreviewPrs ) // if the image is changed by choosing another file
482   {
483     myPreviewPrs->setImage( myImage );
484     myPreviewPrs->compute();
485
486     if( myPreviewViewManager )
487     {
488       if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
489       {
490         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
491         {
492           aViewPort->onBoundingRectChanged();
493           aViewPort->fitAll();
494         }
495       }
496     }
497   }
498   else
499   {
500     LightApp_Application* anApp = module()->getApp();
501
502     myActiveViewManager = anApp->activeViewManager();
503
504     myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
505     myPreviewPrs->setImage( myImage );
506     myPreviewPrs->compute();
507
508     myPreviewViewManager =
509       dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
510     if( myPreviewViewManager )
511     {
512       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
513                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
514
515       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
516       myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
517       if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
518       {
519         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
520         {
521           //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
522
523           aViewPort->addItem( myPreviewPrs );
524           aViewPort->fitAll();
525
526           myPreviewPrs->setIsTransformationPointPreview( true );
527         }
528         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
529                  this, SLOT( onPointSelected() ) );
530       }
531     }
532
533     QString anImageName;
534     if( myIsEdit )
535     {
536       if( !myEditedObject.IsNull() )
537         anImageName = myEditedObject->GetName();
538     }
539     else
540       anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
541     aPanel->setImageName( anImageName );
542   }
543
544   aPanel->setImageSize( myImage.size() );
545
546   aPanel->initializePointSelection();
547   onPointSelected( false );
548 }
549
550 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
551 {
552   myPointType = thePointType;
553   if( myPreviewPrs )
554     myPreviewPrs->setTransformationPointType( thePointType );
555   if( myRefPreviewPrs )
556     myRefPreviewPrs->setTransformationPointType( thePointType );
557 }
558
559 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
560                                                   int thePointType,
561                                                   bool theIsY,
562                                                   int theValue )
563 {
564   if( !theIsRef && myPreviewPrs )
565     myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
566   else if( theIsRef && myRefPreviewPrs )
567     myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
568 }
569
570 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
571 {
572   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
573
574   QString aRefImageName;
575   if( theMode == HYDROData_Image::ReferenceImage )
576   {
577     aRefImageName = aPanel->getRefImageName();
578     if( aRefImageName.isEmpty() )
579       return; // do nothing in this case to avoid visual moving of preview prs
580     onRefImageActivated( aRefImageName );
581   }
582   else
583   {
584     if( myRefViewManager )
585     {
586       closeView( myRefViewManager );
587     }
588   }
589 }
590
591 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
592 {
593   GraphicsView_ViewPort* aViewPort = 0;
594
595   LightApp_Application* anApp = module()->getApp();
596   ///// Get a view port for the reference image preview
597   if( myRefViewManager )
598   {
599     if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
600     {
601       aViewPort = aViewer->getActiveViewPort();
602     }
603   }
604   else
605   {
606     anApp = module()->getApp();
607     // Init reference image preview
608     myRefViewManager =
609       dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
610     if( myRefViewManager )
611     {
612       connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
613                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
614
615       module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
616       myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
617       if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
618       {
619         aViewPort = aViewer->getActiveViewPort();
620         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
621                  this, SLOT( onRefPointSelected() ) );
622       }
623     }
624   }
625
626   if( !aViewPort )
627     return;
628
629   // Remove the old presentation of the reference image if any
630   if( myRefPreviewPrs )
631   {
632     myRefPreviewPrs->setCaption( QString() );
633     aViewPort->removeItem( myRefPreviewPrs );
634
635     delete myRefPreviewPrs;
636     myRefPreviewPrs = 0;
637   }
638
639   // Create a new reference image presentation
640   QImage anImage;
641   Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
642     HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
643   if( !anImageObj.IsNull() )
644   {
645     anImage = anImageObj->Image();
646
647     myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
648     myRefPreviewPrs->setImage( anImage );
649     myRefPreviewPrs->compute();
650
651     myRefPreviewPrs->setIsTransformationPointPreview( true );
652     myRefPreviewPrs->setTransformationPointType( myPointType );
653
654     // Add the new reference image presentation to the appropriate view
655     aViewPort->addItem( myRefPreviewPrs );
656   }
657
658   aViewPort->fitAll();
659
660   // Split views horizontally
661   if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
662   {
663     qApp->processEvents();
664     QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
665     aViewPort->activateWindow();
666     aViewPort->show();
667     aViewPort->setFocus(Qt::ActiveWindowFocusReason);
668     aWorkstack->splitHorizontal();
669   }
670
671   // Initialize the dialog
672   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
673   aPanel->setImageSize( anImage.size(), true );
674   aPanel->initializePointSelection();
675   onPointSelected( true );
676 }
677
678 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
679 {
680   closePreview();
681 }
682
683 void HYDROGUI_ImportImageOp::onPointSelected()
684 {
685   onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
686 }
687
688 void HYDROGUI_ImportImageOp::onRefPointSelected()
689 {
690   onPointSelected( true );
691 }
692
693 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
694 {
695   HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
696   if( !aPrs )
697     return;
698
699   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
700
701   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
702     aPrs->getTransformationPointMap();
703   
704   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
705   while( anIter.hasNext() )
706   {
707     int aPointType = anIter.next().key();
708     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
709     const QPoint& aPoint = aTransformationPoint.Point;
710
711     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
712     aDataMap[ aPointType ] = aData;
713   }
714
715   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
716 }
717
718 void HYDROGUI_ImportImageOp::closePreview()
719 {
720   closeView( myPreviewViewManager );
721   closeView( myRefViewManager );
722
723   if( myActiveViewManager )
724     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
725 }
726
727 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
728 {
729   if( aViewMgr )
730   {
731     GraphicsView_ViewPort* aViewPort = 0;
732     if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
733     {
734       aViewPort = aViewer->getActiveViewPort();
735     }
736     disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
737                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
738
739     // Nullify appropriate presentation pointer
740     HYDROGUI_PrsImage* aPrs = 0;
741     switch ( module()->getViewManagerRole( aViewMgr ) )
742     {
743       case HYDROGUI_Module::VMR_ReferenceImage:
744         aPrs = myRefPreviewPrs;
745         myRefPreviewPrs = 0;
746         break;
747       case HYDROGUI_Module::VMR_TransformImage:
748         aPrs = myPreviewPrs;
749         myPreviewPrs = 0;
750     }
751
752     // Remove the appropriate presentation from the view
753     if( aPrs && aViewPort )
754     {
755       aViewPort->removeItem( aPrs );
756       delete aPrs;
757     }
758
759     // Delete the view
760     module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here
761     aViewMgr = 0;
762   }
763 }