Salome HOME
1) Edit Image operation
[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 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
106 {
107   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
108   connect( aPanel, SIGNAL( createPreview( QImage ) ),
109            this, SLOT( onCreatePreview( QImage ) ) );
110   connect( aPanel, SIGNAL( activatePointSelection( int ) ),
111            this, SLOT( onActivatePointSelection( int ) ) );
112   return aPanel;
113 }
114
115 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags )
116 {
117   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
118
119   HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
120   bool anIsOk = aPanel->getTransformationDataMap( aMap );
121   if( !anIsOk || !myPreviewPrs )
122     return false;
123
124   QPoint aPointA1 = aMap[ HYDROGUI_PrsImage::PointA ].first;
125   QPoint aPointB1 = aMap[ HYDROGUI_PrsImage::PointB ].first;
126   QPoint aPointC1 = aMap[ HYDROGUI_PrsImage::PointC ].first;
127
128   QPointF aPointA2 = aMap[ HYDROGUI_PrsImage::PointA ].second;
129   QPointF aPointB2 = aMap[ HYDROGUI_PrsImage::PointB ].second;
130   QPointF aPointC2 = aMap[ HYDROGUI_PrsImage::PointC ].second;
131
132   double xa1 = aPointA1.x();
133   double ya1 = aPointA1.y();
134   double xb1 = aPointB1.x();
135   double yb1 = aPointB1.y();
136   double xc1 = aPointC1.x();
137   double yc1 = aPointC1.y();
138
139   double xa2 = aPointA2.x();
140   double ya2 = aPointA2.y();
141   double xb2 = aPointB2.x();
142   double yb2 = aPointB2.y();
143   double xc2 = aPointC2.x();
144   double yc2 = aPointC2.y();
145
146   QTransform aTransform1( xa1, ya1, 1, xb1, yb1, 1, xc1, yc1, 1 );
147   QTransform aTransform2( xa2, ya2, 1, xb2, yb2, 1, xc2, yc2, 1 );
148
149   QTransform aTransform = aTransform1.inverted() * aTransform2;
150
151   QImage anImage = myPreviewPrs->getImage();
152
153   closePreview();
154
155   Handle(HYDROData_Image) anImageObj;
156   if( myIsEdit )
157     anImageObj = myEditedObject;
158   else
159     anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
160
161   if( anImageObj.IsNull() )
162     return false;
163
164   if( !myIsEdit )
165   {
166     static int ImageId = 0;
167     anImageObj->SetName( QString( "Image_%1" ).arg( QString::number( ++ImageId ) ) );
168   }
169
170   anImageObj->SetImage( anImage );
171   anImageObj->SetTrsf( aTransform );
172
173   anImageObj->SetTrsfPoints( aPointA1, aPointB1, aPointC1,
174                              aPointA2, aPointB2, aPointC2 );
175
176   anImageObj->SetVisibility( true );
177
178   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced;
179   return true;
180 }
181
182 void HYDROGUI_ImportImageOp::processCancel()
183 {
184   closePreview();
185 }
186
187 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
188 {
189   LightApp_Application* anApp = module()->getApp();
190
191   myActiveViewManager = anApp->activeViewManager();
192
193   myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
194   myPreviewPrs->setImage( theImage );
195   myPreviewPrs->compute();
196
197   myPreviewViewManager =
198     dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
199   if( myPreviewViewManager )
200   {
201     myPreviewViewManager->setTitle( tr( "MAPPING" ) );
202     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
203     {
204       if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
205       {
206         aViewPort->setMousePositionEnabled( true );
207
208         aViewPort->addItem( myPreviewPrs );
209         aViewPort->fitAll();
210
211         myPreviewPrs->setIsTransformationPointPreview( true );
212       }
213       connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
214                this, SLOT( onPointSelected() ) );
215     }
216   }
217
218   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
219
220   aPanel->initializePointSelection();
221   onPointSelected();
222 }
223
224 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
225 {
226   myPointType = thePointType;
227   if( myPreviewPrs )
228     myPreviewPrs->setTransformationPointMode( thePointType );
229 }
230
231 void HYDROGUI_ImportImageOp::onPointSelected()
232 {
233   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
234
235   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
236     myPreviewPrs->getTransformationPointMap();
237   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
238   while( anIter.hasNext() )
239   {
240     int aPointType = anIter.next().key();
241     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
242     const QPoint& aPoint = aTransformationPoint.Point;
243
244     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF() );
245     aDataMap[ aPointType ] = aData;
246   }
247
248   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true );
249 }
250
251 void HYDROGUI_ImportImageOp::closePreview()
252 {
253   if( myPreviewPrs )
254   {
255     delete myPreviewPrs;
256     myPreviewPrs = 0;
257   }
258
259   if( myPreviewViewManager )
260   {
261     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
262     myPreviewViewManager = 0;
263   }
264
265   if( myActiveViewManager )
266     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
267 }