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