1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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_DataObject.h>
27 #include "HYDROGUI_ImportImageDlg.h"
28 #include "HYDROGUI_Module.h"
29 #include "HYDROGUI_PrsImage.h"
30 #include "HYDROGUI_Tool.h"
31 #include "HYDROGUI_UpdateFlags.h"
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_Tool.h>
36 #include <GraphicsView_ViewManager.h>
37 #include <GraphicsView_ViewPort.h>
38 #include <GraphicsView_Viewer.h>
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
43 #include <STD_TabDesktop.h>
44 #include <SUIT_Desktop.h>
45 #include <SUIT_MessageBox.h>
46 #include <QtxWorkstack.h>
47 #include <QApplication>
50 HYDROGUI_ImportImageOp::HYDROGUI_ImportImageOp( HYDROGUI_Module* theModule,
51 const bool theIsEdit )
52 : HYDROGUI_Operation( theModule ),
53 myIsEdit( theIsEdit ),
55 myActiveViewManager( 0 ),
56 myPreviewViewManager( 0 ),
57 myRefViewManager( 0 ),
60 myPointType( HYDROGUI_PrsImage::None )
62 setName( theIsEdit ? tr( "EDIT_IMPORTED_IMAGE" ) : tr( "IMPORT_IMAGE" ) );
65 HYDROGUI_ImportImageOp::~HYDROGUI_ImportImageOp()
69 void HYDROGUI_ImportImageOp::startOperation()
71 HYDROGUI_Operation::startOperation();
73 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
75 aPanel->setIsEdit( myIsEdit );
77 QString aRefImageName;
81 myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
82 if( !myEditedObject.IsNull() )
84 QImage anImage = myEditedObject->Image();
85 bool anIsByTwoPoints = myEditedObject->IsByTwoPoints();
87 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
88 myEditedObject->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
90 // Create the edited image preview presentation in the viewer
91 onCreatePreview( anImage );
93 // Set transformation local points A,B,C to the image preview presentation
94 setPresentationTrsfPoints( myPreviewPrs, anIsByTwoPoints, aLocalPointA,
95 aLocalPointB, aLocalPointC );
97 // Build the existing image local and global points mapping
98 // according to the current transformation mode.
99 HYDROData_Image::TransformationMode aTrsfMode = myEditedObject->GetTrsfMode();
100 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
101 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
102 if ( aTrsfMode == HYDROData_Image::ReferenceImage )
104 // Compute global points using the transformation matrix of the reference image
105 Handle(HYDROData_Image) aRefImage = myEditedObject->GetTrsfReferenceImage();
106 if ( !aRefImage.IsNull() )
108 QTransform aRefTrsf = aRefImage->Trsf(); // The reference image transformation matrix
109 aTrsfPointA = aRefTrsf.map( aLocalPointA ); // Compute the global point A
110 aTrsfPointB = aRefTrsf.map( aLocalPointB ); // Compute the global point B
111 if ( !anIsByTwoPoints )
113 aTrsfPointC = aRefTrsf.map( aLocalPointC ); // Compute the global point C if used
115 // Build the local-global points map
116 // Use the reference image transformation mode for interpreting global points
117 computeTrsfData( aRefImage->GetTrsfMode(), anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
118 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
123 // Get global points from the edited image
124 myEditedObject->GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC );
125 // Build the local-global points map
126 computeTrsfData( aTrsfMode, anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
127 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
130 // Initialize the dialog mode, local and global coordinates
131 // except coordinates on the reference image
132 aPanel->setTransformationMode( aTrsfMode );
133 aPanel->setTransformationDataMap( aDataMap );
134 aPanel->setByTwoPoints( anIsByTwoPoints );
136 // Set points of the reference image
137 if ( aTrsfMode == HYDROData_Image::ReferenceImage )
139 Handle(HYDROData_Image) aRefImage;
140 myEditedObject->GetReferencePoints( aRefImage,
141 aTrsfPointA, aTrsfPointB, aTrsfPointC );
142 if ( !aRefImage.IsNull() )
144 aRefImageName = aRefImage->GetName();
146 // Create the reference image presentation in the viewer
147 onRefImageActivated( aRefImageName );
149 // Set transformation points A,B,C to the reference image presentation
150 setPresentationTrsfPoints( myRefPreviewPrs, anIsByTwoPoints, aTrsfPointA.toPoint(),
151 aTrsfPointB.toPoint(), aTrsfPointC.toPoint() );
153 // Prepare A, B, C points on the reference image
154 HYDROGUI_ImportImageDlg::TransformationDataMap aRefDataMap;
155 aRefDataMap[ HYDROGUI_PrsImage::PointA ] =
156 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointA.toPoint() );
157 aRefDataMap[ HYDROGUI_PrsImage::PointB ] =
158 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointB.toPoint() );
159 if ( !anIsByTwoPoints )
160 aRefDataMap[ HYDROGUI_PrsImage::PointC ] =
161 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointC.toPoint() );
163 // Initialize the dialog's A, B, C points coordinates of the reference image
164 aPanel->setTransformationDataMap( aRefDataMap, true, true );
170 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
171 getReferenceDataList( aPrsPointDataList );
172 aPanel->setPrsPointDataList( aPrsPointDataList );
173 // Select the current reference image in the dialog combobox
174 aPanel->setRefImageName( aRefImageName );
177 void HYDROGUI_ImportImageOp::setPresentationTrsfPoints( HYDROGUI_PrsImage* thePrs,
178 bool theIsByTwoPoints,
179 const QPoint theLocalPointA,
180 const QPoint theLocalPointB,
181 const QPoint theLocalPointC )
183 // Set transformation points A,B,C to the image presentation
186 HYDROGUI_PrsImage::TransformationPointMap aPointMap =
187 thePrs->getTransformationPointMap();
188 if( !aPointMap.isEmpty() )
190 aPointMap[ HYDROGUI_PrsImage::PointA ].Point = theLocalPointA;
191 aPointMap[ HYDROGUI_PrsImage::PointB ].Point = theLocalPointB;
192 if ( !theIsByTwoPoints )
193 aPointMap[ HYDROGUI_PrsImage::PointC ].Point = theLocalPointC;
195 thePrs->setIsByTwoPoints( theIsByTwoPoints );
196 thePrs->setTransformationPointMap( aPointMap );
201 void HYDROGUI_ImportImageOp::getReferenceDataList(
202 HYDROGUI_ImportImageDlg::PrsPointDataList& theList ) const
204 // Collect information about existing images and initialize the combobox
205 // reference image selector in the dialog.
206 HYDROData_Iterator anIterator( doc(), KIND_IMAGE );
207 for( ; anIterator.More(); anIterator.Next() )
209 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
210 if( !anImageObj.IsNull() )
212 if( myIsEdit && IsEqual( anImageObj, myEditedObject ) )
215 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
216 anImageObj->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
218 HYDROData_Image::TransformationMode aImgTrsfMode;
219 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
220 anImageObj->GetGlobalPoints( aImgTrsfMode,
221 aTrsfPointA, aTrsfPointB, aTrsfPointC );
223 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
224 computeTrsfData( aImgTrsfMode, false, aLocalPointA, aLocalPointB, aLocalPointC,
225 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
227 HYDROGUI_ImportImageDlg::PrsPointData aPrsPointData( anImageObj->GetName(), aDataMap );
228 theList.append( aPrsPointData );
233 void HYDROGUI_ImportImageOp::computeTrsfData( HYDROData_Image::TransformationMode theTrsfMode,
234 bool theIsByTwoPoints,
235 const QPoint& theLocalPointA,
236 const QPoint& theLocalPointB,
237 const QPoint& theLocalPointC,
238 const QPointF& theGlobalPointA,
239 const QPointF& theGlobalPointB,
240 const QPointF& theGlobalPointC,
241 HYDROGUI_ImportImageDlg::TransformationDataMap& theDataMap ) const
243 // Build the local-global points map
244 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
245 theDataMap[ HYDROGUI_PrsImage::PointA ] =
246 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointA, theGlobalPointA );
247 theDataMap[ HYDROGUI_PrsImage::PointB ] =
248 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointB, theGlobalPointB );
249 if ( !theIsByTwoPoints )
251 theDataMap[ HYDROGUI_PrsImage::PointC ] =
252 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointC, theGlobalPointC );
256 void HYDROGUI_ImportImageOp::abortOperation()
260 HYDROGUI_Operation::abortOperation();
263 void HYDROGUI_ImportImageOp::commitOperation()
267 HYDROGUI_Operation::commitOperation();
270 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
272 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
273 connect( aPanel, SIGNAL( createPreview( QImage ) ), SLOT( onCreatePreview( QImage ) ) );
274 connect( aPanel, SIGNAL( activatePointSelection( int ) ), SLOT( onActivatePointSelection( int ) ) );
275 connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ),
276 SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
277 connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
278 connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
279 SLOT( onRefImageActivated( const QString& ) ) );
280 connect( aPanel, SIGNAL( setCIsUsed( bool ) ), SLOT( onSetCIsUsed( bool ) ) );
284 void HYDROGUI_ImportImageOp::onSetCIsUsed( bool theCIsUsed )
288 myPreviewPrs->setIsByTwoPoints( !theCIsUsed );
290 if ( myRefPreviewPrs )
292 myRefPreviewPrs->setIsByTwoPoints( !theCIsUsed );
296 bool HYDROGUI_ImportImageOp::checkPoints( const QPointF& thePointA,
297 const QPointF& thePointB,
298 const QPointF& thePointC,
299 const bool theIsByTwoPoints,
300 const QString& theLineErrMsg,
301 const QString& thePoinErrMsg,
302 QString& theErrorMsg,
303 const bool theIsToCheckInvertibles ) const
305 double xa = thePointA.x();
306 double ya = thePointA.y();
307 double xb = thePointB.x();
308 double yb = thePointB.y();
309 double xc = thePointC.x();
310 double yc = thePointC.y();
312 if ( !theIsByTwoPoints )
314 // check that three input points don't belong to a single line
315 if ( ValuesEquals( ( yb - ya ) * ( xc - xa ), ( yc - ya ) * ( xb - xa ) ) )
317 theErrorMsg = theLineErrMsg;
321 if ( theIsToCheckInvertibles )
323 QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
325 bool anIsInvertible = false;
326 QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
327 if( !anIsInvertible )
329 theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
336 // check that two points are not identical
337 if ( ValuesEquals( xa, xb ) && ValuesEquals( ya, yb ) )
339 theErrorMsg = thePoinErrMsg;
347 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
348 QString& theErrorMsg,
349 QStringList& theBrowseObjectsEntries )
351 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
353 QString anImageName = aPanel->getImageName();
354 if( anImageName.isEmpty() )
357 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
359 // check that there are no other objects with the same name in the document
360 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
361 if( !anObject.IsNull() )
363 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
368 HYDROData_Image::TransformationMode aTransformationMode =
369 (HYDROData_Image::TransformationMode)aPanel->getTransformationMode();
371 QPoint aPointA, aPointB, aPointC;
372 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC( INT_MIN, INT_MIN );
373 Handle(HYDROData_Image) aRefImageObj;
375 if ( aTransformationMode != HYDROData_Image::CartesianFromFile ) {
376 HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
377 if( !aPanel->getTransformationDataMap( aMap ) )
380 bool anIsByTwoPoints = aPanel->isByTwoPoints();
382 aPointA = aMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
383 aPointB = aMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
384 aPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) :
385 aMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
387 // first, we check correctness of image points
388 if ( !checkPoints( aPointA, aPointB, aPointC, anIsByTwoPoints,
389 tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
390 tr( "POINTS_A_B_ARE_IDENTICAL" ),
391 theErrorMsg, true ) )
394 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
396 QString aRefImageName = aPanel->getRefImageName();
397 if( aRefImageName.isEmpty() )
399 theErrorMsg = tr( "REFERENCE_IMAGE_IS_NOT_SELECTED" );
403 HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
404 if( !aPanel->getTransformationDataMap( aRefMap, true ) )
407 aRefImageObj = Handle(HYDROData_Image)::DownCast(
408 HYDROGUI_Tool::FindObjectByName( module(), aRefImageName, KIND_IMAGE ) );
409 if( aRefImageObj.IsNull() ) {
412 else if ( !isReferenceCorrect() ) {
413 aRefImageObj->RemoveAllReferences();
416 aTrsfPointA = aRefMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
417 aTrsfPointB = aRefMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
418 if ( !anIsByTwoPoints )
419 aTrsfPointC = aRefMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
421 // the same check of correctness for the reference points
422 if ( !checkPoints( aTrsfPointA, aTrsfPointB, aTrsfPointC, anIsByTwoPoints,
423 tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
424 tr( "REFERENCE_POINTS_A_B_ARE_IDENTICAL" ),
425 theErrorMsg, false ) )
430 if ( aTransformationMode == HYDROData_Image::ManualGeodesic )
432 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].GeodesicPoint;
433 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].GeodesicPoint;
434 if ( !anIsByTwoPoints )
435 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].GeodesicPoint;
439 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].CartesianPoint;
440 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].CartesianPoint;
441 if ( !anIsByTwoPoints )
442 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].CartesianPoint;
447 Handle(HYDROData_Image) anImageObj;
449 anImageObj = myEditedObject;
452 anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
453 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anImageObj );
454 theBrowseObjectsEntries.append( anEntry );
457 if( anImageObj.IsNull() )
460 anImageObj->SetName( anImageName );
461 anImageObj->SetImage( myImage );
463 if ( aTransformationMode == HYDROData_Image::CartesianFromFile ) {
464 QString aGeoreferencementFileName = aPanel->getGeoreferencementFileName();
465 if ( aGeoreferencementFileName.isEmpty() ) {
469 QPoint aLocalPointA( 0, 0 ),
470 aLocalPointB( anImageObj->Image().width(), 0 ),
471 aLocalPointC( INT_MIN, INT_MIN );
472 anImageObj->SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
473 if ( !anImageObj->SetGlobalPointsFromFile( aGeoreferencementFileName ) ) {
474 theErrorMsg = tr( "CANT_LOAD_GEOREFERENCEMENT_FILE" );
478 anImageObj->SetLocalPoints( aPointA, aPointB, aPointC, false );
480 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
482 anImageObj->SetReferencePoints( aRefImageObj,
483 aTrsfPointA, aTrsfPointB, aTrsfPointC );
487 anImageObj->SetGlobalPoints( aTransformationMode,
488 aTrsfPointA, aTrsfPointB, aTrsfPointC );
494 // Set imported file name for image
495 QString aFilePath = aPanel->getFileName();
496 anImageObj->SetFilePath( aFilePath );
499 // must be done after all checks and before calling SetVisible() method below
503 module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
505 anImageObj->Update();
507 theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
511 bool HYDROGUI_ImportImageOp::isReferenceCorrect() const
513 bool isCorrect = true;
515 if( myIsEdit && !myEditedObject.IsNull() )
517 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
518 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
519 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
520 if( !aRefImageObj.IsNull() )
522 bool isFoundEdited = false;
523 HYDROData_SequenceOfObjects aRefSeq = aRefImageObj->GetAllReferenceObjects();
524 for ( int i = 1, n = aRefSeq.Length(); i <= n && !isFoundEdited; ++i )
526 Handle(HYDROData_Entity) anObject = aRefSeq.Value( i );
527 isFoundEdited = anObject->GetName() == myEditedObject->GetName();
529 isCorrect = !isFoundEdited;
535 void HYDROGUI_ImportImageOp::onApply()
537 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
539 bool aCanApply = isReferenceCorrect();
540 if ( !aCanApply && !myEditedObject.IsNull() )
542 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
543 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
544 if ( !aRefImageObj.IsNull() )
545 aCanApply = SUIT_MessageBox::question( module()->getApp()->desktop(),
546 tr( "CORRECT_INPUT_DATA" ), tr( "CONFIRM_REMOVE_REFERENCE_FROM_IMAGE" ).
547 arg( aRefImageObj->GetName() ).arg( myEditedObject->GetName() ),
548 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes;
551 HYDROGUI_Operation::onApply();
553 aPanel->setRefImageName( "" );
554 onRefImageActivated( aPanel->getRefImageName() );
558 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
560 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
564 if( myPreviewPrs ) // if the image is changed by choosing another file
566 myPreviewPrs->setImage( myImage, true );
567 if ( sender() ) // reset the previous presentation settings
569 QString aFileName = aPanel->getFileName();
572 aPanel->setIsEdit( myIsEdit );
573 // restore the file name
574 aPanel->setFileName( aFileName );
575 // fill the reference list
576 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
577 getReferenceDataList( aPrsPointDataList );
578 aPanel->setPrsPointDataList( aPrsPointDataList );
580 if( myPreviewViewManager )
582 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
584 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
586 aViewPort->onBoundingRectChanged();
594 LightApp_Application* anApp = module()->getApp();
596 myActiveViewManager = anApp->activeViewManager();
598 myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
599 myPreviewPrs->setImage( myImage, true );
601 myPreviewViewManager =
602 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
603 if( myPreviewViewManager )
605 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
606 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
608 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
609 myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
610 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
612 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
614 //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
616 aViewPort->addItem( myPreviewPrs );
619 if ( myEditedObject ) {
620 size_t aViewId = (size_t)aViewer;
621 module()->setObjectVisible( aViewId, myEditedObject, true );
624 myPreviewPrs->setIsTransformationPointPreview( true );
626 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
627 this, SLOT( onPointSelected() ) );
632 // Set the image name in the dialog
633 QString anImageName = aPanel->getImageName().simplified();
634 // If edit mode and the name was not set yet then get from the edited object
635 if( myIsEdit && anImageName.isEmpty() )
637 if( !myEditedObject.IsNull() )
638 anImageName = myEditedObject->GetName();
640 // If the name was not set then initialize it from the selected file name
641 if ( anImageName.isEmpty() )
643 anImageName = aPanel->getFileName();
644 if ( !anImageName.isEmpty() ) {
645 anImageName = QFileInfo( anImageName ).baseName();
647 // If no file name then generate a new image name
648 if ( anImageName.isEmpty() ) {
649 anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
652 aPanel->setImageName( anImageName );
654 aPanel->setImageSize( myImage.size() );
656 aPanel->initializePointSelection();
657 onPointSelected( false );
658 onSetCIsUsed( !aPanel->isByTwoPoints() );
661 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
663 myPointType = thePointType;
665 myPreviewPrs->setTransformationPointType( thePointType );
666 if( myRefPreviewPrs )
667 myRefPreviewPrs->setTransformationPointType( thePointType );
670 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
675 if( !theIsRef && myPreviewPrs )
676 myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
677 else if( theIsRef && myRefPreviewPrs )
678 myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
681 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
683 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
685 QString aRefImageName;
686 if( theMode == HYDROData_Image::ReferenceImage )
688 aRefImageName = aPanel->getRefImageName();
689 if( aRefImageName.isEmpty() )
690 return; // do nothing in this case to avoid visual moving of preview prs
691 onRefImageActivated( aRefImageName );
695 if( myRefViewManager )
697 closeView( myRefViewManager );
702 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
704 if( theName.isEmpty() ) {
705 if( myRefViewManager )
706 closeView( myRefViewManager );
710 GraphicsView_ViewPort* aViewPort = 0;
712 LightApp_Application* anApp = module()->getApp();
713 ///// Get a view port for the reference image preview
714 if( myRefViewManager )
716 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
718 aViewPort = aViewer->getActiveViewPort();
723 anApp = module()->getApp();
724 // Init reference image preview
726 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
727 if( myRefViewManager )
729 connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
730 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
732 module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
733 myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
734 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
736 aViewPort = aViewer->getActiveViewPort();
737 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
738 this, SLOT( onRefPointSelected() ) );
746 size_t aViewId = (size_t)myRefViewManager->getViewer();
748 // Remove the old presentation of the reference image if any
749 if( myRefPreviewPrs )
751 module()->setObjectVisible( aViewId, myRefPreviewPrs->getObject(), false );
753 myRefPreviewPrs->setCaption( QString() );
754 aViewPort->removeItem( myRefPreviewPrs );
756 delete myRefPreviewPrs;
760 // Create a new reference image presentation
762 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
763 HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
764 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
765 if( !anImageObj.IsNull() )
767 anImage = anImageObj->Image();
769 myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
770 myRefPreviewPrs->setImage( anImage, true );
772 myRefPreviewPrs->setIsTransformationPointPreview( true );
773 myRefPreviewPrs->setTransformationPointType( myPointType );
775 myRefPreviewPrs->setIsByTwoPoints( aPanel->isByTwoPoints() );
777 // Add the new reference image presentation to the appropriate view
778 aViewPort->addItem( myRefPreviewPrs );
780 module()->setObjectVisible( aViewId, anImageObj, true );
785 // Split views horizontally
786 if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
788 qApp->processEvents();
789 QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
790 aViewPort->activateWindow();
792 aViewPort->setFocus(Qt::ActiveWindowFocusReason);
793 aWorkstack->splitHorizontal();
796 // Initialize the dialog
797 aPanel->setImageSize( anImage.size(), true );
798 aPanel->initializePointSelection();
799 onPointSelected( true );
802 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
807 void HYDROGUI_ImportImageOp::onPointSelected()
809 onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
812 void HYDROGUI_ImportImageOp::onRefPointSelected()
814 onPointSelected( true );
817 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
819 HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
823 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
825 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
826 aPrs->getTransformationPointMap();
828 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
829 while( anIter.hasNext() )
831 int aPointType = anIter.next().key();
832 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
833 const QPoint& aPoint = aTransformationPoint.Point;
835 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
836 aDataMap[ aPointType ] = aData;
839 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
842 void HYDROGUI_ImportImageOp::closePreview()
844 closeView( myPreviewViewManager );
845 closeView( myRefViewManager );
847 if( myActiveViewManager )
848 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
851 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
855 GraphicsView_ViewPort* aViewPort = 0;
856 if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
858 aViewPort = aViewer->getActiveViewPort();
860 disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
861 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
863 // Nullify appropriate presentation pointer
864 HYDROGUI_PrsImage* aPrs = 0;
865 switch ( module()->getViewManagerRole( aViewMgr ) )
867 case HYDROGUI_Module::VMR_ReferenceImage:
868 aPrs = myRefPreviewPrs;
871 case HYDROGUI_Module::VMR_TransformImage:
876 // Remove the appropriate presentation from the view
877 if( aPrs && aViewPort )
879 aViewPort->removeItem( aPrs );
884 module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here