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