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_Tool2.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, NULL ); //TODO
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, HYDROData_Image::ECW_FileInfo* ) ),
272 SLOT( onCreatePreview( QImage, HYDROData_Image::ECW_FileInfo* ) ) );
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 ) ) );
280 connect( aPanel, SIGNAL( filesSelected( const QStringList& ) ),
281 SLOT( onFilesSelected( const QStringList& ) ) );
285 void HYDROGUI_ImportImageOp::onSetCIsUsed( bool theCIsUsed )
289 myPreviewPrs->setIsByTwoPoints( !theCIsUsed );
291 if ( myRefPreviewPrs )
293 myRefPreviewPrs->setIsByTwoPoints( !theCIsUsed );
297 bool HYDROGUI_ImportImageOp::checkPoints( const QPointF& thePointA,
298 const QPointF& thePointB,
299 const QPointF& thePointC,
300 const bool theIsByTwoPoints,
301 const QString& theLineErrMsg,
302 const QString& thePoinErrMsg,
303 QString& theErrorMsg,
304 const bool theIsToCheckInvertibles ) const
306 double xa = thePointA.x();
307 double ya = thePointA.y();
308 double xb = thePointB.x();
309 double yb = thePointB.y();
310 double xc = thePointC.x();
311 double yc = thePointC.y();
313 if ( !theIsByTwoPoints )
315 // check that three input points don't belong to a single line
316 if ( ValuesEquals( ( yb - ya ) * ( xc - xa ), ( yc - ya ) * ( xb - xa ) ) )
318 theErrorMsg = theLineErrMsg;
322 if ( theIsToCheckInvertibles )
324 QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
326 bool anIsInvertible = false;
327 QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
328 if( !anIsInvertible )
330 theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
337 // check that two points are not identical
338 if ( ValuesEquals( xa, xb ) && ValuesEquals( ya, yb ) )
340 theErrorMsg = thePoinErrMsg;
348 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
349 QString& theErrorMsg,
350 QStringList& theBrowseObjectsEntries )
352 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
356 QString aFilePath = aPanel->getFileName();
357 if( aFilePath.isEmpty() )
359 theErrorMsg = tr( "SELECT_IMAGE_FILE" ).arg( aFilePath );
364 QString anImageName = aPanel->getImageName();
365 if( anImageName.isEmpty() )
367 theErrorMsg = tr( "SELECT_IMAGE_NAME" ).arg( anImageName );
371 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
373 // check that there are no other objects with the same name in the document
374 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
375 if( !anObject.IsNull() )
377 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
382 HYDROData_Image::TransformationMode aTransformationMode =
383 (HYDROData_Image::TransformationMode)aPanel->getTransformationMode();
385 QPoint aPointA, aPointB, aPointC;
386 QPointF aTrsfPointA, aTrsfPointB, aTrsfPointC( INT_MIN, INT_MIN );
387 Handle(HYDROData_Image) aRefImageObj;
389 if ( aTransformationMode != HYDROData_Image::CartesianFromFile ) {
390 HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
391 if( !aPanel->getTransformationDataMap( aMap ) )
394 bool anIsByTwoPoints = aPanel->isByTwoPoints();
396 aPointA = aMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
397 aPointB = aMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
398 aPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) :
399 aMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
401 // first, we check correctness of image points
402 if ( !checkPoints( aPointA, aPointB, aPointC, anIsByTwoPoints,
403 tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
404 tr( "POINTS_A_B_ARE_IDENTICAL" ),
405 theErrorMsg, true ) )
408 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
410 QString aRefImageName = aPanel->getRefImageName();
411 if( aRefImageName.isEmpty() )
413 theErrorMsg = tr( "REFERENCE_IMAGE_IS_NOT_SELECTED" );
417 HYDROGUI_ImportImageDlg::TransformationDataMap aRefMap;
418 if( !aPanel->getTransformationDataMap( aRefMap, true ) )
421 aRefImageObj = Handle(HYDROData_Image)::DownCast(
422 HYDROGUI_Tool::FindObjectByName( module(), aRefImageName, KIND_IMAGE ) );
423 if( aRefImageObj.IsNull() ) {
426 else if ( !isReferenceCorrect() ) {
427 aRefImageObj->RemoveAllReferences();
430 aTrsfPointA = aRefMap[ HYDROGUI_PrsImage::PointA ].ImagePoint;
431 aTrsfPointB = aRefMap[ HYDROGUI_PrsImage::PointB ].ImagePoint;
432 if ( !anIsByTwoPoints )
433 aTrsfPointC = aRefMap[ HYDROGUI_PrsImage::PointC ].ImagePoint;
435 // the same check of correctness for the reference points
436 if ( !checkPoints( aTrsfPointA, aTrsfPointB, aTrsfPointC, anIsByTwoPoints,
437 tr( "REFERENCE_POINTS_A_B_C_BELONG_TO_SINGLE_LINE" ),
438 tr( "REFERENCE_POINTS_A_B_ARE_IDENTICAL" ),
439 theErrorMsg, false ) )
444 if ( aTransformationMode == HYDROData_Image::ManualGeodesic )
446 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].GeodesicPoint;
447 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].GeodesicPoint;
448 if ( !anIsByTwoPoints )
449 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].GeodesicPoint;
453 aTrsfPointA = aMap[ HYDROGUI_PrsImage::PointA ].CartesianPoint;
454 aTrsfPointB = aMap[ HYDROGUI_PrsImage::PointB ].CartesianPoint;
455 if ( !anIsByTwoPoints )
456 aTrsfPointC = aMap[ HYDROGUI_PrsImage::PointC ].CartesianPoint;
461 Handle(HYDROData_Image) anImageObj;
463 anImageObj = myEditedObject;
466 anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
467 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anImageObj );
468 theBrowseObjectsEntries.append( anEntry );
471 if( anImageObj.IsNull() )
474 anImageObj->SetName( anImageName );
475 anImageObj->SetImage( myImage );
477 if ( aTransformationMode == HYDROData_Image::CartesianFromFile ) {
478 QString aGeoreferencementFileName = aPanel->getGeoreferencementFileName();
479 if ( aGeoreferencementFileName.isEmpty() ) {
483 QPoint aLocalPointA( 0, 0 ),
484 aLocalPointB( anImageObj->Image().width(), 0 ),
485 aLocalPointC( INT_MIN, INT_MIN );
486 anImageObj->SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
487 if ( !anImageObj->SetGlobalPointsFromFile( aGeoreferencementFileName ) ) {
488 theErrorMsg = tr( "CANT_LOAD_GEOREFERENCEMENT_FILE" );
492 anImageObj->SetLocalPoints( aPointA, aPointB, aPointC, false );
494 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
496 anImageObj->SetReferencePoints( aRefImageObj,
497 aTrsfPointA, aTrsfPointB, aTrsfPointC );
501 anImageObj->SetGlobalPoints( aTransformationMode,
502 aTrsfPointA, aTrsfPointB, aTrsfPointC );
508 // Set imported file name for image
509 QString aFilePath = aPanel->getFileName();
510 anImageObj->SetFilePath( aFilePath );
513 // must be done after all checks and before calling SetVisible() method below
514 if ( isApplyAndClose() )
518 module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
520 anImageObj->Update();
522 theUpdateFlags = UF_Model;
523 if ( isApplyAndClose() )
524 theUpdateFlags |= UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
526 if( isApplyAndClose() )
528 commitDocOperation(); // to save the modifications in the data model
535 module()->updateObjBrowser();
536 return false; // and to continue the operation
539 /*if( myFiles.count() > 1 )
541 setIsApplyAndClose( true );
546 bool HYDROGUI_ImportImageOp::isReferenceCorrect() const
548 bool isCorrect = true;
550 if( myIsEdit && !myEditedObject.IsNull() )
552 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
553 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
554 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
555 if( !aRefImageObj.IsNull() )
557 bool isFoundEdited = false;
558 HYDROData_SequenceOfObjects aRefSeq = aRefImageObj->GetAllReferenceObjects();
559 for ( int i = 1, n = aRefSeq.Length(); i <= n && !isFoundEdited; ++i )
561 Handle(HYDROData_Entity) anObject = aRefSeq.Value( i );
562 isFoundEdited = anObject->GetName() == myEditedObject->GetName();
564 isCorrect = !isFoundEdited;
570 void HYDROGUI_ImportImageOp::apply()
572 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
574 bool aCanApply = isReferenceCorrect();
575 if ( !aCanApply && !myEditedObject.IsNull() )
577 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
578 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
579 if ( !aRefImageObj.IsNull() )
580 aCanApply = SUIT_MessageBox::question( module()->getApp()->desktop(),
581 tr( "CORRECT_INPUT_DATA" ), tr( "CONFIRM_REMOVE_REFERENCE_FROM_IMAGE" ).
582 arg( aRefImageObj->GetName() ).arg( myEditedObject->GetName() ),
583 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes;
586 HYDROGUI_Operation::apply();
588 aPanel->setRefImageName( "" );
589 onRefImageActivated( aPanel->getRefImageName() );
593 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage, HYDROData_Image::ECW_FileInfo* theFileInfo )
595 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
599 if( myPreviewPrs ) // if the image is changed by choosing another file
601 myPreviewPrs->setImage( myImage, true );
602 if ( sender() ) // reset the previous presentation settings
604 QString aFileName = aPanel->getFileName();
607 aPanel->setIsEdit( myIsEdit );
608 // restore the file name
609 aPanel->setFileName( aFileName );
610 // fill the reference list
611 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
612 getReferenceDataList( aPrsPointDataList );
613 aPanel->setPrsPointDataList( aPrsPointDataList );
615 if( myPreviewViewManager )
617 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
619 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
621 aViewPort->onBoundingRectChanged();
629 LightApp_Application* anApp = module()->getApp();
631 myActiveViewManager = anApp->activeViewManager();
633 myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
634 myPreviewPrs->setImage( myImage, true );
636 myPreviewViewManager =
637 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
638 if( myPreviewViewManager )
640 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
641 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
643 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
644 myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
645 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
647 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
649 //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
651 aViewPort->addItem( myPreviewPrs );
654 if ( myEditedObject ) {
655 size_t aViewId = (size_t)aViewer;
656 module()->setObjectVisible( aViewId, myEditedObject, true );
659 myPreviewPrs->setIsTransformationPointPreview( true );
660 myPreviewPrs->setTransformationPointCursorShape( module()->getPrefEditCursor().shape() );
662 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
663 this, SLOT( onPointSelected() ) );
668 // Set the image name in the dialog
669 QString anImageName = aPanel->getImageName().simplified();
670 // If edit mode and the name was not set yet then get from the edited object
671 if( myIsEdit && anImageName.isEmpty() )
673 if( !myEditedObject.IsNull() )
674 anImageName = myEditedObject->GetName();
676 // If the name was not set then initialize it from the selected file name
677 if ( anImageName.isEmpty() )
679 anImageName = aPanel->getFileName();
680 if ( !anImageName.isEmpty() ) {
681 anImageName = QFileInfo( anImageName ).baseName();
683 // If no file name then generate a new image name
684 if ( anImageName.isEmpty() ) {
685 anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
688 aPanel->setImageName( anImageName );
690 aPanel->setImageSize( myImage.size() );
693 aPanel->initializePointSelection();
695 aPanel->ECW_initializePointSelection(theFileInfo);
696 onPointSelected( false );
697 onSetCIsUsed( !aPanel->isByTwoPoints() );
700 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
702 myPointType = thePointType;
704 myPreviewPrs->setTransformationPointType( thePointType );
705 if( myRefPreviewPrs )
706 myRefPreviewPrs->setTransformationPointType( thePointType );
709 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
714 if( !theIsRef && myPreviewPrs )
715 myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
716 else if( theIsRef && myRefPreviewPrs )
717 myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
720 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
722 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
724 QString aRefImageName;
725 if( theMode == HYDROData_Image::ReferenceImage )
727 aRefImageName = aPanel->getRefImageName();
728 if( aRefImageName.isEmpty() )
729 return; // do nothing in this case to avoid visual moving of preview prs
730 onRefImageActivated( aRefImageName );
734 if( myRefViewManager )
736 closeView( myRefViewManager );
741 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
743 if( theName.isEmpty() ) {
744 if( myRefViewManager )
745 closeView( myRefViewManager );
749 GraphicsView_ViewPort* aViewPort = 0;
751 LightApp_Application* anApp = module()->getApp();
752 ///// Get a view port for the reference image preview
753 if( myRefViewManager )
755 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
757 aViewPort = aViewer->getActiveViewPort();
762 anApp = module()->getApp();
763 // Init reference image preview
765 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
766 if( myRefViewManager )
768 connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
769 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
771 module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
772 myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
773 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
775 aViewPort = aViewer->getActiveViewPort();
776 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
777 this, SLOT( onRefPointSelected() ) );
785 size_t aViewId = (size_t)myRefViewManager->getViewer();
787 // Remove the old presentation of the reference image if any
788 if( myRefPreviewPrs )
790 module()->setObjectVisible( aViewId, myRefPreviewPrs->getObject(), false );
792 myRefPreviewPrs->setCaption( QString() );
793 aViewPort->removeItem( myRefPreviewPrs );
795 delete myRefPreviewPrs;
799 // Create a new reference image presentation
801 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
802 HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
803 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
804 if( !anImageObj.IsNull() )
806 anImage = anImageObj->Image();
808 myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
809 myRefPreviewPrs->setImage( anImage, true );
811 myRefPreviewPrs->setIsTransformationPointPreview( true );
812 myRefPreviewPrs->setTransformationPointType( myPointType );
814 myRefPreviewPrs->setIsByTwoPoints( aPanel->isByTwoPoints() );
816 myRefPreviewPrs->setTransformationPointCursorShape( module()->getPrefEditCursor().shape() );
818 // Add the new reference image presentation to the appropriate view
819 aViewPort->addItem( myRefPreviewPrs );
821 module()->setObjectVisible( aViewId, anImageObj, true );
826 // Split views horizontally
827 if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
829 qApp->processEvents();
830 QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
831 aViewPort->activateWindow();
833 aViewPort->setFocus(Qt::ActiveWindowFocusReason);
834 aWorkstack->splitHorizontal();
837 // Initialize the dialog
838 aPanel->setImageSize( anImage.size(), true );
839 aPanel->initializePointSelection();
840 onPointSelected( true );
843 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
848 void HYDROGUI_ImportImageOp::onPointSelected()
850 onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
853 void HYDROGUI_ImportImageOp::onRefPointSelected()
855 onPointSelected( true );
858 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
860 HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
864 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
866 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
867 aPrs->getTransformationPointMap();
869 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
870 while( anIter.hasNext() )
872 int aPointType = anIter.next().key();
873 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
874 const QPoint& aPoint = aTransformationPoint.Point;
876 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
877 aDataMap[ aPointType ] = aData;
880 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
883 void HYDROGUI_ImportImageOp::closePreview()
885 closeView( myPreviewViewManager );
886 closeView( myRefViewManager );
888 if( myActiveViewManager )
889 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
892 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
896 GraphicsView_ViewPort* aViewPort = 0;
897 if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
899 aViewPort = aViewer->getActiveViewPort();
901 disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
902 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
904 // Nullify appropriate presentation pointer
905 HYDROGUI_PrsImage* aPrs = 0;
906 switch ( module()->getViewManagerRole( aViewMgr ) )
908 case HYDROGUI_Module::VMR_ReferenceImage:
909 aPrs = myRefPreviewPrs;
912 case HYDROGUI_Module::VMR_TransformImage:
917 // Remove the appropriate presentation from the view
918 if( aPrs && aViewPort )
920 aViewPort->removeItem( aPrs );
925 module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here
930 void HYDROGUI_ImportImageOp::onFilesSelected( const QStringList& theFileNames )
932 myFiles = theFileNames;
937 bool HYDROGUI_ImportImageOp::SetNextFile()
940 bool isEnabledEdit = myFiles.count()==1 || myFileIndex==myFiles.count();
941 bool isValid = ( myFileIndex>=0 && myFileIndex<myFiles.count() );
942 QString aFile = isValid ? myFiles[myFileIndex] : "";
944 HYDROGUI_ImportImageDlg* aPanel = dynamic_cast<HYDROGUI_ImportImageDlg*>( inputPanel() );
945 aPanel->ActivateFile( aFile, isEnabledEdit );