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 if ( isApplyAndClose() )
78 myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
79 if( !myEditedObject.IsNull() )
81 QImage anImage = myEditedObject->Image();
82 bool anIsByTwoPoints = myEditedObject->IsByTwoPoints();
84 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
85 myEditedObject->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
87 // Create the edited image preview presentation in the viewer
88 onCreatePreview( anImage );
90 // Set transformation local points A,B,C to the image preview presentation
91 setPresentationTrsfPoints( myPreviewPrs, anIsByTwoPoints, aLocalPointA,
92 aLocalPointB, aLocalPointC );
94 // Build the existing image local and global points mapping
95 // according to the current transformation mode.
96 HYDROData_Image::TransformationMode aTrsfMode = myEditedObject->GetTrsfMode();
97 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
98 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
99 if ( aTrsfMode == HYDROData_Image::ReferenceImage )
101 // Compute global points using the transformation matrix of the reference image
102 Handle(HYDROData_Image) aRefImage = myEditedObject->GetTrsfReferenceImage();
103 if ( !aRefImage.IsNull() )
105 QTransform aRefTrsf = aRefImage->Trsf(); // The reference image transformation matrix
106 aTrsfPointA = aRefTrsf.map( aLocalPointA ); // Compute the global point A
107 aTrsfPointB = aRefTrsf.map( aLocalPointB ); // Compute the global point B
108 if ( !anIsByTwoPoints )
110 aTrsfPointC = aRefTrsf.map( aLocalPointC ); // Compute the global point C if used
112 // Build the local-global points map
113 // Use the reference image transformation mode for interpreting global points
114 computeTrsfData( aRefImage->GetTrsfMode(), anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
115 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
120 // Get global points from the edited image
121 myEditedObject->GetGlobalPoints( aTrsfMode, aTrsfPointA, aTrsfPointB, aTrsfPointC );
122 // Build the local-global points map
123 computeTrsfData( aTrsfMode, anIsByTwoPoints, aLocalPointA, aLocalPointB, aLocalPointC,
124 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
127 // Initialize the dialog mode, local and global coordinates
128 // except coordinates on the reference image
129 aPanel->setTransformationMode( aTrsfMode );
130 aPanel->setTransformationDataMap( aDataMap );
131 aPanel->setByTwoPoints( anIsByTwoPoints );
133 // Set points of the reference image
134 if ( aTrsfMode == HYDROData_Image::ReferenceImage )
136 Handle(HYDROData_Image) aRefImage;
137 myEditedObject->GetReferencePoints( aRefImage,
138 aTrsfPointA, aTrsfPointB, aTrsfPointC );
139 if ( !aRefImage.IsNull() )
141 aRefImageName = aRefImage->GetName();
143 // Create the reference image presentation in the viewer
144 onRefImageActivated( aRefImageName );
146 // Set transformation points A,B,C to the reference image presentation
147 setPresentationTrsfPoints( myRefPreviewPrs, anIsByTwoPoints, aTrsfPointA.toPoint(),
148 aTrsfPointB.toPoint(), aTrsfPointC.toPoint() );
150 // Prepare A, B, C points on the reference image
151 HYDROGUI_ImportImageDlg::TransformationDataMap aRefDataMap;
152 aRefDataMap[ HYDROGUI_PrsImage::PointA ] =
153 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointA.toPoint() );
154 aRefDataMap[ HYDROGUI_PrsImage::PointB ] =
155 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointB.toPoint() );
156 if ( !anIsByTwoPoints )
157 aRefDataMap[ HYDROGUI_PrsImage::PointC ] =
158 HYDROGUI_ImportImageDlg::TransformationData( aTrsfPointC.toPoint() );
160 // Initialize the dialog's A, B, C points coordinates of the reference image
161 aPanel->setTransformationDataMap( aRefDataMap, true, true );
167 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
168 getReferenceDataList( aPrsPointDataList );
169 aPanel->setPrsPointDataList( aPrsPointDataList );
170 // Select the current reference image in the dialog combobox
171 aPanel->setRefImageName( aRefImageName );
174 void HYDROGUI_ImportImageOp::setPresentationTrsfPoints( HYDROGUI_PrsImage* thePrs,
175 bool theIsByTwoPoints,
176 const QPoint theLocalPointA,
177 const QPoint theLocalPointB,
178 const QPoint theLocalPointC )
180 // Set transformation points A,B,C to the image presentation
183 HYDROGUI_PrsImage::TransformationPointMap aPointMap =
184 thePrs->getTransformationPointMap();
185 if( !aPointMap.isEmpty() )
187 aPointMap[ HYDROGUI_PrsImage::PointA ].Point = theLocalPointA;
188 aPointMap[ HYDROGUI_PrsImage::PointB ].Point = theLocalPointB;
189 if ( !theIsByTwoPoints )
190 aPointMap[ HYDROGUI_PrsImage::PointC ].Point = theLocalPointC;
192 thePrs->setIsByTwoPoints( theIsByTwoPoints );
193 thePrs->setTransformationPointMap( aPointMap );
198 void HYDROGUI_ImportImageOp::getReferenceDataList(
199 HYDROGUI_ImportImageDlg::PrsPointDataList& theList ) const
201 // Collect information about existing images and initialize the combobox
202 // reference image selector in the dialog.
203 HYDROData_Iterator anIterator( doc(), KIND_IMAGE );
204 for( ; anIterator.More(); anIterator.Next() )
206 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
207 if( !anImageObj.IsNull() )
209 if( myIsEdit && IsEqual( anImageObj, myEditedObject ) )
212 QPoint aLocalPointA, aLocalPointB, aLocalPointC;
213 anImageObj->GetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC );
215 HYDROData_Image::TransformationMode aImgTrsfMode;
216 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC;
217 anImageObj->GetGlobalPoints( aImgTrsfMode,
218 aTrsfPointA, aTrsfPointB, aTrsfPointC );
220 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
221 computeTrsfData( aImgTrsfMode, false, aLocalPointA, aLocalPointB, aLocalPointC,
222 aTrsfPointA, aTrsfPointB, aTrsfPointC, aDataMap );
224 HYDROGUI_ImportImageDlg::PrsPointData aPrsPointData( anImageObj->GetName(), aDataMap );
225 theList.append( aPrsPointData );
230 void HYDROGUI_ImportImageOp::computeTrsfData( HYDROData_Image::TransformationMode theTrsfMode,
231 bool theIsByTwoPoints,
232 const QPoint& theLocalPointA,
233 const QPoint& theLocalPointB,
234 const QPoint& theLocalPointC,
235 const QPointF& theGlobalPointA,
236 const QPointF& theGlobalPointB,
237 const QPointF& theGlobalPointC,
238 HYDROGUI_ImportImageDlg::TransformationDataMap& theDataMap ) const
240 // Build the local-global points map
241 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
242 theDataMap[ HYDROGUI_PrsImage::PointA ] =
243 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointA, theGlobalPointA );
244 theDataMap[ HYDROGUI_PrsImage::PointB ] =
245 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointB, theGlobalPointB );
246 if ( !theIsByTwoPoints )
248 theDataMap[ HYDROGUI_PrsImage::PointC ] =
249 HYDROGUI_ImportImageDlg::ComputeTrsfData( theTrsfMode, theLocalPointC, theGlobalPointC );
253 void HYDROGUI_ImportImageOp::abortOperation()
257 HYDROGUI_Operation::abortOperation();
260 void HYDROGUI_ImportImageOp::commitOperation()
262 if ( isApplyAndClose() )
265 HYDROGUI_Operation::commitOperation();
268 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
270 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
271 connect( aPanel, SIGNAL( createPreview( QImage ) ), SLOT( onCreatePreview( QImage ) ) );
272 connect( aPanel, SIGNAL( activatePointSelection( int ) ), SLOT( onActivatePointSelection( int ) ) );
273 connect( aPanel, SIGNAL( pointCoordChanged( bool, int, bool, int ) ),
274 SLOT( onPointCoordChanged( bool, int, bool, int ) ) );
275 connect( aPanel, SIGNAL( modeActivated( int ) ), SLOT( onModeActivated( int ) ) );
276 connect( aPanel, SIGNAL( refImageActivated( const QString& ) ),
277 SLOT( onRefImageActivated( const QString& ) ) );
278 connect( aPanel, SIGNAL( setCIsUsed( bool ) ), SLOT( onSetCIsUsed( bool ) ) );
279 connect( aPanel, SIGNAL( filesSelected( const QStringList& ) ),
280 SLOT( onFilesSelected( const QStringList& ) ) );
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
500 if ( isApplyAndClose() )
504 module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
506 anImageObj->Update();
508 theUpdateFlags = UF_Model;
509 if ( isApplyAndClose() )
510 theUpdateFlags |= UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
516 commitDocOperation(); // to save the modifications in the data model
517 return false; // and to continue the operation
523 bool HYDROGUI_ImportImageOp::isReferenceCorrect() const
525 bool isCorrect = true;
527 if( myIsEdit && !myEditedObject.IsNull() )
529 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
530 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
531 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
532 if( !aRefImageObj.IsNull() )
534 bool isFoundEdited = false;
535 HYDROData_SequenceOfObjects aRefSeq = aRefImageObj->GetAllReferenceObjects();
536 for ( int i = 1, n = aRefSeq.Length(); i <= n && !isFoundEdited; ++i )
538 Handle(HYDROData_Entity) anObject = aRefSeq.Value( i );
539 isFoundEdited = anObject->GetName() == myEditedObject->GetName();
541 isCorrect = !isFoundEdited;
547 void HYDROGUI_ImportImageOp::apply()
549 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
551 bool aCanApply = isReferenceCorrect();
552 if ( !aCanApply && !myEditedObject.IsNull() )
554 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
555 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
556 if ( !aRefImageObj.IsNull() )
557 aCanApply = SUIT_MessageBox::question( module()->getApp()->desktop(),
558 tr( "CORRECT_INPUT_DATA" ), tr( "CONFIRM_REMOVE_REFERENCE_FROM_IMAGE" ).
559 arg( aRefImageObj->GetName() ).arg( myEditedObject->GetName() ),
560 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes;
563 HYDROGUI_Operation::apply();
565 aPanel->setRefImageName( "" );
566 onRefImageActivated( aPanel->getRefImageName() );
570 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
572 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
576 if( myPreviewPrs ) // if the image is changed by choosing another file
578 myPreviewPrs->setImage( myImage, true );
579 if ( sender() ) // reset the previous presentation settings
581 QString aFileName = aPanel->getFileName();
584 aPanel->setIsEdit( myIsEdit );
585 // restore the file name
586 aPanel->setFileName( aFileName );
587 // fill the reference list
588 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
589 getReferenceDataList( aPrsPointDataList );
590 aPanel->setPrsPointDataList( aPrsPointDataList );
592 if( myPreviewViewManager )
594 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
596 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
598 aViewPort->onBoundingRectChanged();
606 LightApp_Application* anApp = module()->getApp();
608 myActiveViewManager = anApp->activeViewManager();
610 myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
611 myPreviewPrs->setImage( myImage, true );
613 myPreviewViewManager =
614 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
615 if( myPreviewViewManager )
617 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
618 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
620 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
621 myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
622 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
624 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
626 //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
628 aViewPort->addItem( myPreviewPrs );
631 if ( myEditedObject ) {
632 size_t aViewId = (size_t)aViewer;
633 module()->setObjectVisible( aViewId, myEditedObject, true );
636 myPreviewPrs->setIsTransformationPointPreview( true );
637 myPreviewPrs->setTransformationPointCursorShape( module()->getPrefEditCursor().shape() );
639 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
640 this, SLOT( onPointSelected() ) );
645 // Set the image name in the dialog
646 QString anImageName = aPanel->getImageName().simplified();
647 // If edit mode and the name was not set yet then get from the edited object
648 if( myIsEdit && anImageName.isEmpty() )
650 if( !myEditedObject.IsNull() )
651 anImageName = myEditedObject->GetName();
653 // If the name was not set then initialize it from the selected file name
654 if ( anImageName.isEmpty() )
656 anImageName = aPanel->getFileName();
657 if ( !anImageName.isEmpty() ) {
658 anImageName = QFileInfo( anImageName ).baseName();
660 // If no file name then generate a new image name
661 if ( anImageName.isEmpty() ) {
662 anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
665 aPanel->setImageName( anImageName );
667 aPanel->setImageSize( myImage.size() );
669 aPanel->initializePointSelection();
670 onPointSelected( false );
671 onSetCIsUsed( !aPanel->isByTwoPoints() );
674 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
676 myPointType = thePointType;
678 myPreviewPrs->setTransformationPointType( thePointType );
679 if( myRefPreviewPrs )
680 myRefPreviewPrs->setTransformationPointType( thePointType );
683 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
688 if( !theIsRef && myPreviewPrs )
689 myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
690 else if( theIsRef && myRefPreviewPrs )
691 myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
694 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
696 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
698 QString aRefImageName;
699 if( theMode == HYDROData_Image::ReferenceImage )
701 aRefImageName = aPanel->getRefImageName();
702 if( aRefImageName.isEmpty() )
703 return; // do nothing in this case to avoid visual moving of preview prs
704 onRefImageActivated( aRefImageName );
708 if( myRefViewManager )
710 closeView( myRefViewManager );
715 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
717 if( theName.isEmpty() ) {
718 if( myRefViewManager )
719 closeView( myRefViewManager );
723 GraphicsView_ViewPort* aViewPort = 0;
725 LightApp_Application* anApp = module()->getApp();
726 ///// Get a view port for the reference image preview
727 if( myRefViewManager )
729 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
731 aViewPort = aViewer->getActiveViewPort();
736 anApp = module()->getApp();
737 // Init reference image preview
739 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
740 if( myRefViewManager )
742 connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
743 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
745 module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
746 myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
747 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
749 aViewPort = aViewer->getActiveViewPort();
750 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
751 this, SLOT( onRefPointSelected() ) );
759 size_t aViewId = (size_t)myRefViewManager->getViewer();
761 // Remove the old presentation of the reference image if any
762 if( myRefPreviewPrs )
764 module()->setObjectVisible( aViewId, myRefPreviewPrs->getObject(), false );
766 myRefPreviewPrs->setCaption( QString() );
767 aViewPort->removeItem( myRefPreviewPrs );
769 delete myRefPreviewPrs;
773 // Create a new reference image presentation
775 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
776 HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
777 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
778 if( !anImageObj.IsNull() )
780 anImage = anImageObj->Image();
782 myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
783 myRefPreviewPrs->setImage( anImage, true );
785 myRefPreviewPrs->setIsTransformationPointPreview( true );
786 myRefPreviewPrs->setTransformationPointType( myPointType );
788 myRefPreviewPrs->setIsByTwoPoints( aPanel->isByTwoPoints() );
790 myRefPreviewPrs->setTransformationPointCursorShape( module()->getPrefEditCursor().shape() );
792 // Add the new reference image presentation to the appropriate view
793 aViewPort->addItem( myRefPreviewPrs );
795 module()->setObjectVisible( aViewId, anImageObj, true );
800 // Split views horizontally
801 if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
803 qApp->processEvents();
804 QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
805 aViewPort->activateWindow();
807 aViewPort->setFocus(Qt::ActiveWindowFocusReason);
808 aWorkstack->splitHorizontal();
811 // Initialize the dialog
812 aPanel->setImageSize( anImage.size(), true );
813 aPanel->initializePointSelection();
814 onPointSelected( true );
817 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
822 void HYDROGUI_ImportImageOp::onPointSelected()
824 onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
827 void HYDROGUI_ImportImageOp::onRefPointSelected()
829 onPointSelected( true );
832 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
834 HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
838 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
840 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
841 aPrs->getTransformationPointMap();
843 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
844 while( anIter.hasNext() )
846 int aPointType = anIter.next().key();
847 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
848 const QPoint& aPoint = aTransformationPoint.Point;
850 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
851 aDataMap[ aPointType ] = aData;
854 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
857 void HYDROGUI_ImportImageOp::closePreview()
859 closeView( myPreviewViewManager );
860 closeView( myRefViewManager );
862 if( myActiveViewManager )
863 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
866 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
870 GraphicsView_ViewPort* aViewPort = 0;
871 if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
873 aViewPort = aViewer->getActiveViewPort();
875 disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
876 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
878 // Nullify appropriate presentation pointer
879 HYDROGUI_PrsImage* aPrs = 0;
880 switch ( module()->getViewManagerRole( aViewMgr ) )
882 case HYDROGUI_Module::VMR_ReferenceImage:
883 aPrs = myRefPreviewPrs;
886 case HYDROGUI_Module::VMR_TransformImage:
891 // Remove the appropriate presentation from the view
892 if( aPrs && aViewPort )
894 aViewPort->removeItem( aPrs );
899 module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here
904 void HYDROGUI_ImportImageOp::onFilesSelected( const QStringList& theFileNames )
906 myFiles = theFileNames;
911 bool HYDROGUI_ImportImageOp::SetNextFile()
914 bool isEnabledEdit = myFiles.count()==1 || myFileIndex==myFiles.count();
915 bool isValid = ( myFileIndex>=0 && myFileIndex<myFiles.count() );
916 QString aFile = isValid ? myFiles[myFileIndex] : "";
918 HYDROGUI_ImportImageDlg* aPanel = dynamic_cast<HYDROGUI_ImportImageDlg*>( inputPanel() );
919 aPanel->ActivateFile( aFile, isEnabledEdit );