1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ImportImageOp.h"
25 #include "HYDROGUI_ImportImageDlg.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_PrsImage.h"
28 #include "HYDROGUI_Tool.h"
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Image.h>
33 #include <GraphicsView_ViewManager.h>
34 #include <GraphicsView_ViewPort.h>
35 #include <GraphicsView_Viewer.h>
37 #include <LightApp_Application.h>
38 #include <LightApp_UpdateFlags.h>
40 HYDROGUI_ImportImageOp::HYDROGUI_ImportImageOp( HYDROGUI_Module* theModule )
41 : HYDROGUI_Operation( theModule ),
42 myActiveViewManager( 0 ),
43 myPreviewViewManager( 0 ),
45 myPointType( HYDROGUI_PrsImage::None )
47 setName( tr( "IMPORT_IMAGE" ) );
50 HYDROGUI_ImportImageOp::~HYDROGUI_ImportImageOp()
54 void HYDROGUI_ImportImageOp::startOperation()
56 HYDROGUI_Operation::startOperation();
58 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
62 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
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 ) ) );
72 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags )
74 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
76 HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
77 bool anIsOk = aPanel->getTransformationDataMap( aMap );
78 if( !anIsOk || !myPreviewPrs )
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();
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();
95 QTransform aTransform1( xa1, ya1, 1, xb1, yb1, 1, xc1, yc1, 1 );
96 QTransform aTransform2( xa2, ya2, 1, xb2, yb2, 1, xc2, yc2, 1 );
98 QTransform aTransform = aTransform1.inverted() * aTransform2;
100 QImage anImage = myPreviewPrs->getImage();
104 int aStudyId = module()->getStudyId();
105 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
106 if( aDocument.IsNull() )
109 Handle(HYDROData_Image) anImageObj =
110 Handle(HYDROData_Image)::DownCast( aDocument->CreateObject( KIND_IMAGE ) );
111 if( anImageObj.IsNull() )
114 static int ImageId = 0;
115 anImageObj->SetName( QString( "Image_%1" ).arg( QString::number( ++ImageId ) ) );
117 anImageObj->SetImage( anImage );
118 anImageObj->SetTrsf( aTransform );
120 theUpdateFlags = UF_Model | UF_Viewer;
124 void HYDROGUI_ImportImageOp::processCancel()
129 void HYDROGUI_ImportImageOp::onCreatePreview( QString theFileName )
131 LightApp_Application* anApp = module()->getApp();
133 myActiveViewManager = anApp->activeViewManager();
135 myPreviewPrs = new HYDROGUI_PrsImage( 0 ); // no data object
137 QImage anImage( theFileName );
138 myPreviewPrs->setImage( anImage );
140 myPreviewPrs->compute();
142 myPreviewViewManager =
143 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
144 if( myPreviewViewManager )
146 myPreviewViewManager->setTitle( tr( "MAPPING" ) );
147 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
149 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
151 aViewPort->setMousePositionEnabled( true );
153 aViewPort->addItem( myPreviewPrs );
156 myPreviewPrs->setIsTransformationPointPreview( true );
158 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
159 this, SLOT( onPointSelected() ) );
163 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
165 aPanel->initializePointSelection();
168 aPanel->synchronizeTransformedPoints();
171 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
173 myPointType = thePointType;
175 myPreviewPrs->setTransformationPointMode( thePointType );
178 void HYDROGUI_ImportImageOp::onPointSelected()
180 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
182 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
183 myPreviewPrs->getTransformationPointMap();
184 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
185 while( anIter.hasNext() )
187 int aPointType = anIter.next().key();
188 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
189 const QPoint& aPoint = aTransformationPoint.Point;
191 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF() );
192 aDataMap[ aPointType ] = aData;
195 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap );
198 void HYDROGUI_ImportImageOp::closePreview()
206 if( myPreviewViewManager )
208 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
209 myPreviewViewManager = 0;
212 if( myActiveViewManager )
213 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );