Salome HOME
Merge remote-tracking branch 'remotes/origin/BR_v14_rc' into BR_SHP_FORMAT
[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     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 );
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 ) ), SLOT( onCreatePreview( QImage ) ) );
272   connect( aPanel, SIGNAL( activatePointSelection( int ) ), SLOT( onActivatePointSelection( int ) ) );
273   connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ), 
274                      SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
275   connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
276   connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
277                      SLOT( onRefImageActivated( const QString& ) ) );
278   connect( aPanel, SIGNAL( setCIsUsed( bool ) ), SLOT( onSetCIsUsed( bool ) ) );
279   connect( aPanel, SIGNAL( filesSelected( const QStringList& ) ),
280                    SLOT( onFilesSelected( const QStringList& ) ) );
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   if( !myIsEdit )
354   {
355     QString aFilePath = aPanel->getFileName();
356     if( aFilePath.isEmpty() )
357     {
358       theErrorMsg = tr( "SELECT_IMAGE_FILE" ).arg( aFilePath );
359       return false;
360     }
361   }
362
363   QString anImageName = aPanel->getImageName();
364   if( anImageName.isEmpty() )
365   {
366     theErrorMsg = tr( "SELECT_IMAGE_NAME" ).arg( anImageName );
367     return false;
368   }
369
370   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
371   {
372     // check that there are no other objects with the same name in the document
373     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
374     if( !anObject.IsNull() )
375     {
376       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
377       return false;
378     }
379   }
380
381   HYDROData_Image::TransformationMode aTransformationMode = 
382     (HYDROData_Image::TransformationMode)aPanel->getTransformationMode();
383   
384   QPoint aPointA, aPointB, aPointC;
385   QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC( INT_MIN, INT_MIN );
386   Handle(HYDROData_Image) aRefImageObj;
387
388   if ( aTransformationMode != HYDROData_Image::CartesianFromFile ) {
389     HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
390     if( !aPanel->getTransformationDataMap( aMap ) )
391       return false;
392
393     bool anIsByTwoPoints = aPanel->isByTwoPoints();
394
395     aPointA = aMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
396     aPointB = aMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
397     aPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) :
398                                 aMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
399
400     // first, we check correctness of image points
401     if ( !checkPoints( aPointA, aPointB, aPointC, anIsByTwoPoints, 
402                        tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
403                        tr( "POINTS_A_B_ARE_IDENTICAL" ),
404                        theErrorMsg, true ) )
405       return false;
406
407     if ( aTransformationMode == HYDROData_Image::ReferenceImage )
408     {
409       QString aRefImageName = aPanel->getRefImageName();
410       if( aRefImageName.isEmpty() )
411       {
412         theErrorMsg = tr( "REFERENCE_IMAGE_IS_NOT_SELECTED" );
413         return false;
414       }
415
416       HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
417       if( !aPanel->getTransformationDataMap( aRefMap, true ) )
418         return false;
419
420       aRefImageObj = Handle(HYDROData_Image)::DownCast(
421         HYDROGUI_Tool::FindObjectByName( module(), aRefImageName, KIND_IMAGE ) );
422       if( aRefImageObj.IsNull() ) {
423         return false;
424       }
425       else if ( !isReferenceCorrect() ) {
426         aRefImageObj->RemoveAllReferences();
427       }
428
429       aTrsfPointA = aRefMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
430       aTrsfPointB = aRefMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
431       if ( !anIsByTwoPoints )
432       aTrsfPointC = aRefMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
433
434       // the same check of correctness for the reference points
435       if ( !checkPoints( aTrsfPointA, aTrsfPointB, aTrsfPointC, anIsByTwoPoints, 
436                          tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
437                          tr( "REFERENCE_POINTS_A_B_ARE_IDENTICAL" ),
438                          theErrorMsg, false ) )
439         return false;
440     }
441     else
442     {
443       if ( aTransformationMode == HYDROData_Image::ManualGeodesic )
444       {
445         aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].GeodesicPoint;
446         aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].GeodesicPoint;
447         if ( !anIsByTwoPoints )
448           aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].GeodesicPoint;
449       }
450       else
451       {
452         aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].CartesianPoint;
453         aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].CartesianPoint;
454         if ( !anIsByTwoPoints )
455           aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].CartesianPoint;
456       }
457     }
458   }
459
460   Handle(HYDROData_Image) anImageObj;
461   if( myIsEdit )
462     anImageObj = myEditedObject;
463   else
464   {
465     anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
466     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anImageObj );
467     theBrowseObjectsEntries.append( anEntry );
468   }
469
470   if( anImageObj.IsNull() )
471     return false;
472
473   anImageObj->SetName( anImageName );
474   anImageObj->SetImage( myImage );
475   
476   if ( aTransformationMode == HYDROData_Image::CartesianFromFile ) {
477     QString aGeoreferencementFileName = aPanel->getGeoreferencementFileName();
478     if ( aGeoreferencementFileName.isEmpty() ) {
479       return false;
480     }
481
482     QPoint aLocalPointA( 0, 0 ),
483            aLocalPointB( anImageObj->Image().width(), 0 ), 
484            aLocalPointC( INT_MIN, INT_MIN );
485     anImageObj->SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
486     if ( !anImageObj->SetGlobalPointsFromFile( aGeoreferencementFileName ) ) {
487       theErrorMsg = tr( "CANT_LOAD_GEOREFERENCEMENT_FILE" );
488       return false;
489     }
490   } else {
491     anImageObj->SetLocalPoints( aPointA, aPointB, aPointC, false );
492
493     if ( aTransformationMode == HYDROData_Image::ReferenceImage )
494     {
495       anImageObj->SetReferencePoints( aRefImageObj,
496                                     aTrsfPointA, aTrsfPointB, aTrsfPointC );
497     }
498     else
499     {
500       anImageObj->SetGlobalPoints( aTransformationMode,
501                                  aTrsfPointA, aTrsfPointB, aTrsfPointC );
502     }
503   }
504
505   if( !myIsEdit )
506   {
507     // Set imported file name for image
508     QString aFilePath = aPanel->getFileName();
509     anImageObj->SetFilePath( aFilePath );
510   }
511    
512   // must be done after all checks and before calling SetVisible() method below
513   if ( isApplyAndClose() )
514     closePreview();
515
516   if( !myIsEdit )
517     module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
518
519   anImageObj->Update();
520
521   theUpdateFlags = UF_Model;
522   if ( isApplyAndClose() )
523     theUpdateFlags |= UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
524
525   if( isApplyAndClose() )
526   {
527     commitDocOperation(); // to save the modifications in the data model
528     return true;
529   }
530
531   if( SetNextFile() )
532   {
533     theErrorMsg = "";
534     module()->updateObjBrowser();
535     return false;         // and to continue the operation
536   }
537
538   /*if( myFiles.count() > 1 )
539   {
540     setIsApplyAndClose( true );
541   }*/
542   return true;
543 }
544
545 bool HYDROGUI_ImportImageOp::isReferenceCorrect() const
546 {
547   bool isCorrect = true;
548
549   if( myIsEdit && !myEditedObject.IsNull() )
550   {
551     HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
552     Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
553       HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
554     if( !aRefImageObj.IsNull() )
555     {
556       bool isFoundEdited = false;
557       HYDROData_SequenceOfObjects aRefSeq = aRefImageObj->GetAllReferenceObjects();
558       for ( int i = 1, n = aRefSeq.Length(); i <= n && !isFoundEdited; ++i )
559       {
560         Handle(HYDROData_Entity) anObject = aRefSeq.Value( i );
561         isFoundEdited = anObject->GetName() == myEditedObject->GetName();
562       }
563       isCorrect = !isFoundEdited;
564     }
565   }
566   return isCorrect;
567 }
568
569 void HYDROGUI_ImportImageOp::apply()
570 {
571   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
572
573   bool aCanApply = isReferenceCorrect();
574   if ( !aCanApply && !myEditedObject.IsNull() )
575   {
576     Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
577       HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
578     if ( !aRefImageObj.IsNull() )
579       aCanApply = SUIT_MessageBox::question( module()->getApp()->desktop(),
580                     tr( "CORRECT_INPUT_DATA" ), tr( "CONFIRM_REMOVE_REFERENCE_FROM_IMAGE" ).
581                     arg( aRefImageObj->GetName() ).arg( myEditedObject->GetName() ),
582                     QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes;
583   }
584   if ( aCanApply )
585     HYDROGUI_Operation::apply();
586   else {
587     aPanel->setRefImageName( "" );
588     onRefImageActivated( aPanel->getRefImageName() );
589   }
590 }
591
592 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
593 {
594   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
595
596   myImage = theImage;
597
598   if( myPreviewPrs ) // if the image is changed by choosing another file
599   {
600     myPreviewPrs->setImage( myImage, true );
601     if ( sender() ) // reset the previous presentation settings
602     {
603       QString aFileName = aPanel->getFileName();
604
605       aPanel->reset();
606       aPanel->setIsEdit( myIsEdit );
607       // restore the file name
608       aPanel->setFileName( aFileName );
609       // fill the reference list
610       HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
611       getReferenceDataList( aPrsPointDataList );
612       aPanel->setPrsPointDataList( aPrsPointDataList );
613     }
614     if( myPreviewViewManager )
615     {
616       if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
617       {
618         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
619         {
620           aViewPort->onBoundingRectChanged();
621           aViewPort->fitAll();
622         }
623       }
624     }
625   }
626   else
627   {
628     LightApp_Application* anApp = module()->getApp();
629
630     myActiveViewManager = anApp->activeViewManager();
631
632     myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
633     myPreviewPrs->setImage( myImage, true );
634
635     myPreviewViewManager =
636       dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
637     if( myPreviewViewManager )
638     {
639       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
640                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
641
642       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
643       myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
644       if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
645       {
646         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
647         {
648           //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
649
650           aViewPort->addItem( myPreviewPrs );
651           aViewPort->fitAll();
652           
653           if ( myEditedObject ) {
654             size_t aViewId = (size_t)aViewer;
655             module()->setObjectVisible( aViewId, myEditedObject, true );
656           }
657           
658           myPreviewPrs->setIsTransformationPointPreview( true );
659           myPreviewPrs->setTransformationPointCursorShape( module()->getPrefEditCursor().shape() );
660         }
661         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
662                  this, SLOT( onPointSelected() ) );
663       }
664     }
665   }
666
667   // Set the image name in the dialog
668   QString anImageName = aPanel->getImageName().simplified();
669   // If edit mode and the name was not set yet then get from the edited object
670   if( myIsEdit && anImageName.isEmpty() )
671   {
672     if( !myEditedObject.IsNull() )
673       anImageName = myEditedObject->GetName();
674   }
675   // If the name was not set then initialize it from the selected file name
676   if ( anImageName.isEmpty() )
677   {
678     anImageName = aPanel->getFileName();
679     if ( !anImageName.isEmpty() ) {
680         anImageName = QFileInfo( anImageName ).baseName();
681     }
682     // If no file name then generate a new image name
683     if ( anImageName.isEmpty() ) {
684       anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
685     }
686   }
687   aPanel->setImageName( anImageName );
688
689   aPanel->setImageSize( myImage.size() );
690
691   aPanel->initializePointSelection();
692   onPointSelected( false );
693   onSetCIsUsed( !aPanel->isByTwoPoints() );
694 }
695
696 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
697 {
698   myPointType = thePointType;
699   if( myPreviewPrs )
700     myPreviewPrs->setTransformationPointType( thePointType );
701   if( myRefPreviewPrs )
702     myRefPreviewPrs->setTransformationPointType( thePointType );
703 }
704
705 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
706                                                   int thePointType,
707                                                   bool theIsY,
708                                                   int theValue )
709 {
710   if( !theIsRef && myPreviewPrs )
711     myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
712   else if( theIsRef && myRefPreviewPrs )
713     myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
714 }
715
716 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
717 {
718   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
719
720   QString aRefImageName;
721   if( theMode == HYDROData_Image::ReferenceImage )
722   {
723     aRefImageName = aPanel->getRefImageName();
724     if( aRefImageName.isEmpty() )
725       return; // do nothing in this case to avoid visual moving of preview prs
726     onRefImageActivated( aRefImageName );
727   }
728   else
729   {
730     if( myRefViewManager )
731     {
732       closeView( myRefViewManager );
733     }
734   }
735 }
736
737 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
738 {
739   if( theName.isEmpty() ) {
740     if( myRefViewManager )
741       closeView( myRefViewManager );
742     return;
743   }
744
745   GraphicsView_ViewPort* aViewPort = 0;
746
747   LightApp_Application* anApp = module()->getApp();
748   ///// Get a view port for the reference image preview
749   if( myRefViewManager )
750   {
751     if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
752     {
753       aViewPort = aViewer->getActiveViewPort();
754     }
755   }
756   else
757   {
758     anApp = module()->getApp();
759     // Init reference image preview
760     myRefViewManager =
761       dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
762     if( myRefViewManager )
763     {
764       connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
765                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
766
767       module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
768       myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
769       if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
770       {
771         aViewPort = aViewer->getActiveViewPort();
772         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
773                  this, SLOT( onRefPointSelected() ) );
774       }
775     }
776   }
777
778   if( !aViewPort )
779     return;
780
781   size_t aViewId = (size_t)myRefViewManager->getViewer();
782
783   // Remove the old presentation of the reference image if any
784   if( myRefPreviewPrs )
785   {
786     module()->setObjectVisible( aViewId, myRefPreviewPrs->getObject(), false );
787
788     myRefPreviewPrs->setCaption( QString() );
789     aViewPort->removeItem( myRefPreviewPrs );
790
791     delete myRefPreviewPrs;
792     myRefPreviewPrs = 0;
793   }
794
795   // Create a new reference image presentation
796   QImage anImage;
797   Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
798     HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
799   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
800   if( !anImageObj.IsNull() )
801   {
802     anImage = anImageObj->Image();
803
804     myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
805     myRefPreviewPrs->setImage( anImage, true );
806
807     myRefPreviewPrs->setIsTransformationPointPreview( true );
808     myRefPreviewPrs->setTransformationPointType( myPointType );
809
810     myRefPreviewPrs->setIsByTwoPoints( aPanel->isByTwoPoints() );
811
812     myRefPreviewPrs->setTransformationPointCursorShape( module()->getPrefEditCursor().shape() );
813
814     // Add the new reference image presentation to the appropriate view
815     aViewPort->addItem( myRefPreviewPrs );
816
817     module()->setObjectVisible( aViewId, anImageObj, true );
818   }
819
820   aViewPort->fitAll();
821
822   // Split views horizontally
823   if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
824   {
825     qApp->processEvents();
826     QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
827     aViewPort->activateWindow();
828     aViewPort->show();
829     aViewPort->setFocus(Qt::ActiveWindowFocusReason);
830     aWorkstack->splitHorizontal();
831   }
832
833   // Initialize the dialog
834   aPanel->setImageSize( anImage.size(), true );
835   aPanel->initializePointSelection();
836   onPointSelected( true );
837 }
838
839 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
840 {
841   closePreview();
842 }
843
844 void HYDROGUI_ImportImageOp::onPointSelected()
845 {
846   onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
847 }
848
849 void HYDROGUI_ImportImageOp::onRefPointSelected()
850 {
851   onPointSelected( true );
852 }
853
854 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
855 {
856   HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
857   if( !aPrs )
858     return;
859
860   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
861
862   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
863     aPrs->getTransformationPointMap();
864   
865   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
866   while( anIter.hasNext() )
867   {
868     int aPointType = anIter.next().key();
869     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
870     const QPoint& aPoint = aTransformationPoint.Point;
871
872     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
873     aDataMap[ aPointType ] = aData;
874   }
875
876   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
877 }
878
879 void HYDROGUI_ImportImageOp::closePreview()
880 {
881   closeView( myPreviewViewManager );
882   closeView( myRefViewManager );
883
884   if( myActiveViewManager )
885     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
886 }
887
888 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
889 {
890   if( aViewMgr )
891   {
892     GraphicsView_ViewPort* aViewPort = 0;
893     if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
894     {
895       aViewPort = aViewer->getActiveViewPort();
896     }
897     disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
898                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
899
900     // Nullify appropriate presentation pointer
901     HYDROGUI_PrsImage* aPrs = 0;
902     switch ( module()->getViewManagerRole( aViewMgr ) )
903     {
904       case HYDROGUI_Module::VMR_ReferenceImage:
905         aPrs = myRefPreviewPrs;
906         myRefPreviewPrs = 0;
907         break;
908       case HYDROGUI_Module::VMR_TransformImage:
909         aPrs = myPreviewPrs;
910         myPreviewPrs = 0;
911     }
912
913     // Remove the appropriate presentation from the view
914     if( aPrs && aViewPort )
915     {
916       aViewPort->removeItem( aPrs );
917       delete aPrs;
918     }
919
920     // Delete the view
921     module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here
922     aViewMgr = 0;
923   }
924 }
925
926 void HYDROGUI_ImportImageOp::onFilesSelected( const QStringList& theFileNames )
927 {
928   myFiles = theFileNames;
929   myFileIndex = -1;
930   SetNextFile();
931 }
932
933 bool HYDROGUI_ImportImageOp::SetNextFile()
934 {
935   myFileIndex++;
936   bool isEnabledEdit = myFiles.count()==1 || myFileIndex==myFiles.count();
937   bool isValid = ( myFileIndex>=0 && myFileIndex<myFiles.count() );
938   QString aFile = isValid ? myFiles[myFileIndex] : "";
939
940   HYDROGUI_ImportImageDlg* aPanel = dynamic_cast<HYDROGUI_ImportImageDlg*>( inputPanel() );
941   aPanel->ActivateFile( aFile, isEnabledEdit );
942   return isValid;
943 }