Salome HOME
Update mechanism is corrected (Bug #182).
[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   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     // Set the image name in the dialog
534     QString anImageName = aPanel->getImageName().simplified();
535     // If edit mode and the name was not set yet then get from the edited object
536     if( myIsEdit && anImageName.isEmpty() )
537     {
538       if( !myEditedObject.IsNull() )
539         anImageName = myEditedObject->GetName();
540     }
541     // If the name was not set then initialize it from the selected file name
542     if ( anImageName.isEmpty() )
543     {
544       anImageName = aPanel->getFileName();
545       if ( !anImageName.isEmpty() ) {
546           anImageName = QFileInfo( anImageName ).baseName();
547       }
548       // If no file name then generate a new image name
549       if ( anImageName.isEmpty() ) {
550         anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
551       }
552     }
553
554     aPanel->setImageName( anImageName );
555   }
556
557   aPanel->setImageSize( myImage.size() );
558
559   aPanel->initializePointSelection();
560   onPointSelected( false );
561 }
562
563 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
564 {
565   myPointType = thePointType;
566   if( myPreviewPrs )
567     myPreviewPrs->setTransformationPointType( thePointType );
568   if( myRefPreviewPrs )
569     myRefPreviewPrs->setTransformationPointType( thePointType );
570 }
571
572 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
573                                                   int thePointType,
574                                                   bool theIsY,
575                                                   int theValue )
576 {
577   if( !theIsRef && myPreviewPrs )
578     myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
579   else if( theIsRef && myRefPreviewPrs )
580     myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
581 }
582
583 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
584 {
585   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
586
587   QString aRefImageName;
588   if( theMode == HYDROData_Image::ReferenceImage )
589   {
590     aRefImageName = aPanel->getRefImageName();
591     if( aRefImageName.isEmpty() )
592       return; // do nothing in this case to avoid visual moving of preview prs
593     onRefImageActivated( aRefImageName );
594   }
595   else
596   {
597     if( myRefViewManager )
598     {
599       closeView( myRefViewManager );
600     }
601   }
602 }
603
604 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
605 {
606   GraphicsView_ViewPort* aViewPort = 0;
607
608   LightApp_Application* anApp = module()->getApp();
609   ///// Get a view port for the reference image preview
610   if( myRefViewManager )
611   {
612     if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
613     {
614       aViewPort = aViewer->getActiveViewPort();
615     }
616   }
617   else
618   {
619     anApp = module()->getApp();
620     // Init reference image preview
621     myRefViewManager =
622       dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
623     if( myRefViewManager )
624     {
625       connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
626                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
627
628       module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
629       myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
630       if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
631       {
632         aViewPort = aViewer->getActiveViewPort();
633         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
634                  this, SLOT( onRefPointSelected() ) );
635       }
636     }
637   }
638
639   if( !aViewPort )
640     return;
641
642   // Remove the old presentation of the reference image if any
643   if( myRefPreviewPrs )
644   {
645     myRefPreviewPrs->setCaption( QString() );
646     aViewPort->removeItem( myRefPreviewPrs );
647
648     delete myRefPreviewPrs;
649     myRefPreviewPrs = 0;
650   }
651
652   // Create a new reference image presentation
653   QImage anImage;
654   Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
655     HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
656   if( !anImageObj.IsNull() )
657   {
658     anImage = anImageObj->Image();
659
660     myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
661     myRefPreviewPrs->setImage( anImage );
662     myRefPreviewPrs->compute();
663
664     myRefPreviewPrs->setIsTransformationPointPreview( true );
665     myRefPreviewPrs->setTransformationPointType( myPointType );
666
667     // Add the new reference image presentation to the appropriate view
668     aViewPort->addItem( myRefPreviewPrs );
669   }
670
671   aViewPort->fitAll();
672
673   // Split views horizontally
674   if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
675   {
676     qApp->processEvents();
677     QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
678     aViewPort->activateWindow();
679     aViewPort->show();
680     aViewPort->setFocus(Qt::ActiveWindowFocusReason);
681     aWorkstack->splitHorizontal();
682   }
683
684   // Initialize the dialog
685   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
686   aPanel->setImageSize( anImage.size(), true );
687   aPanel->initializePointSelection();
688   onPointSelected( true );
689 }
690
691 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
692 {
693   closePreview();
694 }
695
696 void HYDROGUI_ImportImageOp::onPointSelected()
697 {
698   onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
699 }
700
701 void HYDROGUI_ImportImageOp::onRefPointSelected()
702 {
703   onPointSelected( true );
704 }
705
706 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
707 {
708   HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
709   if( !aPrs )
710     return;
711
712   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
713
714   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
715     aPrs->getTransformationPointMap();
716   
717   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
718   while( anIter.hasNext() )
719   {
720     int aPointType = anIter.next().key();
721     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
722     const QPoint& aPoint = aTransformationPoint.Point;
723
724     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
725     aDataMap[ aPointType ] = aData;
726   }
727
728   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
729 }
730
731 void HYDROGUI_ImportImageOp::closePreview()
732 {
733   closeView( myPreviewViewManager );
734   closeView( myRefViewManager );
735
736   if( myActiveViewManager )
737     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
738 }
739
740 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
741 {
742   if( aViewMgr )
743   {
744     GraphicsView_ViewPort* aViewPort = 0;
745     if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
746     {
747       aViewPort = aViewer->getActiveViewPort();
748     }
749     disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
750                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
751
752     // Nullify appropriate presentation pointer
753     HYDROGUI_PrsImage* aPrs = 0;
754     switch ( module()->getViewManagerRole( aViewMgr ) )
755     {
756       case HYDROGUI_Module::VMR_ReferenceImage:
757         aPrs = myRefPreviewPrs;
758         myRefPreviewPrs = 0;
759         break;
760       case HYDROGUI_Module::VMR_TransformImage:
761         aPrs = myPreviewPrs;
762         myPreviewPrs = 0;
763     }
764
765     // Remove the appropriate presentation from the view
766     if( aPrs && aViewPort )
767     {
768       aViewPort->removeItem( aPrs );
769       delete aPrs;
770     }
771
772     // Delete the view
773     module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here
774     aViewMgr = 0;
775   }
776 }