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