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 )
50 setName( tr( "IMPORT_IMAGE" ) );
53 HYDROGUI_ImportImageOp::~HYDROGUI_ImportImageOp()
57 void HYDROGUI_ImportImageOp::startOperation()
59 HYDROGUI_Operation::startOperation();
61 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
65 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
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 ) ) );
75 void HYDROGUI_ImportImageOp::onApply()
77 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
79 HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
80 bool anIsOk = aPanel->getTransformationDataMap( aMap );
81 if( !anIsOk || !myPreviewPrs )
83 SUIT_MessageBox::critical( module()->getApp()->desktop(),
84 tr( "INSUFFICIENT_INPUT_DATA" ),
85 tr( "INPUT_VALID_DATA" ) );
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();
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();
103 QTransform aTransform1( xa1, ya1, 1, xb1, yb1, 1, xc1, yc1, 1 );
104 QTransform aTransform2( xa2, ya2, 1, xb2, yb2, 1, xc2, yc2, 1 );
106 QTransform aTransform = aTransform1.inverted() * aTransform2;
108 QImage anImage = myPreviewPrs->getImage();
112 int aStudyId = module()->getStudyId();
113 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
114 if( !aDocument.IsNull() )
116 Handle(HYDROData_Image) anImageObj =
117 Handle(HYDROData_Image)::DownCast( aDocument->CreateObject( KIND_IMAGE ) );
118 if( !anImageObj.IsNull() )
120 static int ImageId = 0;
121 anImageObj->SetName( QString( "Image_%1" ).arg( QString::number( ++ImageId ) ) );
123 anImageObj->SetImage( anImage );
124 anImageObj->SetTrsf( aTransform );
126 module()->update( UF_Model | UF_Viewer );
129 HYDROGUI_Operation::onApply();
132 void HYDROGUI_ImportImageOp::onCancel()
135 HYDROGUI_Operation::onCancel();
138 void HYDROGUI_ImportImageOp::onCreatePreview( QString theFileName )
140 LightApp_Application* anApp = module()->getApp();
142 myActiveViewManager = anApp->activeViewManager();
144 myPreviewPrs = new HYDROGUI_PrsImage( 0 ); // no data object
146 QImage anImage( theFileName );
147 myPreviewPrs->setImage( anImage );
149 myPreviewPrs->compute();
151 myPreviewViewManager =
152 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
153 if( myPreviewViewManager )
155 myPreviewViewManager->setTitle( tr( "MAPPING" ) );
156 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
158 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
160 aViewPort->setMousePositionEnabled( true );
162 aViewPort->addItem( myPreviewPrs );
165 myPreviewPrs->setIsTransformationPointPreview( true );
167 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
168 this, SLOT( onPointSelected() ) );
172 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
174 aPanel->initializePointSelection();
177 aPanel->synchronizeTransformedPoints();
180 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
182 myPointType = thePointType;
184 myPreviewPrs->setTransformationPointMode( thePointType );
187 void HYDROGUI_ImportImageOp::onPointSelected()
189 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
191 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
192 myPreviewPrs->getTransformationPointMap();
193 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
194 while( anIter.hasNext() )
196 int aPointType = anIter.next().key();
197 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
198 const QPoint& aPoint = aTransformationPoint.Point;
200 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF() );
201 aDataMap[ aPointType ] = aData;
204 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap );
207 void HYDROGUI_ImportImageOp::closePreview()
215 if( myPreviewViewManager )
217 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
218 myPreviewViewManager = 0;
221 if( myActiveViewManager )
222 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );