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