1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_ImportImageOp.h"
21 #include "HYDROGUI_DataModel.h"
22 #include <HYDROGUI_DataObject.h>
23 #include "HYDROGUI_ImportImageDlg.h"
24 #include "HYDROGUI_Module.h"
25 #include "HYDROGUI_PrsImage.h"
26 #include "HYDROGUI_Tool.h"
27 #include "HYDROGUI_UpdateFlags.h"
29 #include <HYDROData_Iterator.h>
30 #include <HYDROData_Tool.h>
32 #include <GraphicsView_ViewManager.h>
33 #include <GraphicsView_ViewPort.h>
34 #include <GraphicsView_Viewer.h>
36 #include <LightApp_Application.h>
37 #include <LightApp_UpdateFlags.h>
39 #include <STD_TabDesktop.h>
40 #include <SUIT_Desktop.h>
41 #include <SUIT_MessageBox.h>
42 #include <QtxWorkstack.h>
43 #include <QApplication>
46 HYDROGUI_ImportImageOp::HYDROGUI_ImportImageOp( HYDROGUI_Module* theModule,
47 const bool theIsEdit )
48 : HYDROGUI_Operation( theModule ),
49 myIsEdit( theIsEdit ),
51 myActiveViewManager( 0 ),
52 myPreviewViewManager( 0 ),
53 myRefViewManager( 0 ),
56 myPointType( HYDROGUI_PrsImage::None )
58 setName( theIsEdit ? tr( "EDIT_IMPORTED_IMAGE" ) : tr( "IMPORT_IMAGE" ) );
61 HYDROGUI_ImportImageOp::~HYDROGUI_ImportImageOp()
65 void HYDROGUI_ImportImageOp::startOperation()
67 HYDROGUI_Operation::startOperation();
69 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
71 aPanel->setIsEdit( myIsEdit );
73 QString aRefImageName;
77 myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
78 if( !myEditedObject.IsNull() )
80 QImage anImage = myEditedObject->Image();
81 bool anIsByTwoPoints = myEditedObject->IsByTwoPoints();
83 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
84 myEditedObject->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
86 // Create the edited image preview presentation in the viewer
87 onCreatePreview( anImage );
89 // Set transformation local points A,B,C to the image preview presentation
90 setPresentationTrsfPoints( myPreviewPrs, anIsByTwoPoints, aLocalPointA,
91 aLocalPointB, aLocalPointC );
93 // Build the existing image local and global points mapping
94 // according to the current transformation mode.
95 HYDROData_Image::TransformationMode aTrsfMode = myEditedObject->GetTrsfMode();
96 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
97 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
98 if ( aTrsfMode == HYDROData_Image::ReferenceImage )
100 // Compute global points using the transformation matrix of the reference image
101 Handle(HYDROData_Image) aRefImage = myEditedObject->GetTrsfReferenceImage();
102 if ( !aRefImage.IsNull() )
104 QTransform aRefTrsf = aRefImage->Trsf(); // The reference image transformation matrix
105 aTrsfPointA = aRefTrsf.map( aLocalPointA ); // Compute the global point A
106 aTrsfPointB = aRefTrsf.map( aLocalPointB ); // Compute the global point B
107 if ( !anIsByTwoPoints )
109 aTrsfPointC = aRefTrsf.map( aLocalPointC ); // Compute the global point C if used
111 // Build the local-global points map
112 // Use the reference image transformation mode for interpreting global points
113 computeTrsfData( aRefImage->GetTrsfMode(), anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
114 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
119 // Get global points from the edited image
120 myEditedObject->GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC );
121 // Build the local-global points map
122 computeTrsfData( aTrsfMode, anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
123 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
126 // Initialize the dialog mode, local and global coordinates
127 // except coordinates on the reference image
128 aPanel->setTransformationMode( aTrsfMode );
129 aPanel->setTransformationDataMap( aDataMap );
130 aPanel->setByTwoPoints( anIsByTwoPoints );
132 // Set points of the reference image
133 if ( aTrsfMode == HYDROData_Image::ReferenceImage )
135 Handle(HYDROData_Image) aRefImage;
136 myEditedObject->GetReferencePoints( aRefImage,
137 aTrsfPointA, aTrsfPointB, aTrsfPointC );
138 if ( !aRefImage.IsNull() )
140 aRefImageName = aRefImage->GetName();
142 // Create the reference image presentation in the viewer
143 onRefImageActivated( aRefImageName );
145 // Set transformation points A,B,C to the reference image presentation
146 setPresentationTrsfPoints( myRefPreviewPrs, anIsByTwoPoints, aTrsfPointA.toPoint(),
147 aTrsfPointB.toPoint(), aTrsfPointC.toPoint() );
149 // Prepare A, B, C points on the reference image
150 HYDROGUI_ImportImageDlg::TransformationDataMap aRefDataMap;
151 aRefDataMap[ HYDROGUI_PrsImage::PointA ] =
152 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointA.toPoint() );
153 aRefDataMap[ HYDROGUI_PrsImage::PointB ] =
154 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointB.toPoint() );
155 if ( !anIsByTwoPoints )
156 aRefDataMap[ HYDROGUI_PrsImage::PointC ] =
157 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointC.toPoint() );
159 // Initialize the dialog's A, B, C points coordinates of the reference image
160 aPanel->setTransformationDataMap( aRefDataMap, true, true );
166 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
167 getReferenceDataList( aPrsPointDataList );
168 aPanel->setPrsPointDataList( aPrsPointDataList );
169 // Select the current reference image in the dialog combobox
170 aPanel->setRefImageName( aRefImageName );
173 void HYDROGUI_ImportImageOp::setPresentationTrsfPoints( HYDROGUI_PrsImage* thePrs,
174 bool theIsByTwoPoints,
175 const QPoint theLocalPointA,
176 const QPoint theLocalPointB,
177 const QPoint theLocalPointC )
179 // Set transformation points A,B,C to the image presentation
182 HYDROGUI_PrsImage::TransformationPointMap aPointMap =
183 thePrs->getTransformationPointMap();
184 if( !aPointMap.isEmpty() )
186 aPointMap[ HYDROGUI_PrsImage::PointA ].Point = theLocalPointA;
187 aPointMap[ HYDROGUI_PrsImage::PointB ].Point = theLocalPointB;
188 if ( !theIsByTwoPoints )
189 aPointMap[ HYDROGUI_PrsImage::PointC ].Point = theLocalPointC;
191 thePrs->setIsByTwoPoints( theIsByTwoPoints );
192 thePrs->setTransformationPointMap( aPointMap );
197 void HYDROGUI_ImportImageOp::getReferenceDataList(
198 HYDROGUI_ImportImageDlg::PrsPointDataList& theList ) const
200 // Collect information about existing images and initialize the combobox
201 // reference image selector in the dialog.
202 HYDROData_Iterator anIterator( doc(), KIND_IMAGE );
203 for( ; anIterator.More(); anIterator.Next() )
205 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
206 if( !anImageObj.IsNull() )
208 if( myIsEdit && IsEqual( anImageObj, myEditedObject ) )
211 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
212 anImageObj->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
214 HYDROData_Image::TransformationMode aImgTrsfMode;
215 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
216 anImageObj->GetGlobalPoints( aImgTrsfMode,
217 aTrsfPointA, aTrsfPointB, aTrsfPointC );
219 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
220 computeTrsfData( aImgTrsfMode, false, aLocalPointA, aLocalPointB, aLocalPointC,
221 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
223 HYDROGUI_ImportImageDlg::PrsPointData aPrsPointData( anImageObj->GetName(), aDataMap );
224 theList.append( aPrsPointData );
229 void HYDROGUI_ImportImageOp::computeTrsfData( HYDROData_Image::TransformationMode theTrsfMode,
230 bool theIsByTwoPoints,
231 const QPoint& theLocalPointA,
232 const QPoint& theLocalPointB,
233 const QPoint& theLocalPointC,
234 const QPointF& theGlobalPointA,
235 const QPointF& theGlobalPointB,
236 const QPointF& theGlobalPointC,
237 HYDROGUI_ImportImageDlg::TransformationDataMap& theDataMap ) const
239 // Build the local-global points map
240 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
241 theDataMap[ HYDROGUI_PrsImage::PointA ] =
242 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointA, theGlobalPointA );
243 theDataMap[ HYDROGUI_PrsImage::PointB ] =
244 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointB, theGlobalPointB );
245 if ( !theIsByTwoPoints )
247 theDataMap[ HYDROGUI_PrsImage::PointC ] =
248 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointC, theGlobalPointC );
252 void HYDROGUI_ImportImageOp::abortOperation()
256 HYDROGUI_Operation::abortOperation();
259 void HYDROGUI_ImportImageOp::commitOperation()
263 HYDROGUI_Operation::commitOperation();
266 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
268 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
269 connect( aPanel, SIGNAL( createPreview( QImage ) ), SLOT( onCreatePreview( QImage ) ) );
270 connect( aPanel, SIGNAL( activatePointSelection( int ) ), SLOT( onActivatePointSelection( int ) ) );
271 connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ),
272 SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
273 connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
274 connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
275 SLOT( onRefImageActivated( const QString& ) ) );
276 connect( aPanel, SIGNAL( setCIsUsed( bool ) ), SLOT( onSetCIsUsed( bool ) ) );
280 void HYDROGUI_ImportImageOp::onSetCIsUsed( bool theCIsUsed )
284 myPreviewPrs->setIsByTwoPoints( !theCIsUsed );
286 if ( myRefPreviewPrs )
288 myRefPreviewPrs->setIsByTwoPoints( !theCIsUsed );
292 bool HYDROGUI_ImportImageOp::checkPoints( const QPointF& thePointA,
293 const QPointF& thePointB,
294 const QPointF& thePointC,
295 const bool theIsByTwoPoints,
296 const QString& theLineErrMsg,
297 const QString& thePoinErrMsg,
298 QString& theErrorMsg,
299 const bool theIsToCheckInvertibles ) const
301 double xa = thePointA.x();
302 double ya = thePointA.y();
303 double xb = thePointB.x();
304 double yb = thePointB.y();
305 double xc = thePointC.x();
306 double yc = thePointC.y();
308 if ( !theIsByTwoPoints )
310 // check that three input points don't belong to a single line
311 if ( ValuesEquals( ( yb - ya ) * ( xc - xa ), ( yc - ya ) * ( xb - xa ) ) )
313 theErrorMsg = theLineErrMsg;
317 if ( theIsToCheckInvertibles )
319 QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
321 bool anIsInvertible = false;
322 QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
323 if( !anIsInvertible )
325 theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
332 // check that two points are not identical
333 if ( ValuesEquals( xa, xb ) && ValuesEquals( ya, yb ) )
335 theErrorMsg = thePoinErrMsg;
343 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
344 QString& theErrorMsg,
345 QStringList& theBrowseObjectsEntries )
347 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
349 QString anImageName = aPanel->getImageName();
350 if( anImageName.isEmpty() )
353 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
355 // check that there are no other objects with the same name in the document
356 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
357 if( !anObject.IsNull() )
359 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
364 HYDROData_Image::TransformationMode aTransformationMode =
365 (HYDROData_Image::TransformationMode)aPanel->getTransformationMode();
367 QPoint aPointA, aPointB, aPointC;
368 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC( INT_MIN, INT_MIN );
369 Handle(HYDROData_Image) aRefImageObj;
371 if ( aTransformationMode != HYDROData_Image::CartesianFromFile ) {
372 HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
373 if( !aPanel->getTransformationDataMap( aMap ) )
376 bool anIsByTwoPoints = aPanel->isByTwoPoints();
378 aPointA = aMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
379 aPointB = aMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
380 aPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) :
381 aMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
383 // first, we check correctness of image points
384 if ( !checkPoints( aPointA, aPointB, aPointC, anIsByTwoPoints,
385 tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
386 tr( "POINTS_A_B_ARE_IDENTICAL" ),
387 theErrorMsg, true ) )
390 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
392 QString aRefImageName = aPanel->getRefImageName();
393 if( aRefImageName.isEmpty() )
395 theErrorMsg = tr( "REFERENCE_IMAGE_IS_NOT_SELECTED" );
399 HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
400 if( !aPanel->getTransformationDataMap( aRefMap, true ) )
403 aRefImageObj = Handle(HYDROData_Image)::DownCast(
404 HYDROGUI_Tool::FindObjectByName( module(), aRefImageName, KIND_IMAGE ) );
405 if( aRefImageObj.IsNull() ) {
408 else if ( !isReferenceCorrect() ) {
409 aRefImageObj->RemoveAllReferences();
412 aTrsfPointA = aRefMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
413 aTrsfPointB = aRefMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
414 if ( !anIsByTwoPoints )
415 aTrsfPointC = aRefMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
417 // the same check of correctness for the reference points
418 if ( !checkPoints( aTrsfPointA, aTrsfPointB, aTrsfPointC, anIsByTwoPoints,
419 tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
420 tr( "REFERENCE_POINTS_A_B_ARE_IDENTICAL" ),
421 theErrorMsg, false ) )
426 if ( aTransformationMode == HYDROData_Image::ManualGeodesic )
428 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].GeodesicPoint;
429 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].GeodesicPoint;
430 if ( !anIsByTwoPoints )
431 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].GeodesicPoint;
435 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].CartesianPoint;
436 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].CartesianPoint;
437 if ( !anIsByTwoPoints )
438 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].CartesianPoint;
443 Handle(HYDROData_Image) anImageObj;
445 anImageObj = myEditedObject;
448 anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
449 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anImageObj );
450 theBrowseObjectsEntries.append( anEntry );
453 if( anImageObj.IsNull() )
456 anImageObj->SetName( anImageName );
457 anImageObj->SetImage( myImage );
459 if ( aTransformationMode == HYDROData_Image::CartesianFromFile ) {
460 QString aGeoreferencementFileName = aPanel->getGeoreferencementFileName();
461 if ( aGeoreferencementFileName.isEmpty() ) {
465 QPoint aLocalPointA( 0, 0 ),
466 aLocalPointB( anImageObj->Image().width(), 0 ),
467 aLocalPointC( INT_MIN, INT_MIN );
468 anImageObj->SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
469 if ( !anImageObj->SetGlobalPointsFromFile( aGeoreferencementFileName ) ) {
470 theErrorMsg = tr( "CANT_LOAD_GEOREFERENCEMENT_FILE" );
474 anImageObj->SetLocalPoints( aPointA, aPointB, aPointC, false );
476 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
478 anImageObj->SetReferencePoints( aRefImageObj,
479 aTrsfPointA, aTrsfPointB, aTrsfPointC );
483 anImageObj->SetGlobalPoints( aTransformationMode,
484 aTrsfPointA, aTrsfPointB, aTrsfPointC );
490 // Set imported file name for image
491 QString aFilePath = aPanel->getFileName();
492 anImageObj->SetFilePath( aFilePath );
495 // must be done after all checks and before calling SetVisible() method below
499 module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
501 anImageObj->Update();
503 theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
507 bool HYDROGUI_ImportImageOp::isReferenceCorrect() const
509 bool isCorrect = true;
511 if( myIsEdit && !myEditedObject.IsNull() )
513 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
514 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
515 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
516 if( !aRefImageObj.IsNull() )
518 bool isFoundEdited = false;
519 HYDROData_SequenceOfObjects aRefSeq = aRefImageObj->GetAllReferenceObjects();
520 for ( int i = 1, n = aRefSeq.Length(); i <= n && !isFoundEdited; ++i )
522 Handle(HYDROData_Entity) anObject = aRefSeq.Value( i );
523 isFoundEdited = anObject->GetName() == myEditedObject->GetName();
525 isCorrect = !isFoundEdited;
531 void HYDROGUI_ImportImageOp::onApply()
533 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
535 bool aCanApply = isReferenceCorrect();
536 if ( !aCanApply && !myEditedObject.IsNull() )
538 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
539 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
540 if ( !aRefImageObj.IsNull() )
541 aCanApply = SUIT_MessageBox::question( module()->getApp()->desktop(),
542 tr( "CORRECT_INPUT_DATA" ), tr( "CONFIRM_REMOVE_REFERENCE_FROM_IMAGE" ).
543 arg( aRefImageObj->GetName() ).arg( myEditedObject->GetName() ),
544 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes;
547 HYDROGUI_Operation::onApply();
549 aPanel->setRefImageName( "" );
550 onRefImageActivated( aPanel->getRefImageName() );
554 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
556 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
560 if( myPreviewPrs ) // if the image is changed by choosing another file
562 myPreviewPrs->setImage( myImage, true );
563 if ( sender() ) // reset the previous presentation settings
565 QString aFileName = aPanel->getFileName();
568 aPanel->setIsEdit( myIsEdit );
569 // restore the file name
570 aPanel->setFileName( aFileName );
571 // fill the reference list
572 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
573 getReferenceDataList( aPrsPointDataList );
574 aPanel->setPrsPointDataList( aPrsPointDataList );
576 if( myPreviewViewManager )
578 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
580 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
582 aViewPort->onBoundingRectChanged();
590 LightApp_Application* anApp = module()->getApp();
592 myActiveViewManager = anApp->activeViewManager();
594 myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
595 myPreviewPrs->setImage( myImage, true );
597 myPreviewViewManager =
598 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
599 if( myPreviewViewManager )
601 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
602 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
604 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
605 myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
606 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
608 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
610 //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
612 aViewPort->addItem( myPreviewPrs );
615 if ( myEditedObject ) {
616 size_t aViewId = (size_t)aViewer;
617 module()->setObjectVisible( aViewId, myEditedObject, true );
620 myPreviewPrs->setIsTransformationPointPreview( true );
622 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
623 this, SLOT( onPointSelected() ) );
628 // Set the image name in the dialog
629 QString anImageName = aPanel->getImageName().simplified();
630 // If edit mode and the name was not set yet then get from the edited object
631 if( myIsEdit && anImageName.isEmpty() )
633 if( !myEditedObject.IsNull() )
634 anImageName = myEditedObject->GetName();
636 // If the name was not set then initialize it from the selected file name
637 if ( anImageName.isEmpty() )
639 anImageName = aPanel->getFileName();
640 if ( !anImageName.isEmpty() ) {
641 anImageName = QFileInfo( anImageName ).baseName();
643 // If no file name then generate a new image name
644 if ( anImageName.isEmpty() ) {
645 anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
648 aPanel->setImageName( anImageName );
650 aPanel->setImageSize( myImage.size() );
652 aPanel->initializePointSelection();
653 onPointSelected( false );
654 onSetCIsUsed( !aPanel->isByTwoPoints() );
657 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
659 myPointType = thePointType;
661 myPreviewPrs->setTransformationPointType( thePointType );
662 if( myRefPreviewPrs )
663 myRefPreviewPrs->setTransformationPointType( thePointType );
666 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
671 if( !theIsRef && myPreviewPrs )
672 myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
673 else if( theIsRef && myRefPreviewPrs )
674 myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
677 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
679 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
681 QString aRefImageName;
682 if( theMode == HYDROData_Image::ReferenceImage )
684 aRefImageName = aPanel->getRefImageName();
685 if( aRefImageName.isEmpty() )
686 return; // do nothing in this case to avoid visual moving of preview prs
687 onRefImageActivated( aRefImageName );
691 if( myRefViewManager )
693 closeView( myRefViewManager );
698 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
700 if( theName.isEmpty() ) {
701 if( myRefViewManager )
702 closeView( myRefViewManager );
706 GraphicsView_ViewPort* aViewPort = 0;
708 LightApp_Application* anApp = module()->getApp();
709 ///// Get a view port for the reference image preview
710 if( myRefViewManager )
712 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
714 aViewPort = aViewer->getActiveViewPort();
719 anApp = module()->getApp();
720 // Init reference image preview
722 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
723 if( myRefViewManager )
725 connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
726 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
728 module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
729 myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
730 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
732 aViewPort = aViewer->getActiveViewPort();
733 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
734 this, SLOT( onRefPointSelected() ) );
742 size_t aViewId = (size_t)myRefViewManager->getViewer();
744 // Remove the old presentation of the reference image if any
745 if( myRefPreviewPrs )
747 module()->setObjectVisible( aViewId, myRefPreviewPrs->getObject(), false );
749 myRefPreviewPrs->setCaption( QString() );
750 aViewPort->removeItem( myRefPreviewPrs );
752 delete myRefPreviewPrs;
756 // Create a new reference image presentation
758 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
759 HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
760 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
761 if( !anImageObj.IsNull() )
763 anImage = anImageObj->Image();
765 myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
766 myRefPreviewPrs->setImage( anImage, true );
768 myRefPreviewPrs->setIsTransformationPointPreview( true );
769 myRefPreviewPrs->setTransformationPointType( myPointType );
771 myRefPreviewPrs->setIsByTwoPoints( aPanel->isByTwoPoints() );
773 // Add the new reference image presentation to the appropriate view
774 aViewPort->addItem( myRefPreviewPrs );
776 module()->setObjectVisible( aViewId, anImageObj, true );
781 // Split views horizontally
782 if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
784 qApp->processEvents();
785 QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
786 aViewPort->activateWindow();
788 aViewPort->setFocus(Qt::ActiveWindowFocusReason);
789 aWorkstack->splitHorizontal();
792 // Initialize the dialog
793 aPanel->setImageSize( anImage.size(), true );
794 aPanel->initializePointSelection();
795 onPointSelected( true );
798 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
803 void HYDROGUI_ImportImageOp::onPointSelected()
805 onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
808 void HYDROGUI_ImportImageOp::onRefPointSelected()
810 onPointSelected( true );
813 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
815 HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
819 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
821 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
822 aPrs->getTransformationPointMap();
824 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
825 while( anIter.hasNext() )
827 int aPointType = anIter.next().key();
828 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
829 const QPoint& aPoint = aTransformationPoint.Point;
831 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
832 aDataMap[ aPointType ] = aData;
835 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
838 void HYDROGUI_ImportImageOp::closePreview()
840 closeView( myPreviewViewManager );
841 closeView( myRefViewManager );
843 if( myActiveViewManager )
844 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
847 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
851 GraphicsView_ViewPort* aViewPort = 0;
852 if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
854 aViewPort = aViewer->getActiveViewPort();
856 disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
857 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
859 // Nullify appropriate presentation pointer
860 HYDROGUI_PrsImage* aPrs = 0;
861 switch ( module()->getViewManagerRole( aViewMgr ) )
863 case HYDROGUI_Module::VMR_ReferenceImage:
864 aPrs = myRefPreviewPrs;
867 case HYDROGUI_Module::VMR_TransformImage:
872 // Remove the appropriate presentation from the view
873 if( aPrs && aViewPort )
875 aViewPort->removeItem( aPrs );
880 module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here