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