Salome HOME
refs #525: autocolor of zones in a calculation case.
[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     myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
82     if( !myEditedObject.IsNull() )
83     {
84       QImage anImage = myEditedObject->Image();
85       bool anIsByTwoPoints = myEditedObject->IsByTwoPoints();
86
87       QPoint aLocalPointA, aLocalPointB, aLocalPointC;
88       myEditedObject->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
89
90       // Create the edited image preview presentation in the viewer
91       onCreatePreview( anImage );
92
93       // Set transformation local points A,B,C to the image preview presentation
94       setPresentationTrsfPoints( myPreviewPrs, anIsByTwoPoints, aLocalPointA, 
95         aLocalPointB, aLocalPointC );
96
97       // Build the existing image local and global points mapping 
98       // according to the current transformation mode.
99       HYDROData_Image::TransformationMode aTrsfMode = myEditedObject->GetTrsfMode();
100       QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
101       HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
102       if ( aTrsfMode == HYDROData_Image::ReferenceImage )
103       {
104         // Compute global points using the transformation matrix of the reference image
105         Handle(HYDROData_Image) aRefImage = myEditedObject->GetTrsfReferenceImage();
106         if ( !aRefImage.IsNull() )
107         {
108           QTransform aRefTrsf = aRefImage->Trsf(); // The reference image transformation matrix
109           aTrsfPointA = aRefTrsf.map( aLocalPointA ); // Compute the global point A
110           aTrsfPointB = aRefTrsf.map( aLocalPointB ); // Compute the global point B
111           if ( !anIsByTwoPoints )
112           {
113             aTrsfPointC = aRefTrsf.map( aLocalPointC ); // Compute the global point C if used
114           }
115           // Build the local-global points map
116           // Use the reference image transformation mode for interpreting global points
117           computeTrsfData( aRefImage->GetTrsfMode(), anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
118             aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
119         }
120       }
121       else
122       {
123         // Get global points from the edited image
124         myEditedObject->GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC );
125         // Build the local-global points map
126         computeTrsfData( aTrsfMode, anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
127           aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
128       }
129
130       // Initialize the dialog mode, local and global coordinates 
131       // except coordinates on the reference image
132       aPanel->setTransformationMode( aTrsfMode );
133       aPanel->setTransformationDataMap( aDataMap );
134       aPanel->setByTwoPoints( anIsByTwoPoints );
135
136       // Set points of the reference image
137       if ( aTrsfMode == HYDROData_Image::ReferenceImage )
138       {
139         Handle(HYDROData_Image) aRefImage;
140         myEditedObject->GetReferencePoints( aRefImage,
141                                          aTrsfPointA, aTrsfPointB, aTrsfPointC );
142         if ( !aRefImage.IsNull() )
143         {
144           aRefImageName = aRefImage->GetName();
145
146           // Create the reference image presentation in the viewer
147           onRefImageActivated( aRefImageName );
148
149           // Set transformation points A,B,C to the reference image presentation
150           setPresentationTrsfPoints( myRefPreviewPrs, anIsByTwoPoints, aTrsfPointA.toPoint(), 
151             aTrsfPointB.toPoint(), aTrsfPointC.toPoint() );
152
153           // Prepare A, B, C points on the reference image
154           HYDROGUI_ImportImageDlg::TransformationDataMap aRefDataMap;
155           aRefDataMap[ HYDROGUI_PrsImage::PointA ] = 
156             HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointA.toPoint() );
157           aRefDataMap[ HYDROGUI_PrsImage::PointB ] = 
158             HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointB.toPoint() );
159           if ( !anIsByTwoPoints )
160             aRefDataMap[ HYDROGUI_PrsImage::PointC ] = 
161             HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointC.toPoint() );
162
163           // Initialize the dialog's A, B, C points coordinates of the reference image
164           aPanel->setTransformationDataMap( aRefDataMap, true, true );
165         }
166       }
167     }
168   }
169
170   HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
171   getReferenceDataList( aPrsPointDataList );
172   aPanel->setPrsPointDataList( aPrsPointDataList );
173   // Select the current reference image in the dialog combobox
174   aPanel->setRefImageName( aRefImageName );
175 }
176
177 void HYDROGUI_ImportImageOp::setPresentationTrsfPoints( HYDROGUI_PrsImage* thePrs, 
178                                                         bool theIsByTwoPoints,
179                                                         const QPoint theLocalPointA, 
180                                                         const QPoint theLocalPointB, 
181                                                         const QPoint theLocalPointC )
182 {
183   // Set transformation points A,B,C to the image presentation
184   if( thePrs )
185   {
186     HYDROGUI_PrsImage::TransformationPointMap aPointMap =
187       thePrs->getTransformationPointMap();
188     if( !aPointMap.isEmpty() )
189     {
190       aPointMap[ HYDROGUI_PrsImage::PointA ].Point = theLocalPointA;
191       aPointMap[ HYDROGUI_PrsImage::PointB ].Point = theLocalPointB;
192       if ( !theIsByTwoPoints )
193         aPointMap[ HYDROGUI_PrsImage::PointC ].Point = theLocalPointC;
194
195       thePrs->setIsByTwoPoints( theIsByTwoPoints );
196       thePrs->setTransformationPointMap( aPointMap );
197     }
198   }
199 }
200
201 void HYDROGUI_ImportImageOp::getReferenceDataList(
202                         HYDROGUI_ImportImageDlg::PrsPointDataList& theList ) const
203 {
204   // Collect information about existing images and initialize the combobox 
205   // reference image selector in the dialog.
206   HYDROData_Iterator anIterator( doc(), KIND_IMAGE );
207   for( ; anIterator.More(); anIterator.Next() )
208   {
209     Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
210     if( !anImageObj.IsNull() )
211     {
212       if( myIsEdit && IsEqual( anImageObj, myEditedObject ) )
213         continue;
214
215       QPoint aLocalPointA, aLocalPointB, aLocalPointC;
216       anImageObj->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
217       
218       HYDROData_Image::TransformationMode aImgTrsfMode;
219       QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
220       anImageObj->GetGlobalPoints( aImgTrsfMode,
221                                    aTrsfPointA, aTrsfPointB, aTrsfPointC );
222
223       HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
224       computeTrsfData( aImgTrsfMode, false, aLocalPointA, aLocalPointB, aLocalPointC,
225         aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
226
227       HYDROGUI_ImportImageDlg::PrsPointData aPrsPointData( anImageObj->GetName(), aDataMap );
228       theList.append( aPrsPointData );
229     }
230   }
231 }
232
233 void HYDROGUI_ImportImageOp::computeTrsfData( HYDROData_Image::TransformationMode theTrsfMode,
234                                               bool theIsByTwoPoints,
235                                               const QPoint& theLocalPointA,
236                                               const QPoint& theLocalPointB,
237                                               const QPoint& theLocalPointC,
238                                               const QPointF& theGlobalPointA,
239                                               const QPointF& theGlobalPointB,
240                                               const QPointF& theGlobalPointC,
241                                               HYDROGUI_ImportImageDlg::TransformationDataMap& theDataMap ) const
242 {
243   // Build the local-global points map
244   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
245   theDataMap[ HYDROGUI_PrsImage::PointA ] = 
246     HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointA, theGlobalPointA );
247   theDataMap[ HYDROGUI_PrsImage::PointB ] =
248     HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointB, theGlobalPointB );
249   if ( !theIsByTwoPoints )
250   {
251     theDataMap[ HYDROGUI_PrsImage::PointC ] =
252       HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointC, theGlobalPointC );
253   }
254 }
255
256 void HYDROGUI_ImportImageOp::abortOperation()
257 {
258   closePreview();
259
260   HYDROGUI_Operation::abortOperation();
261 }
262
263 void HYDROGUI_ImportImageOp::commitOperation()
264 {
265   closePreview();
266
267   HYDROGUI_Operation::commitOperation();
268 }
269
270 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
271 {
272   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
273   connect( aPanel, SIGNAL( createPreview( QImage ) ), SLOT( onCreatePreview( QImage ) ) );
274   connect( aPanel, SIGNAL( activatePointSelection( int ) ), SLOT( onActivatePointSelection( int ) ) );
275   connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ), 
276                      SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
277   connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
278   connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
279                      SLOT( onRefImageActivated( const QString& ) ) );
280   connect( aPanel, SIGNAL( setCIsUsed( bool ) ), SLOT( onSetCIsUsed( bool ) ) );
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   closePreview();
501
502   if( !myIsEdit )
503     module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
504
505   anImageObj->Update();
506
507   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
508   return true;
509 }
510
511 bool HYDROGUI_ImportImageOp::isReferenceCorrect() const
512 {
513   bool isCorrect = true;
514
515   if( myIsEdit && !myEditedObject.IsNull() )
516   {
517     HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
518     Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
519       HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
520     if( !aRefImageObj.IsNull() )
521     {
522       bool isFoundEdited = false;
523       HYDROData_SequenceOfObjects aRefSeq = aRefImageObj->GetAllReferenceObjects();
524       for ( int i = 1, n = aRefSeq.Length(); i <= n && !isFoundEdited; ++i )
525       {
526         Handle(HYDROData_Entity) anObject = aRefSeq.Value( i );
527         isFoundEdited = anObject->GetName() == myEditedObject->GetName();
528       }
529       isCorrect = !isFoundEdited;
530     }
531   }
532   return isCorrect;
533 }
534
535 void HYDROGUI_ImportImageOp::onApply()
536 {
537   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
538
539   bool aCanApply = isReferenceCorrect();
540   if ( !aCanApply && !myEditedObject.IsNull() )
541   {
542     Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
543       HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
544     if ( !aRefImageObj.IsNull() )
545       aCanApply = SUIT_MessageBox::question( module()->getApp()->desktop(),
546                     tr( "CORRECT_INPUT_DATA" ), tr( "CONFIRM_REMOVE_REFERENCE_FROM_IMAGE" ).
547                     arg( aRefImageObj->GetName() ).arg( myEditedObject->GetName() ),
548                     QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes;
549   }
550   if ( aCanApply )
551     HYDROGUI_Operation::onApply();
552   else {
553     aPanel->setRefImageName( "" );
554     onRefImageActivated( aPanel->getRefImageName() );
555   }
556 }
557
558 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
559 {
560   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
561
562   myImage = theImage;
563
564   if( myPreviewPrs ) // if the image is changed by choosing another file
565   {
566     myPreviewPrs->setImage( myImage, true );
567     if ( sender() ) // reset the previous presentation settings
568     {
569       QString aFileName = aPanel->getFileName();
570
571       aPanel->reset();
572       aPanel->setIsEdit( myIsEdit );
573       // restore the file name
574       aPanel->setFileName( aFileName );
575       // fill the reference list
576       HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
577       getReferenceDataList( aPrsPointDataList );
578       aPanel->setPrsPointDataList( aPrsPointDataList );
579     }
580     if( myPreviewViewManager )
581     {
582       if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
583       {
584         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
585         {
586           aViewPort->onBoundingRectChanged();
587           aViewPort->fitAll();
588         }
589       }
590     }
591   }
592   else
593   {
594     LightApp_Application* anApp = module()->getApp();
595
596     myActiveViewManager = anApp->activeViewManager();
597
598     myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
599     myPreviewPrs->setImage( myImage, true );
600
601     myPreviewViewManager =
602       dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
603     if( myPreviewViewManager )
604     {
605       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
606                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
607
608       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
609       myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
610       if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
611       {
612         if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
613         {
614           //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
615
616           aViewPort->addItem( myPreviewPrs );
617           aViewPort->fitAll();
618           
619           if ( myEditedObject ) {
620             size_t aViewId = (size_t)aViewer;
621             module()->setObjectVisible( aViewId, myEditedObject, true );
622           }
623           
624           myPreviewPrs->setIsTransformationPointPreview( true );
625         }
626         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
627                  this, SLOT( onPointSelected() ) );
628       }
629     }
630   }
631
632   // Set the image name in the dialog
633   QString anImageName = aPanel->getImageName().simplified();
634   // If edit mode and the name was not set yet then get from the edited object
635   if( myIsEdit && anImageName.isEmpty() )
636   {
637     if( !myEditedObject.IsNull() )
638       anImageName = myEditedObject->GetName();
639   }
640   // If the name was not set then initialize it from the selected file name
641   if ( anImageName.isEmpty() )
642   {
643     anImageName = aPanel->getFileName();
644     if ( !anImageName.isEmpty() ) {
645         anImageName = QFileInfo( anImageName ).baseName();
646     }
647     // If no file name then generate a new image name
648     if ( anImageName.isEmpty() ) {
649       anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
650     }
651   }
652   aPanel->setImageName( anImageName );
653
654   aPanel->setImageSize( myImage.size() );
655
656   aPanel->initializePointSelection();
657   onPointSelected( false );
658   onSetCIsUsed( !aPanel->isByTwoPoints() );
659 }
660
661 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
662 {
663   myPointType = thePointType;
664   if( myPreviewPrs )
665     myPreviewPrs->setTransformationPointType( thePointType );
666   if( myRefPreviewPrs )
667     myRefPreviewPrs->setTransformationPointType( thePointType );
668 }
669
670 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
671                                                   int thePointType,
672                                                   bool theIsY,
673                                                   int theValue )
674 {
675   if( !theIsRef && myPreviewPrs )
676     myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
677   else if( theIsRef && myRefPreviewPrs )
678     myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
679 }
680
681 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
682 {
683   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
684
685   QString aRefImageName;
686   if( theMode == HYDROData_Image::ReferenceImage )
687   {
688     aRefImageName = aPanel->getRefImageName();
689     if( aRefImageName.isEmpty() )
690       return; // do nothing in this case to avoid visual moving of preview prs
691     onRefImageActivated( aRefImageName );
692   }
693   else
694   {
695     if( myRefViewManager )
696     {
697       closeView( myRefViewManager );
698     }
699   }
700 }
701
702 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
703 {
704   if( theName.isEmpty() ) {
705     if( myRefViewManager )
706       closeView( myRefViewManager );
707     return;
708   }
709
710   GraphicsView_ViewPort* aViewPort = 0;
711
712   LightApp_Application* anApp = module()->getApp();
713   ///// Get a view port for the reference image preview
714   if( myRefViewManager )
715   {
716     if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
717     {
718       aViewPort = aViewer->getActiveViewPort();
719     }
720   }
721   else
722   {
723     anApp = module()->getApp();
724     // Init reference image preview
725     myRefViewManager =
726       dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
727     if( myRefViewManager )
728     {
729       connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
730                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
731
732       module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
733       myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
734       if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
735       {
736         aViewPort = aViewer->getActiveViewPort();
737         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
738                  this, SLOT( onRefPointSelected() ) );
739       }
740     }
741   }
742
743   if( !aViewPort )
744     return;
745
746   size_t aViewId = (size_t)myRefViewManager->getViewer();
747
748   // Remove the old presentation of the reference image if any
749   if( myRefPreviewPrs )
750   {
751     module()->setObjectVisible( aViewId, myRefPreviewPrs->getObject(), false );
752
753     myRefPreviewPrs->setCaption( QString() );
754     aViewPort->removeItem( myRefPreviewPrs );
755
756     delete myRefPreviewPrs;
757     myRefPreviewPrs = 0;
758   }
759
760   // Create a new reference image presentation
761   QImage anImage;
762   Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
763     HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
764   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
765   if( !anImageObj.IsNull() )
766   {
767     anImage = anImageObj->Image();
768
769     myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
770     myRefPreviewPrs->setImage( anImage, true );
771
772     myRefPreviewPrs->setIsTransformationPointPreview( true );
773     myRefPreviewPrs->setTransformationPointType( myPointType );
774
775     myRefPreviewPrs->setIsByTwoPoints( aPanel->isByTwoPoints() );
776
777     // Add the new reference image presentation to the appropriate view
778     aViewPort->addItem( myRefPreviewPrs );
779
780     module()->setObjectVisible( aViewId, anImageObj, true );
781   }
782
783   aViewPort->fitAll();
784
785   // Split views horizontally
786   if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
787   {
788     qApp->processEvents();
789     QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
790     aViewPort->activateWindow();
791     aViewPort->show();
792     aViewPort->setFocus(Qt::ActiveWindowFocusReason);
793     aWorkstack->splitHorizontal();
794   }
795
796   // Initialize the dialog
797   aPanel->setImageSize( anImage.size(), true );
798   aPanel->initializePointSelection();
799   onPointSelected( true );
800 }
801
802 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
803 {
804   closePreview();
805 }
806
807 void HYDROGUI_ImportImageOp::onPointSelected()
808 {
809   onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
810 }
811
812 void HYDROGUI_ImportImageOp::onRefPointSelected()
813 {
814   onPointSelected( true );
815 }
816
817 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
818 {
819   HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
820   if( !aPrs )
821     return;
822
823   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
824
825   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
826     aPrs->getTransformationPointMap();
827   
828   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
829   while( anIter.hasNext() )
830   {
831     int aPointType = anIter.next().key();
832     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
833     const QPoint& aPoint = aTransformationPoint.Point;
834
835     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
836     aDataMap[ aPointType ] = aData;
837   }
838
839   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
840 }
841
842 void HYDROGUI_ImportImageOp::closePreview()
843 {
844   closeView( myPreviewViewManager );
845   closeView( myRefViewManager );
846
847   if( myActiveViewManager )
848     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
849 }
850
851 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
852 {
853   if( aViewMgr )
854   {
855     GraphicsView_ViewPort* aViewPort = 0;
856     if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
857     {
858       aViewPort = aViewer->getActiveViewPort();
859     }
860     disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
861                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
862
863     // Nullify appropriate presentation pointer
864     HYDROGUI_PrsImage* aPrs = 0;
865     switch ( module()->getViewManagerRole( aViewMgr ) )
866     {
867       case HYDROGUI_Module::VMR_ReferenceImage:
868         aPrs = myRefPreviewPrs;
869         myRefPreviewPrs = 0;
870         break;
871       case HYDROGUI_Module::VMR_TransformImage:
872         aPrs = myPreviewPrs;
873         myPreviewPrs = 0;
874     }
875
876     // Remove the appropriate presentation from the view
877     if( aPrs && aViewPort )
878     {
879       aViewPort->removeItem( aPrs );
880       delete aPrs;
881     }
882
883     // Delete the view
884     module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here
885     aViewMgr = 0;
886   }
887 }