Salome HOME
21ab2da2defd8682bce888cee104266e1b7edf53
[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_ImportImageDlg.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_PrsImage.h"
28 #include "HYDROGUI_Tool.h"
29
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Image.h>
32
33 #include <GraphicsView_ViewManager.h>
34 #include <GraphicsView_ViewPort.h>
35 #include <GraphicsView_Viewer.h>
36
37 #include <LightApp_Application.h>
38 #include <LightApp_UpdateFlags.h>
39
40 HYDROGUI_ImportImageOp::HYDROGUI_ImportImageOp( HYDROGUI_Module* theModule )
41 : HYDROGUI_Operation( theModule ),
42   myActiveViewManager( 0 ),
43   myPreviewViewManager( 0 ),
44   myPreviewPrs( 0 ),
45   myPointType( HYDROGUI_PrsImage::None )
46 {
47   setName( tr( "IMPORT_IMAGE" ) );
48 }
49
50 HYDROGUI_ImportImageOp::~HYDROGUI_ImportImageOp()
51 {
52 }
53
54 void HYDROGUI_ImportImageOp::startOperation()
55 {
56   HYDROGUI_Operation::startOperation();
57
58   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
59   aPanel->reset();
60 }
61
62 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
63 {
64   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
65   connect( aPanel, SIGNAL( createPreview( QString ) ),
66            this, SLOT( onCreatePreview( QString ) ) );
67   connect( aPanel, SIGNAL( activatePointSelection( int ) ),
68            this, SLOT( onActivatePointSelection( int ) ) );
69   return aPanel;
70 }
71
72 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags )
73 {
74   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
75
76   HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
77   bool anIsOk = aPanel->getTransformationDataMap( aMap );
78   if( !anIsOk || !myPreviewPrs )
79     return false;
80
81   double xa1 = aMap[ HYDROGUI_PrsImage::PointA ].first.x();
82   double ya1 = aMap[ HYDROGUI_PrsImage::PointA ].first.y();
83   double xb1 = aMap[ HYDROGUI_PrsImage::PointB ].first.x();
84   double yb1 = aMap[ HYDROGUI_PrsImage::PointB ].first.y();
85   double xc1 = aMap[ HYDROGUI_PrsImage::PointC ].first.x();
86   double yc1 = aMap[ HYDROGUI_PrsImage::PointC ].first.y();
87
88   double xa2 = aMap[ HYDROGUI_PrsImage::PointA ].second.x();
89   double ya2 = aMap[ HYDROGUI_PrsImage::PointA ].second.y();
90   double xb2 = aMap[ HYDROGUI_PrsImage::PointB ].second.x();
91   double yb2 = aMap[ HYDROGUI_PrsImage::PointB ].second.y();
92   double xc2 = aMap[ HYDROGUI_PrsImage::PointC ].second.x();
93   double yc2 = aMap[ HYDROGUI_PrsImage::PointC ].second.y();
94
95   QTransform aTransform1( xa1, ya1, 1, xb1, yb1, 1, xc1, yc1, 1 );
96   QTransform aTransform2( xa2, ya2, 1, xb2, yb2, 1, xc2, yc2, 1 );
97
98   QTransform aTransform = aTransform1.inverted() * aTransform2;
99
100   QImage anImage = myPreviewPrs->getImage();
101
102   closePreview();
103
104   int aStudyId = module()->getStudyId();
105   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
106   if( aDocument.IsNull() )
107     return false;
108
109   Handle(HYDROData_Image) anImageObj =
110     Handle(HYDROData_Image)::DownCast( aDocument->CreateObject( KIND_IMAGE ) );
111   if( anImageObj.IsNull() )
112     return false;
113
114   static int ImageId = 0;
115   anImageObj->SetName( QString( "Image_%1" ).arg( QString::number( ++ImageId ) ) );
116
117   anImageObj->SetImage( anImage );
118   anImageObj->SetTrsf( aTransform );
119   anImageObj->SetVisibility( true );
120
121   theUpdateFlags = UF_Model | UF_Viewer;
122   return true;
123 }
124
125 void HYDROGUI_ImportImageOp::processCancel()
126 {
127   closePreview();
128 }
129
130 void HYDROGUI_ImportImageOp::onCreatePreview( QString theFileName )
131 {
132   LightApp_Application* anApp = module()->getApp();
133
134   myActiveViewManager = anApp->activeViewManager();
135
136   myPreviewPrs = new HYDROGUI_PrsImage( 0 ); // no data object
137
138   QImage anImage( theFileName );
139   myPreviewPrs->setImage( anImage );
140
141   myPreviewPrs->compute();
142
143   myPreviewViewManager =
144     dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
145   if( myPreviewViewManager )
146   {
147     myPreviewViewManager->setTitle( tr( "MAPPING" ) );
148     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
149     {
150       if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
151       {
152         aViewPort->setMousePositionEnabled( true );
153
154         aViewPort->addItem( myPreviewPrs );
155         aViewPort->fitAll();
156
157         myPreviewPrs->setIsTransformationPointPreview( true );
158       }
159       connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
160                this, SLOT( onPointSelected() ) );
161     }
162   }
163
164   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
165
166   aPanel->initializePointSelection();
167   onPointSelected();
168
169   aPanel->synchronizeTransformedPoints();
170 }
171
172 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
173 {
174   myPointType = thePointType;
175   if( myPreviewPrs )
176     myPreviewPrs->setTransformationPointMode( thePointType );
177 }
178
179 void HYDROGUI_ImportImageOp::onPointSelected()
180 {
181   HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
182
183   const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
184     myPreviewPrs->getTransformationPointMap();
185   HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
186   while( anIter.hasNext() )
187   {
188     int aPointType = anIter.next().key();
189     const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
190     const QPoint& aPoint = aTransformationPoint.Point;
191
192     HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF() );
193     aDataMap[ aPointType ] = aData;
194   }
195
196   ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap );
197 }
198
199 void HYDROGUI_ImportImageOp::closePreview()
200 {
201   if( myPreviewPrs )
202   {
203     delete myPreviewPrs;
204     myPreviewPrs = 0;
205   }
206
207   if( myPreviewViewManager )
208   {
209     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
210     myPreviewViewManager = 0;
211   }
212
213   if( myActiveViewManager )
214     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
215 }