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