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 myPreviewPrs->setIsTransformationPointPreview( true );
621 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
622 this, SLOT( onPointSelected() ) );
627 // Set the image name in the dialog
628 QString anImageName = aPanel->getImageName().simplified();
629 // If edit mode and the name was not set yet then get from the edited object
630 if( myIsEdit && anImageName.isEmpty() )
632 if( !myEditedObject.IsNull() )
633 anImageName = myEditedObject->GetName();
635 // If the name was not set then initialize it from the selected file name
636 if ( anImageName.isEmpty() )
638 anImageName = aPanel->getFileName();
639 if ( !anImageName.isEmpty() ) {
640 anImageName = QFileInfo( anImageName ).baseName();
642 // If no file name then generate a new image name
643 if ( anImageName.isEmpty() ) {
644 anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
647 aPanel->setImageName( anImageName );
649 aPanel->setImageSize( myImage.size() );
651 aPanel->initializePointSelection();
652 onPointSelected( false );
653 onSetCIsUsed( !aPanel->isByTwoPoints() );
656 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
658 myPointType = thePointType;
660 myPreviewPrs->setTransformationPointType( thePointType );
661 if( myRefPreviewPrs )
662 myRefPreviewPrs->setTransformationPointType( thePointType );
665 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
670 if( !theIsRef && myPreviewPrs )
671 myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
672 else if( theIsRef && myRefPreviewPrs )
673 myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
676 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
678 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
680 QString aRefImageName;
681 if( theMode == HYDROData_Image::ReferenceImage )
683 aRefImageName = aPanel->getRefImageName();
684 if( aRefImageName.isEmpty() )
685 return; // do nothing in this case to avoid visual moving of preview prs
686 onRefImageActivated( aRefImageName );
690 if( myRefViewManager )
692 closeView( myRefViewManager );
697 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
699 if( theName.isEmpty() ) {
700 if( myRefViewManager )
701 closeView( myRefViewManager );
705 GraphicsView_ViewPort* aViewPort = 0;
707 LightApp_Application* anApp = module()->getApp();
708 ///// Get a view port for the reference image preview
709 if( myRefViewManager )
711 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
713 aViewPort = aViewer->getActiveViewPort();
718 anApp = module()->getApp();
719 // Init reference image preview
721 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
722 if( myRefViewManager )
724 connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
725 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
727 module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
728 myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
729 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
731 aViewPort = aViewer->getActiveViewPort();
732 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
733 this, SLOT( onRefPointSelected() ) );
741 // Remove the old presentation of the reference image if any
742 if( myRefPreviewPrs )
744 myRefPreviewPrs->setCaption( QString() );
745 aViewPort->removeItem( myRefPreviewPrs );
747 delete myRefPreviewPrs;
751 // Create a new reference image presentation
753 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
754 HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
755 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
756 if( !anImageObj.IsNull() )
758 anImage = anImageObj->Image();
760 myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
761 myRefPreviewPrs->setImage( anImage, true );
763 myRefPreviewPrs->setIsTransformationPointPreview( true );
764 myRefPreviewPrs->setTransformationPointType( myPointType );
766 myRefPreviewPrs->setIsByTwoPoints( aPanel->isByTwoPoints() );
768 // Add the new reference image presentation to the appropriate view
769 aViewPort->addItem( myRefPreviewPrs );
774 // Split views horizontally
775 if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
777 qApp->processEvents();
778 QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
779 aViewPort->activateWindow();
781 aViewPort->setFocus(Qt::ActiveWindowFocusReason);
782 aWorkstack->splitHorizontal();
785 // Initialize the dialog
786 aPanel->setImageSize( anImage.size(), true );
787 aPanel->initializePointSelection();
788 onPointSelected( true );
791 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
796 void HYDROGUI_ImportImageOp::onPointSelected()
798 onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
801 void HYDROGUI_ImportImageOp::onRefPointSelected()
803 onPointSelected( true );
806 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
808 HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
812 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
814 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
815 aPrs->getTransformationPointMap();
817 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
818 while( anIter.hasNext() )
820 int aPointType = anIter.next().key();
821 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
822 const QPoint& aPoint = aTransformationPoint.Point;
824 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
825 aDataMap[ aPointType ] = aData;
828 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
831 void HYDROGUI_ImportImageOp::closePreview()
833 closeView( myPreviewViewManager );
834 closeView( myRefViewManager );
836 if( myActiveViewManager )
837 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
840 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
844 GraphicsView_ViewPort* aViewPort = 0;
845 if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
847 aViewPort = aViewer->getActiveViewPort();
849 disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
850 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
852 // Nullify appropriate presentation pointer
853 HYDROGUI_PrsImage* aPrs = 0;
854 switch ( module()->getViewManagerRole( aViewMgr ) )
856 case HYDROGUI_Module::VMR_ReferenceImage:
857 aPrs = myRefPreviewPrs;
860 case HYDROGUI_Module::VMR_TransformImage:
865 // Remove the appropriate presentation from the view
866 if( aPrs && aViewPort )
868 aViewPort->removeItem( aPrs );
873 module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here