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_TwoImagesOp.h"
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_Tool.h"
23 #include "HYDROGUI_TwoImagesDlg.h"
24 #include "HYDROGUI_UpdateFlags.h"
25 #include <HYDROGUI_DataObject.h>
27 #include <HYDROData_Document.h>
28 #include <HYDROData_Image.h>
30 #include <HYDROData_OperationsFactory.h>
32 #include <ImageComposer_CutOperator.h>
33 #include <ImageComposer_CropOperator.h>
34 #include <ImageComposer_FuseOperator.h>
36 #include <LightApp_Application.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_MessageBox.h>
40 HYDROGUI_TwoImagesOp::HYDROGUI_TwoImagesOp( HYDROGUI_Module* theModule,
42 const bool theIsEdit )
43 : HYDROGUI_Operation( theModule ),
45 myIsEdit( theIsEdit ),
51 case Fuse: aName = theIsEdit ? tr( "EDIT_FUSED_IMAGE" ) : tr( "FUSE_IMAGES" ); break;
52 case Cut: aName = theIsEdit ? tr( "EDIT_CUT_IMAGE" ) : tr( "CUT_IMAGES" ); break;
53 case Split: aName = theIsEdit ? tr( "EDIT_SPLITTED_IMAGE" ) : tr( "SPLIT_IMAGE" ); break;
59 HYDROGUI_TwoImagesOp::~HYDROGUI_TwoImagesOp()
63 HYDROGUI_InputPanel* HYDROGUI_TwoImagesOp::createInputPanel() const
65 return new HYDROGUI_TwoImagesDlg( module(), getName() );
68 void HYDROGUI_TwoImagesOp::startOperation()
70 HYDROGUI_Operation::startOperation();
72 HYDROGUI_TwoImagesDlg* aPanel = (HYDROGUI_TwoImagesDlg*)inputPanel();
77 aMode = HYDROGUI_TwoImagesDlg::TwoFuseImage;
79 aMode = HYDROGUI_TwoImagesDlg::TwoCutImage;
80 else if( myType == Split )
81 aMode = HYDROGUI_TwoImagesDlg::ImageAndPolyline;
82 aPanel->setMode( aMode, myIsEdit );
87 myEditedObject = Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
88 if( !myEditedObject.IsNull() )
89 anImageName = myEditedObject->GetName();
96 case Fuse: aPrefix = tr( "FUSE" ); break;
97 case Cut: aPrefix = tr( "CUT" ); break;
98 case Split: aPrefix = tr( "SPLIT" ); break;
101 anImageName = HYDROGUI_Tool::GenerateObjectName( module(), aPrefix );
103 aPanel->setImageName( anImageName );
105 QString aSelectedName1, aSelectedName2;
106 if( myIsEdit && !myEditedObject.IsNull() )
108 if( myEditedObject->NbReferences() > 0 )
110 Handle(HYDROData_Entity) anObject1 = myEditedObject->Reference( 0 );
111 if( !anObject1.IsNull() )
112 aSelectedName1 = anObject1->GetName();
114 if( myEditedObject->NbReferences() > 1 )
116 Handle(HYDROData_Entity) anObject2 = myEditedObject->Reference( 1 );
117 if( !anObject2.IsNull() )
118 aSelectedName2 = anObject2->GetName();
120 aPanel->setSelectedObjects( aSelectedName1, aSelectedName2 );
122 HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
123 if( ImageComposer_Operator* anOperator = aFactory->Operator( myEditedObject ) )
126 anOperator->getArgs( aColor );
127 aPanel->setColor( aColor );
132 Handle(HYDROData_Image) aSelectedImage =
133 Handle(HYDROData_Image)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
134 if( !aSelectedImage.IsNull() )
136 QString aSelectedName = aSelectedImage->GetName();
137 aPanel->setPreselectedObject( aSelectedName );
140 connect( aPanel, SIGNAL( alreadySelected( const QString& ) ), SLOT( onAlreadySelected( const QString& ) ) );
143 void HYDROGUI_TwoImagesOp::onAlreadySelected( const QString& theName )
145 QString aTitle = tr( "INSUFFICIENT_INPUT_DATA" );
146 QString aMessage = tr( "OBJECT_ALREADY_SELECTED" ).arg( theName );
147 SUIT_MessageBox::critical( module()->getApp()->desktop(), aTitle, aMessage );
150 bool HYDROGUI_TwoImagesOp::processApply( int& theUpdateFlags,
151 QString& theErrorMsg,
152 QStringList& theBrowseObjectsEntries )
154 HYDROGUI_TwoImagesDlg* aPanel = dynamic_cast<HYDROGUI_TwoImagesDlg*>( inputPanel() );
156 bool anIsModifySelected = myType == Split && aPanel->isModifySelected();
158 QString anImageName = aPanel->getImageName();
159 if( !anIsModifySelected && anImageName.isEmpty() )
162 QString aSelectedName1, aSelectedName2;
163 if( !aPanel->getSelectedObjects( aSelectedName1, aSelectedName2 ) )
166 if( !anIsModifySelected &&
167 ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) ) )
169 // check that there are no other objects with the same name in the document
170 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
171 if( !anObject.IsNull() )
173 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
178 Handle(HYDROData_Entity) anObject1 =
179 HYDROGUI_Tool::FindObjectByName( module(), aSelectedName1, KIND_UNKNOWN ) ;
180 Handle(HYDROData_Entity) anObject2 =
181 HYDROGUI_Tool::FindObjectByName( module(), aSelectedName2, KIND_UNKNOWN );
182 if( anObject1.IsNull() || anObject2.IsNull() )
185 HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
187 Handle(HYDROData_Image) aResult;
188 ImageComposer_Operator* anOperator = 0;
191 aResult = myEditedObject;
192 aResult->ClearReferences();
193 anOperator = aFactory->Operator( aResult );
197 QString anOperatorName;
200 case Fuse: anOperatorName = ImageComposer_FuseOperator::Type(); break;
201 case Cut: anOperatorName = ImageComposer_CutOperator::Type(); break;
202 case Split: anOperatorName = ImageComposer_CropOperator::Type(); break;
206 anOperator = aFactory->Operator( anOperatorName );
208 aResult = aFactory->CreateImage( doc(), anOperator );
209 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aResult );
210 theBrowseObjectsEntries.append( anEntry );
213 if( aResult.IsNull() || !anOperator )
216 // Setting the operator arguments
217 anOperator->setArgs( aPanel->getColor() );
218 aResult->SetArgs( anOperator->getBinArgs() );
220 aResult->SetName( anImageName );
221 aResult->AppendReference( anObject1 );
222 aResult->AppendReference( anObject2 );
226 if( anIsModifySelected )
228 Handle(HYDROData_Image) aSelectedImage = Handle(HYDROData_Image)::DownCast( anObject1 );
229 if( !aSelectedImage.IsNull() )
231 aSelectedImage->SetIsSelfSplitted( true );
232 aSelectedImage->SetImage( aResult->Image() );
233 aSelectedImage->SetTrsf( aResult->Trsf() );
238 if( !myIsEdit && !anIsModifySelected )
240 size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
241 module()->setObjectVisible( aViewId, anObject1, false );
242 module()->setObjectVisible( aViewId, anObject2, false );
243 module()->setObjectVisible( aViewId, aResult, true );
246 theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;