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 if ( isApplyAndClose() )
82 myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
83 if( !myEditedObject.IsNull() )
85 QImage anImage = myEditedObject->Image();
86 bool anIsByTwoPoints = myEditedObject->IsByTwoPoints();
88 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
89 myEditedObject->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
91 // Create the edited image preview presentation in the viewer
92 onCreatePreview( anImage );
94 // Set transformation local points A,B,C to the image preview presentation
95 setPresentationTrsfPoints( myPreviewPrs, anIsByTwoPoints, aLocalPointA,
96 aLocalPointB, aLocalPointC );
98 // Build the existing image local and global points mapping
99 // according to the current transformation mode.
100 HYDROData_Image::TransformationMode aTrsfMode = myEditedObject->GetTrsfMode();
101 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
102 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
103 if ( aTrsfMode == HYDROData_Image::ReferenceImage )
105 // Compute global points using the transformation matrix of the reference image
106 Handle(HYDROData_Image) aRefImage = myEditedObject->GetTrsfReferenceImage();
107 if ( !aRefImage.IsNull() )
109 QTransform aRefTrsf = aRefImage->Trsf(); // The reference image transformation matrix
110 aTrsfPointA = aRefTrsf.map( aLocalPointA ); // Compute the global point A
111 aTrsfPointB = aRefTrsf.map( aLocalPointB ); // Compute the global point B
112 if ( !anIsByTwoPoints )
114 aTrsfPointC = aRefTrsf.map( aLocalPointC ); // Compute the global point C if used
116 // Build the local-global points map
117 // Use the reference image transformation mode for interpreting global points
118 computeTrsfData( aRefImage->GetTrsfMode(), anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
119 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
124 // Get global points from the edited image
125 myEditedObject->GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC );
126 // Build the local-global points map
127 computeTrsfData( aTrsfMode, anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
128 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
131 // Initialize the dialog mode, local and global coordinates
132 // except coordinates on the reference image
133 aPanel->setTransformationMode( aTrsfMode );
134 aPanel->setTransformationDataMap( aDataMap );
135 aPanel->setByTwoPoints( anIsByTwoPoints );
137 // Set points of the reference image
138 if ( aTrsfMode == HYDROData_Image::ReferenceImage )
140 Handle(HYDROData_Image) aRefImage;
141 myEditedObject->GetReferencePoints( aRefImage,
142 aTrsfPointA, aTrsfPointB, aTrsfPointC );
143 if ( !aRefImage.IsNull() )
145 aRefImageName = aRefImage->GetName();
147 // Create the reference image presentation in the viewer
148 onRefImageActivated( aRefImageName );
150 // Set transformation points A,B,C to the reference image presentation
151 setPresentationTrsfPoints( myRefPreviewPrs, anIsByTwoPoints, aTrsfPointA.toPoint(),
152 aTrsfPointB.toPoint(), aTrsfPointC.toPoint() );
154 // Prepare A, B, C points on the reference image
155 HYDROGUI_ImportImageDlg::TransformationDataMap aRefDataMap;
156 aRefDataMap[ HYDROGUI_PrsImage::PointA ] =
157 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointA.toPoint() );
158 aRefDataMap[ HYDROGUI_PrsImage::PointB ] =
159 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointB.toPoint() );
160 if ( !anIsByTwoPoints )
161 aRefDataMap[ HYDROGUI_PrsImage::PointC ] =
162 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointC.toPoint() );
164 // Initialize the dialog's A, B, C points coordinates of the reference image
165 aPanel->setTransformationDataMap( aRefDataMap, true, true );
171 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
172 getReferenceDataList( aPrsPointDataList );
173 aPanel->setPrsPointDataList( aPrsPointDataList );
174 // Select the current reference image in the dialog combobox
175 aPanel->setRefImageName( aRefImageName );
178 void HYDROGUI_ImportImageOp::setPresentationTrsfPoints( HYDROGUI_PrsImage* thePrs,
179 bool theIsByTwoPoints,
180 const QPoint theLocalPointA,
181 const QPoint theLocalPointB,
182 const QPoint theLocalPointC )
184 // Set transformation points A,B,C to the image presentation
187 HYDROGUI_PrsImage::TransformationPointMap aPointMap =
188 thePrs->getTransformationPointMap();
189 if( !aPointMap.isEmpty() )
191 aPointMap[ HYDROGUI_PrsImage::PointA ].Point = theLocalPointA;
192 aPointMap[ HYDROGUI_PrsImage::PointB ].Point = theLocalPointB;
193 if ( !theIsByTwoPoints )
194 aPointMap[ HYDROGUI_PrsImage::PointC ].Point = theLocalPointC;
196 thePrs->setIsByTwoPoints( theIsByTwoPoints );
197 thePrs->setTransformationPointMap( aPointMap );
202 void HYDROGUI_ImportImageOp::getReferenceDataList(
203 HYDROGUI_ImportImageDlg::PrsPointDataList& theList ) const
205 // Collect information about existing images and initialize the combobox
206 // reference image selector in the dialog.
207 HYDROData_Iterator anIterator( doc(), KIND_IMAGE );
208 for( ; anIterator.More(); anIterator.Next() )
210 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
211 if( !anImageObj.IsNull() )
213 if( myIsEdit && IsEqual( anImageObj, myEditedObject ) )
216 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
217 anImageObj->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
219 HYDROData_Image::TransformationMode aImgTrsfMode;
220 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
221 anImageObj->GetGlobalPoints( aImgTrsfMode,
222 aTrsfPointA, aTrsfPointB, aTrsfPointC );
224 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
225 computeTrsfData( aImgTrsfMode, false, aLocalPointA, aLocalPointB, aLocalPointC,
226 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
228 HYDROGUI_ImportImageDlg::PrsPointData aPrsPointData( anImageObj->GetName(), aDataMap );
229 theList.append( aPrsPointData );
234 void HYDROGUI_ImportImageOp::computeTrsfData( HYDROData_Image::TransformationMode theTrsfMode,
235 bool theIsByTwoPoints,
236 const QPoint& theLocalPointA,
237 const QPoint& theLocalPointB,
238 const QPoint& theLocalPointC,
239 const QPointF& theGlobalPointA,
240 const QPointF& theGlobalPointB,
241 const QPointF& theGlobalPointC,
242 HYDROGUI_ImportImageDlg::TransformationDataMap& theDataMap ) const
244 // Build the local-global points map
245 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
246 theDataMap[ HYDROGUI_PrsImage::PointA ] =
247 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointA, theGlobalPointA );
248 theDataMap[ HYDROGUI_PrsImage::PointB ] =
249 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointB, theGlobalPointB );
250 if ( !theIsByTwoPoints )
252 theDataMap[ HYDROGUI_PrsImage::PointC ] =
253 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointC, theGlobalPointC );
257 void HYDROGUI_ImportImageOp::abortOperation()
261 HYDROGUI_Operation::abortOperation();
264 void HYDROGUI_ImportImageOp::commitOperation()
266 if ( isApplyAndClose() )
269 HYDROGUI_Operation::commitOperation();
272 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
274 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
275 connect( aPanel, SIGNAL( createPreview( QImage ) ), SLOT( onCreatePreview( QImage ) ) );
276 connect( aPanel, SIGNAL( activatePointSelection( int ) ), SLOT( onActivatePointSelection( int ) ) );
277 connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ),
278 SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
279 connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
280 connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
281 SLOT( onRefImageActivated( const QString& ) ) );
282 connect( aPanel, SIGNAL( setCIsUsed( bool ) ), SLOT( onSetCIsUsed( bool ) ) );
286 void HYDROGUI_ImportImageOp::onSetCIsUsed( bool theCIsUsed )
290 myPreviewPrs->setIsByTwoPoints( !theCIsUsed );
292 if ( myRefPreviewPrs )
294 myRefPreviewPrs->setIsByTwoPoints( !theCIsUsed );
298 bool HYDROGUI_ImportImageOp::checkPoints( const QPointF& thePointA,
299 const QPointF& thePointB,
300 const QPointF& thePointC,
301 const bool theIsByTwoPoints,
302 const QString& theLineErrMsg,
303 const QString& thePoinErrMsg,
304 QString& theErrorMsg,
305 const bool theIsToCheckInvertibles ) const
307 double xa = thePointA.x();
308 double ya = thePointA.y();
309 double xb = thePointB.x();
310 double yb = thePointB.y();
311 double xc = thePointC.x();
312 double yc = thePointC.y();
314 if ( !theIsByTwoPoints )
316 // check that three input points don't belong to a single line
317 if ( ValuesEquals( ( yb - ya ) * ( xc - xa ), ( yc - ya ) * ( xb - xa ) ) )
319 theErrorMsg = theLineErrMsg;
323 if ( theIsToCheckInvertibles )
325 QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
327 bool anIsInvertible = false;
328 QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
329 if( !anIsInvertible )
331 theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
338 // check that two points are not identical
339 if ( ValuesEquals( xa, xb ) && ValuesEquals( ya, yb ) )
341 theErrorMsg = thePoinErrMsg;
349 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
350 QString& theErrorMsg,
351 QStringList& theBrowseObjectsEntries )
353 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
355 QString anImageName = aPanel->getImageName();
356 if( anImageName.isEmpty() )
359 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
361 // check that there are no other objects with the same name in the document
362 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
363 if( !anObject.IsNull() )
365 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
370 HYDROData_Image::TransformationMode aTransformationMode =
371 (HYDROData_Image::TransformationMode)aPanel->getTransformationMode();
373 QPoint aPointA, aPointB, aPointC;
374 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC( INT_MIN, INT_MIN );
375 Handle(HYDROData_Image) aRefImageObj;
377 if ( aTransformationMode != HYDROData_Image::CartesianFromFile ) {
378 HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
379 if( !aPanel->getTransformationDataMap( aMap ) )
382 bool anIsByTwoPoints = aPanel->isByTwoPoints();
384 aPointA = aMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
385 aPointB = aMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
386 aPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) :
387 aMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
389 // first, we check correctness of image points
390 if ( !checkPoints( aPointA, aPointB, aPointC, anIsByTwoPoints,
391 tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
392 tr( "POINTS_A_B_ARE_IDENTICAL" ),
393 theErrorMsg, true ) )
396 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
398 QString aRefImageName = aPanel->getRefImageName();
399 if( aRefImageName.isEmpty() )
401 theErrorMsg = tr( "REFERENCE_IMAGE_IS_NOT_SELECTED" );
405 HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
406 if( !aPanel->getTransformationDataMap( aRefMap, true ) )
409 aRefImageObj = Handle(HYDROData_Image)::DownCast(
410 HYDROGUI_Tool::FindObjectByName( module(), aRefImageName, KIND_IMAGE ) );
411 if( aRefImageObj.IsNull() ) {
414 else if ( !isReferenceCorrect() ) {
415 aRefImageObj->RemoveAllReferences();
418 aTrsfPointA = aRefMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
419 aTrsfPointB = aRefMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
420 if ( !anIsByTwoPoints )
421 aTrsfPointC = aRefMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
423 // the same check of correctness for the reference points
424 if ( !checkPoints( aTrsfPointA, aTrsfPointB, aTrsfPointC, anIsByTwoPoints,
425 tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
426 tr( "REFERENCE_POINTS_A_B_ARE_IDENTICAL" ),
427 theErrorMsg, false ) )
432 if ( aTransformationMode == HYDROData_Image::ManualGeodesic )
434 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].GeodesicPoint;
435 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].GeodesicPoint;
436 if ( !anIsByTwoPoints )
437 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].GeodesicPoint;
441 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].CartesianPoint;
442 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].CartesianPoint;
443 if ( !anIsByTwoPoints )
444 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].CartesianPoint;
449 Handle(HYDROData_Image) anImageObj;
451 anImageObj = myEditedObject;
454 anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
455 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anImageObj );
456 theBrowseObjectsEntries.append( anEntry );
459 if( anImageObj.IsNull() )
462 anImageObj->SetName( anImageName );
463 anImageObj->SetImage( myImage );
465 if ( aTransformationMode == HYDROData_Image::CartesianFromFile ) {
466 QString aGeoreferencementFileName = aPanel->getGeoreferencementFileName();
467 if ( aGeoreferencementFileName.isEmpty() ) {
471 QPoint aLocalPointA( 0, 0 ),
472 aLocalPointB( anImageObj->Image().width(), 0 ),
473 aLocalPointC( INT_MIN, INT_MIN );
474 anImageObj->SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
475 if ( !anImageObj->SetGlobalPointsFromFile( aGeoreferencementFileName ) ) {
476 theErrorMsg = tr( "CANT_LOAD_GEOREFERENCEMENT_FILE" );
480 anImageObj->SetLocalPoints( aPointA, aPointB, aPointC, false );
482 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
484 anImageObj->SetReferencePoints( aRefImageObj,
485 aTrsfPointA, aTrsfPointB, aTrsfPointC );
489 anImageObj->SetGlobalPoints( aTransformationMode,
490 aTrsfPointA, aTrsfPointB, aTrsfPointC );
496 // Set imported file name for image
497 QString aFilePath = aPanel->getFileName();
498 anImageObj->SetFilePath( aFilePath );
501 // must be done after all checks and before calling SetVisible() method below
502 if ( isApplyAndClose() )
506 module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
508 anImageObj->Update();
510 theUpdateFlags = UF_Model;
511 if ( isApplyAndClose() )
512 theUpdateFlags |= UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
517 bool HYDROGUI_ImportImageOp::isReferenceCorrect() const
519 bool isCorrect = true;
521 if( myIsEdit && !myEditedObject.IsNull() )
523 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
524 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
525 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
526 if( !aRefImageObj.IsNull() )
528 bool isFoundEdited = false;
529 HYDROData_SequenceOfObjects aRefSeq = aRefImageObj->GetAllReferenceObjects();
530 for ( int i = 1, n = aRefSeq.Length(); i <= n && !isFoundEdited; ++i )
532 Handle(HYDROData_Entity) anObject = aRefSeq.Value( i );
533 isFoundEdited = anObject->GetName() == myEditedObject->GetName();
535 isCorrect = !isFoundEdited;
541 void HYDROGUI_ImportImageOp::apply()
543 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
545 bool aCanApply = isReferenceCorrect();
546 if ( !aCanApply && !myEditedObject.IsNull() )
548 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
549 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
550 if ( !aRefImageObj.IsNull() )
551 aCanApply = SUIT_MessageBox::question( module()->getApp()->desktop(),
552 tr( "CORRECT_INPUT_DATA" ), tr( "CONFIRM_REMOVE_REFERENCE_FROM_IMAGE" ).
553 arg( aRefImageObj->GetName() ).arg( myEditedObject->GetName() ),
554 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes;
557 HYDROGUI_Operation::apply();
559 aPanel->setRefImageName( "" );
560 onRefImageActivated( aPanel->getRefImageName() );
564 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
566 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
570 if( myPreviewPrs ) // if the image is changed by choosing another file
572 myPreviewPrs->setImage( myImage, true );
573 if ( sender() ) // reset the previous presentation settings
575 QString aFileName = aPanel->getFileName();
578 aPanel->setIsEdit( myIsEdit );
579 // restore the file name
580 aPanel->setFileName( aFileName );
581 // fill the reference list
582 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
583 getReferenceDataList( aPrsPointDataList );
584 aPanel->setPrsPointDataList( aPrsPointDataList );
586 if( myPreviewViewManager )
588 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
590 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
592 aViewPort->onBoundingRectChanged();
600 LightApp_Application* anApp = module()->getApp();
602 myActiveViewManager = anApp->activeViewManager();
604 myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
605 myPreviewPrs->setImage( myImage, true );
607 myPreviewViewManager =
608 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
609 if( myPreviewViewManager )
611 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
612 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
614 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
615 myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
616 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
618 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
620 //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
622 aViewPort->addItem( myPreviewPrs );
625 if ( myEditedObject ) {
626 size_t aViewId = (size_t)aViewer;
627 module()->setObjectVisible( aViewId, myEditedObject, true );
630 myPreviewPrs->setIsTransformationPointPreview( true );
631 myPreviewPrs->setTransformationPointCursorShape( module()->getPrefEditCursor().shape() );
633 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
634 this, SLOT( onPointSelected() ) );
639 // Set the image name in the dialog
640 QString anImageName = aPanel->getImageName().simplified();
641 // If edit mode and the name was not set yet then get from the edited object
642 if( myIsEdit && anImageName.isEmpty() )
644 if( !myEditedObject.IsNull() )
645 anImageName = myEditedObject->GetName();
647 // If the name was not set then initialize it from the selected file name
648 if ( anImageName.isEmpty() )
650 anImageName = aPanel->getFileName();
651 if ( !anImageName.isEmpty() ) {
652 anImageName = QFileInfo( anImageName ).baseName();
654 // If no file name then generate a new image name
655 if ( anImageName.isEmpty() ) {
656 anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
659 aPanel->setImageName( anImageName );
661 aPanel->setImageSize( myImage.size() );
663 aPanel->initializePointSelection();
664 onPointSelected( false );
665 onSetCIsUsed( !aPanel->isByTwoPoints() );
668 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
670 myPointType = thePointType;
672 myPreviewPrs->setTransformationPointType( thePointType );
673 if( myRefPreviewPrs )
674 myRefPreviewPrs->setTransformationPointType( thePointType );
677 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
682 if( !theIsRef && myPreviewPrs )
683 myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
684 else if( theIsRef && myRefPreviewPrs )
685 myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
688 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
690 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
692 QString aRefImageName;
693 if( theMode == HYDROData_Image::ReferenceImage )
695 aRefImageName = aPanel->getRefImageName();
696 if( aRefImageName.isEmpty() )
697 return; // do nothing in this case to avoid visual moving of preview prs
698 onRefImageActivated( aRefImageName );
702 if( myRefViewManager )
704 closeView( myRefViewManager );
709 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
711 if( theName.isEmpty() ) {
712 if( myRefViewManager )
713 closeView( myRefViewManager );
717 GraphicsView_ViewPort* aViewPort = 0;
719 LightApp_Application* anApp = module()->getApp();
720 ///// Get a view port for the reference image preview
721 if( myRefViewManager )
723 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
725 aViewPort = aViewer->getActiveViewPort();
730 anApp = module()->getApp();
731 // Init reference image preview
733 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
734 if( myRefViewManager )
736 connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
737 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
739 module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
740 myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
741 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
743 aViewPort = aViewer->getActiveViewPort();
744 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
745 this, SLOT( onRefPointSelected() ) );
753 size_t aViewId = (size_t)myRefViewManager->getViewer();
755 // Remove the old presentation of the reference image if any
756 if( myRefPreviewPrs )
758 module()->setObjectVisible( aViewId, myRefPreviewPrs->getObject(), false );
760 myRefPreviewPrs->setCaption( QString() );
761 aViewPort->removeItem( myRefPreviewPrs );
763 delete myRefPreviewPrs;
767 // Create a new reference image presentation
769 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
770 HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
771 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
772 if( !anImageObj.IsNull() )
774 anImage = anImageObj->Image();
776 myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
777 myRefPreviewPrs->setImage( anImage, true );
779 myRefPreviewPrs->setIsTransformationPointPreview( true );
780 myRefPreviewPrs->setTransformationPointType( myPointType );
782 myRefPreviewPrs->setIsByTwoPoints( aPanel->isByTwoPoints() );
784 myRefPreviewPrs->setTransformationPointCursorShape( module()->getPrefEditCursor().shape() );
786 // Add the new reference image presentation to the appropriate view
787 aViewPort->addItem( myRefPreviewPrs );
789 module()->setObjectVisible( aViewId, anImageObj, true );
794 // Split views horizontally
795 if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
797 qApp->processEvents();
798 QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
799 aViewPort->activateWindow();
801 aViewPort->setFocus(Qt::ActiveWindowFocusReason);
802 aWorkstack->splitHorizontal();
805 // Initialize the dialog
806 aPanel->setImageSize( anImage.size(), true );
807 aPanel->initializePointSelection();
808 onPointSelected( true );
811 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
816 void HYDROGUI_ImportImageOp::onPointSelected()
818 onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
821 void HYDROGUI_ImportImageOp::onRefPointSelected()
823 onPointSelected( true );
826 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
828 HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
832 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
834 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
835 aPrs->getTransformationPointMap();
837 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
838 while( anIter.hasNext() )
840 int aPointType = anIter.next().key();
841 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
842 const QPoint& aPoint = aTransformationPoint.Point;
844 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
845 aDataMap[ aPointType ] = aData;
848 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
851 void HYDROGUI_ImportImageOp::closePreview()
853 closeView( myPreviewViewManager );
854 closeView( myRefViewManager );
856 if( myActiveViewManager )
857 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
860 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
864 GraphicsView_ViewPort* aViewPort = 0;
865 if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
867 aViewPort = aViewer->getActiveViewPort();
869 disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
870 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
872 // Nullify appropriate presentation pointer
873 HYDROGUI_PrsImage* aPrs = 0;
874 switch ( module()->getViewManagerRole( aViewMgr ) )
876 case HYDROGUI_Module::VMR_ReferenceImage:
877 aPrs = myRefPreviewPrs;
880 case HYDROGUI_Module::VMR_TransformImage:
885 // Remove the appropriate presentation from the view
886 if( aPrs && aViewPort )
888 aViewPort->removeItem( aPrs );
893 module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here