Salome HOME
View management.
[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_Document.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   myPointType( HYDROGUI_PrsImage::None )
50 {
51   setName( theIsEdit ? tr( "EDIT_IMAGE" ) : tr( "IMPORT_IMAGE" ) );
52 }
53
54 HYDROGUI_ImportImageOp::~HYDROGUI_ImportImageOp()
55 {
56 }
57
58 void HYDROGUI_ImportImageOp::startOperation()
59 {
60   HYDROGUI_Operation::startOperation();
61
62   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
63   aPanel->reset();
64   aPanel->setIsEdit( myIsEdit );
65
66   if( myIsEdit )
67   {
68     myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
69     if( !myEditedObject.IsNull() )
70     {
71       QImage anImage = myEditedObject->Image();
72
73       QPoint aPointA1, aPointB1, aPointC1;
74       QPointF aPointA2, aPointB2, aPointC2;
75       myEditedObject->TrsfPoints( aPointA1, aPointB1, aPointC1,
76                                   aPointA2, aPointB2, aPointC2 );
77
78       onCreatePreview( anImage );
79
80       if( myPreviewPrs )
81       {
82         HYDROGUI_PrsImage::TransformationPointMap aPointMap =
83           myPreviewPrs->getTransformationPointMap();
84         if( !aPointMap.isEmpty() )
85         {
86           aPointMap[ HYDROGUI_PrsImage::PointA ].Point = aPointA1;
87           aPointMap[ HYDROGUI_PrsImage::PointB ].Point = aPointB1;
88           aPointMap[ HYDROGUI_PrsImage::PointC ].Point = aPointC1;
89           myPreviewPrs->setTransformationPointMap( aPointMap );
90         }
91       }
92
93       HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
94       aDataMap[ HYDROGUI_PrsImage::PointA ] =
95         HYDROGUI_ImportImageDlg::TransformationData( aPointA1, aPointA2 );
96       aDataMap[ HYDROGUI_PrsImage::PointB ] =
97         HYDROGUI_ImportImageDlg::TransformationData( aPointB1, aPointB2 );
98       aDataMap[ HYDROGUI_PrsImage::PointC ] =
99         HYDROGUI_ImportImageDlg::TransformationData( aPointC1, aPointC2 );
100       ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap );
101     }
102   }
103 }
104
105 void HYDROGUI_ImportImageOp::abortOperation()
106 {
107   closePreview();
108
109   HYDROGUI_Operation::abortOperation();
110 }
111
112 void HYDROGUI_ImportImageOp::commitOperation()
113 {
114   closePreview();
115
116   HYDROGUI_Operation::commitOperation();
117 }
118
119 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
120 {
121   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
122   connect( aPanel, SIGNAL( createPreview( QImage ) ),
123            this, SLOT( onCreatePreview( QImage ) ) );
124   connect( aPanel, SIGNAL( activatePointSelection( int ) ),
125            this, SLOT( onActivatePointSelection( int ) ) );
126   return aPanel;
127 }
128
129 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
130                                            QString& theErrorMsg )
131 {
132   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
133
134   QString anImageName = aPanel->getImageName();
135   if( anImageName.isEmpty() )
136     return false;
137
138   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
139   {
140     // check that there are no other objects with the same name in the document
141     Handle(HYDROData_Object) anObject = findObjectByName( anImageName, KIND_UNKNOWN );
142     if( !anObject.IsNull() )
143     {
144       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
145       return false;
146     }
147   }
148
149   QImage anImage = myPreviewPrs->getImage();
150
151   HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
152   bool anIsOk = aPanel->getTransformationDataMap( aMap );
153   if( !anIsOk || !myPreviewPrs )
154     return false;
155
156   QPoint aPointA1 = aMap[ HYDROGUI_PrsImage::PointA ].first;
157   QPoint aPointB1 = aMap[ HYDROGUI_PrsImage::PointB ].first;
158   QPoint aPointC1 = aMap[ HYDROGUI_PrsImage::PointC ].first;
159
160   QPointF aPointA2 = aMap[ HYDROGUI_PrsImage::PointA ].second;
161   QPointF aPointB2 = aMap[ HYDROGUI_PrsImage::PointB ].second;
162   QPointF aPointC2 = aMap[ HYDROGUI_PrsImage::PointC ].second;
163
164   int xa1 = aPointA1.x();
165   int ya1 = aPointA1.y();
166   int xb1 = aPointB1.x();
167   int yb1 = aPointB1.y();
168   int xc1 = aPointC1.x();
169   int yc1 = aPointC1.y();
170
171   double xa2 = aPointA2.x();
172   double ya2 = aPointA2.y();
173   double xb2 = aPointB2.x();
174   double yb2 = aPointB2.y();
175   double xc2 = aPointC2.x();
176   double yc2 = aPointC2.y();
177
178   // first, check that three input points don't belong to a single line
179   if( ( yb1 - ya1 ) * ( xc1 - xa1 ) == ( yc1 - ya1 ) * ( xb1 - xa1 ) )
180   {
181     theErrorMsg = tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" );
182     return false;
183   }
184
185   QTransform aTransform1( xa1, ya1, 1, xb1, yb1, 1, xc1, yc1, 1 );
186   QTransform aTransform2( xa2, ya2, 1, xb2, yb2, 1, xc2, yc2, 1 );
187
188   bool anIsInvertible = false;
189   QTransform aTransform = aTransform1.inverted( &anIsInvertible ) * aTransform2;
190   if( !anIsInvertible )
191   {
192     theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
193     return false;
194   }
195
196   Handle(HYDROData_Image) anImageObj;
197   if( myIsEdit )
198     anImageObj = myEditedObject;
199   else
200     anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
201
202   if( anImageObj.IsNull() )
203     return false;
204
205   anImageObj->SetName( anImageName );
206
207   anImageObj->SetImage( anImage );
208   anImageObj->SetTrsf( aTransform );
209
210   anImageObj->SetTrsfPoints( aPointA1, aPointB1, aPointC1,
211                              aPointA2, aPointB2, aPointC2 );
212
213   anImageObj->SetVisibility( true );
214
215   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced;
216   return true;
217 }
218
219 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
220 {
221   LightApp_Application* anApp = module()->getApp();
222
223   myActiveViewManager = anApp->activeViewManager();
224
225   myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
226   myPreviewPrs->setImage( theImage );
227   myPreviewPrs->compute();
228
229   myPreviewViewManager =
230     dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
231   if( myPreviewViewManager )
232   {
233     module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_Mapping );
234     myPreviewViewManager->setTitle( tr( "MAPPING" ) );
235     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
236     {
237       if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
238       {
239         aViewPort->setMousePositionEnabled( true );
240
241         aViewPort->addItem( myPreviewPrs );
242         aViewPort->fitAll();
243
244         myPreviewPrs->setIsTransformationPointPreview( true );
245       }
246       connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
247                this, SLOT( onPointSelected() ) );
248     }
249   }
250
251   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
252
253   QString anImageName;
254   if( myIsEdit )
255   {
256     if( !myEditedObject.IsNull() )
257       anImageName = myEditedObject->GetName();
258   }
259   else
260   {
261     int anImageId = 1;
262     while( anImageId < 100 )
263     {
264       anImageName = QString( "Image_%1" ).arg( QString::number( anImageId++ ) );
265
266       // check that there are no other objects with the same name in the document
267       Handle(HYDROData_Object) anObject = findObjectByName( anImageName, KIND_UNKNOWN );
268       if( anObject.IsNull() )
269         break;
270     }
271   }
272   aPanel->setImageName( anImageName );
273
274   aPanel->initializePointSelection();
275   onPointSelected();
276 }
277
278 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
279 {
280   myPointType = thePointType;
281   if( myPreviewPrs )
282     myPreviewPrs->setTransformationPointMode( thePointType );
283 }
284
285 void HYDROGUI_ImportImageOp::onPointSelected()
286 {
287   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
288
289   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
290     myPreviewPrs->getTransformationPointMap();
291   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
292   while( anIter.hasNext() )
293   {
294     int aPointType = anIter.next().key();
295     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
296     const QPoint& aPoint = aTransformationPoint.Point;
297
298     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF() );
299     aDataMap[ aPointType ] = aData;
300   }
301
302   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true );
303 }
304
305 void HYDROGUI_ImportImageOp::closePreview()
306 {
307   if( myPreviewPrs )
308   {
309     delete myPreviewPrs;
310     myPreviewPrs = 0;
311   }
312
313   if( myPreviewViewManager )
314   {
315     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
316     myPreviewViewManager = 0;
317   }
318
319   if( myActiveViewManager )
320     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
321 }