Salome HOME
12a71ec54cd32c8e6deae6235edf53ef984652fc
[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           myPreviewPrs->setTransformationPointCursorShape( module()->getPrefEditCursor().shape() );
626         }
627         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
628                  this, SLOT( onPointSelected() ) );
629       }
630     }
631   }
632
633   // Set the image name in the dialog
634   QString anImageName = aPanel->getImageName().simplified();
635   // If edit mode and the name was not set yet then get from the edited object
636   if( myIsEdit && anImageName.isEmpty() )
637   {
638     if( !myEditedObject.IsNull() )
639       anImageName = myEditedObject->GetName();
640   }
641   // If the name was not set then initialize it from the selected file name
642   if ( anImageName.isEmpty() )
643   {
644     anImageName = aPanel->getFileName();
645     if ( !anImageName.isEmpty() ) {
646         anImageName = QFileInfo( anImageName ).baseName();
647     }
648     // If no file name then generate a new image name
649     if ( anImageName.isEmpty() ) {
650       anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
651     }
652   }
653   aPanel->setImageName( anImageName );
654
655   aPanel->setImageSize( myImage.size() );
656
657   aPanel->initializePointSelection();
658   onPointSelected( false );
659   onSetCIsUsed( !aPanel->isByTwoPoints() );
660 }
661
662 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
663 {
664   myPointType = thePointType;
665   if( myPreviewPrs )
666     myPreviewPrs->setTransformationPointType( thePointType );
667   if( myRefPreviewPrs )
668     myRefPreviewPrs->setTransformationPointType( thePointType );
669 }
670
671 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
672                                                   int thePointType,
673                                                   bool theIsY,
674                                                   int theValue )
675 {
676   if( !theIsRef && myPreviewPrs )
677     myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
678   else if( theIsRef && myRefPreviewPrs )
679     myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
680 }
681
682 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
683 {
684   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
685
686   QString aRefImageName;
687   if( theMode == HYDROData_Image::ReferenceImage )
688   {
689     aRefImageName = aPanel->getRefImageName();
690     if( aRefImageName.isEmpty() )
691       return; // do nothing in this case to avoid visual moving of preview prs
692     onRefImageActivated( aRefImageName );
693   }
694   else
695   {
696     if( myRefViewManager )
697     {
698       closeView( myRefViewManager );
699     }
700   }
701 }
702
703 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
704 {
705   if( theName.isEmpty() ) {
706     if( myRefViewManager )
707       closeView( myRefViewManager );
708     return;
709   }
710
711   GraphicsView_ViewPort* aViewPort = 0;
712
713   LightApp_Application* anApp = module()->getApp();
714   ///// Get a view port for the reference image preview
715   if( myRefViewManager )
716   {
717     if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
718     {
719       aViewPort = aViewer->getActiveViewPort();
720     }
721   }
722   else
723   {
724     anApp = module()->getApp();
725     // Init reference image preview
726     myRefViewManager =
727       dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
728     if( myRefViewManager )
729     {
730       connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
731                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
732
733       module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
734       myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
735       if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
736       {
737         aViewPort = aViewer->getActiveViewPort();
738         connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
739                  this, SLOT( onRefPointSelected() ) );
740       }
741     }
742   }
743
744   if( !aViewPort )
745     return;
746
747   size_t aViewId = (size_t)myRefViewManager->getViewer();
748
749   // Remove the old presentation of the reference image if any
750   if( myRefPreviewPrs )
751   {
752     module()->setObjectVisible( aViewId, myRefPreviewPrs->getObject(), false );
753
754     myRefPreviewPrs->setCaption( QString() );
755     aViewPort->removeItem( myRefPreviewPrs );
756
757     delete myRefPreviewPrs;
758     myRefPreviewPrs = 0;
759   }
760
761   // Create a new reference image presentation
762   QImage anImage;
763   Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
764     HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
765   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
766   if( !anImageObj.IsNull() )
767   {
768     anImage = anImageObj->Image();
769
770     myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
771     myRefPreviewPrs->setImage( anImage, true );
772
773     myRefPreviewPrs->setIsTransformationPointPreview( true );
774     myRefPreviewPrs->setTransformationPointType( myPointType );
775
776     myRefPreviewPrs->setIsByTwoPoints( aPanel->isByTwoPoints() );
777
778     myRefPreviewPrs->setTransformationPointCursorShape( module()->getPrefEditCursor().shape() );
779
780     // Add the new reference image presentation to the appropriate view
781     aViewPort->addItem( myRefPreviewPrs );
782
783     module()->setObjectVisible( aViewId, anImageObj, true );
784   }
785
786   aViewPort->fitAll();
787
788   // Split views horizontally
789   if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
790   {
791     qApp->processEvents();
792     QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
793     aViewPort->activateWindow();
794     aViewPort->show();
795     aViewPort->setFocus(Qt::ActiveWindowFocusReason);
796     aWorkstack->splitHorizontal();
797   }
798
799   // Initialize the dialog
800   aPanel->setImageSize( anImage.size(), true );
801   aPanel->initializePointSelection();
802   onPointSelected( true );
803 }
804
805 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
806 {
807   closePreview();
808 }
809
810 void HYDROGUI_ImportImageOp::onPointSelected()
811 {
812   onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
813 }
814
815 void HYDROGUI_ImportImageOp::onRefPointSelected()
816 {
817   onPointSelected( true );
818 }
819
820 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
821 {
822   HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
823   if( !aPrs )
824     return;
825
826   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
827
828   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
829     aPrs->getTransformationPointMap();
830   
831   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
832   while( anIter.hasNext() )
833   {
834     int aPointType = anIter.next().key();
835     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
836     const QPoint& aPoint = aTransformationPoint.Point;
837
838     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
839     aDataMap[ aPointType ] = aData;
840   }
841
842   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
843 }
844
845 void HYDROGUI_ImportImageOp::closePreview()
846 {
847   closeView( myPreviewViewManager );
848   closeView( myRefViewManager );
849
850   if( myActiveViewManager )
851     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
852 }
853
854 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
855 {
856   if( aViewMgr )
857   {
858     GraphicsView_ViewPort* aViewPort = 0;
859     if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
860     {
861       aViewPort = aViewer->getActiveViewPort();
862     }
863     disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
864                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
865
866     // Nullify appropriate presentation pointer
867     HYDROGUI_PrsImage* aPrs = 0;
868     switch ( module()->getViewManagerRole( aViewMgr ) )
869     {
870       case HYDROGUI_Module::VMR_ReferenceImage:
871         aPrs = myRefPreviewPrs;
872         myRefPreviewPrs = 0;
873         break;
874       case HYDROGUI_Module::VMR_TransformImage:
875         aPrs = myPreviewPrs;
876         myPreviewPrs = 0;
877     }
878
879     // Remove the appropriate presentation from the view
880     if( aPrs && aViewPort )
881     {
882       aViewPort->removeItem( aPrs );
883       delete aPrs;
884     }
885
886     // Delete the view
887     module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here
888     aViewMgr = 0;
889   }
890 }