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