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