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