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_DataModel.h"
26 #include "HYDROGUI_ImportImageDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_PrsImage.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_Tool.h>
35 #include <GraphicsView_ViewManager.h>
36 #include <GraphicsView_ViewPort.h>
37 #include <GraphicsView_Viewer.h>
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
42 #include <STD_TabDesktop.h>
43 #include <SUIT_Desktop.h>
44 #include <SUIT_MessageBox.h>
45 #include <QtxWorkstack.h>
46 #include <QApplication>
49 HYDROGUI_ImportImageOp::HYDROGUI_ImportImageOp( HYDROGUI_Module* theModule,
50 const bool theIsEdit )
51 : HYDROGUI_Operation( theModule ),
52 myIsEdit( theIsEdit ),
54 myActiveViewManager( 0 ),
55 myPreviewViewManager( 0 ),
56 myRefViewManager( 0 ),
59 myPointType( HYDROGUI_PrsImage::None )
61 setName( theIsEdit ? tr( "EDIT_IMPORTED_IMAGE" ) : tr( "IMPORT_IMAGE" ) );
64 HYDROGUI_ImportImageOp::~HYDROGUI_ImportImageOp()
68 void HYDROGUI_ImportImageOp::startOperation()
70 HYDROGUI_Operation::startOperation();
72 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
74 aPanel->setIsEdit( myIsEdit );
76 QString aRefImageName;
80 myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
81 if( !myEditedObject.IsNull() )
83 QImage anImage = myEditedObject->Image();
84 bool anIsByTwoPoints = myEditedObject->IsByTwoPoints();
86 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
87 myEditedObject->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
89 // Create the edited image preview presentation in the viewer
90 onCreatePreview( anImage );
92 // Set transformation local points A,B,C to the image preview presentation
93 setPresentationTrsfPoints( myPreviewPrs, anIsByTwoPoints, aLocalPointA,
94 aLocalPointB, aLocalPointC );
96 // Build the existing image local and global points mapping
97 // according to the current transformation mode.
98 HYDROData_Image::TransformationMode aTrsfMode = myEditedObject->GetTrsfMode();
99 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
100 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
101 if ( aTrsfMode == HYDROData_Image::ReferenceImage )
103 // Compute global points using the transformation matrix of the reference image
104 Handle(HYDROData_Image) aRefImage = myEditedObject->GetTrsfReferenceImage();
105 if ( !aRefImage.IsNull() )
107 QTransform aRefTrsf = aRefImage->Trsf(); // The reference image transformation matrix
108 aTrsfPointA = aRefTrsf.map( aLocalPointA ); // Compute the global point A
109 aTrsfPointB = aRefTrsf.map( aLocalPointB ); // Compute the global point B
110 if ( !anIsByTwoPoints )
112 aTrsfPointC = aRefTrsf.map( aLocalPointC ); // Compute the global point C if used
114 // Build the local-global points map
115 // Use the reference image transformation mode for interpreting global points
116 computeTrsfData( aRefImage->GetTrsfMode(), anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
117 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
122 // Get global points from the edited image
123 myEditedObject->GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC );
124 // Build the local-global points map
125 computeTrsfData( aTrsfMode, anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
126 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
129 // Initialize the dialog mode, local and global coordinates
130 // except coordinates on the reference image
131 aPanel->setTransformationMode( aTrsfMode );
132 aPanel->setTransformationDataMap( aDataMap );
133 aPanel->setByTwoPoints( anIsByTwoPoints );
135 // Set points of the reference image
136 if ( aTrsfMode == HYDROData_Image::ReferenceImage )
138 Handle(HYDROData_Image) aRefImage;
139 myEditedObject->GetReferencePoints( aRefImage,
140 aTrsfPointA, aTrsfPointB, aTrsfPointC );
141 if ( !aRefImage.IsNull() )
143 aRefImageName = aRefImage->GetName();
145 // Create the reference image presentation in the viewer
146 onRefImageActivated( aRefImageName );
148 // Set transformation points A,B,C to the reference image presentation
149 setPresentationTrsfPoints( myRefPreviewPrs, anIsByTwoPoints, aTrsfPointA.toPoint(),
150 aTrsfPointB.toPoint(), aTrsfPointC.toPoint() );
152 // Prepare A, B, C points on the reference image
153 HYDROGUI_ImportImageDlg::TransformationDataMap aRefDataMap;
154 aRefDataMap[ HYDROGUI_PrsImage::PointA ] =
155 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointA.toPoint() );
156 aRefDataMap[ HYDROGUI_PrsImage::PointB ] =
157 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointB.toPoint() );
158 if ( !anIsByTwoPoints )
159 aRefDataMap[ HYDROGUI_PrsImage::PointC ] =
160 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointC.toPoint() );
162 // Initialize the dialog's A, B, C points coordinates of the reference image
163 aPanel->setTransformationDataMap( aRefDataMap, true, true );
169 // Collect information about existing images and initialize the combobox
170 // reference image selector in the dialog.
171 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
172 HYDROData_Iterator anIterator( doc(), KIND_IMAGE );
173 for( ; anIterator.More(); anIterator.Next() )
175 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
176 if( !anImageObj.IsNull() )
178 if( myIsEdit && IsEqual( anImageObj, myEditedObject ) )
181 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
182 anImageObj->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
184 HYDROData_Image::TransformationMode aImgTrsfMode;
185 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
186 anImageObj->GetGlobalPoints( aImgTrsfMode,
187 aTrsfPointA, aTrsfPointB, aTrsfPointC );
189 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
190 computeTrsfData( aImgTrsfMode, false, aLocalPointA, aLocalPointB, aLocalPointC,
191 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
193 HYDROGUI_ImportImageDlg::PrsPointData aPrsPointData( anImageObj->GetName(), aDataMap );
194 aPrsPointDataList.append( aPrsPointData );
198 // Initialize the combobox in the dialog with the list of available reference images
199 aPanel->setPrsPointDataList( aPrsPointDataList );
200 // Select the current reference image in the dialog combobox
201 aPanel->setRefImageName( aRefImageName );
204 void HYDROGUI_ImportImageOp::setPresentationTrsfPoints( HYDROGUI_PrsImage* thePrs,
205 bool theIsByTwoPoints,
206 const QPoint theLocalPointA,
207 const QPoint theLocalPointB,
208 const QPoint theLocalPointC )
210 // Set transformation points A,B,C to the image presentation
213 HYDROGUI_PrsImage::TransformationPointMap aPointMap =
214 thePrs->getTransformationPointMap();
215 if( !aPointMap.isEmpty() )
217 aPointMap[ HYDROGUI_PrsImage::PointA ].Point = theLocalPointA;
218 aPointMap[ HYDROGUI_PrsImage::PointB ].Point = theLocalPointB;
219 if ( !theIsByTwoPoints )
220 aPointMap[ HYDROGUI_PrsImage::PointC ].Point = theLocalPointC;
222 thePrs->setIsByTwoPoints( theIsByTwoPoints );
223 thePrs->setTransformationPointMap( aPointMap );
228 void HYDROGUI_ImportImageOp::computeTrsfData( HYDROData_Image::TransformationMode theTrsfMode,
229 bool theIsByTwoPoints,
230 const QPoint& theLocalPointA,
231 const QPoint& theLocalPointB,
232 const QPoint& theLocalPointC,
233 const QPointF& theGlobalPointA,
234 const QPointF& theGlobalPointB,
235 const QPointF& theGlobalPointC,
236 HYDROGUI_ImportImageDlg::TransformationDataMap& theDataMap ) const
238 // Build the local-global points map
239 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
240 theDataMap[ HYDROGUI_PrsImage::PointA ] =
241 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointA, theGlobalPointA );
242 theDataMap[ HYDROGUI_PrsImage::PointB ] =
243 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointB, theGlobalPointB );
244 if ( !theIsByTwoPoints )
246 theDataMap[ HYDROGUI_PrsImage::PointC ] =
247 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointC, theGlobalPointC );
251 void HYDROGUI_ImportImageOp::abortOperation()
255 HYDROGUI_Operation::abortOperation();
258 void HYDROGUI_ImportImageOp::commitOperation()
262 HYDROGUI_Operation::commitOperation();
265 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
267 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
268 connect( aPanel, SIGNAL( createPreview( QImage ) ), SLOT( onCreatePreview( QImage ) ) );
269 connect( aPanel, SIGNAL( activatePointSelection( int ) ), SLOT( onActivatePointSelection( int ) ) );
270 connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ),
271 SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
272 connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
273 connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
274 SLOT( onRefImageActivated( const QString& ) ) );
275 connect( aPanel, SIGNAL( setCIsUsed( bool ) ), SLOT( onSetCIsUsed( bool ) ) );
279 void HYDROGUI_ImportImageOp::onSetCIsUsed( bool theCIsUsed )
283 myPreviewPrs->setIsByTwoPoints( !theCIsUsed );
285 if ( myRefPreviewPrs )
287 myRefPreviewPrs->setIsByTwoPoints( !theCIsUsed );
291 bool HYDROGUI_ImportImageOp::checkPoints( const QPointF& thePointA,
292 const QPointF& thePointB,
293 const QPointF& thePointC,
294 const bool theIsByTwoPoints,
295 const QString& theLineErrMsg,
296 const QString& thePoinErrMsg,
297 QString& theErrorMsg,
298 const bool theIsToCheckInvertibles ) const
300 double xa = thePointA.x();
301 double ya = thePointA.y();
302 double xb = thePointB.x();
303 double yb = thePointB.y();
304 double xc = thePointC.x();
305 double yc = thePointC.y();
307 if ( !theIsByTwoPoints )
309 // check that three input points don't belong to a single line
310 if ( ValuesEquals( ( yb - ya ) * ( xc - xa ), ( yc - ya ) * ( xb - xa ) ) )
312 theErrorMsg = theLineErrMsg;
316 if ( theIsToCheckInvertibles )
318 QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
320 bool anIsInvertible = false;
321 QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
322 if( !anIsInvertible )
324 theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
331 // check that two points are not identical
332 if ( ValuesEquals( xa, xb ) && ValuesEquals( ya, yb ) )
334 theErrorMsg = thePoinErrMsg;
342 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
343 QString& theErrorMsg )
345 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
347 QString anImageName = aPanel->getImageName();
348 if( anImageName.isEmpty() )
351 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
353 // check that there are no other objects with the same name in the document
354 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
355 if( !anObject.IsNull() )
357 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
362 HYDROData_Image::TransformationMode aTransformationMode =
363 (HYDROData_Image::TransformationMode)aPanel->getTransformationMode();
365 QPoint aPointA, aPointB, aPointC;
366 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC( INT_MIN, INT_MIN );
367 Handle(HYDROData_Image) aRefImageObj;
369 if ( aTransformationMode != HYDROData_Image::CartesianFromFile ) {
370 HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
371 if( !aPanel->getTransformationDataMap( aMap ) )
374 bool anIsByTwoPoints = aPanel->isByTwoPoints();
376 aPointA = aMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
377 aPointB = aMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
378 aPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) :
379 aMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
381 // first, we check correctness of image points
382 if ( !checkPoints( aPointA, aPointB, aPointC, anIsByTwoPoints,
383 tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
384 tr( "POINTS_A_B_ARE_IDENTICAL" ),
385 theErrorMsg, true ) )
388 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
390 QString aRefImageName = aPanel->getRefImageName();
391 if( aRefImageName.isEmpty() )
393 theErrorMsg = tr( "REFERENCE_IMAGE_IS_NOT_SELECTED" );
397 HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
398 if( !aPanel->getTransformationDataMap( aRefMap, true ) )
401 aRefImageObj = Handle(HYDROData_Image)::DownCast(
402 HYDROGUI_Tool::FindObjectByName( module(), aRefImageName, KIND_IMAGE ) );
403 if( aRefImageObj.IsNull() ) {
406 else if ( !isReferenceCorrect() ) {
407 aRefImageObj->RemoveAllReferences();
410 aTrsfPointA = aRefMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
411 aTrsfPointB = aRefMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
412 if ( !anIsByTwoPoints )
413 aTrsfPointC = aRefMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
415 // the same check of correctness for the reference points
416 if ( !checkPoints( aTrsfPointA, aTrsfPointB, aTrsfPointC, anIsByTwoPoints,
417 tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
418 tr( "REFERENCE_POINTS_A_B_ARE_IDENTICAL" ),
419 theErrorMsg, false ) )
424 if ( aTransformationMode == HYDROData_Image::ManualGeodesic )
426 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].GeodesicPoint;
427 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].GeodesicPoint;
428 if ( !anIsByTwoPoints )
429 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].GeodesicPoint;
433 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].CartesianPoint;
434 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].CartesianPoint;
435 if ( !anIsByTwoPoints )
436 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].CartesianPoint;
441 Handle(HYDROData_Image) anImageObj;
443 anImageObj = myEditedObject;
445 anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
447 if( anImageObj.IsNull() )
450 anImageObj->SetName( anImageName );
451 anImageObj->SetImage( myImage );
453 if ( aTransformationMode == HYDROData_Image::CartesianFromFile ) {
454 QString aGeoreferencementFileName = aPanel->getGeoreferencementFileName();
455 if ( aGeoreferencementFileName.isEmpty() ) {
459 QPoint aLocalPointA( 0, 0 ),
460 aLocalPointB( anImageObj->Image().width(), 0 ),
461 aLocalPointC( INT_MIN, INT_MIN );
462 anImageObj->SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
463 if ( !anImageObj->SetGlobalPointsFromFile( aGeoreferencementFileName ) ) {
464 theErrorMsg = tr( "CANT_LOAD_GEOREFERENCEMENT_FILE" );
468 anImageObj->SetLocalPoints( aPointA, aPointB, aPointC, false );
470 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
472 anImageObj->SetReferencePoints( aRefImageObj,
473 aTrsfPointA, aTrsfPointB, aTrsfPointC );
477 anImageObj->SetGlobalPoints( aTransformationMode,
478 aTrsfPointA, aTrsfPointB, aTrsfPointC );
484 // Set imported file name for image
485 QString aFilePath = aPanel->getFileName();
486 anImageObj->SetFilePath( aFilePath );
489 // must be done after all checks and before calling SetVisible() method below
493 module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
495 anImageObj->Update();
497 theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
501 bool HYDROGUI_ImportImageOp::isReferenceCorrect() const
503 bool isCorrect = true;
505 if( myIsEdit && !myEditedObject.IsNull() )
507 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
508 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
509 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
510 if( !aRefImageObj.IsNull() )
512 bool isFoundEdited = false;
513 HYDROData_SequenceOfObjects aRefSeq = aRefImageObj->GetAllReferenceObjects();
514 for ( int i = 1, n = aRefSeq.Length(); i <= n && !isFoundEdited; ++i )
516 Handle(HYDROData_Entity) anObject = aRefSeq.Value( i );
517 isFoundEdited = anObject->GetName() == myEditedObject->GetName();
519 isCorrect = !isFoundEdited;
525 void HYDROGUI_ImportImageOp::onApply()
527 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
529 bool aCanApply = isReferenceCorrect();
530 if ( !aCanApply && !myEditedObject.IsNull() )
532 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
533 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
534 if ( !aRefImageObj.IsNull() )
535 aCanApply = SUIT_MessageBox::question( module()->getApp()->desktop(),
536 tr( "CORRECT_INPUT_DATA" ), tr( "CONFIRM_REMOVE_REFERENCE_FROM_IMAGE" ).
537 arg( aRefImageObj->GetName() ).arg( myEditedObject->GetName() ),
538 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes;
541 HYDROGUI_Operation::onApply();
543 aPanel->setRefImageName( "" );
544 onRefImageActivated( aPanel->getRefImageName() );
548 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
550 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
554 if( myPreviewPrs ) // if the image is changed by choosing another file
556 myPreviewPrs->setImage( myImage );
557 myPreviewPrs->compute();
559 if( myPreviewViewManager )
561 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
563 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
565 aViewPort->onBoundingRectChanged();
573 LightApp_Application* anApp = module()->getApp();
575 myActiveViewManager = anApp->activeViewManager();
577 myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
578 myPreviewPrs->setImage( myImage );
579 myPreviewPrs->compute();
581 myPreviewViewManager =
582 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
583 if( myPreviewViewManager )
585 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
586 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
588 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
589 myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
590 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
592 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
594 //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
596 aViewPort->addItem( myPreviewPrs );
599 myPreviewPrs->setIsTransformationPointPreview( true );
601 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
602 this, SLOT( onPointSelected() ) );
606 // Set the image name in the dialog
607 QString anImageName = aPanel->getImageName().simplified();
608 // If edit mode and the name was not set yet then get from the edited object
609 if( myIsEdit && anImageName.isEmpty() )
611 if( !myEditedObject.IsNull() )
612 anImageName = myEditedObject->GetName();
614 // If the name was not set then initialize it from the selected file name
615 if ( anImageName.isEmpty() )
617 anImageName = aPanel->getFileName();
618 if ( !anImageName.isEmpty() ) {
619 anImageName = QFileInfo( anImageName ).baseName();
621 // If no file name then generate a new image name
622 if ( anImageName.isEmpty() ) {
623 anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
627 aPanel->setImageName( anImageName );
630 aPanel->setImageSize( myImage.size() );
632 aPanel->initializePointSelection();
633 onPointSelected( false );
634 onSetCIsUsed( !aPanel->isByTwoPoints() );
637 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
639 myPointType = thePointType;
641 myPreviewPrs->setTransformationPointType( thePointType );
642 if( myRefPreviewPrs )
643 myRefPreviewPrs->setTransformationPointType( thePointType );
646 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
651 if( !theIsRef && myPreviewPrs )
652 myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
653 else if( theIsRef && myRefPreviewPrs )
654 myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
657 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
659 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
661 QString aRefImageName;
662 if( theMode == HYDROData_Image::ReferenceImage )
664 aRefImageName = aPanel->getRefImageName();
665 if( aRefImageName.isEmpty() )
666 return; // do nothing in this case to avoid visual moving of preview prs
667 onRefImageActivated( aRefImageName );
671 if( myRefViewManager )
673 closeView( myRefViewManager );
678 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
680 GraphicsView_ViewPort* aViewPort = 0;
682 LightApp_Application* anApp = module()->getApp();
683 ///// Get a view port for the reference image preview
684 if( myRefViewManager )
686 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
688 aViewPort = aViewer->getActiveViewPort();
693 anApp = module()->getApp();
694 // Init reference image preview
696 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
697 if( myRefViewManager )
699 connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
700 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
702 module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
703 myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
704 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
706 aViewPort = aViewer->getActiveViewPort();
707 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
708 this, SLOT( onRefPointSelected() ) );
716 // Remove the old presentation of the reference image if any
717 if( myRefPreviewPrs )
719 myRefPreviewPrs->setCaption( QString() );
720 aViewPort->removeItem( myRefPreviewPrs );
722 delete myRefPreviewPrs;
726 // Create a new reference image presentation
728 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
729 HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
730 if( !anImageObj.IsNull() )
732 anImage = anImageObj->Image();
734 myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
735 myRefPreviewPrs->setImage( anImage );
736 myRefPreviewPrs->compute();
738 myRefPreviewPrs->setIsTransformationPointPreview( true );
739 myRefPreviewPrs->setTransformationPointType( myPointType );
741 // Add the new reference image presentation to the appropriate view
742 aViewPort->addItem( myRefPreviewPrs );
747 // Split views horizontally
748 if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
750 qApp->processEvents();
751 QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
752 aViewPort->activateWindow();
754 aViewPort->setFocus(Qt::ActiveWindowFocusReason);
755 aWorkstack->splitHorizontal();
758 // Initialize the dialog
759 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
760 aPanel->setImageSize( anImage.size(), true );
761 aPanel->initializePointSelection();
762 onPointSelected( true );
765 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
770 void HYDROGUI_ImportImageOp::onPointSelected()
772 onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
775 void HYDROGUI_ImportImageOp::onRefPointSelected()
777 onPointSelected( true );
780 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
782 HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
786 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
788 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
789 aPrs->getTransformationPointMap();
791 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
792 while( anIter.hasNext() )
794 int aPointType = anIter.next().key();
795 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
796 const QPoint& aPoint = aTransformationPoint.Point;
798 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
799 aDataMap[ aPointType ] = aData;
802 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
805 void HYDROGUI_ImportImageOp::closePreview()
807 closeView( myPreviewViewManager );
808 closeView( myRefViewManager );
810 if( myActiveViewManager )
811 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
814 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
818 GraphicsView_ViewPort* aViewPort = 0;
819 if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
821 aViewPort = aViewer->getActiveViewPort();
823 disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
824 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
826 // Nullify appropriate presentation pointer
827 HYDROGUI_PrsImage* aPrs = 0;
828 switch ( module()->getViewManagerRole( aViewMgr ) )
830 case HYDROGUI_Module::VMR_ReferenceImage:
831 aPrs = myRefPreviewPrs;
834 case HYDROGUI_Module::VMR_TransformImage:
839 // Remove the appropriate presentation from the view
840 if( aPrs && aViewPort )
842 aViewPort->removeItem( aPrs );
847 module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here