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 ) ) );
277 connect( aPanel, SIGNAL( filesSelected( const QStringList& ) ),
278 SLOT( onFilesSelected( const QStringList& ) ) );
282 void HYDROGUI_ImportImageOp::onSetCIsUsed( bool theCIsUsed )
286 myPreviewPrs->setIsByTwoPoints( !theCIsUsed );
288 if ( myRefPreviewPrs )
290 myRefPreviewPrs->setIsByTwoPoints( !theCIsUsed );
294 bool HYDROGUI_ImportImageOp::checkPoints( const QPointF& thePointA,
295 const QPointF& thePointB,
296 const QPointF& thePointC,
297 const bool theIsByTwoPoints,
298 const QString& theLineErrMsg,
299 const QString& thePoinErrMsg,
300 QString& theErrorMsg,
301 const bool theIsToCheckInvertibles ) const
303 double xa = thePointA.x();
304 double ya = thePointA.y();
305 double xb = thePointB.x();
306 double yb = thePointB.y();
307 double xc = thePointC.x();
308 double yc = thePointC.y();
310 if ( !theIsByTwoPoints )
312 // check that three input points don't belong to a single line
313 if ( ValuesEquals( ( yb - ya ) * ( xc - xa ), ( yc - ya ) * ( xb - xa ) ) )
315 theErrorMsg = theLineErrMsg;
319 if ( theIsToCheckInvertibles )
321 QTransform aTransform1( xa, ya, 1, xb, yb, 1, xc, yc, 1 );
323 bool anIsInvertible = false;
324 QTransform aTransform1Inverted = aTransform1.inverted( &anIsInvertible );
325 if( !anIsInvertible )
327 theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
334 // check that two points are not identical
335 if ( ValuesEquals( xa, xb ) && ValuesEquals( ya, yb ) )
337 theErrorMsg = thePoinErrMsg;
345 bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
346 QString& theErrorMsg,
347 QStringList& theBrowseObjectsEntries )
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;
450 anImageObj = Handle(HYDROData_Image)::DownCast( doc()->CreateObject( KIND_IMAGE ) );
451 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anImageObj );
452 theBrowseObjectsEntries.append( anEntry );
455 if( anImageObj.IsNull() )
458 anImageObj->SetName( anImageName );
459 anImageObj->SetImage( myImage );
461 if ( aTransformationMode == HYDROData_Image::CartesianFromFile ) {
462 QString aGeoreferencementFileName = aPanel->getGeoreferencementFileName();
463 if ( aGeoreferencementFileName.isEmpty() ) {
467 QPoint aLocalPointA( 0, 0 ),
468 aLocalPointB( anImageObj->Image().width(), 0 ),
469 aLocalPointC( INT_MIN, INT_MIN );
470 anImageObj->SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
471 if ( !anImageObj->SetGlobalPointsFromFile( aGeoreferencementFileName ) ) {
472 theErrorMsg = tr( "CANT_LOAD_GEOREFERENCEMENT_FILE" );
476 anImageObj->SetLocalPoints( aPointA, aPointB, aPointC, false );
478 if ( aTransformationMode == HYDROData_Image::ReferenceImage )
480 anImageObj->SetReferencePoints( aRefImageObj,
481 aTrsfPointA, aTrsfPointB, aTrsfPointC );
485 anImageObj->SetGlobalPoints( aTransformationMode,
486 aTrsfPointA, aTrsfPointB, aTrsfPointC );
492 // Set imported file name for image
493 QString aFilePath = aPanel->getFileName();
494 anImageObj->SetFilePath( aFilePath );
497 // must be done after all checks and before calling SetVisible() method below
501 module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true );
503 anImageObj->Update();
505 theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
510 commitDocOperation(); // to save the modifications in the data model
511 return false; // and to continue the operation
517 bool HYDROGUI_ImportImageOp::isReferenceCorrect() const
519 bool isCorrect = true;
521 if( myIsEdit && !myEditedObject.IsNull() )
523 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
524 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
525 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
526 if( !aRefImageObj.IsNull() )
528 bool isFoundEdited = false;
529 HYDROData_SequenceOfObjects aRefSeq = aRefImageObj->GetAllReferenceObjects();
530 for ( int i = 1, n = aRefSeq.Length(); i <= n && !isFoundEdited; ++i )
532 Handle(HYDROData_Entity) anObject = aRefSeq.Value( i );
533 isFoundEdited = anObject->GetName() == myEditedObject->GetName();
535 isCorrect = !isFoundEdited;
541 void HYDROGUI_ImportImageOp::onApply()
543 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
545 bool aCanApply = isReferenceCorrect();
546 if ( !aCanApply && !myEditedObject.IsNull() )
548 Handle(HYDROData_Image) aRefImageObj = Handle(HYDROData_Image)::DownCast(
549 HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRefImageName(), KIND_IMAGE ) );
550 if ( !aRefImageObj.IsNull() )
551 aCanApply = SUIT_MessageBox::question( module()->getApp()->desktop(),
552 tr( "CORRECT_INPUT_DATA" ), tr( "CONFIRM_REMOVE_REFERENCE_FROM_IMAGE" ).
553 arg( aRefImageObj->GetName() ).arg( myEditedObject->GetName() ),
554 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes;
557 HYDROGUI_Operation::onApply();
559 aPanel->setRefImageName( "" );
560 onRefImageActivated( aPanel->getRefImageName() );
564 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
566 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
570 if( myPreviewPrs ) // if the image is changed by choosing another file
572 myPreviewPrs->setImage( myImage, true );
573 if ( sender() ) // reset the previous presentation settings
575 QString aFileName = aPanel->getFileName();
578 aPanel->setIsEdit( myIsEdit );
579 // restore the file name
580 aPanel->setFileName( aFileName );
581 // fill the reference list
582 HYDROGUI_ImportImageDlg::PrsPointDataList aPrsPointDataList;
583 getReferenceDataList( aPrsPointDataList );
584 aPanel->setPrsPointDataList( aPrsPointDataList );
586 if( myPreviewViewManager )
588 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
590 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
592 aViewPort->onBoundingRectChanged();
600 LightApp_Application* anApp = module()->getApp();
602 myActiveViewManager = anApp->activeViewManager();
604 myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
605 myPreviewPrs->setImage( myImage, true );
607 myPreviewViewManager =
608 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
609 if( myPreviewViewManager )
611 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
612 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
614 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
615 myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
616 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
618 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
620 //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
622 aViewPort->addItem( myPreviewPrs );
625 if ( myEditedObject ) {
626 size_t aViewId = (size_t)aViewer;
627 module()->setObjectVisible( aViewId, myEditedObject, true );
630 myPreviewPrs->setIsTransformationPointPreview( true );
632 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
633 this, SLOT( onPointSelected() ) );
638 // Set the image name in the dialog
639 QString anImageName = aPanel->getImageName().simplified();
640 // If edit mode and the name was not set yet then get from the edited object
641 if( myIsEdit && anImageName.isEmpty() )
643 if( !myEditedObject.IsNull() )
644 anImageName = myEditedObject->GetName();
646 // If the name was not set then initialize it from the selected file name
647 if ( anImageName.isEmpty() )
649 anImageName = aPanel->getFileName();
650 if ( !anImageName.isEmpty() ) {
651 anImageName = QFileInfo( anImageName ).baseName();
653 // If no file name then generate a new image name
654 if ( anImageName.isEmpty() ) {
655 anImageName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMAGE_NAME" ) );
658 aPanel->setImageName( anImageName );
660 aPanel->setImageSize( myImage.size() );
662 aPanel->initializePointSelection();
663 onPointSelected( false );
664 onSetCIsUsed( !aPanel->isByTwoPoints() );
667 void HYDROGUI_ImportImageOp::onActivatePointSelection( int thePointType )
669 myPointType = thePointType;
671 myPreviewPrs->setTransformationPointType( thePointType );
672 if( myRefPreviewPrs )
673 myRefPreviewPrs->setTransformationPointType( thePointType );
676 void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
681 if( !theIsRef && myPreviewPrs )
682 myPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
683 else if( theIsRef && myRefPreviewPrs )
684 myRefPreviewPrs->updateTransformationPoint( thePointType, theIsY, theValue );
687 void HYDROGUI_ImportImageOp::onModeActivated( int theMode )
689 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
691 QString aRefImageName;
692 if( theMode == HYDROData_Image::ReferenceImage )
694 aRefImageName = aPanel->getRefImageName();
695 if( aRefImageName.isEmpty() )
696 return; // do nothing in this case to avoid visual moving of preview prs
697 onRefImageActivated( aRefImageName );
701 if( myRefViewManager )
703 closeView( myRefViewManager );
708 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
710 if( theName.isEmpty() ) {
711 if( myRefViewManager )
712 closeView( myRefViewManager );
716 GraphicsView_ViewPort* aViewPort = 0;
718 LightApp_Application* anApp = module()->getApp();
719 ///// Get a view port for the reference image preview
720 if( myRefViewManager )
722 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
724 aViewPort = aViewer->getActiveViewPort();
729 anApp = module()->getApp();
730 // Init reference image preview
732 dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
733 if( myRefViewManager )
735 connect( myRefViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
736 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
738 module()->setViewManagerRole( myRefViewManager, HYDROGUI_Module::VMR_ReferenceImage );
739 myRefViewManager->setTitle( tr( "REFERENCE_IMAGE" ) );
740 if( GraphicsView_Viewer* aViewer = myRefViewManager->getViewer() )
742 aViewPort = aViewer->getActiveViewPort();
743 connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
744 this, SLOT( onRefPointSelected() ) );
752 size_t aViewId = (size_t)myRefViewManager->getViewer();
754 // Remove the old presentation of the reference image if any
755 if( myRefPreviewPrs )
757 module()->setObjectVisible( aViewId, myRefPreviewPrs->getObject(), false );
759 myRefPreviewPrs->setCaption( QString() );
760 aViewPort->removeItem( myRefPreviewPrs );
762 delete myRefPreviewPrs;
766 // Create a new reference image presentation
768 Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast(
769 HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_IMAGE ) );
770 HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
771 if( !anImageObj.IsNull() )
773 anImage = anImageObj->Image();
775 myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
776 myRefPreviewPrs->setImage( anImage, true );
778 myRefPreviewPrs->setIsTransformationPointPreview( true );
779 myRefPreviewPrs->setTransformationPointType( myPointType );
781 myRefPreviewPrs->setIsByTwoPoints( aPanel->isByTwoPoints() );
783 // Add the new reference image presentation to the appropriate view
784 aViewPort->addItem( myRefPreviewPrs );
786 module()->setObjectVisible( aViewId, anImageObj, true );
791 // Split views horizontally
792 if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
794 qApp->processEvents();
795 QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
796 aViewPort->activateWindow();
798 aViewPort->setFocus(Qt::ActiveWindowFocusReason);
799 aWorkstack->splitHorizontal();
802 // Initialize the dialog
803 aPanel->setImageSize( anImage.size(), true );
804 aPanel->initializePointSelection();
805 onPointSelected( true );
808 void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
813 void HYDROGUI_ImportImageOp::onPointSelected()
815 onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
818 void HYDROGUI_ImportImageOp::onRefPointSelected()
820 onPointSelected( true );
823 void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
825 HYDROGUI_PrsImage* aPrs = theIsRefImage ? myRefPreviewPrs : myPreviewPrs;
829 HYDROGUI_ImportImageDlg::TransformationDataMap aDataMap;
831 const HYDROGUI_PrsImage::TransformationPointMap& aPointMap =
832 aPrs->getTransformationPointMap();
834 HYDROGUI_PrsImage::TransformationPointMapIterator anIter( aPointMap );
835 while( anIter.hasNext() )
837 int aPointType = anIter.next().key();
838 const HYDROGUI_PrsImage::TransformationPoint& aTransformationPoint = anIter.value();
839 const QPoint& aPoint = aTransformationPoint.Point;
841 HYDROGUI_ImportImageDlg::TransformationData aData( aPoint, QPointF(), QPointF() );
842 aDataMap[ aPointType ] = aData;
845 ( (HYDROGUI_ImportImageDlg*)inputPanel() )->setTransformationDataMap( aDataMap, true, theIsRefImage );
848 void HYDROGUI_ImportImageOp::closePreview()
850 closeView( myPreviewViewManager );
851 closeView( myRefViewManager );
853 if( myActiveViewManager )
854 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
857 void HYDROGUI_ImportImageOp::closeView( GraphicsView_ViewManager* &aViewMgr )
861 GraphicsView_ViewPort* aViewPort = 0;
862 if( GraphicsView_Viewer* aViewer = aViewMgr->getViewer() )
864 aViewPort = aViewer->getActiveViewPort();
866 disconnect( aViewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
867 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
869 // Nullify appropriate presentation pointer
870 HYDROGUI_PrsImage* aPrs = 0;
871 switch ( module()->getViewManagerRole( aViewMgr ) )
873 case HYDROGUI_Module::VMR_ReferenceImage:
874 aPrs = myRefPreviewPrs;
877 case HYDROGUI_Module::VMR_TransformImage:
882 // Remove the appropriate presentation from the view
883 if( aPrs && aViewPort )
885 aViewPort->removeItem( aPrs );
890 module()->getApp()->removeViewManager( aViewMgr ); // aViewMgr is deleted here
895 void HYDROGUI_ImportImageOp::onFilesSelected( const QStringList& theFileNames )
897 myFiles = theFileNames;
902 bool HYDROGUI_ImportImageOp::SetNextFile()
905 bool isEnabledEdit = myFiles.count()==1 || myFileIndex==myFiles.count();
906 bool isValid = ( myFileIndex>=0 && myFileIndex<myFiles.count() );
907 QString aFile = isValid ? myFiles[myFileIndex] : "";
909 HYDROGUI_ImportImageDlg* aPanel = dynamic_cast<HYDROGUI_ImportImageDlg*>( inputPanel() );
910 aPanel->ActivateFile( aFile, isEnabledEdit );