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