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 );
119 anImageObj->SetVisibility( true );
121 theUpdateFlags = UF_Model | UF_Viewer;
125 void HYDROGUI_ImportImageOp::processCancel()
130 void HYDROGUI_ImportImageOp::onCreatePreview( QString theFileName )
132 LightApp_Application* anApp = module()->getApp();
134 myActiveViewManager = anApp->activeViewManager();
136 myPreviewPrs = new HYDROGUI_PrsImage( 0 ); // no data object
138 QImage anImage( theFileName );
139 myPreviewPrs->setImage( anImage );
141 myPreviewPrs->compute();
143 myPreviewViewManager =
144 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
145 if( myPreviewViewManager )
147 myPreviewViewManager->setTitle( tr( "MAPPING" ) );
148 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
150 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
152 aViewPort->setMousePositionEnabled( true );
154 aViewPort->addItem( myPreviewPrs );
157 myPreviewPrs->setIsTransformationPointPreview( true );
159 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
160 this, SLOT( onPointSelected() ) );
164 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
166 aPanel->initializePointSelection();
169 aPanel->synchronizeTransformedPoints();
172 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
174 myPointType = thePointType;
176 myPreviewPrs->setTransformationPointMode( thePointType );
179 void HYDROGUI_ImportImageOp::onPointSelected()
181 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
183 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
184 myPreviewPrs->getTransformationPointMap();
185 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
186 while( anIter.hasNext() )
188 int aPointType = anIter.next().key();
189 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
190 const QPoint& aPoint = aTransformationPoint.Point;
192 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF() );
193 aDataMap[ aPointType ] = aData;
196 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap );
199 void HYDROGUI_ImportImageOp::closePreview()
207 if( myPreviewViewManager )
209 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
210 myPreviewViewManager = 0;
213 if( myActiveViewManager )
214 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );