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