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 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
170 getReferenceDataList( aPrsPointDataList );
171 aPanel->setPrsPointDataList( aPrsPointDataList );
172 // Select the current reference image in the dialog combobox
173 aPanel->setRefImageName( aRefImageName );
176 void HYDROGUI_ImportImageOp::setPresentationTrsfPoints( HYDROGUI_PrsImage* thePrs,
177 bool theIsByTwoPoints,
178 const QPoint theLocalPointA,
179 const QPoint theLocalPointB,
180 const QPoint theLocalPointC )
182 // Set transformation points A,B,C to the image presentation
185 HYDROGUI_PrsImage::TransformationPointMap aPointMap =
186 thePrs->getTransformationPointMap();
187 if( !aPointMap.isEmpty() )
189 aPointMap[ HYDROGUI_PrsImage::PointA ].Point = theLocalPointA;
190 aPointMap[ HYDROGUI_PrsImage::PointB ].Point = theLocalPointB;
191 if ( !theIsByTwoPoints )
192 aPointMap[ HYDROGUI_PrsImage::PointC ].Point = theLocalPointC;
194 thePrs->setIsByTwoPoints( theIsByTwoPoints );
195 thePrs->setTransformationPointMap( aPointMap );
200 void HYDROGUI_ImportImageOp::getReferenceDataList(
201 HYDROGUI_ImportImageDlg::PrsPointDataList& theList ) const
203 // Collect information about existing images and initialize the combobox
204 // reference image selector in the dialog.
205 HYDROData_Iterator anIterator( doc(), KIND_IMAGE );
206 for( ; anIterator.More(); anIterator.Next() )
208 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
209 if( !anImageObj.IsNull() )
211 if( myIsEdit && IsEqual( anImageObj, myEditedObject ) )
214 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
215 anImageObj->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
217 HYDROData_Image::TransformationMode aImgTrsfMode;
218 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
219 anImageObj->GetGlobalPoints( aImgTrsfMode,
220 aTrsfPointA, aTrsfPointB, aTrsfPointC );
222 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
223 computeTrsfData( aImgTrsfMode, false, aLocalPointA, aLocalPointB, aLocalPointC,
224 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
226 HYDROGUI_ImportImageDlg::PrsPointData aPrsPointData( anImageObj->GetName(), aDataMap );
227 theList.append( aPrsPointData );
232 void HYDROGUI_ImportImageOp::computeTrsfData( HYDROData_Image::TransformationMode theTrsfMode,
233 bool theIsByTwoPoints,
234 const QPoint& theLocalPointA,
235 const QPoint& theLocalPointB,
236 const QPoint& theLocalPointC,
237 const QPointF& theGlobalPointA,
238 const QPointF& theGlobalPointB,
239 const QPointF& theGlobalPointC,
240 HYDROGUI_ImportImageDlg::TransformationDataMap& theDataMap ) const
242 // Build the local-global points map
243 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
244 theDataMap[ HYDROGUI_PrsImage::PointA ] =
245 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointA, theGlobalPointA );
246 theDataMap[ HYDROGUI_PrsImage::PointB ] =
247 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointB, theGlobalPointB );
248 if ( !theIsByTwoPoints )
250 theDataMap[ HYDROGUI_PrsImage::PointC ] =
251 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointC, theGlobalPointC );
255 void HYDROGUI_ImportImageOp::abortOperation()
259 HYDROGUI_Operation::abortOperation();
262 void HYDROGUI_ImportImageOp::commitOperation()
266 HYDROGUI_Operation::commitOperation();
269 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
271 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
272 connect( aPanel, SIGNAL( createPreview( QImage ) ), SLOT( onCreatePreview( QImage ) ) );
273 connect( aPanel, SIGNAL( activatePointSelection( int ) ), SLOT( onActivatePointSelection( int ) ) );
274 connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ),
275 SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
276 connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
277 connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
278 SLOT( onRefImageActivated( const QString& ) ) );
279 connect( aPanel, SIGNAL( setCIsUsed( bool ) ), SLOT( onSetCIsUsed( bool ) ) );
283 void HYDROGUI_ImportImageOp::onSetCIsUsed( bool theCIsUsed )
287 myPreviewPrs->setIsByTwoPoints( !theCIsUsed );
289 if ( myRefPreviewPrs )
291 myRefPreviewPrs->setIsByTwoPoints( !theCIsUsed );
295 bool HYDROGUI_ImportImageOp::checkPoints( const QPointF& thePointA,
296 const QPointF& thePointB,
297 const QPointF& thePointC,
298 const bool theIsByTwoPoints,
299 const QString& theLineErrMsg,
300 const QString& thePoinErrMsg,
301 QString& theErrorMsg,
302 const bool theIsToCheckInvertibles ) const
304 double xa = thePointA.x();
305 double ya = thePointA.y();
306 double xb = thePointB.x();
307 double yb = thePointB.y();
308 double xc = thePointC.x();
309 double yc = thePointC.y();
311 if ( !theIsByTwoPoints )
313 // check that three input points don't belong to a single line
314 if ( ValuesEquals( ( yb - ya ) * ( xc - xa ), ( yc - ya ) * ( xb - xa ) ) )
316 theErrorMsg = theLineErrMsg;
320 if ( theIsToCheckInvertibles )
322 QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
324 bool anIsInvertible = false;
325 QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
326 if( !anIsInvertible )
328 theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
335 // check that two points are not identical
336 if ( ValuesEquals( xa, xb ) && ValuesEquals( ya, yb ) )
338 theErrorMsg = thePoinErrMsg;
346 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
347 QString& theErrorMsg )
349 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
351 QString anImageName = aPanel->getImageName();
352 if( anImageName.isEmpty() )
355 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
357 // check that there are no other objects with the same name in the document
358 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
359 if( !anObject.IsNull() )
361 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
366 HYDROData_Image::TransformationMode aTransformationMode =
367 (HYDROData_Image::TransformationMode)aPanel->getTransformationMode();
369 QPoint aPointA, aPointB, aPointC;
370 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC( INT_MIN, INT_MIN );
371 Handle(HYDROData_Image) aRefImageObj;
373 if ( aTransformationMode != HYDROData_Image::CartesianFromFile ) {
374 HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
375 if( !aPanel->getTransformationDataMap( aMap ) )
378 bool anIsByTwoPoints = aPanel->isByTwoPoints();
380 aPointA = aMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
381 aPointB = aMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
382 aPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) :
383 aMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
385 // first, we check correctness of image points
386 if ( !checkPoints( aPointA, aPointB, aPointC, anIsByTwoPoints,
387 tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
388 tr( "POINTS_A_B_ARE_IDENTICAL" ),
389 theErrorMsg, true ) )
392 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
394 QString aRefImageName = aPanel->getRefImageName();
395 if( aRefImageName.isEmpty() )
397 theErrorMsg = tr( "REFERENCE_IMAGE_IS_NOT_SELECTED" );
401 HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
402 if( !aPanel->getTransformationDataMap( aRefMap, true ) )
405 aRefImageObj = Handle(HYDROData_Image)::DownCast(
406 HYDROGUI_Tool::FindObjectByName( module(), aRefImageName, KIND_IMAGE ) );
407 if( aRefImageObj.IsNull() ) {
410 else if ( !isReferenceCorrect() ) {
411 aRefImageObj->RemoveAllReferences();
414 aTrsfPointA = aRefMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
415 aTrsfPointB = aRefMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
416 if ( !anIsByTwoPoints )
417 aTrsfPointC = aRefMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
419 // the same check of correctness for the reference points
420 if ( !checkPoints( aTrsfPointA, aTrsfPointB, aTrsfPointC, anIsByTwoPoints,
421 tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
422 tr( "REFERENCE_POINTS_A_B_ARE_IDENTICAL" ),
423 theErrorMsg, false ) )
428 if ( aTransformationMode == HYDROData_Image::ManualGeodesic )
430 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].GeodesicPoint;
431 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].GeodesicPoint;
432 if ( !anIsByTwoPoints )
433 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].GeodesicPoint;
437 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].CartesianPoint;
438 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].CartesianPoint;
439 if ( !anIsByTwoPoints )
440 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].CartesianPoint;
445 Handle(HYDROData_Image) anImageObj;
447 anImageObj = myEditedObject;
449 anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
451 if( anImageObj.IsNull() )
454 anImageObj->SetName( anImageName );
455 anImageObj->SetImage( myImage );
457 if ( aTransformationMode == HYDROData_Image::CartesianFromFile ) {
458 QString aGeoreferencementFileName = aPanel->getGeoreferencementFileName();
459 if ( aGeoreferencementFileName.isEmpty() ) {
463 QPoint aLocalPointA( 0, 0 ),
464 aLocalPointB( anImageObj->Image().width(), 0 ),
465 aLocalPointC( INT_MIN, INT_MIN );
466 anImageObj->SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
467 if ( !anImageObj->SetGlobalPointsFromFile( aGeoreferencementFileName ) ) {
468 theErrorMsg = tr( "CANT_LOAD_GEOREFERENCEMENT_FILE" );
472 anImageObj->SetLocalPoints( aPointA, aPointB, aPointC, false );
474 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
476 anImageObj->SetReferencePoints( aRefImageObj,
477 aTrsfPointA, aTrsfPointB, aTrsfPointC );
481 anImageObj->SetGlobalPoints( aTransformationMode,
482 aTrsfPointA, aTrsfPointB, aTrsfPointC );
488 // Set imported file name for image
489 QString aFilePath = aPanel->getFileName();
490 anImageObj->SetFilePath( aFilePath );
493 // must be done after all checks and before calling SetVisible() method below
497 module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
499 anImageObj->Update();
501 theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
505 bool HYDROGUI_ImportImageOp::isReferenceCorrect() const
507 bool isCorrect = true;
509 if( myIsEdit && !myEditedObject.IsNull() )
511 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
512 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
513 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
514 if( !aRefImageObj.IsNull() )
516 bool isFoundEdited = false;
517 HYDROData_SequenceOfObjects aRefSeq = aRefImageObj->GetAllReferenceObjects();
518 for ( int i = 1, n = aRefSeq.Length(); i <= n && !isFoundEdited; ++i )
520 Handle(HYDROData_Entity) anObject = aRefSeq.Value( i );
521 isFoundEdited = anObject->GetName() == myEditedObject->GetName();
523 isCorrect = !isFoundEdited;
529 void HYDROGUI_ImportImageOp::onApply()
531 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
533 bool aCanApply = isReferenceCorrect();
534 if ( !aCanApply && !myEditedObject.IsNull() )
536 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
537 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
538 if ( !aRefImageObj.IsNull() )
539 aCanApply = SUIT_MessageBox::question( module()->getApp()->desktop(),
540 tr( "CORRECT_INPUT_DATA" ), tr( "CONFIRM_REMOVE_REFERENCE_FROM_IMAGE" ).
541 arg( aRefImageObj->GetName() ).arg( myEditedObject->GetName() ),
542 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes;
545 HYDROGUI_Operation::onApply();
547 aPanel->setRefImageName( "" );
548 onRefImageActivated( aPanel->getRefImageName() );
552 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
554 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
558 if( myPreviewPrs ) // if the image is changed by choosing another file
560 myPreviewPrs->setImage( myImage, true );
561 if ( sender() ) // reset the previous presentation settings
563 QString aFileName = aPanel->getFileName();
566 aPanel->setIsEdit( myIsEdit );
567 // restore the file name
568 aPanel->setFileName( aFileName );
569 // fill the reference list
570 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
571 getReferenceDataList( aPrsPointDataList );
572 aPanel->setPrsPointDataList( aPrsPointDataList );
574 if( myPreviewViewManager )
576 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
578 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
580 aViewPort->onBoundingRectChanged();
588 LightApp_Application* anApp = module()->getApp();
590 myActiveViewManager = anApp->activeViewManager();
592 myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
593 myPreviewPrs->setImage( myImage, true );
595 myPreviewViewManager =
596 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
597 if( myPreviewViewManager )
599 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
600 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
602 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
603 myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
604 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
606 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
608 //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
610 aViewPort->addItem( myPreviewPrs );
613 myPreviewPrs->setIsTransformationPointPreview( true );
615 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
616 this, SLOT( onPointSelected() ) );
621 // Set the image name in the dialog
622 QString anImageName = aPanel->getImageName().simplified();
623 // If edit mode and the name was not set yet then get from the edited object
624 if( myIsEdit && anImageName.isEmpty() )
626 if( !myEditedObject.IsNull() )
627 anImageName = myEditedObject->GetName();
629 // If the name was not set then initialize it from the selected file name
630 if ( anImageName.isEmpty() )
632 anImageName = aPanel->getFileName();
633 if ( !anImageName.isEmpty() ) {
634 anImageName = QFileInfo( anImageName ).baseName();
636 // If no file name then generate a new image name
637 if ( anImageName.isEmpty() ) {
638 anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
641 aPanel->setImageName( anImageName );
643 aPanel->setImageSize( myImage.size() );
645 aPanel->initializePointSelection();
646 onPointSelected( false );
647 onSetCIsUsed( !aPanel->isByTwoPoints() );
650 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
652 myPointType = thePointType;
654 myPreviewPrs->setTransformationPointType( thePointType );
655 if( myRefPreviewPrs )
656 myRefPreviewPrs->setTransformationPointType( thePointType );
659 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
664 if( !theIsRef && myPreviewPrs )
665 myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
666 else if( theIsRef && myRefPreviewPrs )
667 myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
670 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
672 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
674 QString aRefImageName;
675 if( theMode == HYDROData_Image::ReferenceImage )
677 aRefImageName = aPanel->getRefImageName();
678 if( aRefImageName.isEmpty() )
679 return; // do nothing in this case to avoid visual moving of preview prs
680 onRefImageActivated( aRefImageName );
684 if( myRefViewManager )
686 closeView( myRefViewManager );
691 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
693 if( theName.isEmpty() ) {
694 if( myRefViewManager )
695 closeView( myRefViewManager );
699 GraphicsView_ViewPort* aViewPort = 0;
701 LightApp_Application* anApp = module()->getApp();
702 ///// Get a view port for the reference image preview
703 if( myRefViewManager )
705 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
707 aViewPort = aViewer->getActiveViewPort();
712 anApp = module()->getApp();
713 // Init reference image preview
715 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
716 if( myRefViewManager )
718 connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
719 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
721 module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
722 myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
723 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
725 aViewPort = aViewer->getActiveViewPort();
726 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
727 this, SLOT( onRefPointSelected() ) );
735 // Remove the old presentation of the reference image if any
736 if( myRefPreviewPrs )
738 myRefPreviewPrs->setCaption( QString() );
739 aViewPort->removeItem( myRefPreviewPrs );
741 delete myRefPreviewPrs;
745 // Create a new reference image presentation
747 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
748 HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
749 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
750 if( !anImageObj.IsNull() )
752 anImage = anImageObj->Image();
754 myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
755 myRefPreviewPrs->setImage( anImage, true );
757 myRefPreviewPrs->setIsTransformationPointPreview( true );
758 myRefPreviewPrs->setTransformationPointType( myPointType );
760 myRefPreviewPrs->setIsByTwoPoints( aPanel->isByTwoPoints() );
762 // Add the new reference image presentation to the appropriate view
763 aViewPort->addItem( myRefPreviewPrs );
768 // Split views horizontally
769 if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
771 qApp->processEvents();
772 QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
773 aViewPort->activateWindow();
775 aViewPort->setFocus(Qt::ActiveWindowFocusReason);
776 aWorkstack->splitHorizontal();
779 // Initialize the dialog
780 aPanel->setImageSize( anImage.size(), true );
781 aPanel->initializePointSelection();
782 onPointSelected( true );
785 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
790 void HYDROGUI_ImportImageOp::onPointSelected()
792 onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
795 void HYDROGUI_ImportImageOp::onRefPointSelected()
797 onPointSelected( true );
800 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
802 HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
806 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
808 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
809 aPrs->getTransformationPointMap();
811 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
812 while( anIter.hasNext() )
814 int aPointType = anIter.next().key();
815 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
816 const QPoint& aPoint = aTransformationPoint.Point;
818 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
819 aDataMap[ aPointType ] = aData;
822 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
825 void HYDROGUI_ImportImageOp::closePreview()
827 closeView( myPreviewViewManager );
828 closeView( myRefViewManager );
830 if( myActiveViewManager )
831 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
834 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
838 GraphicsView_ViewPort* aViewPort = 0;
839 if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
841 aViewPort = aViewer->getActiveViewPort();
843 disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
844 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
846 // Nullify appropriate presentation pointer
847 HYDROGUI_PrsImage* aPrs = 0;
848 switch ( module()->getViewManagerRole( aViewMgr ) )
850 case HYDROGUI_Module::VMR_ReferenceImage:
851 aPrs = myRefPreviewPrs;
854 case HYDROGUI_Module::VMR_TransformImage:
859 // Remove the appropriate presentation from the view
860 if( aPrs && aViewPort )
862 aViewPort->removeItem( aPrs );
867 module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here