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