Salome HOME
Minor changes.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportImageOp.cxx
1 // Copyright (C) 2007-2013  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.
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_ImportImageDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_PrsImage.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
31
32 #include <HYDROData_Iterator.h>
33
34 #include <GraphicsView_ViewManager.h>
35 #include <GraphicsView_ViewPort.h>
36 #include <GraphicsView_Viewer.h>
37
38 #include <LightApp_Application.h>
39 #include <LightApp_UpdateFlags.h>
40
41 HYDROGUI_ImportImageOp::HYDROGUI_ImportImageOp( HYDROGUI_Module* theModule,
42                                                 const bool theIsEdit )
43 : HYDROGUI_Operation( theModule ),
44   myIsEdit( theIsEdit ),
45   myEditedObject( 0 ),
46   myActiveViewManager( 0 ),
47   myPreviewViewManager( 0 ),
48   myPreviewPrs( 0 ),
49   myRefPreviewPrs( 0 ),
50   myPointType( HYDROGUI_PrsImage::None )
51 {
52   setName( theIsEdit ? tr( "EDIT_IMPORTED_IMAGE" ) : tr( "IMPORT_IMAGE" ) );
53 }
54
55 HYDROGUI_ImportImageOp::~HYDROGUI_ImportImageOp()
56 {
57 }
58
59 void HYDROGUI_ImportImageOp::startOperation()
60 {
61   HYDROGUI_Operation::startOperation();
62
63   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
64   aPanel->reset();
65   aPanel->setIsEdit( myIsEdit );
66
67   if( myIsEdit )
68   {
69     myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
70     if( !myEditedObject.IsNull() )
71     {
72       QImage anImage = myEditedObject->Image();
73
74       QPoint aPointA1, aPointB1, aPointC1;
75       QPointF aPointA2, aPointB2, aPointC2;
76       myEditedObject->TrsfPoints( aPointA1, aPointB1, aPointC1,
77                                   aPointA2, aPointB2, aPointC2 );
78
79       onCreatePreview( anImage );
80
81       if( myPreviewPrs )
82       {
83         HYDROGUI_PrsImage::TransformationPointMap aPointMap =
84           myPreviewPrs->getTransformationPointMap();
85         if( !aPointMap.isEmpty() )
86         {
87           aPointMap[ HYDROGUI_PrsImage::PointA ].Point = aPointA1;
88           aPointMap[ HYDROGUI_PrsImage::PointB ].Point = aPointB1;
89           aPointMap[ HYDROGUI_PrsImage::PointC ].Point = aPointC1;
90           myPreviewPrs->setTransformationPointMap( aPointMap );
91         }
92       }
93
94       HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
95       aDataMap[ HYDROGUI_PrsImage::PointA ] =
96         HYDROGUI_ImportImageDlg::TransformationData( aPointA1, aPointA2 );
97       aDataMap[ HYDROGUI_PrsImage::PointB ] =
98         HYDROGUI_ImportImageDlg::TransformationData( aPointB1, aPointB2 );
99       aDataMap[ HYDROGUI_PrsImage::PointC ] =
100         HYDROGUI_ImportImageDlg::TransformationData( aPointC1, aPointC2 );
101       ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap );
102     }
103   }
104
105   // collect information about existing images
106   HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
107   HYDROData_Iterator anIterator( doc(), KIND_IMAGE );
108   for( ; anIterator.More(); anIterator.Next() )
109   {
110     Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
111     if( !anImageObj.IsNull() )
112     {
113       if( myIsEdit && HYDROGUI_Tool::IsEqual( anImageObj, myEditedObject ) )
114         continue;
115
116       QPoint aPointA1, aPointB1, aPointC1;
117       QPointF aPointA2, aPointB2, aPointC2;
118       anImageObj->TrsfPoints( aPointA1, aPointB1, aPointC1,
119                               aPointA2, aPointB2, aPointC2 );
120
121       HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
122       aDataMap[ HYDROGUI_PrsImage::PointA ] =
123         HYDROGUI_ImportImageDlg::TransformationData( aPointA1, aPointA2 );
124       aDataMap[ HYDROGUI_PrsImage::PointB ] =
125         HYDROGUI_ImportImageDlg::TransformationData( aPointB1, aPointB2 );
126       aDataMap[ HYDROGUI_PrsImage::PointC ] =
127         HYDROGUI_ImportImageDlg::TransformationData( aPointC1, aPointC2 );
128
129       HYDROGUI_ImportImageDlg::PrsPointData aPrsPointData( anImageObj->GetName(), aDataMap );
130       aPrsPointDataList.append( aPrsPointData );
131     }
132   }
133   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setPrsPointDataList( aPrsPointDataList );
134 }
135
136 void HYDROGUI_ImportImageOp::abortOperation()
137 {
138   closePreview();
139
140   HYDROGUI_Operation::abortOperation();
141 }
142
143 void HYDROGUI_ImportImageOp::commitOperation()
144 {
145   closePreview();
146
147   HYDROGUI_Operation::commitOperation();
148 }
149
150 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
151 {
152   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
153   connect( aPanel, SIGNAL( createPreview( QImage ) ),
154            this, SLOT( onCreatePreview( QImage ) ) );
155   connect( aPanel, SIGNAL( activatePointSelection( int ) ),
156            this, SLOT( onActivatePointSelection( int ) ) );
157   connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ),
158            this, SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
159   connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
160            this, SLOT( onRefImageActivated( const QString& ) ) );
161   return aPanel;
162 }
163
164 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
165                                            QString& theErrorMsg )
166 {
167   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
168
169   QString anImageName = aPanel->getImageName();
170   if( anImageName.isEmpty() )
171     return false;
172
173   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
174   {
175     // check that there are no other objects with the same name in the document
176     Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
177     if( !anObject.IsNull() )
178     {
179       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
180       return false;
181     }
182   }
183
184   if( !myPreviewPrs )
185     return false;
186
187   QImage anImage = myPreviewPrs->getImage();
188
189   bool anIsRefImage = aPanel->getTransformationMode() == HYDROGUI_ImportImageDlg::RefImage;
190
191   QTransform aRefTransform;
192   if( anIsRefImage && myRefPreviewPrs )
193   {
194     Handle(HYDROData_Image) aRefImageObj =
195       Handle(HYDROData_Image)::DownCast( myRefPreviewPrs->getObject() );
196     if( !aRefImageObj.IsNull() )
197       aRefTransform = aRefImageObj->Trsf();
198   }
199
200   HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
201   if( !aPanel->getTransformationDataMap( aMap ) )
202     return false;
203
204   HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
205   if( anIsRefImage && !aPanel->getTransformationDataMap( aRefMap, true ) )
206     return false;
207
208   QPoint aPointA1 = aMap[ HYDROGUI_PrsImage::PointA ].first;
209   QPoint aPointB1 = aMap[ HYDROGUI_PrsImage::PointB ].first;
210   QPoint aPointC1 = aMap[ HYDROGUI_PrsImage::PointC ].first;
211
212   QPointF aPointA2 = aMap[ HYDROGUI_PrsImage::PointA ].second;
213   QPointF aPointB2 = aMap[ HYDROGUI_PrsImage::PointB ].second;
214   QPointF aPointC2 = aMap[ HYDROGUI_PrsImage::PointC ].second;
215
216   QPoint aPointA3, aPointB3, aPointC3;
217   if( anIsRefImage )
218   {
219     aPointA3 = aRefMap[ HYDROGUI_PrsImage::PointA ].first;
220     aPointB3 = aRefMap[ HYDROGUI_PrsImage::PointB ].first;
221     aPointC3 = aRefMap[ HYDROGUI_PrsImage::PointC ].first;
222   }
223
224   int xa1 = aPointA1.x();
225   int ya1 = aPointA1.y();
226   int xb1 = aPointB1.x();
227   int yb1 = aPointB1.y();
228   int xc1 = aPointC1.x();
229   int yc1 = aPointC1.y();
230
231   double xa2 = aPointA2.x();
232   double ya2 = aPointA2.y();
233   double xb2 = aPointB2.x();
234   double yb2 = aPointB2.y();
235   double xc2 = aPointC2.x();
236   double yc2 = aPointC2.y();
237
238   int xa3 = aPointA3.x();
239   int ya3 = aPointA3.y();
240   int xb3 = aPointB3.x();
241   int yb3 = aPointB3.y();
242   int xc3 = aPointC3.x();
243   int yc3 = aPointC3.y();
244
245   // first, check that three input points don't belong to a single line
246   if( ( yb1 - ya1 ) * ( xc1 - xa1 ) == ( yc1 - ya1 ) * ( xb1 - xa1 ) )
247   {
248     theErrorMsg = tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" );
249     return false;
250   }
251
252   // the same check for the reference points
253   if( anIsRefImage && ( ( yb3 - ya3 ) * ( xc3 - xa3 ) == ( yc3 - ya3 ) * ( xb3 - xa3 ) ) )
254   {
255     theErrorMsg = tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" );
256     return false;
257   }
258
259   QTransform aTransform1( xa1, ya1, 1, xb1, yb1, 1, xc1, yc1, 1 );
260   QTransform aTransform2( xa2, ya2, 1, xb2, yb2, 1, xc2, yc2, 1 );
261   QTransform aTransform3( xa3, ya3, 1, xb3, yb3, 1, xc3, yc3, 1 );
262
263   bool anIsInvertible = false;
264   QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
265   if( !anIsInvertible )
266   {
267     theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
268     return false;
269   }
270
271   QTransform aTransform;
272   if( anIsRefImage )
273     aTransform = aTransform1Inverted * aTransform3 * aRefTransform;
274   else
275     aTransform = aTransform1Inverted * aTransform2;
276
277   Handle(HYDROData_Image) anImageObj;
278   if( myIsEdit )
279     anImageObj = myEditedObject;
280   else
281     anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
282
283   if( anImageObj.IsNull() )
284     return false;
285
286   anImageObj->SetName( anImageName );
287
288   anImageObj->SetImage( anImage );
289   anImageObj->SetTrsf( aTransform );
290
291   if( anIsRefImage )
292   {
293     aPointA2 = QPointF( aTransform.map( aPointA1 ) );
294     aPointB2 = QPointF( aTransform.map( aPointB1 ) );
295     aPointC2 = QPointF( aTransform.map( aPointC1 ) );
296   }
297
298   anImageObj->SetTrsfPoints( aPointA1, aPointB1, aPointC1,
299                              aPointA2, aPointB2, aPointC2 );
300
301   // must be done after all checks and before calling SetVisible() method below
302   closePreview();
303
304   anImageObj->SetVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), true );
305
306   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced;
307   return true;
308 }
309
310 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
311 {
312   LightApp_Application* anApp = module()->getApp();
313
314   myActiveViewManager = anApp->activeViewManager();
315
316   myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
317   myPreviewPrs->setImage( theImage );
318   myPreviewPrs->compute();
319
320   myPreviewViewManager =
321     dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
322   if( myPreviewViewManager )
323   {
324     module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
325     myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
326     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
327     {
328       if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
329       {
330         //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
331
332         aViewPort->addItem( myPreviewPrs );
333         aViewPort->fitAll();
334
335         myPreviewPrs->setIsTransformationPointPreview( true );
336       }
337       connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
338                this, SLOT( onPointSelected() ) );
339     }
340   }
341
342   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
343
344   QString anImageName;
345   if( myIsEdit )
346   {
347     if( !myEditedObject.IsNull() )
348       anImageName = myEditedObject->GetName();
349   }
350   else
351     anImageName = HYDROGUI_Tool::GenerateObjectName( module(), "Image" );
352   aPanel->setImageName( anImageName );
353
354   aPanel->setImageSize( theImage.size() );
355
356   aPanel->initializePointSelection();
357   onPointSelected( false );
358 }
359
360 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
361 {
362   myPointType = thePointType;
363   if( myPreviewPrs )
364     myPreviewPrs->setTransformationPointType( thePointType );
365   if( myRefPreviewPrs )
366     myRefPreviewPrs->setTransformationPointType( thePointType );
367 }
368
369 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
370                                                   int thePointType,
371                                                   bool theIsY,
372                                                   int theValue )
373 {
374   if( !theIsRef && myPreviewPrs )
375     myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
376   else if( theIsRef && myRefPreviewPrs )
377     myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
378 }
379
380 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
381 {
382   GraphicsView_ViewPort* aViewPort = 0;
383   if( myPreviewViewManager )
384     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
385       aViewPort = aViewer->getActiveViewPort();
386
387   if( !aViewPort )
388     return;
389
390   aViewPort->removeItem( myRefPreviewPrs );
391
392   delete myRefPreviewPrs;
393   myRefPreviewPrs = 0;
394
395   QImage anImage;
396   Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
397     HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
398   if( !anImageObj.IsNull() )
399   {
400     anImage = anImageObj->Image();
401
402     myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
403     myRefPreviewPrs->setImage( anImage );
404     myRefPreviewPrs->compute();
405
406     myRefPreviewPrs->setIsTransformationPointPreview( true );
407     myRefPreviewPrs->setTransformationPointType( myPointType );
408
409     // vertically shift the reference prs relatively to the main prs
410     if( myPreviewPrs )
411     {
412       QImage anImage = myPreviewPrs->getImage();
413       myRefPreviewPrs->moveBy( 0, anImage.height() + 20 );
414     }
415
416     aViewPort->addItem( myRefPreviewPrs );
417   }
418
419   aViewPort->fitAll();
420
421   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
422
423   aPanel->setImageSize( anImage.size(), true );
424
425   aPanel->initializePointSelection();
426   onPointSelected( true );
427 }
428
429 void HYDROGUI_ImportImageOp::onPointSelected()
430 {
431   onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
432 }
433
434 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
435 {
436   HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
437   if( !aPrs )
438     return;
439
440   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
441
442   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
443     aPrs->getTransformationPointMap();
444   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
445   while( anIter.hasNext() )
446   {
447     int aPointType = anIter.next().key();
448     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
449     const QPoint& aPoint = aTransformationPoint.Point;
450
451     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF() );
452     aDataMap[ aPointType ] = aData;
453   }
454
455   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
456 }
457
458 void HYDROGUI_ImportImageOp::closePreview()
459 {
460   if( myPreviewPrs )
461   {
462     delete myPreviewPrs;
463     myPreviewPrs = 0;
464   }
465
466   if( myRefPreviewPrs )
467   {
468     delete myRefPreviewPrs;
469     myRefPreviewPrs = 0;
470   }
471
472   if( myPreviewViewManager )
473   {
474     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
475     myPreviewViewManager = 0;
476   }
477
478   if( myActiveViewManager )
479     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
480 }