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 #include <SUIT_Desktop.h>
41 #include <SUIT_MessageBox.h>
43 HYDROGUI_ImportImageOp::HYDROGUI_ImportImageOp( HYDROGUI_Module* theModule )
44 : HYDROGUI_Operation( theModule ),
45 myActiveViewManager( 0 ),
46 myPreviewViewManager( 0 ),
48 myPointType( HYDROGUI_PrsImage::None )
52 HYDROGUI_ImportImageOp::~HYDROGUI_ImportImageOp()
56 void HYDROGUI_ImportImageOp::startOperation()
58 HYDROGUI_Operation::startOperation();
60 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
64 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
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 ) ) );
74 void HYDROGUI_ImportImageOp::OnApply()
76 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
78 HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
79 bool anIsOk = aPanel->getTransformationDataMap( aMap );
80 if( !anIsOk || !myPreviewPrs )
82 SUIT_MessageBox::critical( module()->getApp()->desktop(),
83 tr( "INSUFFICIENT_INPUT_DATA" ),
84 tr( "INPUT_VALID_DATA" ) );
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();
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();
102 QTransform aTransform1( xa1, ya1, 1, xb1, yb1, 1, xc1, yc1, 1 );
103 QTransform aTransform2( xa2, ya2, 1, xb2, yb2, 1, xc2, yc2, 1 );
105 QTransform aTransform = aTransform1.inverted() * aTransform2;
107 QImage anImage = myPreviewPrs->getImage();
111 int aStudyId = module()->getStudyId();
112 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
113 if( !aDocument.IsNull() )
115 Handle(HYDROData_Image) anImageObj =
116 Handle(HYDROData_Image)::DownCast( aDocument->CreateObject( KIND_IMAGE ) );
117 if( !anImageObj.IsNull() )
119 static int ImageId = 0;
120 anImageObj->SetName( QString( "Image_%1" ).arg( QString::number( ++ImageId ) ) );
122 anImageObj->SetImage( anImage );
123 anImageObj->SetTrsf( aTransform );
125 module()->update( UF_Model | UF_Viewer );
131 void HYDROGUI_ImportImageOp::OnCancel()
137 void HYDROGUI_ImportImageOp::onCreatePreview( QString theFileName )
139 LightApp_Application* anApp = module()->getApp();
141 myActiveViewManager = anApp->activeViewManager();
143 myPreviewPrs = new HYDROGUI_PrsImage( 0 ); // no data object
145 QImage anImage( theFileName );
146 myPreviewPrs->setImage( anImage );
148 myPreviewPrs->compute();
150 myPreviewViewManager =
151 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
152 if( myPreviewViewManager )
154 myPreviewViewManager->setTitle( tr( "MAPPING" ) );
155 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
157 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
159 aViewPort->setMousePositionEnabled( true );
161 aViewPort->addItem( myPreviewPrs );
164 myPreviewPrs->setIsTransformationPointPreview( true );
166 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
167 this, SLOT( onPointSelected() ) );
171 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
173 aPanel->initializePointSelection();
176 aPanel->synchronizeTransformedPoints();
179 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
181 myPointType = thePointType;
183 myPreviewPrs->setTransformationPointMode( thePointType );
186 void HYDROGUI_ImportImageOp::onPointSelected()
188 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
190 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
191 myPreviewPrs->getTransformationPointMap();
192 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
193 while( anIter.hasNext() )
195 int aPointType = anIter.next().key();
196 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
197 const QPoint& aPoint = aTransformationPoint.Point;
199 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF() );
200 aDataMap[ aPointType ] = aData;
203 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap );
206 void HYDROGUI_ImportImageOp::closePreview()
214 if( myPreviewViewManager )
216 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
217 myPreviewViewManager = 0;
220 if( myActiveViewManager )
221 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );