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